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ů.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.