openvpn.inc.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. class OpenVPNStatus {
  3. function __construct($logfile = NULL, $ippfile = NULL) {
  4. if( file_exists( $logfile ) )
  5. {
  6. $this->parse_logfile( $logfile );
  7. }
  8. if( file_exists( $ippfile ) )
  9. {
  10. $this->parse_ippfile( $ippfile );
  11. }
  12. }
  13. private function parse_logfile( $filename ) {
  14. $contents = file_get_contents( $filename );
  15. //echo "<h1>$filename</h1><pre>$contents</pre>";
  16. /* Parse statusfile */
  17. $log = array();
  18. $log = explode("\n", $contents);
  19. foreach($log as $entry)
  20. {
  21. $entry = explode(",", $entry);
  22. if( sizeof($entry) == 5 )
  23. {
  24. $current_entry['hostname'] = $entry[0];
  25. $current_entry['ip'] = $entry[1];
  26. $current_entry['received'] = $this->bytesToSize($entry[2]);
  27. $current_entry['send'] = $this->bytesToSize($entry[3]);
  28. $current_entry['total'] = $this->bytesToSize($entry[2] + $entry[3]);
  29. $current_entry['date'] = $entry[4];
  30. $this->logfile[] = $current_entry;
  31. }
  32. }
  33. //echo '<pre>';
  34. //echo var_dump( $this->logfile );
  35. //echo '</pre>';
  36. return $this->logfile;
  37. }
  38. private function parse_ippfile( $filename ) {
  39. $contents = file_get_contents( $filename );
  40. echo "<h1>$filename</h1><pre>$contents</pre>";
  41. /* Parse hostfile */
  42. $ipp = array();
  43. $ipp = explode("\n", $contents);
  44. foreach($ipp as $entry)
  45. {
  46. /* Dont process empty entries */
  47. if( $entry == NULL )
  48. break;
  49. /* Split and organise entry */
  50. $entry = explode(',', $entry);
  51. $host['hostname'] = $entry[0];
  52. $host['ip'] = $entry[1];
  53. $host['remote_ip'] = "";
  54. $host['date'] = "";
  55. $host['received'] = "";
  56. $host['send'] = "";
  57. /* Check ipp against status log */
  58. foreach($this->logfile as $entry)
  59. {
  60. if( $host['hostname'] == $entry['hostname'] )
  61. {
  62. $host['up'] = true;
  63. $host['remote_ip'] = $entry['ip'];
  64. $host['date'] = $entry['date'];
  65. $host['received'] = $entry['received'];
  66. $host['send'] = $entry['send'];
  67. break;
  68. }
  69. else
  70. {
  71. $host['up'] = false;
  72. }
  73. }
  74. /* Append entry */
  75. $this->ippfile[] = $host;
  76. }
  77. echo '<pre>';
  78. echo var_dump( $this->ippfile );
  79. echo '</pre>';
  80. }
  81. private function bytesToSize($bytes, $precision = 2) {
  82. $kilobyte = 1024;
  83. $megabyte = $kilobyte * 1024;
  84. $gigabyte = $megabyte * 1024;
  85. $terabyte = $gigabyte * 1024;
  86. if (($bytes >= 0) && ($bytes < $kilobyte))
  87. return $bytes . ' B';
  88. elseif (($bytes >= $kilobyte) && ($bytes < $megabyte))
  89. return round($bytes / $kilobyte, $precision) . ' KB';
  90. elseif (($bytes >= $megabyte) && ($bytes < $gigabyte))
  91. return round($bytes / $megabyte, $precision) . ' MB';
  92. elseif (($bytes >= $gigabyte) && ($bytes < $terabyte))
  93. return round($bytes / $gigabyte, $precision) . ' GB';
  94. elseif ($bytes >= $terabyte)
  95. return round($bytes / $terabyte, $precision) . ' TB';
  96. else
  97. return $bytes . ' B';
  98. }
  99. };
  100. ?>