source: client/php/Linux.php

Last change on this file was 66, checked in by chronos, 9 years ago
  • Moved: Measure scripts should be part of client, not statistic web itself.
File size: 4.6 KB
Line 
1<?php
2
3// Funkce pro získání stavových informací o systému
4
5function ErrorHandler($errno,$errmsg,$filename,$linenum,$vars)
6{
7}
8
9function CheckPortStatus($Ip, $Port, $Timeout = 0.5)
10{
11 set_error_handler('ErrorHandler');
12 //error_reporting(0);
13 if($Fp1 = fsockopen($Ip, $Port, $ERROR_NO, $ERROR_STR, (float)$Timeout))
14 {
15 fclose($Fp1);
16 return(TRUE);
17 } else
18 {
19 //echo($ERROR_NO.','.$ERROR_STR);
20 return(FALSE);
21 }
22 restore_error_handler();
23}
24
25function Ping($Host = 'nix.cz')
26{
27 exec('ping '.$Host.' -c 1 -W 1|grep time=', $Row);
28 // W - timeout in seconds
29 // c - ping count
30 $Parts = explode(' ', $Row[0]);
31 if(count($Parts) > 6)
32 {
33 $Time = $Parts[7];
34 $TimeParts = explode('=', $Time);
35 return($TimeParts[1]);
36 } else return(0);
37}
38
39function MemoryUsage()
40{
41 $Output = array();
42 exec('free -b', $Output);
43 $Row = $Output[2];
44 while(strpos($Row, ' ') !== false) $Row = str_replace(' ', ' ', $Row);
45 $RowParts = explode(' ', $Row);
46 $Row = $Output[3];
47 while(strpos($Row, ' ') !== false) $Row = str_replace(' ', ' ', $Row);
48 $RowParts2 = explode(' ', $Row);
49 return($RowParts[2] + $RowParts2[2]);
50}
51
52function CpuUsage()
53{
54 global $cpuIDLEprev, $cpuSYSTprev, $cpuUSERprev;
55 // get processor usage seconds for pct stats ###
56 $File = fopen('/proc/stat', 'r');
57 $Row = fgets($File);
58 fclose($File);
59 $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)
68 {
69 $cpuUSERcent = ($cpuUSERdiff / $cpuIDLEdiffTOTAL) * 100;
70 $cpuSYSTcent = ($cpuSYSTdiff / $cpuIDLEdiffTOTAL) * 100;
71 $cpuIDLEcent = ($cpuIDLEdiff / $cpuIDLEdiffTOTAL * 100);
72 } else
73 {
74 $cpuUSERcent =0;
75 $cpuSYSTcent = 0;
76 $cpuIDLEcent = 0;
77 }
78 $cpuUSERprev = $cpuUSER;
79 $cpuSYSTprev = $cpuSYST;
80 $cpuIDLEprev = $cpuIDLE;
81 return(100 - round($cpuIDLEcent * 100) / 100);
82}
83
84function GetNetworkState()
85{
86 global $LastNetworkState;
87
88 if(!isset($LastNetworkState)) $LastNetworkState = array();
89 $NetworkState = array('Time' => time());
90 $Output = array();
91 exec('cat /proc/net/dev', $Output);
92 array_shift($Output); // Skip header
93 array_shift($Output); // Skip header
94 foreach($Output as $Item)
95 {
96 while(strpos($Item, ' ') !== false) $Item = str_replace(' ', ' ', $Item); // Rrmove multiple spaces
97 $Item = explode(':', $Item);
98 $Interface = trim($Item[0]);
99 $Item = explode(' ', trim($Item[1]));
100 $NetworkState[$Interface] = array('Down' => $Item[0], 'Up' => $Item[8]);
101 if(array_key_exists($Interface, $LastNetworkState))
102 {
103 $Period = time() - $LastNetworkState['Time'];
104 $NetworkState[$Interface]['DownAverage'] = round(($NetworkState[$Interface]['Down'] - $LastNetworkState[$Interface]['Down']) / $Period);
105 $NetworkState[$Interface]['UpAverage'] = round(($NetworkState[$Interface]['Up'] - $LastNetworkState[$Interface]['Up']) / $Period);
106 } else
107 {
108 $NetworkState[$Interface]['DownAverage'] = 0;
109 $NetworkState[$Interface]['UpAverage'] = 0;
110 }
111 if($NetworkState[$Interface]['DownAverage'] < 0) $NetworkState[$Interface]['DownAverage'] = 0;
112 if($NetworkState[$Interface]['UpAverage'] < 0) $NetworkState[$Interface]['UpAverage'] = 0;
113 }
114 $LastNetworkState = $NetworkState;
115 return($NetworkState);
116}
117
118function NetworkServiceConnectionCount($Port)
119{
120 $HostIP = gethostbyname(trim(`hostname`));
121 $Output = array();
122 exec('cat /proc/net/nf_conntrack|grep "dst='.$HostIP.' "|grep "dport='.$Port.' "|grep "ASSURED"', $Output);
123 return(count($Output));
124}
125
126function DiskUtilization($Device = 'sda')
127{
128 $Output = array();
129 exec('iostat -d '.$Device.' -x -m 2 2', $Output); // 2 second measure delay
130 $Row = $Output[6];
131 while(strpos($Row, ' ') !== false) $Row = str_replace(' ', ' ', $Row);
132 $Parts = explode(' ', $Row);
133 $Value = str_replace(',', '.', $Parts[11]);
134 return($Value);
135}
136
137function DiskFree($Path)
138{
139 return(disk_free_space($Path));
140}
141
142function ProcessorTemperature($Sensor)
143{
144 $Output = array();
145 exec('/usr/bin/sensors', $Output);
146 foreach($Output as $Line)
147 {
148 if(substr($Line, 0, strlen($Sensor)) == $Sensor)
149 {
150 $Line = substr($Line, strpos($Line, '+') + 1);
151 $Line = substr($Line, 0, strpos($Line, '°'));
152 return($Line);
153 }
154 }
155 return(0);
156}
157
158function SystemUptime()
159{
160 $File = fopen('/proc/uptime', 'r');
161 $Uptime = fgets($File);
162 fclose($File);
163 $UptimeParts = explode(' ', $Uptime);
164 return($UptimeParts[0]);
165}
Note: See TracBrowser for help on using the repository browser.