Skip to content

Commit 2ba3b16

Browse files
authored
Merge pull request #21 from gshively11/parsers
Extended parsing on HAProxy.prototype.parse.
2 parents 8a33147 + 668c2db commit 2ba3b16

4 files changed

Lines changed: 328 additions & 26 deletions

File tree

index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ HAProxy.prototype.parse = function parse(using, buffer, fn, command) {
205205
delete row[''];
206206
return row;
207207
});
208+
} else if (using === 'string') {
209+
result = buffer.toString();
210+
} else if (using === 'array') {
211+
result = buffer.split('\n').map(function (line) { return line.trim(); });
212+
} else if (typeof using === 'function') {
213+
result = using(buffer);
208214
} else if (~buffer.indexOf('\n')) {
209215
result = buffer.split('\n').reduce(function reducer(data, line) {
210216
line = line.trim();
@@ -217,9 +223,12 @@ HAProxy.prototype.parse = function parse(using, buffer, fn, command) {
217223
//
218224
if ('object' === using) {
219225
var kv = line.split(':');
220-
221-
kv[1] = kv[1].trim();
222-
data[kv[0]] = !isNaN(+kv[1]) ? +kv[1] : kv[1];
226+
if(kv.length === 1) {
227+
data[kv[0]] = '';
228+
} else {
229+
kv[1] = kv[1].trim();
230+
data[kv[0]] = !isNaN(+kv[1]) ? +kv[1] : kv[1];
231+
}
223232
}
224233

225234
return data;

test/fixtures/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
'show info': read('showinfo'),
2323
'show stat': read('showstat'),
2424
'show stat -1 -1 -1': read('showstatall'),
25+
'show stat parsed': require('./showstat-parsed'),
2526
'show sess': read('showsess'),
2627

2728
'clear counters': read('empty'),

test/fixtures/showstat-parsed.js

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
// showinfo.out that's been run through the parser
2+
3+
module.exports = {
4+
func: '# pxn',
5+
string: '# 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,\npublic,FRONTEND,,,27,81,2000,21994,33421405,32806329,0,0,0,,,,,OPEN,,,,,,,,,1,2,0,,,,0,195,0,538,,,,21967,0,0,0,0,0,,194,540,21992,,,0,0,0,0,\nrealtime,node1,0,0,25,77,32768,21992,33421405,32806329,,0,,0,0,0,0,no check,1,1,0,,,,,,1,3,1,,21992,,2,194,,540,,,,21991,0,0,0,0,0,0,,,,0,0,,,,,\nrealtime,BACKEND,0,0,25,77,200,21992,33421405,32806329,0,0,,0,0,0,0,UP,1,1,0,,0,648,0,,1,3,0,,21992,,1,194,,540,,,,21967,0,0,0,0,0,,,,,0,0,0,0,0,0,\n\n',
6+
array: [
7+
'# 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,',
8+
'public,FRONTEND,,,27,81,2000,21994,33421405,32806329,0,0,0,,,,,OPEN,,,,,,,,,1,2,0,,,,0,195,0,538,,,,21967,0,0,0,0,0,,194,540,21992,,,0,0,0,0,',
9+
'realtime,node1,0,0,25,77,32768,21992,33421405,32806329,,0,,0,0,0,0,no check,1,1,0,,,,,,1,3,1,,21992,,2,194,,540,,,,21991,0,0,0,0,0,0,,,,0,0,,,,,',
10+
'realtime,BACKEND,0,0,25,77,200,21992,33421405,32806329,0,0,,0,0,0,0,UP,1,1,0,,0,648,0,,1,3,0,,21992,,1,194,,540,,,,21967,0,0,0,0,0,,,,,0,0,0,0,0,0,',
11+
'',
12+
''
13+
],
14+
obj: {},
15+
csv: [{
16+
act: "",
17+
bck: "",
18+
bin: "33421405",
19+
bout: "32806329",
20+
check_code: "",
21+
check_duration: "",
22+
check_status: "",
23+
chkdown: "",
24+
chkfail: "",
25+
cli_abrt: "",
26+
comp_byp: "0",
27+
comp_in: "0",
28+
comp_out: "0",
29+
comp_rsp: "0",
30+
downtime: "",
31+
dreq: "0",
32+
dresp: "0",
33+
econ: "",
34+
ereq: "0",
35+
eresp: "",
36+
hanafail: "",
37+
hrsp_1xx: "21967",
38+
hrsp_2xx: "0",
39+
hrsp_3xx: "0",
40+
hrsp_4xx: "0",
41+
hrsp_5xx: "0",
42+
hrsp_other: "0",
43+
iid: "2",
44+
lastchg: "",
45+
lbtot: "",
46+
pid: "1",
47+
pxname: "public",
48+
qcur: "",
49+
qlimit: "",
50+
qmax: "",
51+
rate: "195",
52+
rate_lim: "0",
53+
rate_max: "538",
54+
req_rate: "194",
55+
req_rate_max: "540",
56+
req_tot: "21992",
57+
scur: "27",
58+
sid: "0",
59+
slim: "2000",
60+
smax: "81",
61+
srv_abrt: "",
62+
status: "OPEN",
63+
stot: "21994",
64+
svname: "FRONTEND",
65+
throttle: "",
66+
tracked: "",
67+
type: "0",
68+
weight: "",
69+
wredis: "",
70+
wretr: ""
71+
}, {
72+
act: "1",
73+
bck: "0",
74+
bin: "33421405",
75+
bout: "32806329",
76+
check_code: "",
77+
check_duration: "",
78+
check_status: "",
79+
chkdown: "",
80+
chkfail: "",
81+
cli_abrt: "0",
82+
comp_byp: "",
83+
comp_in: "",
84+
comp_out: "",
85+
comp_rsp: "",
86+
downtime: "",
87+
dreq: "",
88+
dresp: "0",
89+
econ: "0",
90+
ereq: "",
91+
eresp: "0",
92+
hanafail: "0",
93+
hrsp_1xx: "21991",
94+
hrsp_2xx: "0",
95+
hrsp_3xx: "0",
96+
hrsp_4xx: "0",
97+
hrsp_5xx: "0",
98+
hrsp_other: "0",
99+
iid: "3",
100+
lastchg: "",
101+
lbtot: "21992",
102+
pid: "1",
103+
pxname: "realtime",
104+
qcur: "0",
105+
qlimit: "",
106+
qmax: "0",
107+
rate: "194",
108+
rate_lim: "",
109+
rate_max: "540",
110+
req_rate: "",
111+
req_rate_max: "",
112+
req_tot: "",
113+
scur: "25",
114+
sid: "1",
115+
slim: "32768",
116+
smax: "77",
117+
srv_abrt: "0",
118+
status: "no check",
119+
stot: "21992",
120+
svname: "node1",
121+
throttle: "",
122+
tracked: "",
123+
type: "2",
124+
weight: "1",
125+
wredis: "0",
126+
wretr: "0"
127+
}, {
128+
act: "1",
129+
bck: "0",
130+
bin: "33421405",
131+
bout: "32806329",
132+
check_code: "",
133+
check_duration: "",
134+
check_status: "",
135+
chkdown: "0",
136+
chkfail: "",
137+
cli_abrt: "0",
138+
comp_byp: "0",
139+
comp_in: "0",
140+
comp_out: "0",
141+
comp_rsp: "0",
142+
downtime: "0",
143+
dreq: "0",
144+
dresp: "0",
145+
econ: "0",
146+
ereq: "",
147+
eresp: "0",
148+
hanafail: "",
149+
hrsp_1xx: "21967",
150+
hrsp_2xx: "0",
151+
hrsp_3xx: "0",
152+
hrsp_4xx: "0",
153+
hrsp_5xx: "0",
154+
hrsp_other: "0",
155+
iid: "3",
156+
lastchg: "648",
157+
lbtot: "21992",
158+
pid: "1",
159+
pxname: "realtime",
160+
qcur: "0",
161+
qlimit: "",
162+
qmax: "0",
163+
rate: "194",
164+
rate_lim: "",
165+
rate_max: "540",
166+
req_rate: "",
167+
req_rate_max: "",
168+
req_tot: "",
169+
scur: "25",
170+
sid: "0",
171+
slim: "200",
172+
smax: "77",
173+
srv_abrt: "0",
174+
status: "UP",
175+
stot: "21992",
176+
svname: "BACKEND",
177+
throttle: "",
178+
tracked: "",
179+
type: "1",
180+
weight: "1",
181+
wredis: "0",
182+
wretr: "0"
183+
}, {
184+
act: undefined,
185+
bck: undefined,
186+
bin: undefined,
187+
bout: undefined,
188+
check_code: undefined,
189+
check_duration: undefined,
190+
check_status: undefined,
191+
chkdown: undefined,
192+
chkfail: undefined,
193+
cli_abrt: undefined,
194+
comp_byp: undefined,
195+
comp_in: undefined,
196+
comp_out: undefined,
197+
comp_rsp: undefined,
198+
downtime: undefined,
199+
dreq: undefined,
200+
dresp: undefined,
201+
econ: undefined,
202+
ereq: undefined,
203+
eresp: undefined,
204+
hanafail: undefined,
205+
hrsp_1xx: undefined,
206+
hrsp_2xx: undefined,
207+
hrsp_3xx: undefined,
208+
hrsp_4xx: undefined,
209+
hrsp_5xx: undefined,
210+
hrsp_other: undefined,
211+
iid: undefined,
212+
lastchg: undefined,
213+
lbtot: undefined,
214+
pid: undefined,
215+
pxname: "",
216+
qcur: undefined,
217+
qlimit: undefined,
218+
qmax: undefined,
219+
rate: undefined,
220+
rate_lim: undefined,
221+
rate_max: undefined,
222+
req_rate: undefined,
223+
req_rate_max: undefined,
224+
req_tot: undefined,
225+
scur: undefined,
226+
sid: undefined,
227+
slim: undefined,
228+
smax: undefined,
229+
srv_abrt: undefined,
230+
status: undefined,
231+
stot: undefined,
232+
svname: undefined,
233+
throttle: undefined,
234+
tracked: undefined,
235+
type: undefined,
236+
weight: undefined,
237+
wredis: undefined,
238+
wretr: undefined
239+
}]
240+
};

0 commit comments

Comments
 (0)