1 | <?php
|
---|
2 |
|
---|
3 | if(isset($_SERVER['REMOTE_ADDR'])) die();
|
---|
4 | include_once(dirname(__FILE__).'/../../../Common/Global.php');
|
---|
5 | include_once(dirname(__FILE__).'/../Routerboard.php');
|
---|
6 | include_once('Common.php');
|
---|
7 | $Path = array('tool', 'netwatch');
|
---|
8 | $Period = 60; // every 60 seconds
|
---|
9 |
|
---|
10 | while(1)
|
---|
11 | {
|
---|
12 | $StartTime = time();
|
---|
13 |
|
---|
14 | // Load netwatch status from all DHCP routers
|
---|
15 | $DbResult3 = $System->Database->query('SELECT DHCP, AddressRange, Mask FROM `NetworkSubnet` WHERE (`Configure` = 1) AND (`Member` IS NULL) GROUP BY DHCP');
|
---|
16 | while($Subnet = $DbResult3->fetch_assoc())
|
---|
17 | {
|
---|
18 | echo($Subnet['AddressRange'].'/'.$Subnet['Mask'].' on router '.$Subnet['DHCP']."\n");
|
---|
19 | $Routerboard = new Routerboard();
|
---|
20 | $Routerboard->UserName = $Config['MainRouter']['UserName'];
|
---|
21 | $Routerboard->Timeout = $Config['MainRouter']['ConnectTimeout'];
|
---|
22 | $Routerboard->HostName = $Subnet['DHCP'];
|
---|
23 | $List = $Routerboard->ListGetPrint($Path, array('host', 'status'));
|
---|
24 | foreach($List as $Properties)
|
---|
25 | {
|
---|
26 | if($Properties['status'] == 'up')
|
---|
27 | {
|
---|
28 | $DbResult = $System->Database->update('NetworkInterface', '`LocalIP` = "'.$Properties['host'].'"',
|
---|
29 | array('Online' => $Online, 'LastOnline' => TimeToMysqlDateTime($StartTime)));
|
---|
30 | }
|
---|
31 | }
|
---|
32 | }
|
---|
33 | $DbResult = $System->Database->update('NetworkInterface', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
|
---|
34 |
|
---|
35 | // Update device online state
|
---|
36 | $DbResult = $System->Database->select('NetworkInterface', '`Device`, SUM(`Online`) AS `SumOnline`', '`Online` = 1 GROUP BY `Device`');
|
---|
37 | while($Device = $DbResult->fetch_assoc())
|
---|
38 | {
|
---|
39 | if($Device['SumOnline'] > 0)
|
---|
40 | $System->Database->update('NetworkDevice', 'Id='.$Device['Device'], array('LastOnline' => TimeToMysqlDateTime($StartTime)));
|
---|
41 | }
|
---|
42 | $DbResult = $System->Database->update('NetworkDevice', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
|
---|
43 |
|
---|
44 | // Update interface online statistics
|
---|
45 | $DbResult = $System->Database->select('NetworkInterface', 'Id', '`Online` = 1');
|
---|
46 | while($Interface = $DbResult->fetch_assoc())
|
---|
47 | {
|
---|
48 | $DbResult2 = $System->Database->select('NetworkInterfaceStat', '*', '(`NetworkInterface`="'.$Interface['Id'].'") AND (`Time` = DATE_FORMAT(NOW(), "%Y-%m-%d %H:00:00"))');
|
---|
49 | 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)');
|
---|
50 | 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"))');
|
---|
51 | }
|
---|
52 |
|
---|
53 | $EndTime = time();
|
---|
54 | $Delay = $Period - ($EndTime - $StartTime);
|
---|
55 | if($Delay < 0) $Delay = 0;
|
---|
56 |
|
---|
57 | echo('Waiting '.$Delay.' seconds...'."\n");
|
---|
58 | sleep($Delay);
|
---|
59 | }
|
---|