source: branches/mvc/Application/Model/Measurement/System.php

Last change on this file was 47, checked in by chronos, 10 years ago
  • Odstraněno: Zbytečná PHP ukončovací značka "?>" z konce všech souborů.
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 //echo($ERROR_NO.','.$ERROR_STR);
20 return(FALSE);
21 }
22 restore_error_handler();
23 }
24
25 function Ping($Host = 'nix.cz')
26 {
27 $Row = array();
28 exec('ping '.$Host.' -c 1 -W 1|grep time=', $Row);
29 // W - timeout in seconds
30 // c - ping count
31 $Parts = explode(' ', $Row[0]);
32 if(count($Parts) > 6)
33 {
34 $Time = $Parts[7];
35 $TimeParts = explode('=', $Time);
36 return($TimeParts[1]);
37 } else return(0);
38 }
39
40 function MemoryUsage()
41 {
42 $Output = array();
43 exec('free -b', $Output);
44 $Row = $Output[2];
45 while(strpos($Row, ' ') !== false) $Row = str_replace(' ', ' ', $Row);
46 $RowParts = explode(' ', $Row);
47 $Row = $Output[3];
48 while(strpos($Row, ' ') !== false) $Row = str_replace(' ', ' ', $Row);
49 $RowParts2 = explode(' ', $Row);
50 return($RowParts[2] + $RowParts2[2]);
51 }
52
53 function CPUUsage()
54 {
55 $CpuStateFileName = '/tmp/LastCpuUsage';
56 $LastCpuUsage = unserialize(file_get_contents($CpuStateFileName));
57 //$cpuIDLEprev, $cpuSYSTprev, $cpuUSERprev;
58
59 // get processor usage seconds for pct stats ###
60 $File = fopen('/proc/stat', 'r');
61 $Row = fgets($File);
62 fclose($File);
63 $Parts = explode(' ', $Row);
64 $CpuUsage['User'] = $Parts[2] + $Parts[3];
65 $CpuUsage['System'] = $Parts[4];
66 $CpuUsage['Idle'] = $Parts[5];
67 $CpuUsageDiff['User'] = ($CpuUsage['User'] - $LastCpuUsage['User']);
68 $CpuUsageDiff['System'] = ($CpuUsage['System'] - $LastCpuUsage['System']);
69 $CpuUsageDiff['Idle'] = ($CpuUsage['Idle'] - $LastCpuUsage['Idle']);
70 $CpuUsageDiffTotal = (($CpuUsageDiff['User'] + $CpuUsageDiff['System']) + $CpuUsageDiff['Idle']);
71 if ($CpuUsageDiffTotal > 0)
72 {
73 $CpuUsagePercent['User'] = ($CpuUsageDiff['User'] / $CpuUsageDiffTotal) * 100;
74 $CpuUsagePercent['System'] = ($CpuUsageDiff['System'] / $CpuUsageDiffTotal) * 100;
75 $CpuUsagePercent['Idle'] = ($CpuUsageDiff['Idle'] / $CpuUsageDiffTotal * 100);
76 } else
77 {
78 $CpuUsagePercent['User'] = 0;
79 $CpuUsagePercent['System'] = 0;
80 $CpuUsagePercent['Idle'] = 0;
81 }
82 file_put_contents($CpuStateFileName, serialize($CpuUsage));
83 return(100 - round($CpuUsagePercent['Idle'], 2));
84 }
85
86 function GetNetworkState()
87 {
88 $NetworkStateFile = '/tmp/LastNetworkState';
89 $LastNetworkState = unserializa(file_get_contents($NetworkStateFile));
90 $NetworkState = array('Time' => time());
91 $Output = array();
92 exec('cat /proc/net/dev', $Output);
93 array_shift($Output); // Skip header
94 array_shift($Output); // Skip header
95 foreach($Output as $Item)
96 {
97 while(strpos($Item, ' ') !== false) $Item = str_replace(' ', ' ', $Item); // Rrmove multiple spaces
98 $Item = explode(':', $Item);
99 $Interface = trim($Item[0]);
100 $Item = explode(' ', trim($Item[1]));
101 $NetworkState[$Interface] = array('Down' => $Item[0], 'Up' => $Item[8]);
102 if(array_key_exists($Interface, $LastNetworkState))
103 {
104 $Period = time() - $LastNetworkState['Time'];
105 $NetworkState[$Interface]['DownAverage'] = round(($NetworkState[$Interface]['Down'] - $LastNetworkState[$Interface]['Down']) / $Period);
106 $NetworkState[$Interface]['UpAverage'] = round(($NetworkState[$Interface]['Up'] - $LastNetworkState[$Interface]['Up']) / $Period);
107 } else
108 {
109 $NetworkState[$Interface]['DownAverage'] = 0;
110 $NetworkState[$Interface]['UpAverage'] = 0;
111 }
112 if($NetworkState[$Interface]['DownAverage'] < 0) $NetworkState[$Interface]['DownAverage'] = 0;
113 if($NetworkState[$Interface]['UpAverage'] < 0) $NetworkState[$Interface]['UpAverage'] = 0;
114 }
115 file_put_contents($NetworkStateFile, serialize($NetworkState));
116 return($NetworkState);
117 }
118
119 function NetworkSpeedDownload($Interface)
120 {
121 $NetworkState = $this->GetNetworkState();
122 return($NetworkState['Interface']['DownAverage']);
123 }
124
125 function NetworkSpeedUpload($Interface)
126 {
127 $NetworkState = $this->GetNetworkState();
128 return($NetworkState['Interface']['UpAverage']);
129 }
130
131 function NetworkServiceConnectionCount($Port)
132 {
133 $HostIP = gethostbyname(trim(`hostname`));
134 $Output = array();
135 exec('cat /proc/net/nf_conntrack|grep "dst='.$HostIP.' "|grep "dport='.$Port.' "|grep "ASSURED"', $Output);
136 return(count($Output));
137 }
138
139 function DiskUtilization($Device)
140 {
141 $Output = array();
142 exec('iostat -d '.$Device.' -x -m 2 2', $Output); // 2 second measure delay
143 $Row = $Output[6];
144 while(strpos($Row, ' ') !== false) $Row = str_replace(' ', ' ', $Row);
145 $Parts = explode(' ', $Row);
146 $Value = str_replace(',', '.', $Parts[11]);
147 return($Value);
148 }
149
150 function DiskFree($Path)
151 {
152 return(disk_free_space($Path));
153 }
154
155 function ProcessorTemperature($Sensor)
156 {
157 $Output = array();
158 exec('/usr/bin/sensors', $Output);
159 foreach($Output as $Line)
160 {
161 if(substr($Line, 0, strlen($Sensor)) == $Sensor)
162 {
163 $Line = substr($Line, strpos($Line, '+') + 1);
164 $Line = substr($Line, 0, strpos($Line, '°'));
165 return($Line);
166 }
167 }
168 return(0);
169 }
170
171 function SystemUptime()
172 {
173 $File = fopen('/proc/uptime', 'r');
174 $Uptime = fgets($File);
175 fclose($File);
176 $UptimeParts = explode(' ', $Uptime);
177 return($UptimeParts[0]);
178 }
179}
Note: See TracBrowser for help on using the repository browser.