Changeset 39


Ignore:
Timestamp:
Mar 21, 2009, 9:38:58 AM (15 years ago)
Author:
george
Message:
  • Upraveno: Funkce pro zjištění zátěže CPU nyní uchovává poslední stav v jedné proměnné. * Upraveno: Skript monitor.php se nyní spouští periodicky pomocí cronu namísto vnitřní nekonečné smyčky. Stavové informace pro některé funce se nyní ukládáají do temp souborů.
Location:
trunk/measure_scripts
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/measure_scripts/monitor_sample.php

    r37 r39  
    22
    33// Toto je ukázkový skript pro aktualizaci dat. Zkopírujte si jej jako monitor.php a upravte podle potřeby.
     4// Skript spouštět přes crona každou minutu
    45
    56include('../global.php');
     
    78include('wow.php');
    89
    9 //GetNetworkState();
    10 while(1)
    11 {
    12   $NetworkState = GetNetworkState();
    13   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=3&Value='.MemoryUsage());
    14   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=4&Value='.CpuUsage());
    15   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=5&Value='.WoWPlayersOnline());
    16   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=6&Value='.WoWEmulatorRestartCount());
    17   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=7&Value='.DiskFree());
    18   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=8&Value='.WoWAccountCount());
    19   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=10&Value='.WoWGMOnline());
    20   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=11&Value='.round($NetworkState['eth1']['DownAverage']));
    21   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=12&Value='.round($NetworkState['eth1']['UpAverage']));
    22   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=13&Value='.WoWCharacterCount());
    23   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=14&Value='.WoWGuildCount());
    24   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=15&Value='.WoWEmulatorAvailability());
    25   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=16&Value='.Ping());
    26   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=17&Value='.TeamSpeak());
    27   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=18&Value='.WoWTranslatedQuestsCount());
    28   file_get_contents($Config['AddNewValueUrl'].'?MeasureId=19&Value='.DiskUtilization());
     10$LastNetworkState = unserializa(file_get_contents('/tmp/LastNetworkState'));
     11$LastCpuUsage = unserializa(file_get_contents('/tmp/LastCpuUsage'));
    2912
    30   sleep(58);  // 60(measure period) - 2(disk utilization)
    31 }
     13$NetworkState = GetNetworkState();
     14file_get_contents($Config['AddNewValueUrl'].'?MeasureId=3&Value='.MemoryUsage());
     15file_get_contents($Config['AddNewValueUrl'].'?MeasureId=4&Value='.CpuUsage());
     16file_get_contents($Config['AddNewValueUrl'].'?MeasureId=5&Value='.WoWPlayersOnline());
     17file_get_contents($Config['AddNewValueUrl'].'?MeasureId=6&Value='.WoWEmulatorRestartCount());
     18file_get_contents($Config['AddNewValueUrl'].'?MeasureId=7&Value='.DiskFree());
     19file_get_contents($Config['AddNewValueUrl'].'?MeasureId=8&Value='.WoWAccountCount());
     20file_get_contents($Config['AddNewValueUrl'].'?MeasureId=10&Value='.WoWGMOnline());
     21file_get_contents($Config['AddNewValueUrl'].'?MeasureId=11&Value='.round($NetworkState['eth1']['DownAverage']));
     22file_get_contents($Config['AddNewValueUrl'].'?MeasureId=12&Value='.round($NetworkState['eth1']['UpAverage']));
     23file_get_contents($Config['AddNewValueUrl'].'?MeasureId=13&Value='.WoWCharacterCount());
     24file_get_contents($Config['AddNewValueUrl'].'?MeasureId=14&Value='.WoWGuildCount());
     25file_get_contents($Config['AddNewValueUrl'].'?MeasureId=15&Value='.WoWEmulatorAvailability());
     26file_get_contents($Config['AddNewValueUrl'].'?MeasureId=16&Value='.Ping());
     27file_get_contents($Config['AddNewValueUrl'].'?MeasureId=17&Value='.TeamSpeak());
     28file_get_contents($Config['AddNewValueUrl'].'?MeasureId=18&Value='.WoWTranslatedQuestsCount());
     29file_get_contents($Config['AddNewValueUrl'].'?MeasureId=19&Value='.DiskUtilization());
     30
     31file_put_contents('/tmp/LastNetworkState', serialize($LastNetworkState));
     32file_put_contents('/tmp/LastCpuUsage', serialize($LastCpuUsage));
     33
    3234?>
  • trunk/measure_scripts/system.php

    r37 r39  
    2323}
    2424
    25 function Ping()
     25function Ping($Host = 'nix.cz')
    2626
    27   exec('ping nix.cz -c 1 -W 1|grep time=', $Row);
     27  exec('ping '.$Host.' -c 1 -W 1|grep time=', $Row);
    2828  // W - timeout in seconds
    2929  // c - ping count
     
    4747  while(strpos($Row, '  ') !== false) $Row = str_replace('  ', ' ', $Row);
    4848  $RowParts2 = explode(' ', $Row);
    49   return($RowParts[2] + $RowParts2[2]); 
     49  return($RowParts[2] + $RowParts2[2]);
    5050}
    5151
    5252function CpuUsage()
    53 
    54   global $cpuIDLEprev, $cpuSYSTprev, $cpuUSERprev;
     53{
     54  global $LastCpuUsage;
     55  //$cpuIDLEprev, $cpuSYSTprev, $cpuUSERprev;
     56
    5557  // get processor usage seconds for pct stats ###
    5658  $File = fopen('/proc/stat', 'r');
     
    5860  fclose($File);
    5961  $Parts = explode(' ', $Row);
    60   $cpuUSER = $Parts[2] + $Parts[3];
    61   $cpuSYST = $Parts[4];
    62   $cpuIDLE = $Parts[5];
    63   $cpuUSERdiff = ($cpuUSER - $cpuUSERprev);
    64   $cpuSYSTdiff = ($cpuSYST - $cpuSYSTprev);
    65   $cpuIDLEdiff = ($cpuIDLE - $cpuIDLEprev);
    66   $cpuIDLEdiffTOTAL = (($cpuUSERdiff + $cpuSYSTdiff) + $cpuIDLEdiff);
    67   if ($cpuIDLEdiffTOTAL > 0)
     62  $CpuUsage['User'] = $Parts[2] + $Parts[3];
     63  $CpuUsage['System'] = $Parts[4];
     64  $CpuUsage['Idle'] = $Parts[5];
     65  $CpuUsageDiff['User'] = ($CpuUsage['User'] - $LastCpuUsage['User']);
     66  $CpuUsageDiff['System'] = ($CpuUsage['System'] - $LastCpuUsage['System']);
     67  $CpuUsageDiff['Idle'] = ($CpuUsage['Idle'] - $LastCpuUsage['Idle']);
     68  $CpuUsageDiffTotal = (($CpuUsageDiff['User'] + $CpuUsageDiff['System']) + $CpuUsageDiff['Idle']);
     69  if ($CpuUsageDiffTotal > 0)
    6870  {
    69     $cpuUSERcent = ($cpuUSERdiff / $cpuIDLEdiffTOTAL) * 100;
    70     $cpuSYSTcent = ($cpuSYSTdiff / $cpuIDLEdiffTOTAL) * 100;
    71     $cpuIDLEcent = ($cpuIDLEdiff / $cpuIDLEdiffTOTAL * 100);
     71    $CpuUsagePercent['User'] = ($CpuUsageDiff['User'] / $CpuUsageDiffTotal) * 100;
     72    $CpuUsagePercent['System'] = ($CpuUsageDiff['System'] / $CpuUsageDiffTotal) * 100;
     73    $CpuUsagePercent['Idle'] = ($CpuUsageDiff['Idle'] / $CpuUsageDiffTotal * 100);
    7274  } else
    7375  {
    74     $cpuUSERcent  =0;
    75     $cpuSYSTcent = 0;
    76     $cpuIDLEcent = 0;
     76    $CpuUsagePercent['User'] = 0;
     77    $CpuUsagePercent['System'] = 0;
     78    $CpuUsagePercent['Idle'] = 0;
    7779  }
    78   $cpuUSERprev = $cpuUSER;
    79   $cpuSYSTprev = $cpuSYST;
    80   $cpuIDLEprev = $cpuIDLE;
    81   return(100 - round($cpuIDLEcent * 100) / 100);
     80  $LastCpuUsage = $CpuUsage;
     81  return(100 - round($CpuUsagePercent['Idle'], 2));
    8282}
    8383
    8484function GetNetworkState()
    85 { 
     85{
    8686  global $LastNetworkState;
    8787 
     
    124124}
    125125
    126 function DiskUtilization()
     126function DiskUtilization($Device)
    127127{
    128128  $Output = array();
    129   exec('iostat -d sda -x -m 2 2', $Output);   // 2 second measure delay
     129  exec('iostat -d '.$Device.' -x -m 2 2', $Output);   // 2 second measure delay
    130130  $Row = $Output[6];
    131131  while(strpos($Row, '  ') !== false) $Row = str_replace('  ', ' ', $Row);
     
    135135}
    136136
    137 function DiskFree()
     137function DiskFree($Path)
    138138{
    139   return(disk_free_space('/'));
     139  return(disk_free_space($Path));
    140140}
    141141
Note: See TracChangeset for help on using the changeset viewer.