source: branches/mvc/measure_scripts/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: 4.8 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 $LastCpuUsage;
55 //$cpuIDLEprev, $cpuSYSTprev, $cpuUSERprev;
56
57 // get processor usage seconds for pct stats ###
58 $File = fopen('/proc/stat', 'r');
59 $Row = fgets($File);
60 fclose($File);
61 $Parts = explode(' ', $Row);
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)
70 {
71 $CpuUsagePercent['User'] = ($CpuUsageDiff['User'] / $CpuUsageDiffTotal) * 100;
72 $CpuUsagePercent['System'] = ($CpuUsageDiff['System'] / $CpuUsageDiffTotal) * 100;
73 $CpuUsagePercent['Idle'] = ($CpuUsageDiff['Idle'] / $CpuUsageDiffTotal * 100);
74 } else
75 {
76 $CpuUsagePercent['User'] = 0;
77 $CpuUsagePercent['System'] = 0;
78 $CpuUsagePercent['Idle'] = 0;
79 }
80 $LastCpuUsage = $CpuUsage;
81 return(100 - round($CpuUsagePercent['Idle'], 2));
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)
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.