source: trunk/Modules/TimeMeasure/Measurement/System.php

Last change on this file was 929, checked in by chronos, 3 years ago
  • Modified: Removed commended out print_r and echo commands used only for debugging.
File size: 5.6 KB
Line 
1<?php
2
3// Funkce pro získání stavových informací o systému
4
5include_once(dirname(__FILE__).'/Measurement.php');
6
7class SystemMeasurement extends Measurement
8{
9 function 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 return FALSE;
20 }
21 restore_error_handler();
22 }
23
24 function Ping($Host = 'nix.cz')
25 {
26 $Row = array();
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
39 function 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
52 function CPUUsage()
53 {
54 $CpuStateFileName = '/tmp/LastCpuUsage';
55 $LastCpuUsage = unserialize(file_get_contents($CpuStateFileName));
56 //$cpuIDLEprev, $cpuSYSTprev, $cpuUSERprev;
57
58 // get processor usage seconds for pct stats ###
59 $File = fopen('/proc/stat', 'r');
60 $Row = fgets($File);
61 fclose($File);
62 $Parts = explode(' ', $Row);
63 $CpuUsage['User'] = $Parts[2] + $Parts[3];
64 $CpuUsage['System'] = $Parts[4];
65 $CpuUsage['Idle'] = $Parts[5];
66 $CpuUsageDiff['User'] = ($CpuUsage['User'] - $LastCpuUsage['User']);
67 $CpuUsageDiff['System'] = ($CpuUsage['System'] - $LastCpuUsage['System']);
68 $CpuUsageDiff['Idle'] = ($CpuUsage['Idle'] - $LastCpuUsage['Idle']);
69 $CpuUsageDiffTotal = (($CpuUsageDiff['User'] + $CpuUsageDiff['System']) + $CpuUsageDiff['Idle']);
70 if ($CpuUsageDiffTotal > 0)
71 {
72 $CpuUsagePercent['User'] = ($CpuUsageDiff['User'] / $CpuUsageDiffTotal) * 100;
73 $CpuUsagePercent['System'] = ($CpuUsageDiff['System'] / $CpuUsageDiffTotal) * 100;
74 $CpuUsagePercent['Idle'] = ($CpuUsageDiff['Idle'] / $CpuUsageDiffTotal * 100);
75 } else
76 {
77 $CpuUsagePercent['User'] = 0;
78 $CpuUsagePercent['System'] = 0;
79 $CpuUsagePercent['Idle'] = 0;
80 }
81 file_put_contents($CpuStateFileName, serialize($CpuUsage));
82 return 100 - round($CpuUsagePercent['Idle'], 2);
83 }
84
85 function GetNetworkState()
86 {
87 $NetworkStateFile = '/tmp/LastNetworkState';
88 $LastNetworkState = unserialize(file_get_contents($NetworkStateFile));
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 file_put_contents($NetworkStateFile, serialize($NetworkState));
115 return $NetworkState;
116 }
117
118 function NetworkSpeedDownload($Interface)
119 {
120 $NetworkState = $this->GetNetworkState();
121 return $NetworkState['Interface']['DownAverage'];
122 }
123
124 function NetworkSpeedUpload($Interface)
125 {
126 $NetworkState = $this->GetNetworkState();
127 return $NetworkState['Interface']['UpAverage'];
128 }
129
130 function NetworkServiceConnectionCount($Port)
131 {
132 $HostIP = gethostbyname(trim(`hostname`));
133 $Output = array();
134 exec('cat /proc/net/nf_conntrack|grep "dst='.$HostIP.' "|grep "dport='.$Port.' "|grep "ASSURED"', $Output);
135 return count($Output);
136 }
137
138 function DiskUtilization($Device)
139 {
140 $Output = array();
141 exec('iostat -d '.$Device.' -x -m 2 2', $Output); // 2 second measure delay
142 $Row = $Output[6];
143 while (strpos($Row, ' ') !== false) $Row = str_replace(' ', ' ', $Row);
144 $Parts = explode(' ', $Row);
145 $Value = str_replace(',', '.', $Parts[11]);
146 return $Value;
147 }
148
149 function DiskFree($Path)
150 {
151 return disk_free_space($Path);
152 }
153
154 function ProcessorTemperature($Sensor)
155 {
156 $Output = array();
157 exec('/usr/bin/sensors', $Output);
158 foreach ($Output as $Line)
159 {
160 if (substr($Line, 0, strlen($Sensor)) == $Sensor)
161 {
162 $Line = substr($Line, strpos($Line, '+') + 1);
163 $Line = substr($Line, 0, strpos($Line, '°'));
164 return $Line;
165 }
166 }
167 return 0;
168 }
169
170 function SystemUptime()
171 {
172 $File = fopen('/proc/uptime', 'r');
173 $Uptime = fgets($File);
174 fclose($File);
175 $UptimeParts = explode(' ', $Uptime);
176 return $UptimeParts[0];
177 }
178}
Note: See TracBrowser for help on using the repository browser.