<?php

// Funkce pro získání stavových informací o systému

function ErrorHandler($errno,$errmsg,$filename,$linenum,$vars)
{
}  

function CheckPortStatus($Ip, $Port)
{
  set_error_handler('ErrorHandler');
  //error_reporting(0);
  if($Fp1 = fsockopen($Ip, $Port, $ERROR_NO, $ERROR_STR,(float)0.5))
  {
    fclose($Fp1);
    return true;
  } else
  {
    //echo($ERROR_NO.','.$ERROR_STR);
    //die();
    return false;
  }
  restore_error_handler();
}

function Ping()
{  
  exec('ping nix.cz -c 1 -W 1|grep time=', $Row);
  // W - timeout in seconds
  // c - ping count
  $Parts = explode(' ', $Row[0]);
  if(count($Parts) > 6)
  {
    $Time = $Parts[7];
    $TimeParts = explode('=', $Time);
    return($TimeParts[1]);
  } else return(0);
}

function MemoryUsage()
{
  $Output = array();
  exec('free -b', $Output);
  $Row = $Output[2];
  while(strpos($Row, '  ') !== false) $Row = str_replace('  ', ' ', $Row);
  $RowParts = explode(' ', $Row);
  $Row = $Output[3];
  while(strpos($Row, '  ') !== false) $Row = str_replace('  ', ' ', $Row);
  $RowParts2 = explode(' ', $Row);
  return($RowParts[2] + $RowParts2[2]);  
}

function CpuUsage()
{  
  global $cpuIDLEprev, $cpuSYSTprev, $cpuUSERprev;
  // get processor usage seconds for pct stats ###
  $File = fopen('/proc/stat', 'r');
  $Row = fgets($File);
  fclose($File);
  $Parts = explode(' ', $Row);
  $cpuUSER = $Parts[2] + $Parts[3];
  $cpuSYST = $Parts[4];
  $cpuIDLE = $Parts[5];
  $cpuUSERdiff = ($cpuUSER - $cpuUSERprev);
  $cpuSYSTdiff = ($cpuSYST - $cpuSYSTprev);
  $cpuIDLEdiff = ($cpuIDLE - $cpuIDLEprev);
  $cpuIDLEdiffTOTAL = (($cpuUSERdiff + $cpuSYSTdiff) + $cpuIDLEdiff);
  if ($cpuIDLEdiffTOTAL > 0)
  {
    $cpuUSERcent = ($cpuUSERdiff / $cpuIDLEdiffTOTAL) * 100;
    $cpuSYSTcent = ($cpuSYSTdiff / $cpuIDLEdiffTOTAL) * 100;
    $cpuIDLEcent = ($cpuIDLEdiff / $cpuIDLEdiffTOTAL * 100);
  } else
  {
    $cpuUSERcent=0;
    $cpuSYSTcent=0;
    $cpuIDLEcent=0;
  }
  $cpuUSERprev=$cpuUSER;
  $cpuSYSTprev=$cpuSYST;
  $cpuIDLEprev=$cpuIDLE;
  return(100-round($cpuIDLEcent*100)/100);
}

function GetNetworkState()
{  
  // Get last values
  $File = fopen('/tmp/traffic', 'r');
  $LastResult = unserialize(fgets($File));
  fclose($File);
  
  $Result = array('time' => time());
  $Output = array();
  exec('cat /proc/net/dev', $Output);
  array_shift($Output);
  array_shift($Output);
  foreach($Output as $Item)
  { 
//    echo($Item."\n");
    while(strpos($Item, '  ') !== false) $Item = str_replace('  ', ' ', $Item);
    $Item = explode(':', $Item);
    $Interface = trim($Item[0]);
    $Item = explode(' ', trim($Item[1]));
    $Period = time() - $LastResult['time'];
    $Result[$Interface] = array('down' => $Item[0], 'up' => $Item[8]);
    $Result[$Interface]['down_avg'] = round(($Result[$Interface]['down'] - $LastResult[$Interface]['down']) / $Period);
    $Result[$Interface]['up_avg'] = round(($Result[$Interface]['up'] - $LastResult[$Interface]['up']) / $Period);
    if($Result[$Interface]['down_avg'] < 0) $Result[$Interface]['down_avg'] = 0;
    if($Result[$Interface]['up_avg'] < 0) $Result[$Interface]['up_avg'] = 0;
  }
  // Save last values
  $File = fopen('/tmp/traffic', 'w+');
  fputs($File, serialize($Result));
  fclose($File);
//  print_r($Result);
  return($Result);
}

function TeamSpeak()
{
  $Output = array();
  exec('cat /proc/net/nf_conntrack|grep "dst=192.168.0.14 "|grep "dport=8767 "', $Output);
  return(count($Output));
}

function DiskUtilization()
{
  $Output = array();
  exec('iostat -d sda -x -m 2 2', $Output);   // 2 second measure delay
  $Row = $Output[6];
  while(strpos($Row, '  ') !== false) $Row = str_replace('  ', ' ', $Row);
  $Parts = explode(' ', $Row);
  $Value = str_replace(',', '.', $Parts[11]);
  return($Value);
}

function DiskFree()
{
  return(disk_free_space('/'));
}

?>