1 | <?php
|
---|
2 |
|
---|
3 | if(isset($_SERVER['REMOTE_ADDR'])) die();
|
---|
4 | include_once(dirname(__FILE__).'/../../../Application/System.php');
|
---|
5 | $System = new System();
|
---|
6 | $System->ShowPage = false;
|
---|
7 | $System->Run();
|
---|
8 |
|
---|
9 | $Path = array('tool', 'netwatch');
|
---|
10 | $Period = 60; // every 60 seconds
|
---|
11 |
|
---|
12 |
|
---|
13 | while(1)
|
---|
14 | {
|
---|
15 | $StartTime = time();
|
---|
16 |
|
---|
17 | // Load all interfaces to memory
|
---|
18 | $Interfaces = array();
|
---|
19 | $DbResult = $System->Database->select('NetworkInterface', '`Id`, `LocalIP` AS `IP`, `Online`, 0 AS `NewOnline`');
|
---|
20 | while($DbRow = $DbResult->fetch_assoc())
|
---|
21 | $Interfaces[$DbRow['IP']] = $DbRow;
|
---|
22 |
|
---|
23 | // Load netwatch status from all DHCP routers
|
---|
24 | $DbResult3 = $System->Database->query('SELECT `DHCP`, `AddressRange`, `Mask` FROM `NetworkSubnet` '.
|
---|
25 | 'WHERE (`Configure` = 1) AND (`Member` IS NULL) GROUP BY DHCP');
|
---|
26 | while($Subnet = $DbResult3->fetch_assoc())
|
---|
27 | {
|
---|
28 | echo($Subnet['AddressRange'].'/'.$Subnet['Mask'].' on router '.$Subnet['DHCP']."\n");
|
---|
29 | $Routerboard = new Routerboard();
|
---|
30 | $Routerboard->UserName = $Config['MainRouter']['UserName'];
|
---|
31 | $Routerboard->Timeout = $Config['MainRouter']['ConnectTimeout'];
|
---|
32 | $Routerboard->HostName = $Subnet['DHCP'];
|
---|
33 | $List = $Routerboard->ListGetPrint($Path, array('host', 'status'));
|
---|
34 | foreach($List as $Properties)
|
---|
35 | {
|
---|
36 | $IP = $Properties['host'];
|
---|
37 | if($Properties['status'] == 'up') $Online = 1;
|
---|
38 | else $Online = 0;
|
---|
39 |
|
---|
40 | if($Online)
|
---|
41 | {
|
---|
42 | if(array_key_exists($IP, $Interfaces))
|
---|
43 | $Interfaces[$IP]['NewOnline'] = 1;
|
---|
44 | else echo('IP '.$IP.' not found.'."\n");
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | foreach($Interfaces as $Index => $Interface)
|
---|
50 | {
|
---|
51 | // Update last online time if still online
|
---|
52 | if($Interface['NewOnline'])
|
---|
53 | $DbResult = $System->Database->update('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
|
---|
54 | array('LastOnline' => TimeToMysqlDateTime($StartTime)));
|
---|
55 |
|
---|
56 | if($Interface['Online'] != $Interface['NewOnline'])
|
---|
57 | {
|
---|
58 | // Online state changed
|
---|
59 | $System->Database->insert('NetworkInterfaceUpDown', array(
|
---|
60 | 'Interface' => $Interface['Id'], 'State' => $Interface['NewOnline'],
|
---|
61 | 'Time' => TimeToMysqlDateTime($StartTime)));
|
---|
62 | $System->Database->update('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
|
---|
63 | array('Online' => $Interface['NewOnline']));
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | // Set offline all interfaces which were not updated as online
|
---|
68 | $DbResult = $System->Database->select('NetworkInterface', '*', '(`Online` = 1) AND '.
|
---|
69 | '(`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'")');
|
---|
70 | while($DbRow = $DbResult->fetch_assoc())
|
---|
71 | {
|
---|
72 | echo('IP '.$DbRow['LocalIP'].' online but time not updated.'."\n");
|
---|
73 | }
|
---|
74 | $DbResult = $System->Database->select('NetworkInterface', '*', '(`Online` = 0) AND '.
|
---|
75 | '(`LastOnline` >= "'.TimeToMysqlDateTime($StartTime).'")');
|
---|
76 | while($DbRow = $DbResult->fetch_assoc())
|
---|
77 | {
|
---|
78 | echo('IP '.$DbRow['LocalIP'].' not online but time updated.'."\n");
|
---|
79 | }
|
---|
80 |
|
---|
81 | // Update device online state
|
---|
82 | $DbResult = $System->Database->select('NetworkInterface', '`Device`, SUM(`Online`) AS `SumOnline`', '`Online` = 1 GROUP BY `Device`');
|
---|
83 | while($Device = $DbResult->fetch_assoc())
|
---|
84 | {
|
---|
85 | if($Device['SumOnline'] > 0)
|
---|
86 | $System->Database->update('NetworkDevice', 'Id='.$Device['Device'], array('LastOnline' => TimeToMysqlDateTime($StartTime), 'Online' => 1));
|
---|
87 | }
|
---|
88 | $DbResult = $System->Database->update('NetworkDevice', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
|
---|
89 |
|
---|
90 | // Update interface online statistics
|
---|
91 | $DbResult = $System->Database->select('NetworkInterface', 'Id', '`Online` = 1');
|
---|
92 | while($Interface = $DbResult->fetch_assoc())
|
---|
93 | {
|
---|
94 | $DbResult2 = $System->Database->select('NetworkInterfaceStat', '*', '(`NetworkInterface`="'.$Interface['Id'].'") AND (`Time` = DATE_FORMAT(NOW(), "%Y-%m-%d %H:00:00"))');
|
---|
95 | if($DbResult2->num_rows == 0) $System->Database->query('REPLACE INTO `NetworkInterfaceStat` (`NetworkInterface`, `Time`, `PingCount`) VALUES ("'.$Interface['Id'].'", DATE_FORMAT(NOW(), "%Y-%m-%d %H:00:00"), 1)');
|
---|
96 | else $System->Database->query('UPDATE `NetworkInterfaceStat` SET `PingCount` = `PingCount` + 1 WHERE (`NetworkInterface`="'.$Interface['Id'].'") AND (`Time` = DATE_FORMAT(NOW(), "%Y-%m-%d %H:00:00"))');
|
---|
97 | }
|
---|
98 |
|
---|
99 | $EndTime = time();
|
---|
100 | $Delay = $Period - ($EndTime - $StartTime);
|
---|
101 | if($Delay < 0) $Delay = 0;
|
---|
102 |
|
---|
103 | echo('Waiting '.$Delay.' seconds...'."\n");
|
---|
104 | sleep($Delay);
|
---|
105 | }
|
---|
106 |
|
---|