-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopsviewctl_downtime.ps1
106 lines (96 loc) · 4.75 KB
/
opsviewctl_downtime.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
########################################################################################################################################################################################################
########################################################################################################################################################################################################
# Copyright Stamatis Litinakis 2015/02/07
#
# opsviewctl_dowtime.ps1 v0.1
#
# using syntax: opsviewctl_dowtime.ps1
# -server (ip or hostname)
# -user (Opsview Username)
# -user (Opsview Password)
# -action ( "POST", "GET", "DELETE")
# -hostname (Opsview hostname object)
# -hostgroup (Opsview hostgroup object)
# -start (Opsview downtime start time ("+1h" or "YYYY/MM/DD hh:mm:ss"))
# -end (Opsview downtime end time ("+1h" or "YYYY/MM/DD hh:mm:ss"))
# -comment (Opsview downtime comments)
#
# EXAMPLE1: .\opsviewctl_dowtime.ps1 -server 192.168.1.1 -user admin -pass mypassword -action "POST" -hostname SERVER1 -end "+15m" -comment "WEB RELEASE"
# EXAMPLE2: .\opsviewctl_dowtime.ps1 -server 192.168.1.1 -user admin -pass mypassword -action "GET"
# EXAMPLE3: .\opsviewctl_dowtime.ps1 -server 192.168.1.1 -user admin -pass mypassword -action "DELETE" -hostname SERVER1
#
# EXAMPLE4: .\opsviewctl_dowtime.ps1 -server 192.168.1.1 -user admin -pass mypassword -action "POST" -hostgroupname WEBSERVERS -end "+1m" -comment "This is a test"
# EXAMPLE5: .\opsviewctl_dowtime.ps1 -server 192.168.1.1 -user admin -pass mypassword -action "DELETE" -hostgroupname WEBSERVERS
#
# EXAMPLE4: .\opsviewctl_dowtime.ps1 -server 192.168.1.1 -user admin -pass mypassword -action "POST" -hostgroupname WEBSERVERS -start "2015/02/08 20:00:00" -end "+90m" -comment "This is a scheduled downtime of 90 minutes"
########################################################################################################################################################################################################
########################################################################################################################################################################################################
param (
[string]$server = $args[0],
[string]$user = $args[1],
[string]$pass = $args[2],
[string]$action = $args[3],
[string]$hostname = $args[4],
[string]$hostgroupname = $args[4],
[string]$start = $args[5],
[string]$end = $args[6],
[string]$comment = $args[7]
)
$urlauthenticate = "/rest/login"
$urldowntime = "/rest/downtime"
$credentials = '{"username":"' + $user + '","password":"' + $pass + '"}'
$bytes = [System.Text.Encoding]::ASCII.GetBytes($credentials)
$request = [System.Net.WebRequest]::Create("http://" + $server + $urlauthenticate)
$request.Method = "POST"
$request.ContentLength = $bytes.Length
$request.ServicePoint.Expect100Continue = $false
$request.ContentType = "application/json"
$stream = $request.GetRequestStream()
$stream.Write($bytes,0,$bytes.Length)
$stream.Close()
$streamreader = New-Object System.IO.Streamreader -ArgumentList $request.GetResponse().GetResponseStream()
$token = $streamreader.ReadToEnd()
$streamreader.Close()
$token=$token.Replace("{`"token`":`"", "")
$token=$token.Replace("`"}", "")
if ($hostname)
{ $params = "?hst.hostname=$hostname"}
elseif ($hostgroupname)
{ $params = "?hg.hostgroupname=$hostgroupname"}
If ($action -eq "POST")
{
if ($start)
{$hostdata = '{"starttime":"'+$start+'","endtime":"'+$end+'","comment":"'+$comment+'"}'}
else
{$hostdata = '{"starttime":"now","endtime":"'+$end+'","comment":"'+$comment+'"}'}
$bytes = [System.Text.Encoding]::ASCII.GetBytes($hostdata)
$request = [System.Net.WebRequest]::Create("http://$server$urldowntime$params")
$request.Method = $action
$request.ContentLength = $bytes.Length
$request.ServicePoint.Expect100Continue = $false
$request.ContentType = "application/json"
$request.Headers.Add("X-Opsview-Username","$user")
$request.Headers.Add("X-Opsview-Token",$token);
$stream = $request.GetRequestStream()
$stream.Write($bytes,0,$bytes.Length)
$stream.Close()
}
elseif ($action -eq "DELETE")
{
$request = [System.Net.WebRequest]::Create("http://$server$urldowntime$params")
$request.Method = $action
$request.ContentType = "application/json"
$request.Headers.Add("X-Opsview-Username","$user")
$request.Headers.Add("X-Opsview-Token",$token);
}
elseif ($action -eq "GET")
{
$request = [System.Net.WebRequest]::Create("http://$server$urldowntime")
$request.Method = $action
$request.ContentType = "application/json"
$request.Headers.Add("X-Opsview-Username","$user")
$request.Headers.Add("X-Opsview-Token",$token);
}
$streamreader = New-Object System.IO.Streamreader -ArgumentList $request.GetResponse().GetResponseStream()
$streamreader.ReadToEnd()
$streamreader.Close()