Advertisement
stephanlinke

[PRTG] Get Counters

Aug 18th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ___ ___ _____ ___
  2. #| _ \ _ \_   _/ __|
  3. #|  _/   / | || (_ |
  4. #|_| |_|_\ |_| \___|
  5. #    NETWORK MONITOR
  6. #-------------------
  7. # Description: Reads performance counters from a file, retrieves them and puts them into channels
  8. # ------------------
  9. # (c) 2015 Stephan Linke | Paessler AG
  10. Param(
  11.         [string]$Computername,
  12.         [string]$CounterFile = "counter-cpu.dat" )
  13.  
  14. #Variables
  15. [string]$CounterBasePath = "C:\temp\"
  16. [string]$CounterPath = $CounterBasePath + $CounterFile
  17. [int]$i = 1
  18.  
  19. # lets test the connection first
  20. If(!(Test-Connection -Count 1 -Quiet $Computername)) { Write-Host "<prtg><error>1</error><text>Host not found.</text></prtg>"; exit 2; }
  21.  
  22. # create a new counter list
  23. $CounterList =  New-Object System.Collections.ArrayList
  24.  
  25. # retrieve the counters and add them to the list
  26. $Counters = (Get-Content -Path $CounterPath | ConvertFrom-Csv -Delimiter ";")
  27.  
  28. # add the path from the csv to an array list so we can use it with get-counter
  29. $Counters | ForEach-Object { $CounterList.Add($_.Path)} | Out-Null
  30.  
  31. # retrieve the counters
  32. $ResultSet = (Get-Counter -ComputerName $Computername -Counter $CounterList)
  33.  
  34. # add the values to the corresponding counter object
  35. Foreach($Result in $ResultSet.CounterSamples){
  36.     $Counters | Where {$_.ID -eq $i } | Add-Member "Value" $Result.CookedValue
  37.     $i++;
  38. }
  39.  
  40. Write-Host "<?xml version='1.0' encoding='UTF-8' ?><prtg>";
  41. Foreach ($Counter in $Counters){
  42. Write-Host @"
  43.  <result>
  44.  <channel>$($Counter.FriendlyName)</channel>
  45.  <value>$($Counter.Value)</value>
  46.  <unit>$($Counter.Unit)</unit>
  47.  <float>$($Counter.Float)</float>
  48.  </result>
  49. "@
  50. }
  51. Write-Host "</prtg>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement