Advertisement
stephanlinke

Untitled

Feb 9th, 2017
217
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: This sensor will read water pump informations and error out if it has been running too often in the given interval
  8. # Parameters:
  9. # -TargetHost:      The host address of the target appliance. Using %host within PRTG will work.
  10. # -Port             The used SNMP port, defaulting to 161
  11. # -Community        The SNMP community
  12. # -Oids             The OID
  13. # -SensorID         The ID of the sensor
  14. # -Minutes          The Interval the sensor should look for pump state entries
  15. #
  16. # Requirements
  17. # ------------------
  18. # - needs snmpget.exe within the EXEXML directory (can be obtained via https://www.snmpsoft.com/cmd-tools/snmp-get/)
  19. #
  20. # Version History
  21. # ------------------
  22. # Version  Date        Notes
  23. # 1.0      09/02/2017  Initial Release
  24. #
  25. # ------------------
  26. # (c) 2017 Stephan Linke | Paessler AG
  27. param(
  28.     [string]$TargetHost = "",
  29.     [int]$SensorID,
  30.     [int]$Port = 161,
  31.     [int]$Minutes = 30,
  32.     [string]$Community = "public",
  33.     [string[]]$Oids = @("1.3.6.1.5")
  34. )
  35.  
  36. $Result = @"
  37. <?xml version="1.0" encoding="UTF-8" ?>
  38. <prtg>
  39. <result>
  40. <channel>Current State</channel>
  41. <value>{0}</value>
  42. <NotifyChanged></NotifyChanged>
  43. </result>
  44. <result>
  45. <channel>Runs in the past {1} minutes</channel>
  46. <value>{2}</value>
  47. </result>
  48. <text>{3}</text>
  49. </prtg>
  50. "@
  51.  
  52. $EventFilter = @{
  53.             ProviderName="PRTG Network Monitor Sensors";
  54.             LogName="PRTG Sensor Logs";
  55.             ID=1;
  56.             StartTime=(get-date).AddMinutes(-$Minutes)
  57.         }
  58.  
  59. function getOid([string]$Oid){
  60.  
  61.     $pinfo = New-Object System.Diagnostics.ProcessStartInfo
  62.     $pinfo.FileName = "C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\snmpget.exe"
  63.     $pinfo.RedirectStandardError = $true
  64.     $pinfo.RedirectStandardOutput = $true
  65.     $pinfo.UseShellExecute = $false
  66.     $pinfo.Arguments = [string]::Format("-r:{0} -p:{1} -t:10 -c:{2} -o:{3} -q", $TargetHost, $Port,$Community, $Oid);
  67.     $p = New-Object System.Diagnostics.Process
  68.     $p.StartInfo = $pinfo
  69.     $p.Start() | Out-Null
  70.  
  71.     #Do Other Stuff Here....
  72.     $p.WaitForExit()
  73.  
  74.     return $p.StandardOutput.ReadToEnd();
  75.  
  76. }
  77.  
  78. $state = [convert]::ToInt32((getOid -oid $Oids[0]).Trim(), 10);
  79.  
  80. # create an event log entry
  81. Write-EventLog -LogName 'PRTG Sensor Logs' -Source "PRTG Network Monitor Sensors" -Message "Water pump is in state $($state)" -ID $state -EntryType Information
  82.  
  83. # get the amount of entries in the last x minutes
  84. $Events = (Get-WinEvent -FilterHashtable $EventFilter)
  85.  
  86. if($State -eq 1)
  87. { $Message = [string]::Format("The water pump is currently running ({0} runs in the last $($Minutes) minutes)",$Events.Count) }
  88. else
  89. { $Message = [string]::Format("The water pump is currently not running ({0} runs in the last $($Minutes) minutes)",$Events.Count) }
  90.  
  91. Write-Host ([string]::Format($result, $State, $Minutes, $Events.Count, $message));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement