Ignore:
Timestamp:
May 19, 2008, 7:25:54 AM (17 years ago)
Author:
george
Message:

Upraveno: Přepracování a pročištění skriptů.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • measure_scripts/system.php

    r30 r31  
    7373  } else
    7474  {
    75     $cpuUSERcent=0;
    76     $cpuSYSTcent=0;
    77     $cpuIDLEcent=0;
     75    $cpuUSERcent  =0;
     76    $cpuSYSTcent = 0;
     77    $cpuIDLEcent = 0;
    7878  }
    79   $cpuUSERprev=$cpuUSER;
    80   $cpuSYSTprev=$cpuSYST;
    81   $cpuIDLEprev=$cpuIDLE;
    82   return(100-round($cpuIDLEcent*100)/100);
     79  $cpuUSERprev = $cpuUSER;
     80  $cpuSYSTprev = $cpuSYST;
     81  $cpuIDLEprev = $cpuIDLE;
     82  return(100 - round($cpuIDLEcent * 100) / 100);
    8383}
    8484
    8585function GetNetworkState()
    8686
    87   // Get last values
    88   $File = fopen('/tmp/traffic', 'r');
    89   $LastResult = unserialize(fgets($File));
    90   fclose($File);
     87  global $LastNetworkState;
    9188 
    92   $Result = array('time' => time());
     89  if(!isset($LastNetworkState)) $LastNetworkState = array();
     90  $NetworkState = array('Time' => time());
    9391  $Output = array();
    9492  exec('cat /proc/net/dev', $Output);
    95   array_shift($Output);
    96   array_shift($Output);
     93  array_shift($Output); // Skip header
     94  array_shift($Output); // Skip header
    9795  foreach($Output as $Item)
    9896  {
    99 //    echo($Item."\n");
    100     while(strpos($Item, '  ') !== false) $Item = str_replace('  ', ' ', $Item);
     97    while(strpos($Item, '  ') !== false) $Item = str_replace('  ', ' ', $Item);  // Rrmove multiple spaces
    10198    $Item = explode(':', $Item);
    10299    $Interface = trim($Item[0]);
    103100    $Item = explode(' ', trim($Item[1]));
    104     $Period = time() - $LastResult['time'];
    105     $Result[$Interface] = array('down' => $Item[0], 'up' => $Item[8]);
    106     $Result[$Interface]['down_avg'] = round(($Result[$Interface]['down'] - $LastResult[$Interface]['down']) / $Period);
    107     $Result[$Interface]['up_avg'] = round(($Result[$Interface]['up'] - $LastResult[$Interface]['up']) / $Period);
    108     if($Result[$Interface]['down_avg'] < 0) $Result[$Interface]['down_avg'] = 0;
    109     if($Result[$Interface]['up_avg'] < 0) $Result[$Interface]['up_avg'] = 0;
     101    $NetworkState[$Interface] = array('Down' => $Item[0], 'Up' => $Item[8]);
     102    if(array_key_exists($Interface, $LastNetworkState))
     103    {
     104      $Period = time() - $LastNetworkState['Time'];
     105      $NetworkState[$Interface]['DownAverage'] = round(($NetworkState[$Interface]['Down'] - $LastNetworkState[$Interface]['Down']) / $Period);
     106      $NetworkState[$Interface]['UpAverage'] = round(($NetworkState[$Interface]['Up'] - $LastNetworkState[$Interface]['Up']) / $Period);
     107    } else
     108    {
     109      $NetworkState[$Interface]['DownAverage'] = 0;
     110      $NetworkState[$Interface]['UpAverage'] = 0;
     111    }
     112    if($NetworkState[$Interface]['DownAverage'] < 0) $NetworkState[$Interface]['DownAverage'] = 0;
     113    if($NetworkState[$Interface]['UpAverage'] < 0) $NetworkState[$Interface]['UpAverage'] = 0;
    110114  }
    111   // Save last values
    112   $File = fopen('/tmp/traffic', 'w+');
    113   fputs($File, serialize($Result));
    114   fclose($File);
    115 //  print_r($Result);
    116   return($Result);
     115  $LastNetworkState = $NetworkState;
     116  return($NetworkState);
    117117}
    118118
    119 function TeamSpeak()
     119function NetworkServiceConnectionCount($Port)
    120120{
     121  $HostIP = gethostbyname(trim(`hostname`));
    121122  $Output = array();
    122   exec('cat /proc/net/nf_conntrack|grep "dst=192.168.0.14 "|grep "dport=8767 "|grep "ASSURED"', $Output);
     123  exec('cat /proc/net/nf_conntrack|grep "dst='.$HostIP.' "|grep "dport='.$Port.' "|grep "ASSURED"', $Output);
    123124  return(count($Output));
    124125}
Note: See TracChangeset for help on using the changeset viewer.