| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | class ConfigRouterOSNetwatchImport extends NetworkConfigItem
|
|---|
| 4 | {
|
|---|
| 5 | function NetwatchImport(): void
|
|---|
| 6 | {
|
|---|
| 7 | $StartTime = time();
|
|---|
| 8 |
|
|---|
| 9 | // Load all interfaces to memory
|
|---|
| 10 | $Interfaces = array();
|
|---|
| 11 | $DbResult = $this->Database->select('NetworkInterface', '`Id`, `LocalIP` AS `IP`, `Online`, 0 AS `NewOnline`');
|
|---|
| 12 | while ($DbRow = $DbResult->fetch_assoc())
|
|---|
| 13 | $Interfaces[$DbRow['IP']] = $DbRow;
|
|---|
| 14 |
|
|---|
| 15 | // Load netwatch status from all DHCP routers
|
|---|
| 16 | $DbResult3 = $this->Database->query('SELECT `DHCP` FROM `NetworkSubnet` '.
|
|---|
| 17 | 'WHERE (`Configure` = 1) AND (`DHCP` != "") GROUP BY `DHCP`');
|
|---|
| 18 | while ($Subnet = $DbResult3->fetch_assoc())
|
|---|
| 19 | {
|
|---|
| 20 | echo('router '.$Subnet['DHCP']."\n");
|
|---|
| 21 | $Routerboard = new RouterosAPI();
|
|---|
| 22 | $Routerboard->Connect($Subnet['DHCP'], $this->System->Config['API']['UserName'],
|
|---|
| 23 | $this->System->Config['API']['Password']);
|
|---|
| 24 | if (!$Routerboard->Connected) continue;
|
|---|
| 25 | $Routerboard->Write('/tool/netwatch/getall', false);
|
|---|
| 26 | $Routerboard->Write('=.proplist=host,status');
|
|---|
| 27 | $Read = $Routerboard->Read(false);
|
|---|
| 28 | $List = $Routerboard->ParseResponse($Read);
|
|---|
| 29 | foreach ($List as $Properties)
|
|---|
| 30 | {
|
|---|
| 31 | $IP = $Properties['host'];
|
|---|
| 32 | if ($Properties['status'] == 'up') $Online = 1;
|
|---|
| 33 | else $Online = 0;
|
|---|
| 34 |
|
|---|
| 35 | if ($Online)
|
|---|
| 36 | {
|
|---|
| 37 | if (array_key_exists($IP, $Interfaces))
|
|---|
| 38 | $Interfaces[$IP]['NewOnline'] = 1;
|
|---|
| 39 | else echo('IP '.$IP.' not found.'."\n");
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | $Queries = array();
|
|---|
| 45 | $QueriesInsert = array();
|
|---|
| 46 | foreach ($Interfaces as $Interface)
|
|---|
| 47 | {
|
|---|
| 48 | // Update last online time if still online
|
|---|
| 49 | if ($Interface['NewOnline'])
|
|---|
| 50 | $Queries[] = $this->Database->GetUpdate('NetworkInterface', '`Id` = '.$Interface['Id'],
|
|---|
| 51 | array('LastOnline' => TimeToMysqlDateTime($StartTime)));
|
|---|
| 52 |
|
|---|
| 53 | if ($Interface['Online'] != $Interface['NewOnline'])
|
|---|
| 54 | {
|
|---|
| 55 | // Online state changed
|
|---|
| 56 | $QueriesInsert[] = 'INSERT INTO `NetworkInterfaceUpDown` (`Interface`,'.
|
|---|
| 57 | '`State`, `Time`, `Previous`) VALUES ('.$Interface['Id'].', '.$Interface['NewOnline'].', "'.
|
|---|
| 58 | TimeToMysqlDateTime($StartTime).'", (SELECT MAX(T2.Id) FROM NetworkInterfaceUpDown AS T2 WHERE T2.Interface='.$Interface['Id'].'))';
|
|---|
| 59 | $Queries[] = $this->Database->GetUpdate('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
|
|---|
| 60 | array('Online' => $Interface['NewOnline']));
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | echo("transakce insert\n");
|
|---|
| 64 | $this->Database->Transaction($QueriesInsert);
|
|---|
| 65 | echo("done\n");
|
|---|
| 66 | echo("transakce\n");
|
|---|
| 67 | $this->Database->Transaction($Queries);
|
|---|
| 68 | echo("done\n");
|
|---|
| 69 |
|
|---|
| 70 | // Update Duration for new items
|
|---|
| 71 | echo("Update Duration\n");
|
|---|
| 72 | $this->Database->query('UPDATE NetworkInterfaceUpDown AS T1, NetworkInterfaceUpDown AS T2 '.
|
|---|
| 73 | 'SET T1.Duration = TIMESTAMPDIFF(SECOND, T1.Time, T2.Time) '.
|
|---|
| 74 | 'WHERE (T2.Previous = T1.Id) AND (T2.Interface = T1.Interface) AND (T1.Duration IS NULL) AND (T2.Time = "'.TimeToMysqlDateTime($StartTime).'")');
|
|---|
| 75 | echo("done\n");
|
|---|
| 76 |
|
|---|
| 77 | // Set offline all interfaces which were not updated as online
|
|---|
| 78 | $DbResult = $this->Database->select('NetworkInterface', '*', '(`Online` = 1) AND '.
|
|---|
| 79 | '(`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'")');
|
|---|
| 80 | while ($DbRow = $DbResult->fetch_assoc())
|
|---|
| 81 | {
|
|---|
| 82 | echo('IP '.$DbRow['LocalIP'].' online but time not updated.'."\n");
|
|---|
| 83 | }
|
|---|
| 84 | $DbResult = $this->Database->select('NetworkInterface', '*', '(`Online` = 0) AND '.
|
|---|
| 85 | '(`LastOnline` >= "'.TimeToMysqlDateTime($StartTime).'")');
|
|---|
| 86 | while ($DbRow = $DbResult->fetch_assoc())
|
|---|
| 87 | {
|
|---|
| 88 | echo('IP '.$DbRow['LocalIP'].' not online but time updated.'."\n");
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | $Queries = array();
|
|---|
| 92 | // Update device online state
|
|---|
| 93 | $DbResult = $this->Database->select('NetworkInterface', '`Device`, SUM(`Online`) AS `SumOnline`', '`Online` = 1 GROUP BY `Device`');
|
|---|
| 94 | while ($Device = $DbResult->fetch_assoc())
|
|---|
| 95 | {
|
|---|
| 96 | if ($Device['SumOnline'] > 0)
|
|---|
| 97 | $Queries[] = $this->Database->GetUpdate('NetworkDevice', 'Id='.$Device['Device'], array('LastOnline' => TimeToMysqlDateTime($StartTime), 'Online' => 1));
|
|---|
| 98 | }
|
|---|
| 99 | $Queries[] = $this->Database->GetUpdate('NetworkDevice', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
|
|---|
| 100 | echo("Transakce 2\n");
|
|---|
| 101 | $this->Database->Transaction($Queries);
|
|---|
| 102 | echo("done\n");
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | function Run(): void
|
|---|
| 106 | {
|
|---|
| 107 | RepeatFunction(10, array($this, 'NetwatchImport'));
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|