Advertisement
stephanlinke

[PRTG] Check QoS Health

Apr 8th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #require --version 4.0
  2. <#
  3.  ___ ___ _____ ___
  4. | _ \ _ \_   _/ __|
  5. |  _/   / | || (_ |
  6. |_| |_|_\ |_| \___|
  7. #>
  8. [string]$prtg_server = "enter-your-server-address";
  9. [string]$prtg_protoc = "http-or-https";
  10. [int]$prtg_port      = 80;
  11. [string]$prtg_user   = "your-prtg-user";
  12. [string]$prtg_pass   = "your-prtg-users-passhash";
  13. [string]$max_downtime= 59
  14. [string]$find_msg    = "Timeout. No packets received";
  15. [string]$find_status = 5;
  16. [string]$pause_message = "There was a connection problem and the port might have been occupied. Pausing until port is free again."
  17.  
  18.  
  19. <# this will test if the prtg server could be accessed. the sensor will be down otherwise #>
  20. function This-CheckPrtgAccess(){
  21.   $options = @($prtg_protoc, $prtg_server, $prtg_port,$prtg_user,$prtg_pass);
  22.   $prtg_uri = [string]::Format("{0}://{1}:{2}/home?username={3}&passhash={4}",$options);
  23.   $Session = (Invoke-Webrequest -Uri $prtg_uri -UseBasicParsing -SessionVariable PRTG);
  24.  
  25.   if($Session.StatusCode -ne 200){
  26.     write-host "0:Authentication against PRTG failed."; This-Quit 2
  27.   }
  28. }
  29.  
  30. <# this will pause the sensor for 1 minute #>
  31. function This-PauseSensor([int]$sensor_id, [string]$message, [int]$duration){
  32.  
  33.   $options = @($prtg_protoc, $prtg_server, $prtg_port, $sensor_id, $message, $duration, $prtg_user, $prtg_pass);
  34.   [string]$prtg_uri    = [string]::Format("{0}://{1}:{2}/api/pauseobjectfor.htm?id={3}&pausemsg={4}&duration={5}&username={6}&passhash={7}", $options);
  35.  
  36.   $Result = (Invoke-Webrequest -Uri $prtg_uri -UseBasicParsing -SessionVariable $PRTG);
  37.  
  38. }
  39.  
  40. <# this will find all QoS sensors that are down, have a certain message and are down for longer than x seconds #>
  41. function This-FindTroubledQoSSensors([string]$current_message = $find_msg, [int]$current_state = $find_status){
  42.  
  43.     $options = @($prtg_protoc, $prtg_server, $prtg_port, $current_state, $current_message, $prtg_user, $prtg_pass);
  44.     [string]$prtg_uri   = [string]::Format("{0}://{1}:{2}/api/table.json?content=sensors&output=json&columns=objid,name,sensor,message,downtimesince,type&filter_status={3}&filter_message=@sub({4})&filter_sensor=@sub(QoS)&username={5}&passhash={6}", $options);
  45.     $troubled_sensors = ((Invoke-Webrequest -Uri $prtg_uri -UseBasicParsing -SessionVariable $PRTG) | ConvertFrom-Json)
  46.     #write-host $prtg_uri;
  47.     return $troubled_sensors.Sensors;
  48.  
  49. }
  50.  
  51. function This-CheckSensors(){
  52.  
  53.     $sensors = This-FindTroubledQoSSensors;
  54.  
  55.     foreach($sensor in $sensors){
  56.  
  57.      if($sensor.downtimesince_raw -gt $max_downtime){
  58.       This-PauseSensor -sensor_id $sensor.objid -message $pause_message -duration 5
  59.     }
  60.     }
  61.  
  62.     Write-Host "0:QoS Health check running...";
  63. }
  64.  
  65. function This-Quit([int]$exitCode){
  66.     if(!($verbose)){ exit $exitCode }
  67. }
  68.  
  69. This-CheckPrtgAccess;
  70. This-CheckSensors;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement