-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,25 @@ | ||
# haproxy-stats | ||
Get HAProxy statistics in JSON format via HTTP request. | ||
Get HAProxy statistics in JSON format via HTTP request. | ||
Check [docs](http://www.haproxy.org/#docs) for more info. | ||
|
||
|
||
## Install | ||
|
||
``` | ||
$ npm install --save haproxy-stats | ||
``` | ||
|
||
|
||
### Usage | ||
|
||
```js | ||
var haStats = require('haproxy-stats'); | ||
haStats('http://localhost:1337/stats;csv;norefresh', function(err, data) { | ||
console.log(data) | ||
// => { 'ws': {'ws1': {qcur: 12, qmax: 0, scur: 355 ...}, 'ws2': {qcur: 0, qmax: 0, scur: 301 ...} } } | ||
}) | ||
``` | ||
|
||
## License | ||
|
||
MIT © [Nikolay Spiridonov](https://github.com/sohje) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
var parse = require('csv').parse; | ||
var request = require('request'); | ||
|
||
var haFormat = [ | ||
'pxname', 'svname', 'qcur', 'qmax', 'scur', 'smax', 'slim', 'stot', 'bin', 'bout','dreq', | ||
'dresp', 'ereq', 'econ', 'eresp', 'wretr', 'wredis', 'status', 'weight', 'act', 'bck', | ||
'chkfail', 'chkdown', 'lastchg', 'downtime', 'qlimit', 'pid', 'iid', 'sid', 'throttle', | ||
'lbtot', 'tracked', 'type', 'rate', 'rate_lim', 'rate_max', 'check_status', 'check_code', | ||
'check_duration', 'hrsp_1xx', 'hrsp_2xx', 'hrsp_3xx', 'hrsp_4xx', 'hrsp_5xx', 'hrsp_other', | ||
'hanafail', 'req_rate', 'req_rate_max', 'req_tot', 'cli_abrt', 'srv_abrt', 'comp_in', 'comp_out', | ||
'comp_byp', 'comp_rsp', 'lastsess', 'last_chk', 'last_agt', 'qtime', 'ctime', 'rtime','ttime' | ||
]; | ||
|
||
|
||
module.exports = function(url, cb) { | ||
if (typeof url !== 'string') { | ||
throw new Error('Provide correct HAProxy\'s stats url'); | ||
} | ||
|
||
if (typeof cb !== 'function') { | ||
throw new Error('Provide callback function'); | ||
} | ||
|
||
function parseCSV(err, data) { | ||
var result = {}; | ||
|
||
function makeResult(row, i, arr) { | ||
if (row[0] === '# pxname') return; | ||
if (!result[row[0]]) result[row[0]] = {} | ||
|
||
result[row[0]][row[1]] = {} | ||
haFormat.forEach(function(item, i, arr) { | ||
// fix this | ||
if (i < 2) return; | ||
result[row[0]][row[1]][item] = row[i] | ||
}) | ||
} | ||
data.forEach(makeResult) | ||
cb(err, result) | ||
} | ||
|
||
request(url, function (err, resp, body) { | ||
if (!err && resp.statusCode == 200) { | ||
parse(body, {skip_empty_lines: true}, parseCSV); | ||
} else { | ||
cb(err || new Error(resp.statusCode), null) | ||
} | ||
}) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "haproxy-stats", | ||
"version": "0.0.1", | ||
"description": "Get HAProxy statistics in JSON", | ||
"license": "MIT", | ||
"repository": { | ||
"url": "https://github.com/sohje/haproxy-stats.git", | ||
"type": "git" | ||
}, | ||
"author": { | ||
"name": "Nikolay Spiridonov", | ||
"email": "[email protected]" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"stats", | ||
"statistics", | ||
"csv", | ||
"haprox", | ||
"json", | ||
"proxy", | ||
"balancer", | ||
"ha", | ||
"server", | ||
"haproxy-stats", | ||
"monitoring", | ||
"analytics" | ||
], | ||
"dependencies": { | ||
"request": "^2.69.0" | ||
} | ||
} |