1 | <?php
|
---|
2 |
|
---|
3 | include('../../html/global.php');
|
---|
4 | $Database->select_db('statistic');
|
---|
5 | $Config['Web']['ShowError'] = 1;
|
---|
6 | echo("\nSledovac prenesenych dat\n");
|
---|
7 | $ERR = 0;
|
---|
8 |
|
---|
9 | function CpuUsage()
|
---|
10 | {
|
---|
11 | global $cpuIDLEprev, $cpuSYSTprev, $cpuUSERprev, $Database;
|
---|
12 |
|
---|
13 | // get processor usage seconds for pct stats ###
|
---|
14 | $File = fopen('/proc/stat', 'r');
|
---|
15 | $Row = fgets($File);
|
---|
16 | fclose($File);
|
---|
17 | $Parts = explode(' ', $Row);
|
---|
18 | $cpuUSER = $Parts[2] + $Parts[3];
|
---|
19 | $cpuSYST = $Parts[4];
|
---|
20 | $cpuIDLE = $Parts[5];
|
---|
21 | $cpuUSERdiff = ($cpuUSER - $cpuUSERprev);
|
---|
22 | $cpuSYSTdiff = ($cpuSYST - $cpuSYSTprev);
|
---|
23 | $cpuIDLEdiff = ($cpuIDLE - $cpuIDLEprev);
|
---|
24 | $cpuIDLEdiffTOTAL = (($cpuUSERdiff + $cpuSYSTdiff) + $cpuIDLEdiff);
|
---|
25 | if ($cpuIDLEdiffTOTAL > 0)
|
---|
26 | {
|
---|
27 | $cpuUSERcent = ($cpuUSERdiff / $cpuIDLEdiffTOTAL) * 100;
|
---|
28 | $cpuSYSTcent = ($cpuSYSTdiff / $cpuIDLEdiffTOTAL) * 100;
|
---|
29 | $cpuIDLEcent = ($cpuIDLEdiff / $cpuIDLEdiffTOTAL * 100);
|
---|
30 | } else
|
---|
31 | {
|
---|
32 | $cpuUSERcent=0;
|
---|
33 | $cpuSYSTcent=0;
|
---|
34 | $cpuIDLEcent=0;
|
---|
35 | }
|
---|
36 | $cpuUSERprev=$cpuUSER;
|
---|
37 | $cpuSYSTprev=$cpuSYST;
|
---|
38 | $cpuIDLEprev=$cpuIDLE;
|
---|
39 | return(100-round($cpuIDLEcent*100)/100);
|
---|
40 | }
|
---|
41 |
|
---|
42 | function Ping()
|
---|
43 | {
|
---|
44 | $File = fopen('/a/log/ping_nix.txt', 'r');
|
---|
45 | $Row = fgets($File);
|
---|
46 | fclose($File);
|
---|
47 | $Parts = explode(' ', $Row);
|
---|
48 | if(count($Parts) > 6)
|
---|
49 | {
|
---|
50 | $Time = $Parts[7];
|
---|
51 | $TimeParts = explode('=', $Time);
|
---|
52 | return($TimeParts[1]);
|
---|
53 | } else return(0);
|
---|
54 | }
|
---|
55 |
|
---|
56 | function GetNetworkStat()
|
---|
57 | {
|
---|
58 | // Get last values
|
---|
59 | $File = fopen('/tmp/traffic', 'r');
|
---|
60 | $LastResult = unserialize(fgets($File));
|
---|
61 | fclose($File);
|
---|
62 |
|
---|
63 | $Result = array('time' => time());
|
---|
64 | $Output = array();
|
---|
65 | exec('cat /proc/net/dev', $Output);
|
---|
66 | array_shift($Output);
|
---|
67 | array_shift($Output);
|
---|
68 | foreach($Output as $Item)
|
---|
69 | {
|
---|
70 | // echo($Item."\n");
|
---|
71 | while(strpos($Item, ' ') !== false) $Item = str_replace(' ', ' ', $Item);
|
---|
72 | $Item = explode(':', $Item);
|
---|
73 | $Interface = trim($Item[0]);
|
---|
74 | $Item = explode(' ', trim($Item[1]));
|
---|
75 | $Period = time() - $LastResult['time'];
|
---|
76 | $Result[$Interface] = array('down' => $Item[0], 'up' => $Item[8]);
|
---|
77 | $Result[$Interface]['down_avg'] = round(($Result[$Interface]['down'] - $LastResult[$Interface]['down']) / $Period);
|
---|
78 | $Result[$Interface]['up_avg'] = round(($Result[$Interface]['up'] - $LastResult[$Interface]['up']) / $Period);
|
---|
79 | if($Result[$Interface]['down_avg'] < 0) $Result[$Interface]['down_avg'] = 0;
|
---|
80 | if($Result[$Interface]['up_avg'] < 0) $Result[$Interface]['up_avg'] = 0;
|
---|
81 | }
|
---|
82 | // Save last values
|
---|
83 | $File = fopen('/tmp/traffic', 'w+');
|
---|
84 | fputs($File, serialize($Result));
|
---|
85 | fclose($File);
|
---|
86 | // print_r($Result);
|
---|
87 | return($Result);
|
---|
88 | }
|
---|
89 |
|
---|
90 | function Resetuj()
|
---|
91 | {
|
---|
92 | global $ERR, $ID, $Database;
|
---|
93 |
|
---|
94 | echo("Error ".$ERR.". - restartuji pripojeni...");
|
---|
95 | if($ERR == 0)
|
---|
96 | {
|
---|
97 | $Database->insert('failure', array('time' => 'NOW()', 'attempt' => 0));
|
---|
98 | $DbResult = $Database->query('SELECT LAST_INSERT_ID() FROM failure LIMIT 0,1');
|
---|
99 | $Row = $DbResult->fetch_array();
|
---|
100 | $ID = $Row[0];
|
---|
101 | }
|
---|
102 | else $Database->update('failure', 'id='.$ID,array('attempt' => $ERR));
|
---|
103 | // echo($ID);
|
---|
104 | // if($ERR == 5) exec('killall -TERM pppd',$Output);
|
---|
105 | // else if($ERR == 1)
|
---|
106 | // {
|
---|
107 | // exec('reboot');
|
---|
108 | //exec('modprobe -r ohci_hcd');
|
---|
109 | //exec('modprobe ohci_hcd');
|
---|
110 | // } else if($ERR < )
|
---|
111 | // {
|
---|
112 | // exec('rmmod uhci_hcd');
|
---|
113 | // exec('modprobe uhci_hcd');
|
---|
114 | // exec('rmmod ohci_hcd');
|
---|
115 | // exec('modprobe ohci_hcd');
|
---|
116 | //
|
---|
117 | // } else if($ERR > 10) ;
|
---|
118 | //exec('reboot');
|
---|
119 | sleep(30);
|
---|
120 | $ERR = $ERR + 1;
|
---|
121 | }
|
---|
122 | $TemperatureFile = fopen('/tmp/temperature2','r');
|
---|
123 | GetNetworkStat();
|
---|
124 | while(1)
|
---|
125 | {
|
---|
126 | sleep(60);
|
---|
127 | $NetworkStat = GetNetworkStat();
|
---|
128 | // zjit��vyt�n�procesoru
|
---|
129 | $CPU = CpuUsage();
|
---|
130 | //$loadavg = fopen('/proc/loadavg','r');
|
---|
131 | //$data = explode(' ',fread($loadavg,1024));
|
---|
132 | //$CPU = $data[0];
|
---|
133 | //fclose($loadavg);
|
---|
134 | // zjit��po�u zapnutch po�ta�
|
---|
135 |
|
---|
136 | $Database->select_db('is');
|
---|
137 | $DbResult = $Database->query('SELECT COUNT(*) FROM hosts WHERE online=1 AND show_online=1');
|
---|
138 | $Row = $DbResult->fetch_array();
|
---|
139 | $OnlineHosts = $Row[0];
|
---|
140 |
|
---|
141 | $DbResult = $Database->query('SELECT COUNT(*) FROM users WHERE role=0');
|
---|
142 | $Row = $DbResult->fetch_array();
|
---|
143 | $MembersCount = $Row[0];
|
---|
144 |
|
---|
145 | $Database->select_db('statistic');
|
---|
146 | $HDD = round(disk_free_space('/')/1024);
|
---|
147 | $HDD2 = round(disk_free_space('/mnt/sda1')/1024);
|
---|
148 |
|
---|
149 | // Na�en�teploty
|
---|
150 | fseek($TemperatureFile,0);
|
---|
151 | $Temperature = fgets($TemperatureFile, 1000);
|
---|
152 | // $Centrala2UsedMemory = file_get_contents('http://wow/test/memory.php');
|
---|
153 | // $GameServerCpu = file_get_contents('http://wow/test/cpu_usage.php');
|
---|
154 | // $GameServerCpu = str_replace(',', '.', $GameServerCpu);
|
---|
155 | // $WowPlayerCount2 = file_get_contents('http://wow/mangos/players.php');
|
---|
156 | $Ping = Ping();
|
---|
157 |
|
---|
158 | /*
|
---|
159 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=1&Value='.$NetworkStat['eth1']['down_avg']);
|
---|
160 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=2&Value='.$NetworkStat['eth1']['up_avg']);
|
---|
161 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=4&Value='.$CPU);
|
---|
162 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=5&Value='.$HDD);
|
---|
163 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=6&Value='.$HDD2);
|
---|
164 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=8&Value='.$Ping);
|
---|
165 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=10&Value='.$OnlineHosts);
|
---|
166 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=11&Value='.$MembersCount);
|
---|
167 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=12&Value='.$NetworkStat['eth0']['down_avg']);
|
---|
168 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=13&Value='.$NetworkStat['eth0']['up_avg']);
|
---|
169 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=14&Value='.$NetworkStat['wlan0']['down_avg']);
|
---|
170 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=15&Value='.$NetworkStat['wlan0']['up_avg']);
|
---|
171 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=16&Value='.$NetworkStat['wlan1']['down_avg']);
|
---|
172 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=17&Value='.$NetworkStat['wlan1']['up_avg']);
|
---|
173 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=18&Value='.$NetworkStat['wlan2']['down_avg']);
|
---|
174 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=19&Value='.$NetworkStat['wlan2']['up_avg']);
|
---|
175 | file_get_contents('http://localhost/statistic/new/add.php?MeasureId=20&Value='.$NetworkStat['eth2']['down_avg']);
|
---|
176 | echo(file_get_contents('http://localhost/statistic/new/add.php?MeasureId=21&Value='.$NetworkStat['eth2']['up_avg']));
|
---|
177 | */
|
---|
178 |
|
---|
179 | // echo('Down: '.$NetworkStat['eth1']['down_avg'].' Up: '.$NetworkStat['eth1']['up_avg'].' CPU: '.$CPU.' HDD: '.$HDD.' Hosts: '.$OnlineHosts.' Temperature: '.$Temperature.' GameServerCpu: '.$GameServerCpu.' WowPlayerCount2: '.$WowPlayerCount2.' Game-serverUsedMemory: '.$Centrala2UsedMemory.' Ping: '.$Ping);
|
---|
180 |
|
---|
181 | $HDD = round($HDD);
|
---|
182 | $HDD2 = round($HDD2);
|
---|
183 |
|
---|
184 | $NewItem = array('time' => 'NOW()', 'disk_free' => $HDD,'disk2_free' => $HDD2, 'cpu' => $CPU, 'online_hosts' => $OnlineHosts, 'temp' => $Temperature,
|
---|
185 | 'ping_nix' => $Ping,
|
---|
186 | 'inet_down' => $NetworkStat['eth1']['down_avg'], 'inet_up' => $NetworkStat['eth1']['up_avg'],
|
---|
187 | 'eth0_down' => $NetworkStat['eth0']['down_avg'], 'eth0_up' => $NetworkStat['eth0']['up_avg'],
|
---|
188 | // 'wlan0_down' => $NetworkStat['wlan0']['down_avg'], 'wlan0_up' => $NetworkStat['wlan0']['up_avg'],
|
---|
189 | // 'wlan1_down' => $NetworkStat['wlan1']['down_avg'], 'wlan1_up' => $NetworkStat['wlan1']['up_avg'],
|
---|
190 | // 'wlan2_down' => $NetworkStat['wlan2']['down_avg'], 'wlan2_up' => $NetworkStat['wlan2']['up_avg'],
|
---|
191 | 'eth2_down' => $NetworkStat['eth2']['down_avg'], 'eth2_up' => $NetworkStat['eth2']['up_avg']);
|
---|
192 | $Database->insert('traffic', $NewItem);
|
---|
193 | //echo(serialize($NewItem)."\n");
|
---|
194 | //$LevelDivider = 10;
|
---|
195 | //if((round(time() / 60) % $LevelDivider) == 0)
|
---|
196 | //{
|
---|
197 | // DB_Insert('traffic_level2', array('time' => 'NOW()', 'inet_down' => $Down, 'inet_up' => $Up, 'disk_free' => $HDD, 'disk2_free' => $HDD2, 'cpu' => $CPU, 'online_hosts' => $OnlineHosts, 'temp' => $Temperature, 'centrala2_mem' => $Centrala2UsedMemory, 'wow_players' => $WowPlayerCount, 'wow_players2' => $WowPlayerCount2));
|
---|
198 | //}
|
---|
199 | }
|
---|
200 | ?>
|
---|