UNPKG

3.17 kBJavaScriptView Raw
1const fs = require('fs');
2
3/**
4 * Replace any characters that can't be sent on with an underscore
5 */
6function sanitizeTags(value, telegraf) {
7 const blacklist = telegraf ? /:|\||,/g : /:|\||@|,/g;
8 // Replace reserved chars with underscores.
9 return String(value).replace(blacklist, '_');
10}
11
12/**
13 * Format tags properly before sending on
14 */
15function formatTags(tags, telegraf) {
16 if (Array.isArray(tags)) {
17 return tags;
18
19 } else {
20 return Object.keys(tags).map(key => {
21 return `${sanitizeTags(key, telegraf)}:${sanitizeTags(tags[key], telegraf)}`;
22 });
23 }
24}
25
26/**
27 * Overrides tags in parent with tags from child with the same name (case sensitive) and return the result as new
28 * array. parent and child are not mutated.
29 */
30function overrideTags (parent, child, telegraf) {
31 const childCopy = {};
32 const toAppend = [];
33 formatTags(child, telegraf).forEach(tag => {
34 const idx = typeof tag === 'string' ? tag.indexOf(':') : -1;
35 if (idx < 1) { // Not found or first character
36 toAppend.push(tag);
37 } else {
38 childCopy[tag.substring(0, idx)] = tag.substring(idx + 1);
39 }
40 });
41 const result = parent.map(tag => {
42 const idx = typeof tag === 'string' ? tag.indexOf(':') : -1;
43 if (idx < 1) { // Not found or first character
44 return tag;
45 }
46 const key = tag.substring(0, idx);
47 if (childCopy.hasOwnProperty(key)) {
48 const value = childCopy[key];
49 delete childCopy[key];
50 return `${key}:${value}`;
51 }
52 return tag;
53 });
54 Object.keys(childCopy).forEach(key => {
55 result.push(`${key}:${childCopy[key]}`);
56 });
57 return result.concat(toAppend);
58}
59
60/**
61 * Formats a date for use with DataDog
62 */
63function formatDate(date) {
64 let timestamp;
65 if (date instanceof Date) {
66 // Datadog expects seconds.
67 timestamp = Math.round(date.getTime() / 1000);
68 } else if (date instanceof Number) {
69 // Make sure it is an integer, not a float.
70 timestamp = Math.round(date);
71 }
72 return timestamp;
73}
74
75/**
76 * Converts int to a string IP
77 */
78function intToIP(int) {
79 const part1 = int & 255;
80 const part2 = ((int >> 8) & 255);
81 const part3 = ((int >> 16) & 255);
82 const part4 = ((int >> 24) & 255);
83
84 return `${part4}.${part3}.${part2}.${part1}`;
85}
86
87/**
88 * Returns the system default interface on Linux
89 */
90function getDefaultRoute() {
91 try {
92 const fileContents = fs.readFileSync('/proc/net/route', 'utf8'); // eslint-disable-line no-sync
93 const routes = fileContents.split('\n');
94 for (const routeIdx in routes) {
95 const fields = routes[routeIdx].trim().split('\t');
96 if (fields[1] === '00000000') {
97 const address = fields[2];
98 // Convert to little endian by splitting every 2 digits and reversing that list
99 const littleEndianAddress = address.match(/.{2}/g).reverse().join('');
100 return intToIP(parseInt(littleEndianAddress, 16));
101 }
102 }
103 } catch (e) {
104 console.error('Could not get default route from /proc/net/route');
105 }
106 return null;
107}
108
109module.exports = {
110 formatTags: formatTags,
111 overrideTags: overrideTags,
112 formatDate: formatDate,
113 getDefaultRoute: getDefaultRoute,
114 sanitizeTags: sanitizeTags
115};