peerStats 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env php
  2. <?php
  3. require 'Bencode.php';
  4. require 'Cjdns.php';
  5. require 'publicKey2ipv6.php';
  6. define('PASSWORD', 'NONE');
  7. $cjdns = new Cjdns(PASSWORD, "127.0.0.1", 11234);
  8. $peers = []; $page = 0;
  9. do
  10. {
  11. $res = $cjdns->call("InterfaceController_peerStats", ['page' => $page++]);
  12. $peers = array_merge($peers, $res['peers']);
  13. } while(!empty($res['more']));
  14. $table = new table();
  15. $table->add(['Public Key', 'Ipv6', 'State', 'Last', 'Bytes In', 'Bytes Out', 'Lost', 'Ver', 'In']);
  16. foreach ($peers as $a)
  17. {
  18. $pK = preg_match('/.{23}([a-z0-6]{4}).+(.{6})$/', $a['addr'], $m) ? $m[1].'...'.$m[2] : '?';
  19. $ip = preg_replace('#.+:#', ':', publicKey2ipv6(explode('.', $a['addr'])[5]));
  20. $ver = preg_match('/^v(\d+)/', $a['addr'], $m) ? $m[1] : '?';
  21. $table->add([$pK, $ip, $a['state'], date('H:i:s d.m.y', $a['last']/1000),
  22. _h($a['bytesIn']), _h($a['bytesOut']), $a['lostPackets'],
  23. $ver, $a['isIncoming']]);
  24. }
  25. $table->printSimple();
  26. // human readable bytes
  27. function _h($x)
  28. {
  29. if ($x < 1024) return sprintf("%5.2f", $x); $x /= 1024;
  30. if ($x < 1024) return sprintf("%5.2f Kb", $x); $x /= 1024;
  31. if ($x < 1024) return sprintf("%5.2f Mb", $x); $x /= 1024;
  32. if ($x < 1024) return sprintf("%5.2f Gb", $x); $x /= 1024;
  33. return $x;
  34. }
  35. // class for draw table data
  36. class table
  37. {
  38. private $data = [];
  39. private $width = [];
  40. private $cur_row = 0;
  41. public function add($a)
  42. {
  43. array_push($this->data, $a);
  44. foreach ($a as $i => $x)
  45. if (@$this->width[$i] < strlen($x))
  46. $this->width[$i] = strlen($x);
  47. }
  48. public function printSimple()
  49. {
  50. $this->printLine();
  51. $this->printRows(1); // header
  52. $this->printLine();
  53. $this->printRows();
  54. $this->printLine();
  55. }
  56. public function printLine()
  57. {
  58. foreach ($this->width as $w)
  59. {
  60. printf("+");
  61. printf($this->strdup($w+2));
  62. }
  63. printf("+\n");
  64. }
  65. public function printRows($n = NULL)
  66. {
  67. if (is_null($n)) $n = count($this->data);
  68. for ($this->cur_row; $n && $this->cur_row<count($this->data); $this->cur_row++, $n--)
  69. {
  70. foreach ($this->width as $i => $w)
  71. {
  72. $this->printCell($this->data[$this->cur_row][$i], $w);
  73. }
  74. printf("|\n");
  75. }
  76. }
  77. private function strdup($n,$x='-') { $st=''; for ($i=0;$i<$n;$i++)$st.=$x; return $st; }
  78. private function printCell($data, $width)
  79. {
  80. if (empty($data)) $data = ''; // clear zero-value
  81. if (!is_numeric($data)) // align string to center
  82. {
  83. $d = ($width - strlen($data)) >> 1;
  84. $data .= $this->strdup($d, ' ');
  85. }
  86. printf("| %{$width}s ", $data);
  87. }
  88. }