sessionStats 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env node
  2. /* -*- Mode:Js */
  3. /* vim: set expandtab ts=4 sw=4: */
  4. /*
  5. * You may redistribute this program and/or modify it under the terms of
  6. * the GNU General Public License as published by the Free Software Foundation,
  7. * either version 3 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. var Cjdns = require('../contrib/nodejs/cjdnsadmin/cjdnsadmin');
  18. var nThen = require('../contrib/nodejs/cjdnsadmin/nthen');
  19. var printSession = function (session) {
  20. var state = session.state.replace(/CryptoAuth_/,'');
  21. while (state.length < ('ESTABLISHED').length) { state = state + ' ' }
  22. var out = [ session.addr, ' ', state, ' ', session.handle, ' ', session.sendHandle ];
  23. if (Number(session.duplicates) !== 0) { out.push(' DUP ', session.duplicates); }
  24. if (Number(session.lostPackets) !== 0) { out.push(' LOS ', session.lostPackets); }
  25. if (Number(session.receivedOutOfRange) !== 0) { out.push(' OOR ', session.receivedOutOfRange); }
  26. console.log(out.join(''));
  27. };
  28. var cjdns;
  29. var handles = [];
  30. var sessions = [];
  31. nThen(function (waitFor) {
  32. Cjdns.connectWithAdminInfo(waitFor(function (c) { cjdns = c; }));
  33. }).nThen(function (waitFor) {
  34. var more = function (i) {
  35. cjdns.SessionManager_getHandles(i, waitFor(function (err, ret) {
  36. if (err) { throw err; }
  37. handles.push.apply(handles, ret.handles);
  38. if (ret.more) { more(i+1); }
  39. }));
  40. };
  41. more(0);
  42. }).nThen(function (waitFor) {
  43. var next = function (i) {
  44. cjdns.SessionManager_sessionStats(Number(handles[i]), waitFor(function (err, ret) {
  45. if (err) { throw err; }
  46. sessions.push(ret);
  47. i++
  48. if (i < handles.length) { next(i); }
  49. }));
  50. };
  51. if (handles.length) {
  52. next(0);
  53. } else {
  54. console.log("No active sessions");
  55. }
  56. }).nThen(function (waitFor) {
  57. cjdns.disconnect();
  58. sessions.sort(function (a, b) {
  59. return (a.addr.substring(a.addr.indexOf('.')) < b.addr.substring(b.addr.indexOf('.')))
  60. ? -1 : 1;
  61. });
  62. for (var i = 0; i < sessions.length; i++) {
  63. printSession(sessions[i]);
  64. }
  65. });