Skip to content

Commit

Permalink
Initial haproxy parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sohje committed Feb 5, 2016
1 parent 225edd2 commit caa215f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
25 changes: 24 additions & 1 deletion README.md
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)
50 changes: 50 additions & 0 deletions index.js
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)
}
})
}
37 changes: 37 additions & 0 deletions package.json
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"
}
}

0 comments on commit caa215f

Please sign in to comment.