Since I didn't put a XML parser or similar in the code, this must be an issue PRTG is showing you.
How does the output look like when you run it in PowerShell manually?
Pretty sure it is a minor issue...
PS: If the output you posted is from the code/script I provided, I wonder about one thing here - where does this come from?
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12217 100 12217 0 0 12217 0 0:00:01 --:--:-- 0:00:01 20060
Right now I don't see a reason this should be outputted.. and yes - if this is the case - this needs to go away - PRTG can't parse this ...
All you want to have as output is this:
<prtg><result><channel>VPN1</channel><value>0</value><channel>entry</channel><value>0</value><channel>entry</channel><value>0</value><channel>entry</channel><value>0</value><channel>entry</channel><value>0</value><channel>entry</channel><value>0</value><channel>VPN2</channel><value>0</value><channel>VPN3</channel><value>1</value><channel>VPN4</channel><value>1</value><channel>VPN5</channel><value>1</value><channel>VPN6</channel><value>1</value><channel>VPN7</channel><value>1</value><channel>VPN8</channel><value>1</value><channel>VPN9</channel><value>1</value><channel>VPN10</channel><value>1</value><channel>VPN11</channel><value>1</value><channel>VPN12</channel><value>0</value><channel>VPN13</channel><value>0</value><channel>VPN14</channel><value>0</value><channel>VPN15</channel><value>0</value><channel>VPN16</channel><value>1</value><channel>VPN17</channel><value>0</value><channel>VPN18</channel><value>0</value><channel>VPN19</channel><value>0</value><channel>VPN20</channel><value>1</value><channel>VPN21</channel><value>1</value><channel>VPN22</channel><value>0</value><channel>VPN23</channel><value>1</value><channel>VPN24</channel><value>0</value><channel>VPN25</channel><value>1</value><channel>VPN26</channel><value>1</value><channel>VPN27</channel><value>0</value><channel>VPN28</channel><value>0</value><channel>VPN29</channel><value>0</value><channel>VPN30</channel><value>0</value><channel>VPN31</channel><value>1</value><channel>VPN32</channel><value>0</value><channel>VPN33</channel><value>0</value></result></prtg>
more pretty:
<prtg>
<result>
<channel>VPN1</channel>
<value>0</value>
<channel>entry</channel>
<value>0</value>
<channel>entry</channel>
<value>0</value>
<channel>entry</channel>
<value>0</value>
<channel>entry</channel>
<value>0</value>
<channel>entry</channel>
<value>0</value>
<channel>VPN2</channel>
<value>0</value>
<channel>VPN3</channel>
<value>1</value>
<channel>VPN4</channel>
<value>1</value>
<channel>VPN5</channel>
<value>1</value>
<channel>VPN6</channel>
<value>1</value>
<channel>VPN7</channel>
<value>1</value>
<channel>VPN8</channel>
<value>1</value>
<channel>VPN9</channel>
<value>1</value>
<channel>VPN10</channel>
<value>1</value>
<channel>VPN11</channel>
<value>1</value>
<channel>VPN12</channel>
<value>0</value>
<channel>VPN13</channel>
<value>0</value>
<channel>VPN14</channel>
<value>0</value>
<channel>VPN15</channel>
<value>0</value>
<channel>VPN16</channel>
<value>1</value>
<channel>VPN17</channel>
<value>0</value>
<channel>VPN18</channel>
<value>0</value>
<channel>VPN19</channel>
<value>0</value>
<channel>VPN20</channel>
<value>1</value>
<channel>VPN21</channel>
<value>1</value>
<channel>VPN22</channel>
<value>0</value>
<channel>VPN23</channel>
<value>1</value>
<channel>VPN24</channel>
<value>0</value>
<channel>VPN25</channel>
<value>1</value>
<channel>VPN26</channel>
<value>1</value>
<channel>VPN27</channel>
<value>0</value>
<channel>VPN28</channel>
<value>0</value>
<channel>VPN29</channel>
<value>0</value>
<channel>VPN30</channel>
<value>0</value>
<channel>VPN31</channel>
<value>1</value>
<channel>VPN32</channel>
<value>0</value>
<channel>VPN33</channel>
<value>0</value>
</result>
</prtg>
And there we go - the XML has a bug
I think I was under assumption you get only one result - how ever, I did a mistake - the RESULT tag needs to wrap every channel... the output would need to look like this:
<prtg>
<result>
<channel>VPN1</channel>
<value>0</value>
</result>
<result>
<channel>entry</channel>
<value>0</value>
</result>
<result>
<channel>entry</channel>
<value>0</value>
</result>
<result>
<channel>entry</channel>
<value>0</value>
</result>
</prtg>
Here the corrected script:
$SecurePassword = Get-Content "C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\vpnmonitor_password.txt <file:///C:/Program%20Files%20(x86)/PRTG%20Network%20Monitor/Custom%20Sensors/EXEXML/vpnmonitor_password.txt> " | ConvertTo-SecureString
$Marshal = [System.Runtime.InteropServices.Marshal]
$Bstr = $Marshal::SecureStringToBSTR($SecurePassword)
$Password = $Marshal::PtrToStringAuto($Bstr)
$Marshal::ZeroFreeBSTR($Bstr)
$VPNTunnelState = & 'C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\curl.exe' -k -X GET "https://hostname/api/?type=op&cmd=<show><running><tunnel><flow><all></all></flow></tunnel></running></show>&key=$Password" | Out-File vpnmonitor.xml
$XmlDocument = Get-Content -Path .\vpnmonitor.xml
$a = $XmlDocument.SelectNodes('//entry')
#$VPNTunnelState is not used - in theory you could directly use the next two lines instead of the above three - not tested though..
#$VPNTunnelState = [xml]('C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\curl.exe' -k -X GET "https://hostname/api/?type=op&cmd=<show><running><tunnel><flow><all></all></flow></tunnel></running></show>&key=$Password")
#$a = $VPNTunnelState.SelectNodes('//entry')
#having said that, you probably could even use a PS command to directly pase the HTML file mentioned above.. but that's really another story...
$XML = "<prtg>"
foreach($Node in $a) {
if($Node.state -eq 'active') {
$XML += "<result>"
$XML += "<channel>"+ $Node.name +"</channel>"
$XML += "<value>1</value>"
$XML += "</result>"
} Else {
$XML += "<result>"
$XML += "<channel>"+ $Node.name +"</channel>"
$XML += "<value>0</value>"
$XML += "</result>"
}
}
$XML += "</prtg>"
Function WriteXmlToScreen ([xml]$xml) #just to make it clean XML code...
{
$StringWriter = New-Object System.IO.StringWriter;
$XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter;
$XmlWriter.Formatting = "indented";
$xml.WriteTo($XmlWriter);
$XmlWriter.Flush();
$StringWriter.Flush();
Write-Output $StringWriter.ToString();
}
WriteXmlToScreen "$XML"
The above script has a XML-Formatting function as well.. making the output more readable and kind of parsing it correct..
Again - this should not be in the output at all (see below) - not sure where it comes from and I really can't test the script due to not having such a device... developing blind here...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12217 100 12217 0 0 12217 0 0:00:01 --:--:-- 0:00:01 20060
If I would need to guess - I assume this line causes the additional wrong output:
$VPNTunnelState = & 'C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\curl.exe' -k -X GET "https://hostname/api/?type=op&cmd=<show><running><tunnel><flow><all></all></flow></tunnel></running></show>&key=$Password" | Out-File vpnmonitor.xml
Please be aware of a restriction as well - PRTG can process 50 channels per sensor - no more.. just be sure you don't hit that limit..
Regards
Florian
Add comment