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

Last change on this file was 982, checked in by chronos, 8 hours ago
  • Fixed: Handle missing tx/rx rate in RouterOS signal read.
File size: 6.0 KB
Line 
1<?php
2
3class 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 $New = $this->ReadWireless($Device, $Routerboard);
26 $New60G = $this->ReadWireless60G($Device, $Routerboard);
27 $New = array_merge($New, $New60G);
28 $Queries = array_merge($Queries, $New);
29 }
30 echo("\n");
31 }
32 $this->Database->Transaction($Queries);
33 }
34
35 // Read normal wireless clients signal
36 function ReadWireless(array $Device, RouterosAPI $Routerboard): array
37 {
38 $Queries = array();
39
40 $Routerboard->Write('/interface/wireless/registration-table/getall', false);
41 $Routerboard->Write('=.proplist=signal-strength,tx-signal-strength,mac-address,rx-rate,tx-rate', false);
42 $Routerboard->Write('=stats=');
43 $Read = $Routerboard->Read(false);
44 $Array = $Routerboard->ParseResponse($Read);
45 if (array_key_exists('!trap', $Array))
46 {
47 return array();
48 }
49 $NetworkMac = new NetworkMac($this->System);
50 foreach ($Array as $Properties)
51 {
52 $DbResult = $this->Database->select('NetworkInterface', 'Id', 'MAC="'.$Properties['mac-address'].'"');
53 if ($DbResult->num_rows > 0)
54 {
55 $DbRow = $DbResult->fetch_assoc();
56 $Interface = $DbRow['Id'];
57 } else $Interface = 'NULL';
58
59 if (strpos($Properties['signal-strength'], '@') === false)
60 {
61 $Strength = $Properties['signal-strength'];
62 } else
63 {
64 $Parts = explode('@', $Properties['signal-strength']);
65 if (substr($Parts[0], -3) == 'dBm')
66 $Strength = substr($Parts[0], 0, -3); // without dBm
67 else $Strength = $Parts[0];
68 }
69
70 if (array_key_exists('tx-signal-strength', $Properties))
71 {
72 if (strpos($Properties['tx-signal-strength'], '@') === false)
73 {
74 $RemoteSignal = $Properties['tx-signal-strength'];
75 } else
76 {
77 $Parts = explode('@', $Properties['tx-signal-strength']);
78 if (substr($Parts[0], -3) == 'dBm')
79 $RemoteSignal = substr($Parts[0], 0, -3); // without dBm
80 else $RemoteSignal = $Parts[0];
81 }
82 } else $RemoteSignal = 0;
83
84 $RateRx = $this->StripUnits($Properties['rx-rate']);
85 $RateTx = $this->StripUnits($Properties['tx-rate']);
86 $MacRef = $NetworkMac->GetIndex($Properties['mac-address']);
87 $Queries[] = 'INSERT INTO `NetworkSignal` (`MAC`, `Value`, `Remote`, `RateRx`, `RateTx`, `Time`, `Interface`, `Device`) VALUES '.
88 '('.$MacRef.', '.$Strength.', '.$RemoteSignal.', '.$RateRx.', '.$RateTx.', "'.
89 TimeToMysqlDateTime($this->Time).'", '.$Interface.', '.$Device['Id'].')';
90 echo('.');
91 }
92 return $Queries;
93 }
94
95 // Read 60 GHz wireless clients signal
96 function ReadWireless60G(array $Device, RouterosAPI $Routerboard): array
97 {
98 $Queries = array();
99
100 $Routerboard->Write('/interface/w60g/monitor', false);
101 $Routerboard->Write('=.proplist=tx-phy-rate,rssi,remote-address', false);
102 $Routerboard->Write('=numbers=wlan60-1', false);
103 $Routerboard->Write('=once=');
104 $Read = $Routerboard->Read(false);
105 $Array = $Routerboard->ParseResponse($Read);
106 if (array_key_exists('!trap', $Array))
107 {
108 return array();
109 }
110 $NetworkMac = new NetworkMac($this->System);
111 foreach ($Array as $Properties)
112 {
113 $DbResult = $this->Database->select('NetworkInterface', 'Id', 'MAC="'.$Properties['remote-address'].'"');
114 if ($DbResult->num_rows > 0)
115 {
116 $DbRow = $DbResult->fetch_assoc();
117 $Interface = $DbRow['Id'];
118 } else $Interface = 'NULL';
119
120 if (strpos($Properties['rssi'], '@') === false)
121 {
122 $Strength = $Properties['rssi'];
123 } else
124 {
125 $Parts = explode('@', $Properties['rssi']);
126 if (substr($Parts[0], -3) == 'dBm')
127 $Strength = substr($Parts[0], 0, -3); // without dBm
128 else $Strength = $Parts[0];
129 }
130
131 $RateTx = $this->StripUnits($Properties['tx-phy-rate']);
132
133 $MacRef = $NetworkMac->GetIndex($Properties['remote-address']);
134 $Queries[] = 'INSERT INTO `NetworkSignal` (`MAC`, `Value`, `Remote`, `RateRx`, `RateTx`, `Time`, `Interface`, `Device`) VALUES '.
135 '('.$MacRef.', '.$Strength.', 0, 0, '.round($RateTx / 1000000).', "'.
136 TimeToMysqlDateTime($this->Time).'", '.$Interface.', '.$Device['Id'].')';
137 echo('.');
138 }
139 return $Queries;
140 }
141
142 function StripUnits(string $Value): string
143 {
144 if ($Value == '---') $Value = 0; // no speed available
145 if (strpos($Value, '-') !== false) $Value = substr($Value, 0, strpos($Value, '-') - 1); // without channel info
146 if (substr($Value, -3, 3) == "MHz") $Value = substr($Value, 0, -3); // without MHz unit
147 else if (substr($Value, -4, 4) == "Mbps") $Value = substr($Value, 0, -4); // without Mbps unit
148 else if (substr($Value, -3, 3) == "Mbp") $Value = substr($Value, 0, -3); // without Mbp unit
149 else if (substr($Value, -1, 1) == "M") $Value = substr($Value, 0, -1); // without M unit
150 else if (substr($Value, -4, 4) == "Gbps") $Value = substr($Value, 0, -4) * 1000; // without Gbps unit
151 else if (substr($Value, -3, 3) == "Gbp") $Value = substr($Value, 0, -3) * 1000; // without Gbp unit
152 else if (substr($Value, -1, 1) == "G") $Value = substr($Value, 0, -1) * 1000; // without G unit
153 return $Value;
154 }
155
156 function Run(): void
157 {
158 RepeatFunction(60 * 60, array($this, 'ReadWirelessRegistration'));
159 }
160}
Note: See TracBrowser for help on using the repository browser.