stephanlinke

[PRTG] Create HTTP sensors from URL list

Oct 13th, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param(
  2.        [string]$prtgHostName      = "",                        # your-prtg-host, e.g. http://prtg.astrata.eu
  3.        [string]$prtgPort          = 80,                        # the port of your prtg webinterface
  4.        [string]$prtgUserName      = 'prtgadmin',               # the prtg user you'd like to use
  5.        [string]$prtgPasshash      = 12345678,                  # the users passhash, can be obtained from his prtg account page
  6.        [int]$prtgDeviceId         = 8216,                      # the device you want to add the sensors to
  7.        [int]$prtgTemplateSensor   = 8240,                       # the ID of a http sensor you already created
  8.        [string]$urlFile           = "C:\temp\url.txt"          # the path to your url list file
  9. )
  10.  
  11. # this will output debug messages to the console
  12. function Console-ShowMessage([string]$type,$message){
  13.         Write-Host ("[{0}] " -f (Get-Date)) -NoNewline;
  14.         switch ($type){
  15.             "success"       { Write-Host "    success    "  -BackgroundColor Green      -ForegroundColor White -NoNewline; }
  16.             "information"   { Write-Host "  information  "  -BackgroundColor DarkCyan   -ForegroundColor White -NoNewline; }
  17.             "warning"       { Write-Host "    warning    "  -BackgroundColor DarkYellow -ForegroundColor White -NoNewline; }
  18.             "error"         { Write-Host "     error     "  -BackgroundColor DarkRed    -ForegroundColor White -NoNewline; }
  19.             default         { Write-Host "     notes     "  -BackgroundColor DarkGray   -ForegroundColor White -NoNewline; }
  20.         }
  21.         Write-Host (" {0}{1}" -f $message,$Global:blank)
  22. }
  23.  
  24. # @Output: Create HTTP Push (Advanced) sensors
  25. #
  26. # This function will generate the PRTG Metascan Items for the VMs in XML format
  27. function This-AddHTTPPushSensors {
  28.      param([string]$sensorName)
  29.  
  30.          Console-ShowMessage "information" "Starting sensor creation for $($sensorName)";
  31.  
  32.          # create the URL for duplicating the original sensor
  33.          $url = [string]::Format("{0}:{1}/api/duplicateobject.htm?id={2}&name={3}&targetid={4}&username={5}&passhash={6}",
  34.                           $prtgHostName, $prtgPort, $prtgTemplateSensor, $sensorName, $prtgDeviceId, $prtgUserName, $prtgPasshash);
  35.          
  36.          # call the URL and store the content, we need the actual ID of the new sensor to change the settings and unpause it
  37.          $request = [System.Net.WebRequest]::Create($url);
  38.          $request.AllowAutoRedirect = $false
  39.          $response=$request.GetResponse();
  40.    
  41.      
  42.          If ($response.StatusCode -eq "Redirect")
  43.          {
  44.             $response.GetResponseHeader("Location") -match '\d{3,}' | Out-Null;
  45.             $newSensorID = $Matches[0];  
  46.             Console-ShowMessage -type "success" -message "Sensor created successfully. New sensor ID: $($newSensorID)";
  47.          }
  48.          Else
  49.          { Console-ShowMessage "error" "Sensor creation failed. PRTG returned: $($response.StatusCode)"; }
  50.  
  51.          # modify the token to resemble the URL of the device
  52.          $url = [string]::Format("{0}:{1}/api/setobjectproperty.htm?id={2}&name={3}&value={4}&username={5}&passhash={6}",
  53.                          $prtgHostName, $prtgPort, $newSensorID, "name", $sensorname, $prtgUserName, $prtgPasshash);
  54.          
  55.          $UpdateSensorResult = (Invoke-WebRequest $url)
  56.      
  57.          if($UpdateSensorResult.StatusCode -eq "200")
  58.          { Console-ShowMessage -type "success" -message "Sensor has been renamed succesfully"; }
  59.  
  60.           # modify the token to resemble the URL of the device
  61.          $url = [string]::Format("{0}:{1}/api/setobjectproperty.htm?id={2}&name={3}&value={4}&username={5}&passhash={6}",
  62.                          $prtgHostName, $prtgPort, $newSensorID, "httpurl", $sensorname, $prtgUserName, $prtgPasshash);
  63.          
  64.          $UpdateSensorURL = (Invoke-WebRequest $url)
  65.      
  66.          # resume the new sensors
  67.          $url = [string]::Format("{0}:{1}/api/pause.htm?id={2}&action=1&username={3}&passhash={4}",
  68.                          $prtgHostName, $prtgPort, $newSensorID,  $prtgUserName, $prtgPasshash);
  69.          
  70.          $StartSensorURL = (Invoke-WebRequest $url)
  71.      
  72.          if($UpdateSensorURL.StatusCode -eq "200")
  73.          { Console-ShowMessage -type "success" -message "Sensor has been renamed succesfully"; }
  74.  
  75.          if($UpdateSensorResult.StatusCode -eq "200")
  76.          { Console-ShowMessage -type "success" -message "Changed sensor name to URL."; }
  77.      
  78.          if($UpdateSensorURL.StatusCode -eq "200")
  79.          { Console-ShowMessage -type "success" -message "Sensor URL has been changed succesfully."; }
  80.      }
  81.  
  82. $lines = Get-Content $urlFile
  83.  
  84. foreach ($line in $lines) {
  85.  This-AddHTTPPushSensors -sensorName $line
  86. }
  87.  
  88. Console-ShowMessage -type information -message "Created $($lines.Count) sensors!"
Advertisement
Add Comment
Please, Sign In to add comment