-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.php
348 lines (303 loc) · 16.1 KB
/
module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
declare(strict_types=1);
require_once __DIR__.'/../libs/myFunctions.php'; // globale Funktionen
include_once __DIR__.'/../libs/timetest.php';
// Modul Prefix
if (!defined('MODUL_PREFIX'))
{
define("MODUL_PREFIX", "UMEM");
}
class UnifiMultiEndpointMonitor extends IPSModule
{
use myFunctions;
use TestTime;
public function Create()
{
//Never delete this line!
parent::Create();
$this->RegisterPropertyInteger("ControllerType", 0);
$this->RegisterPropertyString("ServerAddress", "192.168.1.1");
$this->RegisterPropertyInteger("ServerPort", "443");
$this->RegisterPropertyString("Site", "default");
$this->RegisterPropertyString("UserName", "");
$this->RegisterPropertyString("Password", "");
$this->RegisterPropertyInteger("Timer", "0");
$this->RegisterPropertyString("DeviceMac", "");
$this->RegisterPropertyString("Devices", "");
//$this->RegisterPropertyInteger("ConnectionType", 0);
$this->RegisterPropertyBoolean("DataPointNetwork", 0);
$this->RegisterPropertyBoolean("DataPointConnection", 0);
$this->RegisterPropertyBoolean("DataPointTransfer", 0);
$this->RegisterTimer("UniFi Multi Endpoint Monitor", 0, MODUL_PREFIX."_EndpointMonitor(\$_IPS['TARGET']);");
}
public function Destroy()
{
//Never delete this line!
parent::Destroy();
}
public function ApplyChanges()
{
//Never delete this line!
parent::ApplyChanges();
$vpos = 10;
//$this->RegisterVariableBoolean('Connected', $this->Translate('Connected'));
$DevicesList = $this->ReadPropertyString("Devices");
$DevicesJSON = json_decode($DevicesList, true);
if (isset($DevicesJSON))
{
foreach ($DevicesJSON as $Device)
{
$DeviceName = $Device["varDeviceName"];
$DeviceMac = $this->removeInvalidChars($Device["varDeviceMAC"], true);
$ConnectionType = $Device["varDeviceConnectionType"];
$vpos = 10;
$this->RegisterVariableBoolean($DeviceMac."_Connected", $DeviceName.$this->Translate(' Connected'));
//Network Data
$this->MaintainVariable($DeviceMac."IPAddress", $DeviceName.$this->Translate(" IP Address"), vtString, "", $vpos++, $this->ReadPropertyBoolean("DataPointNetwork") == 1);
$this->MaintainVariable($DeviceMac."Hostname", $DeviceName.$this->Translate(" Hostname"), vtString, "", $vpos++, $this->ReadPropertyBoolean("DataPointNetwork") == 1);
//Connection Data General
$vpos = 100;
$this->MaintainVariable($DeviceMac."Satisfaction", $DeviceName.$this->Translate(" Satisfaction"), vtInteger, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1);
$this->MaintainVariable($DeviceMac."LastSeen", $DeviceName.$this->Translate(" Last Seen"), vtString, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1);
$this->MaintainVariable($DeviceMac."Uptime", $DeviceName.$this->Translate(" Uptime in hours"), vtInteger, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1);
//Connection Data Wired
$vpos = 300;
//$this->MaintainVariable("SwitchPort", $this->Translate("Switch Port"), vtString, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1 AND $this->ReadPropertyInteger("ConnectionType") == 1);
//$this->MaintainVariable("SwitchMAC", $this->Translate("Switch MAC"), vtString, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1 AND $this->ReadPropertyInteger("ConnectionType") == 1);
//Connection Data Wireless
$vpos = 500;
$this->MaintainVariable($DeviceMac."Accesspoint", $DeviceName.$this->Translate(" Accesspoint"), vtString, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1 && $ConnectionType == 0);
$this->MaintainVariable($DeviceMac."Channel", $DeviceName.$this->Translate(" Channel"), vtInteger, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1 && $ConnectionType == 0);
$this->MaintainVariable($DeviceMac."Radio", $DeviceName.$this->Translate(" Radio"), vtString, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1 && $ConnectionType == 0);
$this->MaintainVariable($DeviceMac."ESSID", $DeviceName.$this->Translate(" ESS ID"), vtString, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1 && $ConnectionType == 0);
$this->MaintainVariable($DeviceMac."RSSI", $DeviceName.$this->Translate(" RSSI"), vtInteger, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1 && $ConnectionType == 0);
$this->MaintainVariable($DeviceMac."Noise", $DeviceName.$this->Translate(" Noise"), vtInteger, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1 && $ConnectionType == 0);
$this->MaintainVariable($DeviceMac."SignalStrength", $DeviceName.$this->Translate(" Signal Strength"), vtInteger, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1 && $ConnectionType == 0);
$this->MaintainVariable($DeviceMac."LastUplinkName", $DeviceName.$this->Translate(" Last Uplink Name"), vtString, "", $vpos++, $this->ReadPropertyBoolean("DataPointConnection") == 1 && $ConnectionType == 0);
//Transfer Data
$vpos = 600;
$this->MaintainVariable($DeviceMac."TXBytes", $DeviceName.$this->Translate(" TX Megabytes"), vtInteger, "", $vpos++, $this->ReadPropertyBoolean("DataPointTransfer") == 1 && $ConnectionType == 0);
$this->MaintainVariable($DeviceMac."RXBytes", $DeviceName.$this->Translate(" RX Megabytes"), vtInteger, "", $vpos++, $this->ReadPropertyBoolean("DataPointTransfer") == 1 && $ConnectionType == 0);
$this->MaintainVariable($DeviceMac."TXPackets", $DeviceName.$this->Translate(" TX Packets"), vtInteger, "", $vpos++, $this->ReadPropertyBoolean("DataPointTransfer") == 1 && $ConnectionType == 0);
$this->MaintainVariable($DeviceMac."RXPackets", $DeviceName.$this->Translate(" RX Packets"), vtInteger, "", $vpos++, $this->ReadPropertyBoolean("DataPointTransfer") == 1 && $ConnectionType == 0);
}
}
$TimerMS = $this->ReadPropertyInteger("Timer") * 1000;
$this->SetTimerInterval("UniFi Multi Endpoint Monitor", $TimerMS);
if (0 == $TimerMS)
{
// instance inactive
$this->SetStatus(104);
}
else
{
// instance active
$this->SetStatus(102);
}
}
public function AuthenticateAndGetData(string $UnifiAPI = "")
{
$ControllerType = $this->ReadPropertyInteger("ControllerType");
$ServerAddress = $this->ReadPropertyString("ServerAddress");
$ServerPort = $this->ReadPropertyInteger("ServerPort");
$Username = $this->ReadPropertyString("UserName");
$Password = $this->ReadPropertyString("Password");
$Site = $this->ReadPropertyString("Site");
$DeviceMac = strtolower($this->ReadPropertyString("DeviceMac"));
//Change the Unifi API to be called here
if ("" == $UnifiAPI)
{
$Site = $this->ReadPropertyString("Site");
//$UnifiAPI = "api/s/".$Site."/stat/sta"."/".$DeviceMac;
$UnifiAPI = "api/s/".$Site."/stat/sta";
}
//Generic Section providing for Authenthication against a DreamMachine or Classic CloudKey
$Cookie = $this->getCookie($Username, $Password, $ServerAddress, $ServerPort, $ControllerType);
// Section below will collect and return the RawData
if (!isset($Cookie) || false == $Cookie)
{
return false;
}
else
{
$RawData = $this->getRawData($Cookie, $ServerAddress, $ServerPort, $UnifiAPI, $ControllerType);
return $RawData;
}
}
public function EndpointMonitor()
{
$ControllerType = $this->ReadPropertyInteger("ControllerType");
$ServerAddress = $this->ReadPropertyString("ServerAddress");
$ServerPort = $this->ReadPropertyInteger("ServerPort");
$Username = $this->ReadPropertyString("UserName");
$Password = $this->ReadPropertyString("Password");
$Site = $this->ReadPropertyString("Site");
$DeviceMac = strtolower($this->ReadPropertyString("DeviceMac"));
//$UnifiAPI = "api/s/".$Site."/stat/sta"."/".$DeviceMac;
$UnifiAPI = "api/s/".$Site."/stat/sta";
//Generic Section providing for Authenthication against a DreamMachine or Classic CloudKey
$Cookie = $this->getCookie($Username, $Password, $ServerAddress, $ServerPort, $ControllerType);
// Section below will collect and return the RawData
if (!isset($Cookie) || false == $Cookie)
{
return false;
}
else
{
$RawData = $this->getRawData($Cookie, $ServerAddress, $ServerPort, $UnifiAPI, $ControllerType);
}
if (false !== $RawData)
{
if ($RawData !== "")
{
$JSONData = json_decode($RawData, true);
$ActiveDevices = $JSONData;
//Load devices from config form
$DevicesList = $this->ReadPropertyString("Devices");
$DevicesJSON = json_decode($DevicesList, true);
foreach ($DevicesJSON as $Device)
{
//Build a clean array out of the devices mentioned in the config form with : or -
$DeviceMac = $this->removeInvalidChars($Device["varDeviceMAC"], true);
$DeviceName = $Device["varDeviceName"];
$ConnectionType = $Device["varDeviceConnectionType"];
$Connected = false;
//Itterate through all device and check if one of them matches the list in the config form.
foreach ($ActiveDevices["data"] as $Index => $DeviceFromController)
{
$DeviceFromControllerClean = $this->removeInvalidChars($DeviceFromController["mac"], true);
if ($DeviceMac == $DeviceFromControllerClean)
{
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Device with Name was found: ").$DeviceName, 0);
$Connected = true;
$ConnectionMethod = $DeviceFromController["is_wired"];
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Setze Wert"), 0);
if ($ConnectionMethod == true && $ConnectionType == 0)
{
$ConnectionConfigError = true;
//$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Config error - device monitored is a wired device. Please select wired in the module configuration."), 0);
}
else
{
$ConnectionConfigError = false;
}
if ($this->ReadPropertyBoolean("DataPointNetwork") == 1)
{
if (isset($DeviceFromController["ip"])) {
$IPAddress = $DeviceFromController["ip"];
$this->SetValue($DeviceMac."IPAddress", $IPAddress);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Network Data IP ").$IPAddress, 0);
}
if ("" != $DeviceFromController["hostname"])
{
$Hostname = $DeviceFromController["hostname"];
}
else
{
$Hostname = "";
}
$this->SetValue($DeviceMac."Hostname", $Hostname);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Network Data Hostname ").$Hostname, 0);
$Connected = true;
}
if ($this->ReadPropertyBoolean("DataPointConnection") == 1)
{
if (isset($DeviceFromController["satisfaction"])) {
$Satisfaction = $DeviceFromController["satisfaction"];
//$Satisfaction = isset($DeviceMac["satisfaction"]);
$this->SetValue($DeviceMac."Satisfaction", $Satisfaction);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data Satisfaction ").$Satisfaction, 0);
}
//$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data Satisfaction ").$Satisfaction, 0);
$SLastSeen = $DeviceFromController["last_seen"];
$SLastSeen = $SLastSeen + date("Z");
$this->SetValue($DeviceMac."LastSeen", gmdate("Y-m-d H:i:s", $SLastSeen));
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data Last Seen ").gmdate("Y-m-d H:i:s", $SLastSeen), 0);
$Uptime = $DeviceFromController["uptime"];
$this->SetValue($DeviceMac."Uptime", round($Uptime / 3600, 0));
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data Uptime in hours ").round($Uptime / 3600, 0), 0);
}
if ($this->ReadPropertyBoolean("DataPointConnection") == 1 AND $ConnectionType == 0 AND $ConnectionConfigError == false)
{
if(isset($DeviceFromController["ap_mac"])) {
$Accesspoint = $DeviceFromController["ap_mac"];
$this->SetValue($DeviceMac."Accesspoint", $Accesspoint);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data Accesspoint ").$Accesspoint, 0);
}
if(isset($DeviceFromController["channel"])) {
$Channel = $DeviceFromController["channel"];
$this->SetValue($DeviceMac."Channel", $Channel);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data Channel ").$Channel, 0);
}
if(isset($DeviceFromController["radio"])) {
$Radio = $DeviceFromController["radio"];
$this->SetValue($DeviceMac."Radio", $Radio);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data Radio ").$Radio, 0);
}
if(isset($DeviceFromController["essid"])) {
$ESSID = $DeviceFromController["essid"];
$this->SetValue($DeviceMac."ESSID", $ESSID);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data ESSID ").$ESSID, 0);
}
if(isset($DeviceFromController["rssi"])) {
$RSSI = $DeviceFromController["rssi"];
$this->SetValue($DeviceMac."RSSI", $RSSI);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data RSSI ").$RSSI, 0);
}
if(isset($DeviceFromController["noise"])) {
$Noise = $DeviceFromController["noise"];
$this->SetValue($DeviceMac."Noise", $Noise);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data Noise ").$Noise, 0);
}
if(isset($DeviceFromController["signal"])) {
$SignalStrength = $DeviceFromController["signal"];
$this->SetValue($DeviceMac."SignalStrength", $SignalStrength);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Connection Data SignalStrength ").$SignalStrength, 0);
}
if(isset($DeviceFromController["last_uplink_name"])) {
$LastUplinkName = $DeviceFromController["last_uplink_name"];
$this->SetValue($DeviceMac."LastUplinkName", $LastUplinkName);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Last Uplink Name ").$LastUplinkName, 0);
}
}
if ($this->ReadPropertyBoolean("DataPointTransfer") == 1 AND $ConnectionType == 0 AND $ConnectionConfigError == false)
{
$TXBytes = $DeviceFromController["tx_bytes"];
$this->SetValue($DeviceMac."TXBytes", $TXBytes / 1000000);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Transfer Data TXBytes ").$TXBytes / 1000000, 0);
$RXBytes = $DeviceFromController["rx_bytes"];
$this->SetValue($DeviceMac."RXBytes", $RXBytes / 1000000);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Transfer Data RXBytes ").$RXBytes / 1000000, 0);
$TXPackets = $DeviceFromController["tx_packets"];
$this->SetValue($DeviceMac."TXPackets", $TXPackets);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Transfer Data TXPackets ").$TXPackets, 0);
$RXPackets = $DeviceFromController["rx_packets"];
$this->SetValue($DeviceMac."RXPackets", $RXPackets);
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("Transfer Data RXPackets ").$RXPackets, 0);
}
}
else
{
//$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("NOT found - Device: ").$DeviceName, 0);
}
$this->SetValue($DeviceMac."_Connected", $Connected);
}
}
}
else
{
$this->SendDebug($this->Translate("Endpoint Monitor"), $this->Translate("There does not seem to be any configuration - no data is available from the UniFi"), 0);
}
}
}
public function checkSiteName()
{
$ControllerType = $this->ReadPropertyInteger("ControllerType");
$ServerAddress = $this->ReadPropertyString("ServerAddress");
$ServerPort = $this->ReadPropertyInteger("ServerPort");
$Username = $this->ReadPropertyString("UserName");
$Password = $this->ReadPropertyString("Password");
$Site = $this->ReadPropertyString("Site");
return $this->getSiteName($Site, $Username, $Password, $ServerAddress, $ServerPort, $ControllerType);
}
}