#prtg-shellyplug
#
# 20200420 Initiale Versoin
# 20200422 Eigene Meters.total als Abs und Diff
# 20200423 Eigene Diff Rechnung mit Speicherung in Datei
# 20200427 Entfernen der eigenen Diff Berechnnug, Parametrisierung addiert
# 20200428 CustomUnit Verbrauch angepasst. Debugausgaben für Fehlersuche mit PRTG +1 Bug
# 20200430 OverPower als Kanal addiert und Exitcode Warnung bei OverPower
# 20200831 Erweiterung um Authentifizierung
# 20210514 Fix XML-Ausgabe mit neuerem Shelly Firmware und einige Tippfehler
param (
[string]$url="", #URL of Shelly Device
[long]$httptimeoutsec = 2, # maximum time to wait for a response
[string]$shellyuser = "" , # optional Username
[string]$shellypass = "" # password forplain text access
)
write-host "PRTG-Shelly URL : $($url)"
write-host "PRTG-Shelly Timeout : $($httptimeoutsec)"
write-host "PRTG-Shelly User : $($shellyuser)"
write-host "PRTG-Shelly Pass : $($shellypass)"
[string]$prtgresult=""
if ($url -eq "") {
$prtgresult+=""
$prtgresult+=" 1"
$prtgresult+=" Error: Specify ShellyURL as Parameter -url:""http://hostname/status"""
$prtgresult+=""
#Also report a PRTG System error
exit 2
}
else {
try{
if ($shellyuser -ne "") {
Write-host "Try to get REST-Data... Authenticated"
$secpasswd = ConvertTo-SecureString $shellypass -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($shellyuser, $secpasswd)
$result = invoke-restmethod -uri "$($url)" -TimeoutSec $httptimeoutsec -Credential $credential
}
else {
Write-host "Try to get REST-Data... Anonym"
$result = invoke-restmethod -uri "$($url)" -TimeoutSec $httptimeoutsec
}
Write-host " MAC : $($result.mac)"
write-host " Data: Power : $($result.meters.power)"
write-host " Data: MeterTotal : $($result.meters.total)"
write-host " Data: Temperature: $($result.temperature)"
write-host " Data: WiFiRSSI : $($result.wifi_sta.rssi)"
$prtgresult+=""
$prtgresult+=" "
$prtgresult+=" Aktuell"
$prtgresult+=" $($result.meters.power)"
$prtgresult+=" Custom"
$prtgresult+=" Watt"
$prtgresult+=" 1"
$prtgresult+=" "
$prtgresult+=" "
$prtgresult+=" Verbrauch"
$prtgresult+=" $([long]($result.meters.total*60))"
$prtgresult+=" Custom"
$prtgresult+=" W s"
$prtgresult+=" Difference"
$prtgresult+=" 0"
$prtgresult+=" "
$prtgresult+=" "
$prtgresult+=" Verbrauch Total"
$prtgresult+=" $($([long]($result.meters.total/60)/10)/100)"
$prtgresult+=" Custom"
$prtgresult+=" kWh"
$prtgresult+=" Absolute"
$prtgresult+=" 1"
$prtgresult+=" "
$prtgresult+=" "
$prtgresult+=" Temperatur"
$prtgresult+=" $([long]$result.temperature)"
$prtgresult+=" Custom"
$prtgresult+=" Grad C"
$prtgresult+=" 0"
$prtgresult+=" "
$prtgresult+=" "
$prtgresult+=" WLAN-RSSI"
$prtgresult+=" $($result.wifi_sta.rssi)"
$prtgresult+=" Custom"
$prtgresult+=" db"
$prtgresult+=" 0"
$prtgresult+=" "
$prtgresult+=" "
$prtgresult+=" Relais"
$prtgresult+=" $(if ($result.relays.ison) {""1""} else {""0""})"
$prtgresult+=" Custom"
$prtgresult+=" On/Off"
$prtgresult+=" 0"
$prtgresult+=" "
$prtgresult+=" "
$prtgresult+=" OverPower"
$prtgresult+=" $(if ($result.relays.overpower) {""1""} else {""0""})"
$prtgresult+=" Custom"
$prtgresult+=" On/Off"
$prtgresult+=" 0"
$prtgresult+=" "
$prtgresult+=" "
$prtgresult+=" Uptime"
$prtgresult+=" $($result.uptime)"
$prtgresult+=" Custom"
$prtgresult+=" Seconds"
$prtgresult+=" 0"
$prtgresult+=" "
$prtgresult+=" "
$prtgresult+=" UpdatePending"
$prtgresult+=" $(if ($result.update.has_update) {""1""} else {""0""})"
$prtgresult+=" Custom"
$prtgresult+=" Update"
$prtgresult+=" 0"
$prtgresult+=" "
$prtgresult+=" 0"
$prtgresult+=" WLAN-SSID:$($result.wifi_sta.ssid) Firmware:$($result.update.new_version) MAC:$($result.mac)"
$prtgresult+=""
$prtgresult
if ($result.relays.overpower) {
# Status auf Warnung setzen, um OverPower zu melden
exit 1
}
else {
exit 0
}
}
catch{
$prtgresult+=""
$prtgresult+=" 1"
$prtgresult+=" Error:$($_.Exception.Message)"
$prtgresult+=""
#Also report a PRTG System error
$prtgresult
exit 2
}
}