source: system/bandwidth2.php@ 111

Last change on this file since 111 was 111, checked in by george, 16 years ago
  • Upraveno: Generovací skripty.
  • Property svn:executable set to *
File size: 5.3 KB
Line 
1<?php
2
3include_once('/a/www/centrala/global.php');
4include_once('generators/global.php');
5
6function GetTrafficStatus()
7{
8 //echo('.');
9 $Hosts = array();
10 $Hosts = GetTrafficStatusTable($Hosts, 'FORWARD');
11 $Hosts = GetTrafficStatusTable($Hosts, 'INPUT');
12 $Hosts = GetTrafficStatusTable($Hosts, 'OUTPUT');
13 return($Hosts);
14}
15
16function GetTrafficStatusTable($Hosts, $Table)
17{
18 $Output = array();
19 exec('/sbin/iptables -t mangle -L '.$Table.' -v -x -n', $Output);
20 array_shift($Output);
21 array_shift($Output);
22 //$Row = $Output[0];
23 foreach($Output as $Row)
24 {
25 //echo($Row."\n");
26 //for($I=0;$I<strlen($Row);$I++) echo(ord($Row{$I}).',');
27
28 $Row = ' '.$Row;
29 while(strpos($Row, ' ') !== false) $Row = str_replace(' ', ' ', $Row); // Remove long spaces
30 // echo("\n\n");
31 //for($I=0;$I<strlen($Row);$I++) echo(ord($Row{$I}).',');
32 $Parts = explode(" ", $Row);
33 $Bytes = $Parts[2];
34 $Target = $Parts[3];
35 $SourceInf = $Parts[6];
36 $DestinationInf = $Parts[7];
37 $Source = $Parts[8];
38 $Destination = $Parts[9];
39// print_r($Parts);
40
41 //echo($Source.' '.$Destination."\n");
42 if(((($Target == 'MARK') or ($Target == 'game-server')) and (($Destination != '0.0.0.0/0') or ($Source != '0.0.0.0/0'))) or ($Target == 'IMQ'))
43 {
44 if($DestinationInf == '*')
45 {
46 $Direction = 'down';
47 $HostIp = $Destination;
48 } else
49 {
50 $Direction = 'up';
51 $HostIp = $Source;
52 }
53 if(!array_key_exists($HostIp, $Hosts)) $Hosts[$HostIp] = array('up' => 0, 'down' => 0);
54 $Hosts[$HostIp][$Direction] = $Hosts[$HostIp][$Direction] + $Bytes;
55 }
56
57 // Network ports statistic
58 $SubTable = 'game-server';
59 if($Target == $SubTable)
60 {
61 //$Hosts[$HostIp]['ports'] = array();
62 $Output2 = array();
63 exec('/sbin/iptables -t mangle -L '.$SubTable.' -v -x -n', $Output2);
64 array_shift($Output2);
65 array_shift($Output2);
66 //$Row = $Output2[0];
67 foreach($Output2 as $Row)
68 {
69 $Row = ' '.$Row;
70 while(strpos($Row, ' ') !== false) $Row = str_replace(' ', ' ', $Row); // Remove long spaces
71 //echo($Row."\n");
72 $Parts = explode(" ", $Row);
73 $Bytes = $Parts[2];
74 $Target = $Parts[3];
75 $SourceInf = $Parts[6];
76 $DestinationInf = $Parts[7];
77 $Source = $Parts[8];
78 $Destination = $Parts[9];
79 $Parameter1 = $Parts[10];
80 $Parameter2 = $Parts[11];
81 if(($Parameter1 == 'tcp') or ($Parameter1 == 'udp'))
82 {
83 $Parts2 = explode(':', $Parameter2);
84 $Port = $Parts2[1];
85 if($DestinationInf == '*')
86 $Hosts[$HostIp]['ports'][$Port]['down'] = $Bytes;
87 else $Hosts[$HostIp]['ports'][$Port]['up'] = $Bytes;
88 }
89 }
90 //print_r($Hosts[$HostIp]['ports']);
91 }
92 }
93 return($Hosts);
94}
95
96function AssignHostName(&$Host)
97{
98 global $DbHosts;
99 if(!array_key_exists('hostname', $Host))
100 {
101 if(array_key_exists($Host['IP'], $DbHosts)) $Host['hostname'] = $DbHosts[$Host['IP']];
102 else $Host['hostname'] = '';
103 }
104}
105
106// Preload db host list from database
107$DbHosts = array();
108$DbResult = $Database->select('hosts','*');
109while($Row = $DbResult->fetch_array())
110{
111 if(($Row['vpn'] == 1) and ($Row['external_ip'] == '')) $DbHosts[ToVpnIp($Row)] = $Row['name'];
112 if($Row['external_ip'] != '') $DbHosts[$Row['external_ip']] = $Row['name'];
113 $DbHosts[$Row['IP']] = $Row['name'];
114
115}
116$DbHosts['0.0.0.0/0'] = 'total';
117
118$Period = 10;
119$LastHosts = GetTrafficStatus();
120$LastSaveTime = time();
121while(1)
122{
123 Sleep($Period);
124 $Hosts = GetTrafficStatus();
125 $Database->query('TRUNCATE TABLE known_hosts_ports');
126 foreach($Hosts as $Index => $Item)
127 {
128 //echo($Index."\n");
129 $Item['total_up'] = $Item['up'];
130 $Item['total_down'] = $Item['down'];
131 $Item['up'] = ($Item['up'] - $LastHosts[$Index]['up']) / $Period;
132 $Item['down'] = ($Item['down'] - $LastHosts[$Index]['down']) / $Period;
133 //AssignHostName($Hosts[$Index]);
134// if($Item['modified'])
135 {
136 //DB_Query('UPDATE known_hosts SET traffic_total_up = traffic_total_up + '.$Item['up'].', traffic_total_down = traffic_total_down + '.$Item['down'].', traffic_avg_up='.$Item['up'].', traffic_avg_down='.$Item['down'].', hostname="'.$DbHosts[$Index].'" WHERE IP="'.$Index.'"');
137 $Database->replace('known_hosts', array('IP' => $Index, 'hostname' => $DbHosts[$Index], 'traffic_avg_up' => $Item['up'], 'traffic_avg_down' => $Item['down'],
138 'traffic_total_up' => $Item['total_up'], 'traffic_total_down' => $Item['total_down']));
139 if(array_key_exists('ports', $Item))
140 foreach($Item['ports'] as $PortNumber => $Port)
141 {
142 $Port['total_up'] = $Port['up'];
143 $Port['total_down'] = $Port['down'];
144 $Port['up'] = ($Port['up'] - $LastHosts[$Index]['ports'][$PortNumber]['up']) / $Period;
145 $Port['down'] = ($Port['down'] - $LastHosts[$Index]['ports'][$PortNumber]['down']) / $Period;
146 $Database->replace('known_hosts_ports', array('host' => $Index, 'port' => $PortNumber, 'up' => $Port['up'],
147 'down' => $Port['down'], 'total_down' => $Port['total_down'], 'total_up' => $Port['total_up']));
148 }
149 }
150 //echo($Index.': '.$Item."\n");
151 }
152 //echod("\n");
153 $LastHosts = $Hosts;
154 $LastSaveTime = time();
155}
156
157?>
Note: See TracBrowser for help on using the repository browser.