Skip to content

Commit f83f51c

Browse files
committed
0.0.4 rs.status()
1 parent cede9cf commit f83f51c

4 files changed

Lines changed: 266 additions & 73 deletions

File tree

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,150 @@ mongo.command('rs.add(\'mongodb2:27017\')').then(function (data) {
297297
//}
298298

299299
```
300+
301+
302+
* rs.status()
303+
304+
```js
305+
306+
mongo.command('rs.status()').then(function (data) {
307+
console.log('data = ', data);
308+
});
309+
310+
311+
//data = {
312+
// command: 'mongo --host localhost --port 27017 --eval "rs.status()"',
313+
// raw: '["MongoDB shell version: 3.2.0\\nconnecting to: localhost:27017/test\\n{\\n\\t\\"set\\" : \\"mongodb\\",\\n\\t\\"date\\" : ISODate(\\"2015-12-25T07:03:18.365Z\\"),\\n\\t\\"myState\\" : 1,\\n\\t\\"term\\" : NumberLong(1),\\n\\t\\"heartbeatIntervalMillis\\" : NumberLong(2000),\\n\\t\\"members\\" : [\\n\\t\\t{\\n\\t\\t\\t\\"_id\\" : 0,\\n\\t\\t\\t\\"name\\" : \\"mongodb1:27017\\",\\n\\t\\t\\t\\"health\\" : 1,\\n\\t\\t\\t\\"state\\" : 1,\\n\\t\\t\\t\\"stateStr\\" : \\"PRIMARY\\",\\n\\t\\t\\t\\"uptime\\" : 4872,\\n\\t\\t\\t\\"optime\\" : {\\n\\t\\t\\t\\t\\"ts\\" : Timestamp(1451023102, 1),\\n\\t\\t\\t\\t\\"t\\" : NumberLong(1)\\n\\t\\t\\t},\\n\\t\\t\\t\\"optimeDate\\" : ISODate(\\"2015-12-25T05:58:22Z\\"),\\n\\t\\t\\t\\"electionTime\\" : Timestamp(1451022242, 2),\\n\\t\\t\\t\\"electionDate\\" : ISODate(\\"2015-12-25T05:44:02Z\\"),\\n\\t\\t\\t\\"configVersion\\" : 3,\\n\\t\\t\\t\\"self\\" : true\\n\\t\\t},\\n\\t\\t{\\n\\t\\t\\t\\"_id\\" : 1,\\n\\t\\t\\t\\"name\\" : \\"mongodb2:27017\\",\\n\\t\\t\\t\\"health\\" : 1,\\n\\t\\t\\t\\"state\\" : 2,\\n\\t\\t\\t\\"stateStr\\" : \\"SECONDARY\\",\\n\\t\\t\\t\\"uptime\\" : 4700,\\n\\t\\t\\t\\"optime\\" : {\\n\\t\\t\\t\\t\\"ts\\" : Timestamp(1451023102, 1),\\n\\t\\t\\t\\t\\"t\\" : NumberLong(1)\\n\\t\\t\\t},\\n\\t\\t\\t\\"optimeDate\\" : ISODate(\\"2015-12-25T05:58:22Z\\"),\\n\\t\\t\\t\\"lastHeartbeat\\" : ISODate(\\"2015-12-25T07:03:17.273Z\\"),\\n\\t\\t\\t\\"lastHeartbeatRecv\\" : ISODate(\\"2015-12-25T07:03:17.601Z\\"),\\n\\t\\t\\t\\"pingMs\\" : NumberLong(0),\\n\\t\\t\\t\\"syncingTo\\" : \\"mongodb1:27017\\",\\n\\t\\t\\t\\"configVersion\\" : 3\\n\\t\\t},\\n\\t\\t{\\n\\t\\t\\t\\"_id\\" : 2,\\n\\t\\t\\t\\"name\\" : \\"mongodb3:27017\\",\\n\\t\\t\\t\\"health\\" : 1,\\n\\t\\t\\t\\"state\\" : 2,\\n\\t\\t\\t\\"stateStr\\" : \\"SECONDARY\\",\\n\\t\\t\\t\\"uptime\\" : 3896,\\n\\t\\t\\t\\"optime\\" : {\\n\\t\\t\\t\\t\\"ts\\" : Timestamp(1451023102, 1),\\n\\t\\t\\t\\t\\"t\\" : NumberLong(1)\\n\\t\\t\\t},\\n\\t\\t\\t\\"optimeDate\\" : ISODate(\\"2015-12-25T05:58:22Z\\"),\\n\\t\\t\\t\\"lastHeartbeat\\" : ISODate(\\"2015-12-25T07:03:17.273Z\\"),\\n\\t\\t\\t\\"lastHeartbeatRecv\\" : ISODate(\\"2015-12-25T07:03:14.084Z\\"),\\n\\t\\t\\t\\"pingMs\\" : NumberLong(0),\\n\\t\\t\\t\\"configVersion\\" : 3\\n\\t\\t}\\n\\t],\\n\\t\\"ok\\" : 1\\n}\\n",""]',
314+
// lines:
315+
// ['MongoDB shell version: 3.2.0',
316+
// 'connecting to: localhost:27017/test',
317+
// '{',
318+
// '\t"set" : "mongodb",',
319+
// '\t"date" : ISODate("2015-12-25T07:03:18.365Z"),',
320+
// '\t"myState" : 1,',
321+
// '\t"term" : NumberLong(1),',
322+
// '\t"heartbeatIntervalMillis" : NumberLong(2000),',
323+
// '\t"members" : [',
324+
// '\t\t{',
325+
// '\t\t\t"_id" : 0,',
326+
// '\t\t\t"name" : "mongodb1:27017",',
327+
// '\t\t\t"health" : 1,',
328+
// '\t\t\t"state" : 1,',
329+
// '\t\t\t"stateStr" : "PRIMARY",',
330+
// '\t\t\t"uptime" : 4872,',
331+
// '\t\t\t"optime" : {',
332+
// '\t\t\t\t"ts" : Timestamp(1451023102, 1),',
333+
// '\t\t\t\t"t" : NumberLong(1)',
334+
// '\t\t\t},',
335+
// '\t\t\t"optimeDate" : ISODate("2015-12-25T05:58:22Z"),',
336+
// '\t\t\t"electionTime" : Timestamp(1451022242, 2),',
337+
// '\t\t\t"electionDate" : ISODate("2015-12-25T05:44:02Z"),',
338+
// '\t\t\t"configVersion" : 3,',
339+
// '\t\t\t"self" : true',
340+
// '\t\t},',
341+
// '\t\t{',
342+
// '\t\t\t"_id" : 1,',
343+
// '\t\t\t"name" : "mongodb2:27017",',
344+
// '\t\t\t"health" : 1,',
345+
// '\t\t\t"state" : 2,',
346+
// '\t\t\t"stateStr" : "SECONDARY",',
347+
// '\t\t\t"uptime" : 4700,',
348+
// '\t\t\t"optime" : {',
349+
// '\t\t\t\t"ts" : Timestamp(1451023102, 1),',
350+
// '\t\t\t\t"t" : NumberLong(1)',
351+
// '\t\t\t},',
352+
// '\t\t\t"optimeDate" : ISODate("2015-12-25T05:58:22Z"),',
353+
// '\t\t\t"lastHeartbeat" : ISODate("2015-12-25T07:03:17.273Z"),',
354+
// '\t\t\t"lastHeartbeatRecv" : ISODate("2015-12-25T07:03:17.601Z"),',
355+
// '\t\t\t"pingMs" : NumberLong(0),',
356+
// '\t\t\t"syncingTo" : "mongodb1:27017",',
357+
// '\t\t\t"configVersion" : 3',
358+
// '\t\t},',
359+
// '\t\t{',
360+
// '\t\t\t"_id" : 2,',
361+
// '\t\t\t"name" : "mongodb3:27017",',
362+
// '\t\t\t"health" : 1,',
363+
// '\t\t\t"state" : 2,',
364+
// '\t\t\t"stateStr" : "SECONDARY",',
365+
// '\t\t\t"uptime" : 3896,',
366+
// '\t\t\t"optime" : {',
367+
// '\t\t\t\t"ts" : Timestamp(1451023102, 1),',
368+
// '\t\t\t\t"t" : NumberLong(1)',
369+
// '\t\t\t},',
370+
// '\t\t\t"optimeDate" : ISODate("2015-12-25T05:58:22Z"),',
371+
// '\t\t\t"lastHeartbeat" : ISODate("2015-12-25T07:03:17.273Z"),',
372+
// '\t\t\t"lastHeartbeatRecv" : ISODate("2015-12-25T07:03:14.084Z"),',
373+
// '\t\t\t"pingMs" : NumberLong(0),',
374+
// '\t\t\t"configVersion" : 3',
375+
// '\t\t}',
376+
// '\t],',
377+
// '\t"ok" : 1',
378+
// '}',
379+
// ''],
380+
// object:
381+
// {
382+
// set: 'mongodb',
383+
// date: { '$date': '2015-12-25T07:03:18.365Z' },
384+
// myState: 1,
385+
// term: { '$numberLong': '1' },
386+
// heartbeatIntervalMillis: { '$numberLong': '2000' },
387+
// members:
388+
// [{
389+
// _id: 0,
390+
// name: 'mongodb1:27017',
391+
// health: 1,
392+
// state: 1,
393+
// stateStr: 'PRIMARY',
394+
// uptime: 4872,
395+
// optime:
396+
// {
397+
// ts: { '$timestamp': { t: 1451023102, i: 1 } },
398+
// t: { '$numberLong': '1' }
399+
// },
400+
// optimeDate: { '$date': '2015-12-25T05:58:22Z' },
401+
// electionTime: { '$timestamp': { t: 1451022242, i: 2 } },
402+
// electionDate: { '$date': '2015-12-25T05:44:02Z' },
403+
// configVersion: 3,
404+
// self: true
405+
// },
406+
// {
407+
// _id: 1,
408+
// name: 'mongodb2:27017',
409+
// health: 1,
410+
// state: 2,
411+
// stateStr: 'SECONDARY',
412+
// uptime: 4700,
413+
// optime:
414+
// {
415+
// ts: { '$timestamp': { t: 1451023102, i: 1 } },
416+
// t: { '$numberLong': '1' }
417+
// },
418+
// optimeDate: { '$date': '2015-12-25T05:58:22Z' },
419+
// lastHeartbeat: { '$date': '2015-12-25T07:03:17.273Z' },
420+
// lastHeartbeatRecv: { '$date': '2015-12-25T07:03:17.601Z' },
421+
// pingMs: { '$numberLong': '0' },
422+
// syncingTo: 'mongodb1:27017',
423+
// configVersion: 3
424+
// },
425+
// {
426+
// _id: 2,
427+
// name: 'mongodb3:27017',
428+
// health: 1,
429+
// state: 2,
430+
// stateStr: 'SECONDARY',
431+
// uptime: 3896,
432+
// optime:
433+
// {
434+
// ts: { '$timestamp': { t: 1451023102, i: 1 } },
435+
// t: { '$numberLong': '1' }
436+
// },
437+
// optimeDate: { '$date': '2015-12-25T05:58:22Z' },
438+
// lastHeartbeat: { '$date': '2015-12-25T07:03:17.273Z' },
439+
// lastHeartbeatRecv: { '$date': '2015-12-25T07:03:14.084Z' },
440+
// pingMs: { '$numberLong': '0' },
441+
// configVersion: 3
442+
// }],
443+
// ok: 1
444+
// }
445+
//}
446+
```

lib/index.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ var extractResult = function (result) {
9898
run: function (resultp) {
9999
return objectStartingRow3(resultp);
100100
}
101+
},
102+
{
103+
re: /rs.status/,
104+
run: function (resultp) {
105+
return objectStartingRow3(resultp);
106+
}
101107
}
102108
];
103109

@@ -134,7 +140,7 @@ var objectStartingRow3 = function (resultp) {
134140
var cleaned = cleanJson(currentValue.trim());
135141
return previousValue + cleaned;
136142
}, '');
137-
//console.log('jsonString', jsonString);
143+
console.log('jsonString', jsonString);
138144
resultp.object = JSON.parse(jsonString);
139145

140146
} catch (e) {
@@ -147,7 +153,7 @@ var objectStartingRow3 = function (resultp) {
147153

148154

149155
var cleanJson = function (line) {
150-
var parts, re, str, m, value;
156+
var parts, re, str, m, value, value2, comma;
151157

152158
if (line.indexOf('ISODate') > -1) {
153159
parts = line.split(':');
@@ -186,7 +192,26 @@ var cleanJson = function (line) {
186192
} else if (line.indexOf('NumberLong') > -1) {
187193
parts = line.split(':');
188194

189-
re = /NumberLong\((.*)/g;
195+
re = /NumberLong\((.*)\)(,*)/g;
196+
str = line;
197+
198+
while ((m = re.exec(str)) !== null) {
199+
if (m.index === re.lastIndex) {
200+
re.lastIndex++;
201+
}
202+
// View your result using the m-variable.
203+
// eg m[0] etc.
204+
//console.log('m', m);
205+
value = m && m[1] ? m[1] : 'error';
206+
comma = m && m[2] ? m[2] : '';
207+
}
208+
209+
return util.format('%s : { "$numberLong" : "%s" }%s', parts[0], value,comma);
210+
211+
} else if (line.indexOf('Timestamp') > -1) {
212+
parts = line.split(':');
213+
214+
re = /Timestamp\((\d*),\s*(\d*)\)/g;
190215
str = line;
191216

192217
while ((m = re.exec(str)) !== null) {
@@ -197,9 +222,11 @@ var cleanJson = function (line) {
197222
// eg m[0] etc.
198223
//console.log('m', m);
199224
value = m && m[1] ? m[1] : 'error';
225+
value2 = m && m[2] ? m[2] : 'error';
200226
}
227+
//return '';
228+
return util.format('%s : { "$timestamp" : { "t" : %s, "i" : %s } },', parts[0], value, value2);
201229

202-
return util.format('%s : { "$numberLong" : "%s" },', parts[0], value);
203230

204231
} else {
205232

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongo-cli-js",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "A node.js wrapper for the mongo CLI",
55
"main": "lib/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)