| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- class OpenVPNStatus {
- function __construct($logfile = NULL, $ippfile = NULL) {
- if( file_exists( $logfile ) )
- {
- $this->parse_logfile( $logfile );
- }
- if( file_exists( $ippfile ) )
- {
- $this->parse_ippfile( $ippfile );
- }
- }
- private function parse_logfile( $filename ) {
- $contents = file_get_contents( $filename );
- //echo "<h1>$filename</h1><pre>$contents</pre>";
- /* Parse statusfile */
- $log = array();
- $log = explode("\n", $contents);
- foreach($log as $entry)
- {
- $entry = explode(",", $entry);
- if( sizeof($entry) == 5 )
- {
- $current_entry['hostname'] = $entry[0];
- $current_entry['ip'] = $entry[1];
- $current_entry['received'] = $this->bytesToSize($entry[2]);
- $current_entry['send'] = $this->bytesToSize($entry[3]);
- $current_entry['total'] = $this->bytesToSize($entry[2] + $entry[3]);
- $current_entry['date'] = $entry[4];
- $this->logfile[] = $current_entry;
- }
- }
- //echo '<pre>';
- //echo var_dump( $this->logfile );
- //echo '</pre>';
- return $this->logfile;
- }
- private function parse_ippfile( $filename ) {
- $contents = file_get_contents( $filename );
- echo "<h1>$filename</h1><pre>$contents</pre>";
- /* Parse hostfile */
- $ipp = array();
- $ipp = explode("\n", $contents);
- foreach($ipp as $entry)
- {
- /* Dont process empty entries */
- if( $entry == NULL )
- break;
- /* Split and organise entry */
- $entry = explode(',', $entry);
- $host['hostname'] = $entry[0];
- $host['ip'] = $entry[1];
- $host['remote_ip'] = "";
- $host['date'] = "";
- $host['received'] = "";
- $host['send'] = "";
- /* Check ipp against status log */
- foreach($this->logfile as $entry)
- {
- if( $host['hostname'] == $entry['hostname'] )
- {
- $host['up'] = true;
- $host['remote_ip'] = $entry['ip'];
- $host['date'] = $entry['date'];
- $host['received'] = $entry['received'];
- $host['send'] = $entry['send'];
- break;
- }
- else
- {
- $host['up'] = false;
- }
- }
- /* Append entry */
- $this->ippfile[] = $host;
- }
- echo '<pre>';
- echo var_dump( $this->ippfile );
- echo '</pre>';
- }
- private function bytesToSize($bytes, $precision = 2) {
- $kilobyte = 1024;
- $megabyte = $kilobyte * 1024;
- $gigabyte = $megabyte * 1024;
- $terabyte = $gigabyte * 1024;
- if (($bytes >= 0) && ($bytes < $kilobyte))
- return $bytes . ' B';
- elseif (($bytes >= $kilobyte) && ($bytes < $megabyte))
- return round($bytes / $kilobyte, $precision) . ' KB';
- elseif (($bytes >= $megabyte) && ($bytes < $gigabyte))
- return round($bytes / $megabyte, $precision) . ' MB';
- elseif (($bytes >= $gigabyte) && ($bytes < $terabyte))
- return round($bytes / $gigabyte, $precision) . ' GB';
- elseif ($bytes >= $terabyte)
- return round($bytes / $terabyte, $precision) . ' TB';
- else
- return $bytes . ' B';
- }
- };
- ?>
|