source: measure_scripts/traffic.php@ 19

Last change on this file since 19 was 19, checked in by george, 17 years ago

Oprava: Odstranění duplikátu funkce Ping

File size: 5.3 KB
Line 
1<?php
2
3include('../global.php');
4
5function CheckPortStatus($Ip, $Port)
6{
7 function ErrorHandler($errno,$errmsg,$filename,$linenum,$vars)
8 {
9 }
10 set_error_handler('ErrorHandler');
11 //error_reporting(0);
12 if($Fp1 = fsockopen($Ip, $Port, $ERROR_NO, $ERROR_STR,(float)0.5))
13 {
14 fclose($Fp1);
15 return true;
16 } else
17 {
18 //echo($ERROR_NO.','.$ERROR_STR);
19 //die();
20 return false;
21 }
22 restore_error_handler();
23}
24
25function Ping()
26{
27 //$File = fopen('/a/log/ping_nix.txt', 'r');
28 //$Row = fgets($File);
29 //fclose($File);
30 exec('ping nix.cz -c 1 |grep time=', $Row);
31 $Parts = explode(' ', $Row);
32 if(count($Parts) > 6)
33 {
34 $Time = $Parts[7];
35 $TimeParts = explode('=', $Time);
36 return($TimeParts[1]);
37 } else return(0);
38}
39
40function 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 return($RowParts[2]);
48}
49
50function CpuUsage()
51{
52 global $cpuIDLEprev, $cpuSYSTprev, $cpuUSERprev;
53 // get processor usage seconds for pct stats ###
54 $File = fopen('/proc/stat', 'r');
55 $Row = fgets($File);
56 fclose($File);
57 $Parts = explode(' ', $Row);
58 $cpuUSER = $Parts[2] + $Parts[3];
59 $cpuSYST = $Parts[4];
60 $cpuIDLE = $Parts[5];
61 $cpuUSERdiff = ($cpuUSER - $cpuUSERprev);
62 $cpuSYSTdiff = ($cpuSYST - $cpuSYSTprev);
63 $cpuIDLEdiff = ($cpuIDLE - $cpuIDLEprev);
64 $cpuIDLEdiffTOTAL = (($cpuUSERdiff + $cpuSYSTdiff) + $cpuIDLEdiff);
65 if ($cpuIDLEdiffTOTAL > 0)
66 {
67 $cpuUSERcent = ($cpuUSERdiff / $cpuIDLEdiffTOTAL) * 100;
68 $cpuSYSTcent = ($cpuSYSTdiff / $cpuIDLEdiffTOTAL) * 100;
69 $cpuIDLEcent = ($cpuIDLEdiff / $cpuIDLEdiffTOTAL * 100);
70 } else
71 {
72 $cpuUSERcent=0;
73 $cpuSYSTcent=0;
74 $cpuIDLEcent=0;
75 }
76 $cpuUSERprev=$cpuUSER;
77 $cpuSYSTprev=$cpuSYST;
78 $cpuIDLEprev=$cpuIDLE;
79 return(100-round($cpuIDLEcent*100)/100);
80}
81
82function GetNetworkStat()
83{
84 // Get last values
85 $File = fopen('/tmp/traffic', 'r');
86 $LastResult = unserialize(fgets($File));
87 fclose($File);
88
89 $Result = array('time' => time());
90 $Output = array();
91 exec('cat /proc/net/dev', $Output);
92 array_shift($Output);
93 array_shift($Output);
94 foreach($Output as $Item)
95 {
96// echo($Item."\n");
97 while(strpos($Item, ' ') !== false) $Item = str_replace(' ', ' ', $Item);
98 $Item = explode(':', $Item);
99 $Interface = trim($Item[0]);
100 $Item = explode(' ', trim($Item[1]));
101 $Period = time() - $LastResult['time'];
102 $Result[$Interface] = array('down' => $Item[0], 'up' => $Item[8]);
103 $Result[$Interface]['down_avg'] = round(($Result[$Interface]['down'] - $LastResult[$Interface]['down']) / $Period);
104 $Result[$Interface]['up_avg'] = round(($Result[$Interface]['up'] - $LastResult[$Interface]['up']) / $Period);
105 if($Result[$Interface]['down_avg'] < 0) $Result[$Interface]['down_avg'] = 0;
106 if($Result[$Interface]['up_avg'] < 0) $Result[$Interface]['up_avg'] = 0;
107 }
108 // Save last values
109 $File = fopen('/tmp/traffic', 'w+');
110 fputs($File, serialize($Result));
111 fclose($File);
112// print_r($Result);
113 return($Result);
114}
115
116$URL = $Config['AddNewValueUrl'];
117//GetNetworkStat();
118while(1)
119{
120 // Network load
121 $NetworkStat = GetNetworkStat();
122 $Value = round($NetworkStat['eth1']['down_avg']);
123 file_get_contents($URL.'?MeasureId=11&Value='.$Value);
124 $Value = round($NetworkStat['eth1']['up_avg']);
125 file_get_contents($URL.'?MeasureId=12&Value='.$Value);
126
127 // Memory usage
128 $Value = MemoryUsage();
129 //echo($Value);
130 file_get_contents($URL.'?MeasureId=3&Value='.$Value);
131
132 // CPU usage
133 $Value = CpuUsage();
134 file_get_contents($URL.'?MeasureId=4&Value='.$Value);
135
136 // GM online
137 $Database->select_db('realmd');
138 $DbResult = $Database->query('SELECT COUNT(*) FROM account WHERE online=1 AND gmlevel > 0');
139 $Row = $DbResult->fetch_array();
140 $Value = $Row[0];
141 file_get_contents($URL.'?MeasureId=10&Value='.$Value);
142
143 // Players online
144 $DbResult = $Database->query('SELECT COUNT(*) FROM account WHERE online=1');
145 $Row = $DbResult->fetch_array();
146 $Value = $Row[0];
147 file_get_contents($URL.'?MeasureId=5&Value='.$Value);
148
149 // Disk free space
150 $Value = disk_free_space('/');
151 file_get_contents($URL.'?MeasureId=7&Value='.$Value);
152
153 // Account count
154 $DbResult = $Database->query('SELECT COUNT(*) FROM account');
155 $Row = $DbResult->fetch_array();
156 $Value = $Row[0];
157 file_get_contents($URL.'?MeasureId=8&Value='.$Value);
158
159 // Character count
160 $Database->select_db('mangos');
161 $DbResult = $Database->query('SELECT COUNT(*) FROM `character`');
162 $Row = $DbResult->fetch_array();
163 $Value = $Row[0];
164 file_get_contents($URL.'?MeasureId=13&Value='.$Value);
165
166 // Guild count
167 $DbResult = $Database->query('SELECT COUNT(*) FROM guild');
168 $Row = $DbResult->fetch_array();
169 $Value = $Row[0];
170 file_get_contents($URL.'?MeasureId=14&Value='.$Value);
171
172 // MaNGOS restarts count
173 $Database->select_db('wow');
174 $DbResult = $Database->query('SELECT COUNT(*) FROM mangos_restart');
175 $Row = $DbResult->fetch_array();
176 $Value = $Row[0];
177 file_get_contents($URL.'?MeasureId=6&Value='.$Value);
178
179 // MaNGOS worldd availability
180 if(CheckPortStatus('localhost', 8085)) $Value = 100; else $Value = 0;
181 file_get_contents($URL.'?MeasureId=15&Value='.$Value);
182
183 // Ping nix.cz
184 $Value = Ping();
185 file_get_contents($URL.'?MeasureId=16&Value='.$Value);
186
187 sleep(60);
188}
189?>
Note: See TracBrowser for help on using the repository browser.