-
Notifications
You must be signed in to change notification settings - Fork 61
/
monitor.js
executable file
·65 lines (57 loc) · 2.28 KB
/
monitor.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
#!/usr/bin/env node
var PM2Tracker = require('./lib/PM2Tracker');
var ZabbixDataProvider = require('./lib/ZabbixDataProvider');
var PM2ZabbixMonitor = require('./lib/PM2ZabbixMonitor');
var ZabbixSender = require('zabbix-sender');
var minimist = require('minimist');
var os = require('os');
var bunyan = require('bunyan');
var argv = minimist(process.argv.slice(2));
var hostname = os.hostname();
if (argv.help || argv.usage) {
console.log('# PM2 monitoring tool for Zabbix');
console.log('');
console.log('Usage: node monitor.js [options]');
console.log('Options:');
console.log('\t--monitor - enable sending of updates to Zabbix via zabbix_sender');
console.log('\t--discover - instead of running constantly, print JSON in Zabbix discovery format and exit');
console.log('\t (This is typically used as the command part for the pm2.processes UserParameter.)');
console.log('\t--hostname=<hostname> - Use <hostname> instead of the system\'s hostname');
console.log('\t--server=<server> - Connect to Zabbix at <server> instead of the default 127.0.0.1');
console.log('\t--debug - Enable printing of console messages at runtime');
process.exit(0);
}
// If we're going to output JSON, we don't want to mix it with log output.
// This means we need to push the logs elsewhere.
var usesOutputMode = (argv.discover);
var logger = bunyan.createLogger({
name: 'pm2-zabbix',
level: process.env.LOG_LEVEL || 'warn',
// Use stderr if writing JSON to stdout - otherwise, normal stdout is fine.
stream: usesOutputMode ? process.stderr : process.stdout
});
var sender = new ZabbixSender({
hostname: argv.hostname || hostname,
server: argv.server || undefined,
logger: logger
});
var tracker = new PM2Tracker();
var provider = new ZabbixDataProvider(sender);
var monitor = new PM2ZabbixMonitor(tracker, provider, {
monitor: argv.monitor,
debug: argv.debug,
logger: logger
});
monitor.start().done(function() {
if (argv.discover) {
var discoveryData = provider.getDiscoveryData();
process.stdout.write(JSON.stringify(discoveryData, null, '\t'));
process.exit(0);
return;
}
if (!argv.discover && !argv.monitor) {
logger.warn({ event: 'pm2-zabbix#noWork', argv: process.argv }, 'Neither --discover nor --monitor was specified - exiting because we have nothing to do');
process.exit(0);
return;
}
});