1
0

peerStats 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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', 'User', '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. $date = str_replace(date(' d.m.y'), '', date('H:i:s d.m.y', $a['last']/1000));
  22. $table->add([$pK, $ip, $a['user'], $a['state'], $date,
  23. _h($a['bytesIn']), _h($a['bytesOut']), $a['lostPackets'],
  24. $ver, $a['isIncoming']]);
  25. }
  26. $table->printSimple();
  27. // human readable bytes
  28. function _h($x)
  29. {
  30. if ($x < 1024) return sprintf("%5.2f", $x); $x /= 1024;
  31. if ($x < 1024) return sprintf("%5.2f Kb", $x); $x /= 1024;
  32. if ($x < 1024) return sprintf("%5.2f Mb", $x); $x /= 1024;
  33. if ($x < 1024) return sprintf("%5.2f Gb", $x); $x /= 1024;
  34. return $x;
  35. }
  36. // class for draw table data
  37. class table
  38. {
  39. private $data = [];
  40. private $width = [];
  41. private $cur_row = 0;
  42. public function add($a)
  43. {
  44. array_push($this->data, $a);
  45. foreach ($a as $i => $x)
  46. if (@$this->width[$i] < strlen($x))
  47. $this->width[$i] = strlen($x);
  48. }
  49. public function printSimple()
  50. {
  51. $this->printLine();
  52. $this->printRows(1); // header
  53. $this->printLine();
  54. $this->printRows();
  55. $this->printLine();
  56. }
  57. public function printLine()
  58. {
  59. foreach ($this->width as $w)
  60. {
  61. printf("+");
  62. printf($this->strdup($w+2));
  63. }
  64. printf("+\n");
  65. }
  66. public function printRows($n = NULL)
  67. {
  68. if (is_null($n)) $n = count($this->data);
  69. for ($this->cur_row; $n && $this->cur_row<count($this->data); $this->cur_row++, $n--)
  70. {
  71. foreach ($this->width as $i => $w)
  72. {
  73. $this->printCell($this->data[$this->cur_row][$i], $w);
  74. }
  75. printf("|\n");
  76. }
  77. }
  78. private function strdup($n,$x='-') { $st=''; for ($i=0;$i<$n;$i++)$st.=$x; return $st; }
  79. private function printCell($data, $width)
  80. {
  81. if (empty($data)) $data = ''; // clear zero-value
  82. if (!is_numeric($data)) // align string to center
  83. {
  84. $d = ($width - strlen($data)) >> 1;
  85. $data .= $this->strdup($d, ' ');
  86. }
  87. printf("| %{$width}s ", $data);
  88. }
  89. }