| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | class ConfigRouterOSSignal extends NetworkConfigItem
|
|---|
| 4 | {
|
|---|
| 5 | private int $Time;
|
|---|
| 6 |
|
|---|
| 7 | function ReadWirelessRegistration(): void
|
|---|
| 8 | {
|
|---|
| 9 | $this->Time = time();
|
|---|
| 10 | $Queries = array();
|
|---|
| 11 |
|
|---|
| 12 | $DbResult3 = $this->Database->query('SELECT `Id`, '.
|
|---|
| 13 | '(SELECT `LocalIP` FROM `NetworkInterface` WHERE `NetworkInterface`.`Device` = `NetworkDevice`.`Id` LIMIT 1) AS `LocalIP`, '.
|
|---|
| 14 | '`Name` '.
|
|---|
| 15 | 'FROM `NetworkDevice` WHERE (`API` = 1) AND (`Used` = 1)');
|
|---|
| 16 | while ($Device = $DbResult3->fetch_assoc())
|
|---|
| 17 | {
|
|---|
| 18 | echo($Device['LocalIP'].' ('.$Device['Name']."): ");
|
|---|
| 19 | $Routerboard = new RouterosAPI();
|
|---|
| 20 | //$Routerboard->SSL = true;
|
|---|
| 21 | //$Routerboard->Port = 8729;
|
|---|
| 22 | $Routerboard->Connect($Device['LocalIP'], $this->System->Config['API']['UserName'], $this->System->Config['API']['Password']);
|
|---|
| 23 | if ($Routerboard->Connected)
|
|---|
| 24 | {
|
|---|
| 25 | $Queries = array_merge($Queries, $this->ReadWireless($Device, $Routerboard));
|
|---|
| 26 | $Queries = array_merge($Queries, $this->ReadWireless60G($Device, $Routerboard));
|
|---|
| 27 | }
|
|---|
| 28 | echo("\n");
|
|---|
| 29 | }
|
|---|
| 30 | $this->Database->Transaction($Queries);
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | // Read normal wireless clients signal
|
|---|
| 34 | function ReadWireless(array $Device, RouterosAPI $Routerboard): array
|
|---|
| 35 | {
|
|---|
| 36 | $Queries = array();
|
|---|
| 37 |
|
|---|
| 38 | $Routerboard->Write('/interface/wireless/registration-table/getall', false);
|
|---|
| 39 | $Routerboard->Write('=.proplist=signal-strength,tx-signal-strength,mac-address,rx-rate,tx-rate', false);
|
|---|
| 40 | $Routerboard->Write('=stats=');
|
|---|
| 41 | $Read = $Routerboard->Read(false);
|
|---|
| 42 | $Array = $Routerboard->ParseResponse($Read);
|
|---|
| 43 | if (array_key_exists('!trap', $Array))
|
|---|
| 44 | {
|
|---|
| 45 | return array();
|
|---|
| 46 | }
|
|---|
| 47 | $NetworkMac = new NetworkMac($this->System);
|
|---|
| 48 | foreach ($Array as $Properties)
|
|---|
| 49 | {
|
|---|
| 50 | $DbResult = $this->Database->select('NetworkInterface', 'Id', 'MAC="'.$Properties['mac-address'].'"');
|
|---|
| 51 | if ($DbResult->num_rows > 0)
|
|---|
| 52 | {
|
|---|
| 53 | $DbRow = $DbResult->fetch_assoc();
|
|---|
| 54 | $Interface = $DbRow['Id'];
|
|---|
| 55 | } else $Interface = 'NULL';
|
|---|
| 56 |
|
|---|
| 57 | if (strpos($Properties['signal-strength'], '@') === false)
|
|---|
| 58 | {
|
|---|
| 59 | $Strength = $Properties['signal-strength'];
|
|---|
| 60 | } else
|
|---|
| 61 | {
|
|---|
| 62 | $Parts = explode('@', $Properties['signal-strength']);
|
|---|
| 63 | if (substr($Parts[0], -3) == 'dBm')
|
|---|
| 64 | $Strength = substr($Parts[0], 0, -3); // without dBm
|
|---|
| 65 | else $Strength = $Parts[0];
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | if (array_key_exists('tx-signal-strength', $Properties))
|
|---|
| 69 | {
|
|---|
| 70 | if (strpos($Properties['tx-signal-strength'], '@') === false)
|
|---|
| 71 | {
|
|---|
| 72 | $RemoteSignal = $Properties['tx-signal-strength'];
|
|---|
| 73 | } else
|
|---|
| 74 | {
|
|---|
| 75 | $Parts = explode('@', $Properties['tx-signal-strength']);
|
|---|
| 76 | if (substr($Parts[0], -3) == 'dBm')
|
|---|
| 77 | $RemoteSignal = substr($Parts[0], 0, -3); // without dBm
|
|---|
| 78 | else $RemoteSignal = $Parts[0];
|
|---|
| 79 | }
|
|---|
| 80 | } else $RemoteSignal = 0;
|
|---|
| 81 |
|
|---|
| 82 | $RateRx = $this->StripUnits($Properties['rx-rate']);
|
|---|
| 83 | $RateTx = $this->StripUnits($Properties['tx-rate']);
|
|---|
| 84 | $MacRef = $NetworkMac->GetIndex($Properties['mac-address']);
|
|---|
| 85 | $Queries[] = 'INSERT INTO `NetworkSignal` (`MAC`, `Value`, `Remote`, `RateRx`, `RateTx`, `Time`, `Interface`, `Device`) VALUES '.
|
|---|
| 86 | '('.$MacRef.', '.$Strength.', '.$RemoteSignal.', '.$RateRx.', '.$RateTx.', "'.
|
|---|
| 87 | TimeToMysqlDateTime($this->Time).'", '.$Interface.', '.$Device['Id'].')';
|
|---|
| 88 | echo('.');
|
|---|
| 89 | }
|
|---|
| 90 | return $Queries;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | // Read 60 GHz wireless clients signal
|
|---|
| 94 | function ReadWireless60G(array $Device, RouterosAPI $Routerboard): array
|
|---|
| 95 | {
|
|---|
| 96 | $Queries = array();
|
|---|
| 97 |
|
|---|
| 98 | $Routerboard->Write('/interface/w60g/monitor', false);
|
|---|
| 99 | $Routerboard->Write('=.proplist=tx-phy-rate,rssi,remote-address', false);
|
|---|
| 100 | $Routerboard->Write('=numbers=wlan60-1', false);
|
|---|
| 101 | $Routerboard->Write('=once=');
|
|---|
| 102 | $Read = $Routerboard->Read(false);
|
|---|
| 103 | $Array = $Routerboard->ParseResponse($Read);
|
|---|
| 104 | if (array_key_exists('!trap', $Array))
|
|---|
| 105 | {
|
|---|
| 106 | return array();
|
|---|
| 107 | }
|
|---|
| 108 | $NetworkMac = new NetworkMac($this->System);
|
|---|
| 109 | foreach ($Array as $Properties)
|
|---|
| 110 | {
|
|---|
| 111 | $DbResult = $this->Database->select('NetworkInterface', 'Id', 'MAC="'.$Properties['remote-address'].'"');
|
|---|
| 112 | if ($DbResult->num_rows > 0)
|
|---|
| 113 | {
|
|---|
| 114 | $DbRow = $DbResult->fetch_assoc();
|
|---|
| 115 | $Interface = $DbRow['Id'];
|
|---|
| 116 | } else $Interface = 'NULL';
|
|---|
| 117 |
|
|---|
| 118 | if (strpos($Properties['rssi'], '@') === false)
|
|---|
| 119 | {
|
|---|
| 120 | $Strength = $Properties['rssi'];
|
|---|
| 121 | } else
|
|---|
| 122 | {
|
|---|
| 123 | $Parts = explode('@', $Properties['rssi']);
|
|---|
| 124 | if (substr($Parts[0], -3) == 'dBm')
|
|---|
| 125 | $Strength = substr($Parts[0], 0, -3); // without dBm
|
|---|
| 126 | else $Strength = $Parts[0];
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | $RateTx = $this->StripUnits($Properties['tx-phy-rate']);
|
|---|
| 130 |
|
|---|
| 131 | $MacRef = $NetworkMac->GetIndex($Properties['remote-address']);
|
|---|
| 132 | $Queries[] = 'INSERT INTO `NetworkSignal` (`MAC`, `Value`, `Remote`, `RateRx`, `RateTx`, `Time`, `Interface`, `Device`) VALUES '.
|
|---|
| 133 | '('.$MacRef.', '.$Strength.', 0, 0, '.round($RateTx / 1000000).', "'.
|
|---|
| 134 | TimeToMysqlDateTime($this->Time).'", '.$Interface.', '.$Device['Id'].')';
|
|---|
| 135 | echo('.');
|
|---|
| 136 | }
|
|---|
| 137 | return $Queries;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | function StripUnits(string $Value): string
|
|---|
| 141 | {
|
|---|
| 142 | if (strpos($Value, '-') !== false) $Value = substr($Value, 0, strpos($Value, '-') - 1); // without channel info
|
|---|
| 143 | if (substr($Value, -3, 3) == "MHz") $Value = substr($Value, 0, -3); // without MHz unit
|
|---|
| 144 | else if (substr($Value, -4, 4) == "Mbps") $Value = substr($Value, 0, -4); // without Mbps unit
|
|---|
| 145 | else if (substr($Value, -3, 3) == "Mbp") $Value = substr($Value, 0, -3); // without Mbp unit
|
|---|
| 146 | else if (substr($Value, -1, 1) == "M") $Value = substr($Value, 0, -1); // without M unit
|
|---|
| 147 | else if (substr($Value, -4, 4) == "Gbps") $Value = substr($Value, 0, -4) * 1000; // without Gbps unit
|
|---|
| 148 | else if (substr($Value, -3, 3) == "Gbp") $Value = substr($Value, 0, -3) * 1000; // without Gbp unit
|
|---|
| 149 | else if (substr($Value, -1, 1) == "G") $Value = substr($Value, 0, -1) * 1000; // without G unit
|
|---|
| 150 | return $Value;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | function Run(): void
|
|---|
| 154 | {
|
|---|
| 155 | RepeatFunction(60 * 60, array($this, 'ReadWirelessRegistration'));
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|