# PRTG Custom Sensor to read Kostal Solar System # # 201411208 Ver 1.0 Initial Version # # Todo: HTML Parsen könnte man schneller machen durch eigene String Suchen # nicht alle Werte sind katuell " # param ( [string]$kostalURL="http://192.168.178.5/", # IP-Adress [string]$kostalUser = "pvserver", # ID of Device [string]$kostalpass = "pvwr" # ID of Device ) #set-psdebug -strict # enforce strict variable checking write-host "Start prtg-kostal" (get-date).tostring() write-host "Initializing PRTG Result variable" [string]$prtgresult="" $prtgresult+="`r`n" $prtgresult+="`r`n" [int]$prtgresultcode = 0 write-host ("Parametercheck: KostalURL :" + $kostalURL) [xml]$kostaldata = $null try { write-host (" Loading " + $kostalURL) [System.Security.SecureString]$strSecurePass = ConvertTo-SecureString -String $kostalpass -AsPlainText -Force $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($kostalUser, $strSecurePass) $webpage = Invoke-Webrequest -uri $kostalURL -credential $cred -UseBasicParsing } catch { # $error write-host "Unable to retrieve data via HTML" $prtgresult+=" 2`r`n" $prtgresult+=" ProtocolError:Unable to get HTML-Data. Check IP-Address and Link`r`n" $prtgresultcode = 3 } if ($prtgresultcode -eq 0) { write-host "Parsing HTML-Result" $count = 0 [hashtable]$tdhash=@{} "1122334455" -match "<\s*td[^>]*>([^<]*?)<\s*/td\s*>" $tdelements = ([regex]"<\s*td[^>]*>([^<]*?)<\s*/td\s*>").matches($webpage.content) # result is not an real PowerShell object with index. it's COM-Object foreach ($td in $tdelements) { $count++ $value = ([string]($td.groups[1].value)) $value = $value.replace(" ","") $value = $value.replace(" ","") $value = $value.replace(" ","") $value = $value.replace("`r`n","") write-host ("add"+$count+":"+$value ) if ($value -eq "xxx") { $tdhash.add([string]$count,"0") } else { $tdhash.add([string]$count,$value) } } if ($tdhash.count -ne 118) { write-host ("Error. invalid number of TD-Elements. Expected 139. Found:" + $tdhash.count) $prtgresult+=" 3`r`n" $prtgresult+=" ContentError: Invalid number of total TD-Elements found. Check definition`r`n" $prtgresultcode = 4 } else { # definition in welchem TD-Element welcher Wert steht. [string]$sensorstatus = "Kein Status" $tdconfig = ( ` "feld,Beschreibung,Einheit,Mode,Float,Formel`n" + ` "10,Leistung aktuell,W,Absolute,1,data 10`n" + ` "13,Gesamtenergie,kWh,Absolute,1,data 13`n" + ` "22,Tagesenergie,kWh,Absolute,1,data 22`n" + ` "28,Status,,Absolute,0,status 28`n" + ` "46,String1 Spannung,V,Absolute,1,data 46`n" + ` "55,String1 Strom,A,Absolute,1,data 55`n" + ` "997,String1 Leistung,W,Absolute,1,power 46 55`n" + ` "70,String2 Spannung,V,Absolute,1,data 70`n" + ` "79,String2 Strom,A,Absolute,1,data 79`n" + ` "998,String2 Leistung,W,Absolute,1,power 70 79`n" + ` "94,String3 Spannung,V,Absolute,1,data 94`n" + ` "103,String3 Strom,A,Absolute,1,data 103`n" + ` "999,String3 Leistung,W,Absolute,1,power 94 103`n" + "49,Spannung L1 Ausgang,V,Absolute,1,data 49 `n" + ` "58,Leistung L1 Ausgang,W,Absolute,1,data 58`n" + ` "73,Spannung L2 Ausgang,V,Absolute,1,data 73`n" + ` "82,Leistung L2 Ausgang,W,Absolute,1,data 82`n" + ` "97,Spannung L3 Ausgang,V,Absolute,1,data 97`n" + ` "106,Leistung L3 Ausgang,W,Absolute,1,data 106" ) | ` convertfrom-csv -Delimiter "," foreach ($datapoint in $tdconfig) { write-host " Processing Datapoint :" $datapoint.feld write-host " Datapoint :" $tdhash.item($datapoint.feld) write-host " DatapointFormel :" $datapoint.Formel $formel = $datapoint.Formel.split(" ") switch ($formel[0].tolower()) { "data" { write-host " Formel: Data" $prtgresult+=" `r`n" $prtgresult+=" "+$datapoint.Beschreibung +"`r`n" $prtgresult+=" "+$datapoint.Einheit +"`r`n" $prtgresult+=" "+$datapoint.Float +"`r`n" $prtgresult+=" "+$datapoint.Mode +"`r`n" $prtgresult+=" "+ $tdhash.item($datapoint.feld)+"`r`n" $prtgresult+=" `r`n" } "power" { write-host (" Formel: Power TDFeld1 :" + $formel[1] + " Value:" + $tdhash.item($formel[1])) write-host (" Formel: Power TDFeld1 :" + $formel[2] + " Value:" + $tdhash.item($formel[2])) write-host (" Formel: Power Calcfresult: " + [string]([double]($tdhash.item($formel[1]))*([double]$tdhash.item($formel[2])))) $prtgresult+=" `r`n" $prtgresult+=" "+$datapoint.Beschreibung +"`r`n" $prtgresult+=" "+$datapoint.Einheit +"`r`n" $prtgresult+=" "+$datapoint.Float +"`r`n" $prtgresult+=" "+$datapoint.Mode +"`r`n" $prtgresult+=" "+[string]([double]($tdhash.item($formel[1]))*([double]$tdhash.item($formel[2])))+"`r`n" $prtgresult+=" `r`n" } "status" { write-host " Formel: Status" $sensorstatus = $tdhash.item($datapoint.feld) # switch ($tdhash.item($datapoint.feld)) { # "Aus" { # $prtgresult+=" Aus`r`n" # $sensorstatus = "Wechselrichter ist Aus" # } # "An" { # $prtgresult+=" An`r`n" # $sensorstatus = "Wechselrichter ist An" # } # "Leerlauf" { # $prtgresult+=" Leerlauf`r`n" # $sensorstatus = "Wechselrichter im Leerlaur" # } # "EinspeisenMPP" { # $prtgresult+=" EinspeisenMPP`r`n" # $sensorstatus = "Einspeisen MPP" # } # default {$prtgresult+=" "+ $tdhash.item($datapoint.feld)+"`r`n"} # } } default { write-host " Formel: Invalid Formel" } } } $prtgresult+=" "+$sensorstatus+"`r`n" } } $prtgresult+="" if ($prtgresultcode -ge 2) { write-host "Error Found." } elseif ($prtgresultcode -eq 1) { write-host "Warning Found." } else { write-host "Sending PRTGRESULT to STDOUT" } write-host "Ending with EXIT Code:"$prtgresultcode $prtgresult exit $prtgresultcode