source: trunk/Modules/NetworkConfigRouterOS/Generators/NetwatchImport.php

Last change on this file was 950, checked in by chronos, 2 years ago
  • Fixed: Netwatch import was not done from subnets assigned to members.
File size: 4.4 KB
RevLine 
[247]1<?php
2
[781]3class ConfigRouterOSNetwatchImport extends NetworkConfigItem
[682]4{
[887]5 function NetwatchImport(): void
[781]6 {
7 $StartTime = time();
[247]8
[781]9 // Load all interfaces to memory
10 $Interfaces = array();
11 $DbResult = $this->Database->select('NetworkInterface', '`Id`, `LocalIP` AS `IP`, `Online`, 0 AS `NewOnline`');
[873]12 while ($DbRow = $DbResult->fetch_assoc())
[781]13 $Interfaces[$DbRow['IP']] = $DbRow;
[317]14
[841]15 // Load netwatch status from all DHCP routers
16 $DbResult3 = $this->Database->query('SELECT `DHCP` FROM `NetworkSubnet` '.
[950]17 'WHERE (`Configure` = 1) AND (`DHCP` != "") GROUP BY `DHCP`');
[873]18 while ($Subnet = $DbResult3->fetch_assoc())
[841]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']);
[873]24 if (!$Routerboard->Connected) continue;
[841]25 $Routerboard->Write('/tool/netwatch/getall', false);
26 $Routerboard->Write('=.proplist=host,status');
27 $Read = $Routerboard->Read(false);
28 $List = $Routerboard->ParseResponse($Read);
[873]29 foreach ($List as $Properties)
[781]30 {
[841]31 $IP = $Properties['host'];
[873]32 if ($Properties['status'] == 'up') $Online = 1;
[781]33 else $Online = 0;
[667]34
[873]35 if ($Online)
[841]36 {
[873]37 if (array_key_exists($IP, $Interfaces))
[841]38 $Interfaces[$IP]['NewOnline'] = 1;
39 else echo('IP '.$IP.' not found.'."\n");
[781]40 }
41 }
[841]42 }
[658]43
[852]44 $Queries = array();
[861]45 $QueriesInsert = array();
[887]46 foreach ($Interfaces as $Interface)
[841]47 {
48 // Update last online time if still online
[873]49 if ($Interface['NewOnline'])
[861]50 $Queries[] = $this->Database->GetUpdate('NetworkInterface', '`Id` = '.$Interface['Id'],
[841]51 array('LastOnline' => TimeToMysqlDateTime($StartTime)));
[658]52
[873]53 if ($Interface['Online'] != $Interface['NewOnline'])
[781]54 {
[841]55 // Online state changed
[861]56 $QueriesInsert[] = 'INSERT INTO `NetworkInterfaceUpDown` (`Interface`,'.
[862]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'].'))';
[852]59 $Queries[] = $this->Database->GetUpdate('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
[841]60 array('Online' => $Interface['NewOnline']));
[781]61 }
[841]62 }
[861]63 echo("transakce insert\n");
64 $this->Database->Transaction($QueriesInsert);
65 echo("done\n");
[860]66 echo("transakce\n");
[852]67 $this->Database->Transaction($Queries);
[860]68 echo("done\n");
[658]69
[862]70 // Update Duration for new items
[863]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");
[862]76
[841]77 // Set offline all interfaces which were not updated as online
78 $DbResult = $this->Database->select('NetworkInterface', '*', '(`Online` = 1) AND '.
79 '(`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'")');
[873]80 while ($DbRow = $DbResult->fetch_assoc())
[841]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).'")');
[873]86 while ($DbRow = $DbResult->fetch_assoc())
[841]87 {
88 echo('IP '.$DbRow['LocalIP'].' not online but time updated.'."\n");
89 }
90
[852]91 $Queries = array();
[841]92 // Update device online state
93 $DbResult = $this->Database->select('NetworkInterface', '`Device`, SUM(`Online`) AS `SumOnline`', '`Online` = 1 GROUP BY `Device`');
[873]94 while ($Device = $DbResult->fetch_assoc())
[841]95 {
[873]96 if ($Device['SumOnline'] > 0)
[852]97 $Queries[] = $this->Database->GetUpdate('NetworkDevice', 'Id='.$Device['Device'], array('LastOnline' => TimeToMysqlDateTime($StartTime), 'Online' => 1));
[841]98 }
[852]99 $Queries[] = $this->Database->GetUpdate('NetworkDevice', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
[860]100 echo("Transakce 2\n");
[852]101 $this->Database->Transaction($Queries);
[860]102 echo("done\n");
[663]103 }
[247]104
[887]105 function Run(): void
[336]106 {
[835]107 RepeatFunction(10, array($this, 'NetwatchImport'));
[247]108 }
[873]109}
Note: See TracBrowser for help on using the repository browser.