Advertisement
stephanlinke

[PRTG] Pause sensors that exceed the license or group limit

Dec 21st, 2016
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #requires -version 4.0
  2. # ___ ___ _____ ___
  3. #| _ \ _ \_   _/ __|
  4. #|  _/   / | || (_ |
  5. #|_| |_|_\ |_| \___|
  6. # Pause Sensors that are paused by a license indefinitely
  7. # ================================
  8. # This script will scan your PRTG for sensors that are paused due to licensing and pause them indefinitely.
  9. # This way, users are able to create sensors beyond their actual group limit and don't mess up with the overall sensor count.
  10. #
  11. # Version History
  12. # ----------------------------
  13. # 1.0        Initial Release
  14. ##################################
  15.  
  16. <#
  17.    .SYNOPSIS
  18.    This script will scan your PRTG for sensors that are paused due to licensing and pause them indefinitely.
  19.    .DESCRIPTION
  20.    Installation:
  21.    1. Save the script as PRTG-PauseUnlicensedSensors.ps1 under <PRTG Application folder>\Custom Sensors\EXE\
  22.    2. Create a new EXE/SCRIPT Sensor on the local probe device
  23.    3. Create a new EXE/Script sensor and select the PRTG-PauseUnlicensedSensors.ps1 script
  24.  
  25.    -prtgHostName <your prtg url, e.g. http://prtg.acme.com or https://prtg.acme.com>
  26.    -prtgPort     <the port your PRTG installation is using>
  27.    -prtgUserName <a user with read/write permissions to the device, or a PRTG administrator>
  28.    -prtgPasshash <the passhash of the above user>
  29.  
  30. #>
  31.  
  32.  
  33. param(
  34.        [string]$prtgHostName      = "<your prtg url, e.g. http://prtg.acme.com or https://prtg.acme.com>",
  35.        [string]$prtgPort          = 80,
  36.        [string]$prtgUsername      = "prtgadmin",
  37.        [string]$prtgPasshash      = "12345678",
  38.        [switch]$verbose = $true
  39. )
  40.  
  41. [int]$global:counter = 0;
  42.  
  43. # this will output debug messages to the console
  44. function Console-ShowMessage([string]$type,$message){
  45.     if($verbose){
  46.         Write-Host ("[{0}] " -f (Get-Date)) -NoNewline;
  47.         switch ($type){
  48.             "success"       { Write-Host "    success    "  -BackgroundColor Green      -ForegroundColor White -NoNewline; }
  49.             "information"   { Write-Host "  information  "  -BackgroundColor DarkCyan   -ForegroundColor White -NoNewline; }
  50.             "warning"       { Write-Host "    warning    "  -BackgroundColor DarkYellow -ForegroundColor White -NoNewline; }
  51.             "error"         { Write-Host "     error     "  -BackgroundColor DarkRed    -ForegroundColor White -NoNewline; }
  52.             default         { Write-Host "     notes     "  -BackgroundColor DarkGray   -ForegroundColor White -NoNewline; }
  53.         }
  54.         Write-Host (" {0}{1}" -f $message,$Global:blank)
  55.     }
  56. }
  57.  
  58. # This function will retrieve all sensors that are paused by license
  59. function This-RetrievePausedSensors(){
  60.  
  61.  
  62. $url = [string]::Format("{0}:{1}/api/table.json?content=sensors&output=json&columns=objid,sensor,status&filter_status=11&username={2}&passhash={3}",$prtgHostName, $prtgPort, $prtgUserName, $prtgPasshash);
  63.  
  64.          # call the URL and store the content, we need the actual ID of the new sensor to change the settings and unpause it
  65.          $request = (Invoke-WebRequest -Uri $url)
  66.          $sensors = ($request.Content | ConvertFrom-Json).sensors
  67.  
  68.          $global:counter = $sensors.count;
  69.  
  70.          Foreach ($sensor in $sensors)
  71.          { This-PauseUnlicensedSensors -sensorName $sensor.sensor -sensorId $sensor.objid; }
  72.  
  73.          
  74.  
  75. }
  76.  
  77. # This function will generate the PRTG Metascan Items for the VMs in XML format
  78. function This-PauseUnlicensedSensors {
  79.  
  80.      param([string]$sensorName,[int]$sensorId)
  81.  
  82.      $message = "Sensor paused indefinitely by License Check sensor."
  83.  
  84.      try{
  85.  
  86.          Console-ShowMessage "information" ([string]::Format("Pausing Sensor {0} with ID {1}",$sensorName,$sensorId));
  87.  
  88.          # create the URL for duplicating the original sensor
  89.          
  90.          $url = [string]::Format("{0}:{1}/api/pause.htm?id={2}&pausemsg={3}&action=0&username={4}&passhash={5}",
  91.                           $prtgHostName, $prtgPort, $sensorId, $message, $prtgUserName, $prtgPasshash);
  92.  
  93.          # call the URL and store the content, we need the actual ID of the new sensor to change the settings and unpause it
  94.          $request = [System.Net.WebRequest]::Create($url);
  95.          $request.AllowAutoRedirect = $false
  96.          $response=$request.GetResponse();
  97.  
  98.  
  99.          If ($response.StatusCode -eq 200)
  100.          {
  101.  
  102.             $response.GetResponseHeader("Location") -match '\d{3,}' | Out-Null;
  103.             $newSensorID = $Matches[0];
  104.             Console-ShowMessage -type "success" -message "Sensor paused succesfully!";
  105.          }
  106.          Else
  107.          { Console-ShowMessage "error" "Pausing the sensor failed. PRTG returned: $($response.StatusCode)"; }
  108.  
  109.      }
  110.      catch{ return $false; }
  111. }
  112.  
  113. This-RetrievePausedSensors
  114. Write-Host ([string]::Format("{0}:{0} sensor(s) were paused in the last run.", $global:counter));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement