1
+ /* eslint-disable no-multi-str */ // Adding this to be able to better format the console logs
1
2
'use strict'
2
3
3
4
// External Modules
5
+ const fs = require ( 'fs' )
6
+ const os = require ( 'os' )
4
7
const async = require ( 'async' )
5
8
const debug = require ( 'debug' ) ( 'logdna:index' )
6
- const fs = require ( 'fs' )
7
9
const getos = require ( 'getos' )
8
- const os = require ( 'os' )
9
10
const { Command} = require ( 'commander' )
10
11
const properties = require ( 'properties' )
11
12
const request = require ( 'request' )
12
13
13
14
// Internal Modules
14
15
const start = require ( './lib/start.js' )
15
16
const pkg = require ( './package.json' )
16
- const utils = require ( './lib/utils' )
17
+ const utils = require ( './lib/utils.js ' )
17
18
18
19
// Constants
19
20
const HOSTNAME_IP_REGEX = / [ ^ 0 - 9 a - z A - Z \- . ] / g
@@ -29,34 +30,76 @@ process.title = 'logdna-agent'
29
30
commander . _name = 'logdna-agent'
30
31
commander
31
32
. version ( pkg . version , '-v, --version' )
32
- . description ( 'This agent collect and ship logs for processing. Defaults to /var/log if run without parameters.' )
33
- . option ( '-c, --config <file>' , `uses alternate config file (default: ${ config . DEFAULT_CONF_FILE } )` )
33
+ . description (
34
+ 'This agent collects and ships logs for processing.'
35
+ + ' Defaults to /var/log if run without parameters.'
36
+ )
37
+ . option (
38
+ '-c, --config <file>'
39
+ , `Uses alternate config file (default: ${ config . DEFAULT_CONF_FILE } )`
40
+ )
34
41
. option ( '-k, --key <key>' , 'sets your LogDNA Ingestion Key in the config' )
35
- . option ( '-d, --logdir <dir>' , 'adds log directories to config, supports glob patterns' , utils . appender ( ) , [ ] )
36
- . option ( '-f, --logfile <file>' , 'adds log files to config' , utils . appender ( ) , [ ] )
37
- . option ( '-e, --exclude <file>' , 'exclude files from logdir' , utils . appender ( ) , [ ] )
42
+ . option (
43
+ '-d, --logdir <dir>'
44
+ , 'Adds log directories to config, supports glob patterns'
45
+ , utils . appender ( )
46
+ , [ ]
47
+ )
48
+ . option ( '-f, --logfile <file>' , 'Adds log files to config' , utils . appender ( ) , [ ] )
49
+ . option ( '-e, --exclude <file>' , 'Exclude files from logdir' , utils . appender ( ) , [ ] )
38
50
. option ( '-r, --exclude-regex <pattern>' , 'filter out lines matching pattern' )
39
- . option ( '-n, --hostname <hostname>' , `uses alternate hostname (default: ${ os . hostname ( ) . replace ( '.ec2.internal' , '' ) } )` )
40
- . option ( '-t, --tags <tags>' , 'add tags for this host, separate multiple tags by comma' , utils . appender ( ) , [ ] )
41
- . option ( '-l, --list [params]' , 'show the saved configuration (all unless params specified)' , utils . appender ( ) , false )
42
- . option ( '-u, --unset <params>' , 'clear some saved configurations (use "all" to unset all except key)' , utils . appender ( ) , [ ] )
43
- . option ( '-s, --set [key=value]' , 'set config variables' , utils . appender ( ) , false )
51
+ . option (
52
+ '-n, --hostname <hostname>'
53
+ , `Uses alternate hostname (default: ${ os . hostname ( ) . replace ( '.ec2.internal' , '' ) } )`
54
+ )
55
+ . option (
56
+ '-t, --tags <tags>' ,
57
+ 'Add tags for this host, separate multiple tags by comma'
58
+ , utils . appender ( ) ,
59
+ [ ]
60
+ )
61
+ . option (
62
+ '-l, --list [params]'
63
+ , 'Show the saved configuration (all unless params specified)'
64
+ , utils . appender ( ) ,
65
+
66
+ false
67
+ )
68
+ . option (
69
+ '-u, --unset <params>'
70
+ , 'Clear some saved configurations (use "all" to unset all except key)'
71
+ , utils . appender ( ) ,
72
+
73
+ [ ]
74
+ )
75
+ . option ( '-s, --set [key=value]' , 'Set config variables' , utils . appender ( ) , false )
44
76
. on ( '--help' , ( ) => {
45
77
console . log ( ' Examples:' )
46
78
console . log ( )
47
79
console . log ( ' $ logdna-agent --key YOUR_INGESTION_KEY' )
48
80
console . log ( ' $ logdna-agent -d /home/ec2-user/logs' )
49
- console . log ( ' $ logdna-agent -d /home/ec2-user/logs -d /path/to/another/log_dir # multiple logdirs in 1 go' )
50
- console . log ( ' $ logdna-agent -d "/var/log/*.txt" # supports glob patterns' )
51
- console . log ( ' $ logdna-agent -d "/var/log/**/myapp.log" # myapp.log in any subfolder' )
81
+ console . log ( '\
82
+ $ logdna-agent -d /home/ec2-user/logs -d /path/to/another/log_dir\
83
+ # multiple logdirs in 1 go\
84
+ ' )
85
+ console . log ( ' $ logdna-agent -d "/var/log/*.txt" '
86
+ + '# supports glob patterns' )
87
+ console . log ( ' $ logdna-agent -d "/var/log/**/myapp.log" '
88
+ + '# myapp.log in any subfolder' )
52
89
console . log ( ' $ logdna-agent -f /usr/local/nginx/logs/access.log' )
53
- console . log ( ' $ logdna-agent -f /usr/local/nginx/logs/access.log -f /usr/local/nginx/logs/error.log' )
54
- console . log ( ' $ logdna-agent -t production # tags' )
90
+ console . log ( ' $ logdna-agent -f /usr/local/nginx/logs/access.log '
91
+ + '-f /usr/local/nginx/logs/error.log' )
92
+ console . log ( ' $ logdna-agent -t production '
93
+ + '# tags' )
55
94
console . log ( ' $ logdna-agent -t staging,2ndtag' )
56
- console . log ( ' $ logdna-agent -l tags,key,logdir # show specific config parameters' )
57
- console . log ( ' $ logdna-agent -l # show all' )
58
- console . log ( ' $ logdna-agent -u tags,logdir # unset specific entries from config' )
59
- console . log ( ' $ logdna-agent -u all # unset all except LogDNA API Key' )
95
+ console . log ( ' $ logdna-agent -l tags,key,logdir '
96
+ + '# show specific config parameters' )
97
+ console . log ( ' $ logdna-agent -l '
98
+ + '# show all' )
99
+ console . log ( ' $ logdna-agent -u tags,logdir '
100
+ + '# unset specific entries from config' )
101
+ console . log ( ' $ logdna-agent -u all '
102
+ + '# unset all except LogDNA API Key' )
60
103
console . log ( )
61
104
} )
62
105
@@ -89,7 +132,8 @@ function loadConfig(program) {
89
132
}
90
133
91
134
if ( ! program . key && ! parsedConfig . key ) {
92
- console . error ( 'LogDNA Ingestion Key not set! Use -k to set or use environment variable LOGDNA_AGENT_KEY.' )
135
+ console . error ( 'LogDNA Ingestion Key not set! '
136
+ + 'Use -k to set or use environment variable LOGDNA_AGENT_KEY.' )
93
137
return
94
138
}
95
139
@@ -99,11 +143,15 @@ function loadConfig(program) {
99
143
100
144
if ( ! program . hostname && ! parsedConfig . hostname ) {
101
145
if ( fs . existsSync ( HOSTNAME_PATH ) && fs . statSync ( HOSTNAME_PATH ) . isFile ( ) ) {
102
- parsedConfig . hostname = fs . readFileSync ( HOSTNAME_PATH ) . toString ( ) . trim ( ) . replace ( HOSTNAME_IP_REGEX , '' )
146
+ parsedConfig . hostname = fs . readFileSync ( HOSTNAME_PATH )
147
+ . toString ( )
148
+ . trim ( )
149
+ . replace ( HOSTNAME_IP_REGEX , '' )
103
150
} else if ( os . hostname ( ) ) {
104
151
parsedConfig . hostname = os . hostname ( ) . replace ( '.ec2.internal' , '' )
105
152
} else {
106
- console . error ( 'Hostname information cannot be found! Use -n to set or use environment variable LOGDNA_HOSTNAME.' )
153
+ console . error ( 'Hostname information cannot be found! '
154
+ + 'Use -n to set or use environment variable LOGDNA_HOSTNAME.' )
107
155
return
108
156
}
109
157
}
@@ -140,7 +188,8 @@ function loadConfig(program) {
140
188
const kvPair = setOption . split ( '=' )
141
189
if ( kvPair . length === 2 ) {
142
190
parsedConfig [ kvPair [ 0 ] ] = kvPair [ 1 ]
143
- saveMessages . push ( `Config variable: ${ kvPair [ 0 ] } = ${ kvPair [ 1 ] } has been saved to config.` )
191
+ saveMessages . push ( `Config variable: ${ kvPair [ 0 ] } = ${ kvPair [ 1 ] } `
192
+ + 'has been saved to config.' )
144
193
} else {
145
194
saveMessages . push ( `Unknown setting: ${ setOption } . Usage: -s [key=value]` )
146
195
}
@@ -191,7 +240,8 @@ function loadConfig(program) {
191
240
// a slash. The user should be forced to provide open and closing slashes,
192
241
// otherwise it's too hard to know what's part of the pattern text.
193
242
parsedConfig . exclude_regex = re . replace ( / ^ \/ / , '' ) . replace ( / \/ $ / , '' )
194
- saveMessages . push ( `Exclude pattern: /${ parsedConfig . exclude_regex } / been saved to config.` )
243
+ saveMessages
244
+ . push ( `Exclude pattern: /${ parsedConfig . exclude_regex } / been saved to config.` )
195
245
}
196
246
197
247
if ( program . hostname ) {
@@ -260,15 +310,20 @@ function loadConfig(program) {
260
310
config . awstype = responseBody . instanceType
261
311
}
262
312
263
- const networkInterface = Object . values ( os . networkInterfaces ( ) ) . reduce ( ( networkData , interfaces ) => {
264
- interfaces . forEach ( interfce => networkData . push ( interfce ) )
265
- return networkData
266
- } , [ ] ) . filter ( ( interfce ) => {
267
- return interfce . family && interfce . family === 'IPv4' && interfce . mac && interfce . address && (
268
- interfce . address . startsWith ( '10.' )
269
- || interfce . address . startsWith ( '172.' )
270
- || interfce . address . startsWith ( '192.168.' ) )
271
- } ) [ 0 ]
313
+ const networkInterface = Object . values ( os . networkInterfaces ( ) )
314
+ . reduce ( ( networkData , interfaces ) => {
315
+ interfaces . forEach ( ( interfce ) => { return networkData . push ( interfce ) } )
316
+ return networkData
317
+ } , [ ] ) . filter ( ( interfce ) => {
318
+ return interfce . family
319
+ && interfce . family === 'IPv4'
320
+ && interfce . mac && interfce . address
321
+ && (
322
+ interfce . address . startsWith ( '10.' )
323
+ || interfce . address . startsWith ( '172.' )
324
+ || interfce . address . startsWith ( '192.168.' )
325
+ )
326
+ } ) [ 0 ]
272
327
273
328
if ( networkInterface ) {
274
329
if ( networkInterface . mac ) { config . mac = networkInterface . mac }
@@ -287,14 +342,21 @@ function loadConfig(program) {
287
342
} )
288
343
}
289
344
290
- process . on ( 'uncaughtException' , err => utils . log ( `uncaught error: ${ ( err . stack || '' ) . split ( '\r\n' ) } ` , 'error' ) )
345
+ process . on ( 'uncaughtException' , ( err ) => {
346
+ return utils . log ( `uncaught error: ${ ( err . stack || '' ) . split ( '\r\n' ) } ` , 'error' )
347
+ } )
291
348
292
349
if ( require . main === module ) {
293
350
commander . parse ( process . argv )
294
- const isAdmin = os . platform ( ) === 'win32' ? require ( 'is-administrator' ) ( ) : process . getuid ( ) === 0
351
+ const isAdmin = os . platform ( ) === 'win32'
352
+ ? require ( 'is-administrator' ) ( )
353
+ : process . getuid ( ) === 0
295
354
296
355
if ( ! isAdmin ) {
297
- console . log ( 'You must be an Administrator (root, sudo) to run this agent! See -h or --help for more info.' )
356
+ console . log ( 'You must be an Administrator (root, sudo) to run this agent! '
357
+ + 'See -h or --help for more info.' )
358
+
359
+ process . exitCode = 1
298
360
return
299
361
}
300
362
0 commit comments