Created
March 10, 2015 22:39
-
-
Save nbrownus/edf0297bc95be99504d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| var net = require('net'), | |
| chunk = '', | |
| regex = /{"name":"(.+?)".*"size":([0-9]*),"enqueued":([0-9]*),"full":([0-9]*),"discarded\.full":([0-9]*),"discarded\.nf":([0-9]*),"maxqsize":([0-9]*)}/, | |
| names = { | |
| 'main Q': 'main_queue', | |
| 'main Q[DA]': 'main_queue_disk_assist', | |
| 'relp_forward queue': 'relp_forward', | |
| 'relp_forward queue[DA]': 'relp_forward_disk_assist', | |
| 'relp_streamstash queue': 'relp_streamstash', | |
| 'relp_streamstash queue[DA]': 'relp_streamstash_disk_assist' | |
| }, | |
| hostname = process.env['HOSTNAME'] || process.argv[2], | |
| graphiteHost = process.env['GRAPHITE_HOST'] || process.argv[3], | |
| graphitePort = process.env['GRAPHITE_PORT'] || process.argv[4] | |
| var graphiteConn = net.connect(graphitePort, graphiteHost, function () { | |
| process.stdin.on('data', function (newChunk) { | |
| chunk += newChunk.toString() | |
| var newLine | |
| while ((newLine = chunk.indexOf('\n')) > 0) { | |
| var matches = chunk.slice(0, newLine).match(regex) | |
| if (matches && names[matches[1]]) { | |
| var name = names[matches[1]] | |
| sendToGraphite(name + '.size', matches[2]) | |
| sendToGraphite(name + '.enqueued', matches[3]) | |
| sendToGraphite(name + '.full', matches[4]) | |
| sendToGraphite(name + '.discarded_full', matches[5]) | |
| sendToGraphite(name + '.discarded_not_full', matches[6]) | |
| sendToGraphite(name + '.max_queue_size', matches[7]) | |
| } | |
| chunk = chunk.slice(newLine + 1) | |
| } | |
| }) | |
| }) | |
| graphiteConn.setNoDelay() | |
| process.stdin.on('end', function () { | |
| process.exit() | |
| }) | |
| graphiteConn.on('end', function () { | |
| console.log('Graphite connection ended') | |
| process.exit(1) | |
| }) | |
| graphiteConn.on('close', function () { | |
| console.log('Graphite connection closed') | |
| process.exit(1) | |
| }) | |
| graphiteConn.on('error', function (err) { | |
| console.log('Graphite connection had an error', err) | |
| process.exit(2) | |
| }) | |
| var sendToGraphite = function (name, value) { | |
| graphiteConn.write('rsyslog.' + hostname + '.' + name + ' ' + value + ' ' + Math.round(Date.now() / 1000) +'\n') | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment