You are here

Schedule downtime using Powershell through the REST API

5 posts / 0 new
Last post
fajasan
fajasan's picture
Schedule downtime using Powershell through the REST API

Hi,

I'm trying to create a Powershell script for scheduling downtime for specific hosts through the REST API. But I cannot get it to work.

My script is below. I think the issue is with the parameters and the way I specify what host to schedule the downtime for.

I hope someone can point me in the right direction?

Thanks,
Sandra

------------------------------------------------------------------------------------

##### opsview-host-scheduledowntime.ps1
##### Get OPSview Host Config

$urlauth = "http://nagios.nl.rsg:3000/rest/login"
$urlconfig = "http://nagios.nl.rsg:3000/rest/downtime"

$user="user"
$pass="password"

$creds = '{"username":"' + $user + '","password":"' + $pass + '"}'

###Generate Auth Token

$bytes1 = [System.Text.Encoding]::ASCII.GetBytes($creds)
$web1 = [System.Net.WebRequest]::Create($urlauth)
$web1.Method = "POST"
$web1.ContentLength = $bytes1.Length
$web1.ContentType = "application/json"
$stream1 = $web1.GetRequestStream()
$stream1.Write($bytes1,0,$bytes1.Length)
$stream1.Close()


$reader1 = New-Object System.IO.Streamreader -ArgumentList $web1.GetResponse().GetResponseStream()
$token1 = $reader1.ReadToEnd()
$reader1.Close()


#Parse Token for follwoing sessions
$token1=$token1.Replace("{`"token`":`"", "")
$token1=$token1.Replace("`"}", "")

$hostname = 'SP-16010'
$hostdata = '{"hst.hostname":"' + $hostname + '","starttime":"now","endtime":"+2h","comment":"Maintenance"}'

#Write-Host $hostdata

$bytes2 = [System.Text.Encoding]::ASCII.GetBytes($hostdata)
$web2 = [System.Net.WebRequest]::Create($urlconfig)
$web2.Method = "POST"
$web2.ContentLength = $bytes2.Length
$web2.ContentType = "application/json"
$web2.Headers.Add("X-Opsview-Username","$user")
$web2.Headers.Add("X-Opsview-Token",$token1);
$stream2 = $web2.GetRequestStream()
$stream2.Write($bytes2,0,$bytes2.Length)
$stream2.Close()

$reader2 = New-Object System.IO.Streamreader -ArgumentList $web2.GetResponse().GetResponseStream()
$output2 = $reader2.ReadToEnd()
$reader2.Close()

Write-Host $output2

daniel.nitz@datef.it
daniel.nitz@datef.it's picture
Same problem here

Hi fajasan,

I'm wokring on the same, did you get this working?

Greetings

feytullahede
feytullahede's picture
Here is the solution

Hi Daniel,

I saw your message when i'm looking for active topics. I hope you're looking for solution yet? I tried many different ways for the solution and solved it!

If i don't misunderstand, problem is in hostdata. "hst.hostname" parameter should not be included in hostdata. Because it's a filtering parameter not an attribute for the downtime 

Actually i'm not so good to understand REST API calls and i don't know powershell at all as a linux administrator :)

I excluded "hst.hostname" in "hostdata" and added "params" variable "urlconfig" as follows.

So i succeded to schedule a downtime for a host :) I really enjoyed to solve an issue. 

------------------------

hostname = "nagios"

#$params = "?host=$hostname"     OR     $params = "?hst.hostname=$hostname"

$params = "?hst.hostname=$hostname"
#$hostdata = '{"hst.hostname":"' + $hostname + '","starttime":"now","endtime":"+2h","comment":"Maintenance"}'
$hostdata = '{"starttime":"now","endtime":"+2h","comment":"Maintenance"}'  

$bytes2 = [System.Text.Encoding]::ASCII.GetBytes($hostdata)
$web2 = [System.Net.WebRequest]::Create("$urlconfig$params")

----------------------------

Regards,

Feytullah Ede

daniel.nitz@datef.it
daniel.nitz@datef.it's picture
Wow!!!

Hi Feytullah,

this works like a charm. Many many thanks!!

Greetings
Daniel

feytullahede
feytullahede's picture
Great

Hi Daniel,

I'm so glad to hear from you :)

Greetings