-
Notifications
You must be signed in to change notification settings - Fork 0
/
Custom Powershell Unifi Status.ps1
146 lines (119 loc) · 5.08 KB
/
Custom Powershell Unifi Status.ps1
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
# Monitor the Status of AP's on Unfi Controller in PRTG v0.8 27/06/2017
# Published Here: https://kb.paessler.com/en/topic/71263
#
# Parameters in PRTG are: Controller's URI, Port, Site, Username and Password. Example without placeholders:
# -server 'unifi.domain.tld' -port '8443' -site 'default' -username 'admin' -password 'somepassword'
#
# -server '%host' -port '8443' -site 'default' -username '%windowsuser' -password '%windowspassword'
# This second option requires the device's address in PRTG to be the controller's address, the credentials for windows devices
# must also match the log-in/password from the controller. This way you don't leave the password exposed in the sensor's settings.
#
# It's recommended to use larger scanning intervals for exe/xml scripts. Please also mind the 50 exe/script sensor's recommendation per probe.
# The sensor will not generate alerts by default, after creating your sensor, define limits accordingly.
# This sensor is to be considered experimental. The Ubnt's API documentation isn't completely disclosed.
#
# Source(s):
# http://community.ubnt.com/t5/UniFi-Wireless/little-php-class-for-unifi-api/m-p/603051
# https://github.com/fbagnol/class.unifi.php
# https://www.ubnt.com/downloads/unifi/5.3.8/unifi_sh_api
# https://github.com/malle-pietje/UniFi-API-browser/blob/master/phpapi/class.unifi.php
param(
[string]$server = 'unifi.domain.com',
[string]$port = '8443',
[string]$site = 'default',
[string]$username = 'admin',
[string]$password = '123456',
[switch]$debug = $false
)
#Ignore SSL Errors
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
#Define supported Protocols
[System.Net.ServicePointManager]::SecurityProtocol = @("Tls12","Tls11","Tls","Ssl3")
# Confirm Powershell Version.
if ($PSVersionTable.PSVersion.Major -lt 3) {
Write-Output "<prtg>"
Write-Output "<error>1</error>"
Write-Output "<text>Powershell Version is $($PSVersionTable.PSVersion.Major) Requires at least 3. </text>"
Write-Output "</prtg>"
Exit
}
# Create $controller and $credential using multiple variables/parameters.
[string]$controller = "https://$($server):$($port)"
[string]$credential = "`{`"username`":`"$username`",`"password`":`"$password`"`}"
# Start debug timer
$queryMeasurement = [System.Diagnostics.Stopwatch]::StartNew()
# Perform the authentication and store the token to myWebSession
try {
$null = Invoke-Restmethod -Uri "$controller/api/login" -method post -body $credential -ContentType "application/json; charset=utf-8" -SessionVariable myWebSession
}catch{
Write-Output "<prtg>"
Write-Output "<error>1</error>"
Write-Output "<text>Authentication Failed: $($_.Exception.Message)</text>"
Write-Output "</prtg>"
Exit
}
#Query API providing token from first query.
try {
$jsonresultat = Invoke-Restmethod -Uri "$controller/api/s/$site/stat/device/" -WebSession $myWebSession
}catch{
Write-Output "<prtg>"
Write-Output "<error>1</error>"
Write-Output "<text>API Query Failed: $($_.Exception.Message)</text>"
Write-Output "</prtg>"
Exit
}
# Load File from Debug Log
# $jsonresultatFile = Get-Content '.\unifi_sensor2017-15-02-05-42-24_log.json'
# $jsonresultat = $jsonresultatFile | ConvertFrom-Json
# Stop debug timer
$queryMeasurement.Stop()
# Iterate jsonresultat and count the number of AP's.
# $_.state -eq "1" = Connected
# $_.type -like "uap" = Access Point ?
$apCount = 0
Foreach ($entry in ($jsonresultat.data | where-object { $_.state -eq "1" -and $_.type -like "uap"})){
$apCount ++
}
$apUpgradeable = 0
Foreach ($entry in ($jsonresultat.data | where-object { $_.state -eq "1" -and $_.type -like "uap" -and $_.upgradable -eq "true"})){
$apUpgradeable ++
}
$userCount = 0
Foreach ($entry in ($jsonresultat.data | where-object { $_.type -like "uap"})){
$userCount += $entry.'num_sta'
}
$guestCount = 0
Foreach ($entry in ($jsonresultat.data | where-object { $_.type -like "uap"})){
$guestCount += $entry.'guest-num_sta'
}
#Write Results
write-host "<prtg>"
Write-Host "<result>"
Write-Host "<channel>Access Points Connected</channel>"
Write-Host "<value>$($apCount)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Access Points Upgradeable</channel>"
Write-Host "<value>$($apUpgradeable)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Clients (Total)</channel>"
Write-Host "<value>$($userCount)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Guests</channel>"
Write-Host "<value>$($guestCount)</value>"
Write-Host "</result>"
Write-Host "<result>"
Write-Host "<channel>Response Time</channel>"
Write-Host "<value>$($queryMeasurement.ElapsedMilliseconds)</value>"
Write-Host "<CustomUnit>msecs</CustomUnit>"
Write-Host "</result>"
write-host "</prtg>"
# Write JSON file to disk when -debug is set. For troubleshooting only.
if ($debug){
[string]$logPath = ((Get-ItemProperty -Path "hklm:SOFTWARE\Wow6432Node\Paessler\PRTG Network Monitor\Server\Core" -Name "Datapath").DataPath) + "Logs (Sensors)\"
$timeStamp = (Get-Date -format yyyy-dd-MM-hh-mm-ss)
$json = $jsonresultat | ConvertTo-Json
$json | Out-File $logPath"unifi_sensor$($timeStamp)_log.json"
}