-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathindex.js
369 lines (323 loc) · 12.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* eslint-disable no-multi-str */ // Adding this to be able to better format the console logs
'use strict'
// External Modules
const fs = require('fs')
const os = require('os')
const async = require('async')
const debug = require('debug')('logdna:index')
const getos = require('getos')
const {Command} = require('commander')
const properties = require('properties')
const request = require('request')
// Internal Modules
const start = require('./lib/start.js')
const pkg = require('./package.json')
const utils = require('./lib/utils.js')
// Constants
const HOSTNAME_IP_REGEX = /[^0-9a-zA-Z\-.]/g
const HOSTNAME_PATH = '/etc/logdna-hostname'
// Variables
let config = require('./lib/config.js')
var processed
const commander = new Command()
process.title = 'logdna-agent'
commander._name = 'logdna-agent'
commander
.version(pkg.version, '-v, --version')
.description(
'This agent collects and ships logs for processing.'
+ ' Defaults to /var/log if run without parameters.'
)
.option(
'-c, --config <file>'
, `Uses alternate config file (default: ${config.DEFAULT_CONF_FILE})`
)
.option('-k, --key <key>', 'sets your LogDNA Ingestion Key in the config')
.option(
'-d, --logdir <dir>'
, 'Adds log directories to config, supports glob patterns'
, utils.appender()
, []
)
.option('-f, --logfile <file>', 'Adds log files to config', utils.appender(), [])
.option('-e, --exclude <file>', 'Exclude files from logdir', utils.appender(), [])
.option('-r, --exclude-regex <pattern>', 'filter out lines matching pattern')
.option(
'-n, --hostname <hostname>'
, `Uses alternate hostname (default: ${os.hostname().replace('.ec2.internal', '')})`
)
.option(
'-t, --tags <tags>',
'Add tags for this host, separate multiple tags by comma'
, utils.appender(),
[]
)
.option(
'-l, --list [params]'
, 'Show the saved configuration (all unless params specified)'
, utils.appender(),
false
)
.option(
'-u, --unset <params>'
, 'Clear some saved configurations (use "all" to unset all except key)'
, utils.appender(),
[]
)
.option('-s, --set [key=value]', 'Set config variables', utils.appender(), false)
.on('--help', () => {
console.log(' Examples:')
console.log()
console.log(' $ logdna-agent --key YOUR_INGESTION_KEY')
console.log(' $ logdna-agent -d /home/ec2-user/logs')
console.log('\
$ logdna-agent -d /home/ec2-user/logs -d /path/to/another/log_dir\
# multiple logdirs in 1 go\
')
console.log(' $ logdna-agent -d "/var/log/*.txt" '
+ '# supports glob patterns')
console.log(' $ logdna-agent -d "/var/log/**/myapp.log" '
+ '# myapp.log in any subfolder')
console.log(' $ logdna-agent -f /usr/local/nginx/logs/access.log')
console.log(' $ logdna-agent -f /usr/local/nginx/logs/access.log '
+ '-f /usr/local/nginx/logs/error.log')
console.log(' $ logdna-agent -t production '
+ '# tags')
console.log(' $ logdna-agent -t staging,2ndtag')
console.log(' $ logdna-agent -l tags,key,logdir '
+ '# show specific config parameters')
console.log(' $ logdna-agent -l '
+ '# show all')
console.log(' $ logdna-agent -u tags,logdir '
+ '# unset specific entries from config')
console.log(' $ logdna-agent -u all '
+ '# unset all except LogDNA API Key')
console.log()
})
function loadConfig(program) {
const conf_file = program.config || config.DEFAULT_CONF_FILE
debug(`Path to Configuration File: ${conf_file}`)
async.waterfall([
(cb) => {
fs.access(conf_file, (error) => {
if (error) {
return cb(null, {})
}
return properties.parse(conf_file, {
path: true
}, (error, parsedConfig) => {
if (error) {
utils.log(`error in parsing ${conf_file}: ${error}`, 'error')
}
return cb(null, error ? {} : parsedConfig)
})
})
}
, (parsedConfig, cb) => {
parsedConfig = parsedConfig || {}
// allow key to be passed via env
if (process.env.LOGDNA_AGENT_KEY) {
parsedConfig.key = process.env.LOGDNA_AGENT_KEY
}
if (!program.key && !parsedConfig.key) {
console.error('LogDNA Ingestion Key not set! '
+ 'Use -k to set or use environment variable LOGDNA_AGENT_KEY.')
return
}
if (process.env.LOGDNA_HOSTNAME) {
parsedConfig.hostname = process.env.LOGDNA_HOSTNAME
}
if (!program.hostname && !parsedConfig.hostname) {
if (fs.existsSync(HOSTNAME_PATH) && fs.statSync(HOSTNAME_PATH).isFile()) {
parsedConfig.hostname = fs.readFileSync(HOSTNAME_PATH)
.toString()
.trim()
.replace(HOSTNAME_IP_REGEX, '')
} else if (os.hostname()) {
parsedConfig.hostname = os.hostname().replace('.ec2.internal', '')
} else {
console.error('Hostname information cannot be found! '
+ 'Use -n to set or use environment variable LOGDNA_HOSTNAME.')
return
}
}
// allow exclude to be passed via env
if (process.env.LOGDNA_EXCLUDE) {
parsedConfig.exclude = process.env.LOGDNA_EXCLUDE
}
if (process.env.USEJOURNALD) {
parsedConfig.usejournald = process.env.USEJOURNALD
}
// sanitize
if (!parsedConfig.logdir) {
parsedConfig.logdir = [config.DEFAULT_LOG_PATH] // default entry
} else if (!Array.isArray(parsedConfig.logdir)) {
parsedConfig.logdir = parsedConfig.logdir.split(',') // force array
}
if (parsedConfig.exclude && !Array.isArray(parsedConfig.exclude)) {
parsedConfig.exclude = parsedConfig.exclude.split(',')
}
var saveMessages = []
if (program.key) {
parsedConfig.key = program.key
saveMessages.push('Your LogDNA Ingestion Key has been successfully saved!')
}
if (program.set && program.set.length > 0) {
program.set.forEach((setOption) => {
const kvPair = setOption.split('=')
if (kvPair.length === 2) {
parsedConfig[kvPair[0]] = kvPair[1]
saveMessages.push(`Config variable: ${kvPair[0]} = ${kvPair[1]} `
+ 'has been saved to config.')
} else {
saveMessages.push(`Unknown setting: ${setOption}. Usage: -s [key=value]`)
}
})
}
if (program.list) {
if (typeof program.list === 'boolean') {
program.list = ['all']
}
var conf = properties.parse(fs.readFileSync(conf_file).toString())
const listResult = utils.pick2list(program.list, conf)
if (listResult.valid) {
var msg = utils.stringify(listResult.cfg)
saveMessages.push(`${conf_file}:\n${msg}`)
} else {
saveMessages.push(listResult.msg)
}
}
if (program.unset && program.unset.length > 0) {
const unsetResult = utils.unsetConfig(program.unset, parsedConfig)
parsedConfig = unsetResult.cfg
saveMessages.push(unsetResult.msg)
}
if (program.logdir && program.logdir.length > 0) {
processed = utils.processOption(program.logdir, parsedConfig.logdir)
parsedConfig.logdir = processed.values
saveMessages.push(`Log Directories: ${processed.diff} been saved to config.`)
}
if (program.logfile && program.logfile.length > 0) {
processed = utils.processOption(program.logfile, parsedConfig.logdir)
parsedConfig.logdir = processed.values
saveMessages.push(`Log Files: ${processed.diff} been saved to config.`)
}
if (program.exclude && program.exclude.length > 0) {
processed = utils.processOption(program.exclude, parsedConfig.exclude)
parsedConfig.exclude = processed.values
saveMessages.push(`Exclusions: ${processed.diff} been saved to config.`)
}
if (program.excludeRegex || process.env.LOGDNA_EXCLUDE_REGEX) {
const re = program.excludeRegex || process.env.LOGDNA_EXCLUDE_REGEX
// FIXME(darinspivey) - Error prone. Doesn't support modifiers or ending with
// a slash. The user should be forced to provide open and closing slashes,
// otherwise it's too hard to know what's part of the pattern text.
parsedConfig.exclude_regex = re.replace(/^\//, '').replace(/\/$/, '')
saveMessages
.push(`Exclude pattern: /${parsedConfig.exclude_regex}/ been saved to config.`)
}
if (program.hostname) {
parsedConfig.hostname = program.hostname
saveMessages.push(`Hostname: ${parsedConfig.hostname} has been saved to config.`)
}
if (program.tags && program.tags.length > 0) {
processed = utils.processOption(program.tags, parsedConfig.tags)
parsedConfig.tags = processed.values
saveMessages.push(`Tags: ${processed.diff} been saved to config.`)
}
if (saveMessages.length) {
utils.saveConfig(parsedConfig, conf_file, (error, success) => {
if (error) {
return utils.log(`error while saving to: ${conf_file}: ${error}`, 'error')
}
saveMessages.forEach((message) => { console.log(message) })
})
return
}
// merge into single var after all potential saveConfigs finished
config = {
...config
, ...parsedConfig
, tags: process.env.LOGDNA_TAGS || parsedConfig.tags
, rescanTimer: null
, sendUserAgent: program.sendUserAgent || process.env.LOGGER_SEND_USER_AGENT
}
if (config.exclude_regex) {
config.exclude_regex = new RegExp(config.exclude_regex)
}
return getos(cb)
}
, (distro, cb) => {
let distroName = `${(distro.dist || distro.os).replace(/Linux| /g, '')}`
if (distro.release) {
distroName += `/${distro.release}`
}
config.UserAgent = `${pkg.name}/${pkg.version} ${distroName}`
if (process.env.LOGDNA_PLATFORM) {
const platform = process.env.LOGDNA_PLATFORM
config.tags = config.tags ? `${config.tags},${platform}` : platform
config.UserAgent += `, ${platform}`
}
return request(config.AWS_INSTANCE_CHECK_URL, {
timeout: 1000
, json: true
}, (error, response, body) => {
return cb(error, body)
})
}
], (_, responseBody) => {
if (responseBody) {
config.awsid = responseBody.instanceId
config.awsregion = responseBody.region
config.awsaz = responseBody.availabilityZone
config.awsami = responseBody.imageId
config.awstype = responseBody.instanceType
}
const networkInterface = Object.values(os.networkInterfaces())
.reduce((networkData, interfaces) => {
interfaces.forEach((interfce) => { return networkData.push(interfce) })
return networkData
}, []).filter((interfce) => {
return interfce.family
&& interfce.family === 'IPv4'
&& interfce.mac && interfce.address
&& (
interfce.address.startsWith('10.')
|| interfce.address.startsWith('172.')
|| interfce.address.startsWith('192.168.')
)
})[0]
if (networkInterface) {
if (networkInterface.mac) { config.mac = networkInterface.mac }
if (networkInterface.address) { config.ip = networkInterface.address }
}
utils.log(`${pkg.name}/${pkg.version} started on ${config.hostname} (${config.ip})`)
if (config.userAgent) {
config.DEFAULT_REQ_HEADERS['user-agent'] = config.userAgent
config.DEFAULT_REQ_HEADERS_GZIP['user-agent'] = config.userAgent
}
debug('Configuration loaded. Starting log client and watching files.')
start(config)
debug('logdna agent successfully started')
})
}
process.on('uncaughtException', (err) => {
return utils.log(`uncaught error: ${(err.stack || '').split('\r\n')}`, 'error')
})
if (require.main === module) {
commander.parse(process.argv)
const isAdmin = os.platform() === 'win32'
? require('is-administrator')()
: process.getuid() === 0
if (!isAdmin) {
console.log('You must be an Administrator (root, sudo) to run this agent! '
+ 'See -h or --help for more info.')
process.exitCode = 1
return
}
loadConfig(commander)
}
module.exports = {
loadConfig
, commander
}