-
Notifications
You must be signed in to change notification settings - Fork 506
Expand file tree
/
Copy pathzabbix-node-api-maintenance.json.human
More file actions
251 lines (237 loc) · 8.83 KB
/
zabbix-node-api-maintenance.json.human
File metadata and controls
251 lines (237 loc) · 8.83 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
{
"Id": "2fdc47cd-d120-4919-b0d5-2ed22ca8ff62",
"Name": "Zabbix API maintenance",
"Description": "This step template adds single host on Zabbix maintenance.",
"ActionType": "Octopus.Script",
"Version": 12,
"Properties": {
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.ScriptBody": "echo \"Hello world\"
$Zserver=\"#{zserver}\"
$Zuser=\"#{zuser}\"
$Zpassword=\"#{zpass}\"
$Zhost=\"#{zhost}\"
$setgmt=#{gmt}
$hours=#{hours}
$action=\"#{action}\"
$number=\"#{number}\"
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
function Get-Auth{
param(
$server,
$user,
$pass,
$url
)
$body='{\"jsonrpc\": \"2.0\", \"method\": \"user.login\", \"params\": {\"user\": \"'+\"$user\"+'\", \"password\": \"'+\"$pass\"+'\"}, \"id\": 1, \"auth\": null}'
try {
$key=Invoke-WebRequest -Uri \"$url\" -ContentType application/json-rpc -Body $body -Method Put -UseBasicParsing
} catch [Exception] {
Write-Error \"Error: cannot connect to zabbix server ($($_.Exception.Message)), check hostname/url! Frequently zabbix is installed on a virtual folder like {hostname}/zabbix, please include the folder into the hostname variable.`r`n\" -ErrorAction Stop
}
$token=($key.Content | ConvertFrom-Json).result
return $token
}
function Remove-Maintenance{
param(
$srvr,
$usr,
$pswd,
$uri,
$mname
)
$remove='{\"jsonrpc\": \"2.0\", \"method\": \"maintenance.get\", \"params\": {\"output\": \"extend\", \"selectHosts\": \"extend\", \"selectTimeperiods\": \"extend\"},\"auth\": \"'+\"$auth\"+'\",\"id\": 1}'
$maintenace=Invoke-WebRequest -Uri \"$uri\" -ContentType application/json-rpc -Body $remove -Method Put -UseBasicParsing
$select= ($maintenace.Content | ConvertFrom-Json).result | where{$_.name -like \"$mname\"}
$id=$select.maintenanceid
if($id){
Write-Output \"Remove maintenance ID: $id\"
$rmv='{\"jsonrpc\": \"2.0\", \"method\": \"maintenance.delete\", \"params\": [\"'+\"$id\"+'\"], \"auth\": \"'+\"$auth\"+'\",\"id\": 1}'
$actionremove=Invoke-WebRequest -Uri \"$uri\" -ContentType application/json-rpc -Body $rmv -Method Put -UseBasicParsing
$check=(($actionremove.Content | ConvertFrom-Json).result).maintenanceids
if($check -like $id){
Write-Output \"Maintenance $id removed\"
}
else{
Write-Error \"Something wrong. Please contact your system administrator\"
}
}
else{
Write-Error \"NO Maintenance ID - contact your system administrator\"
}
}
###GLOBAL VARIABLES###
if (!$Zserver.StartsWith(\"http\")) { $Zserver=\"http://$Zserver\" }
$Zurl=\"$Zserver/api_jsonrpc.php\"
$maintenancename=\"Octo-$number-$Zhost\"
###GET AUTH FROM ZABBIX SERVER###
$auth=Get-Auth -server $Zserver -user $Zuser -pass $Zpassword -url $Zurl
if ($auth -eq $null) {
Write-Error \"Authentication failure for user $Zuser on server $Zserver!\" -ErrorAction Stop
exit
}
###GET HOST ID###
$content='{\"jsonrpc\": \"2.0\", \"method\": \"host.get\", \"params\": {\"output\": \"extend\", \"filter\": {\"host\": \"'+\"$Zhost\"+'\"}},\"auth\": \"'+\"$auth\"+'\",\"id\": 1}'
$zabbixhost=Invoke-WebRequest -Uri \"$Zurl\" -ContentType application/json-rpc -Body $content -Method Put -UseBasicParsing
$nameserver=$zabbixhost.Content | ConvertFrom-Json
$hostid=$nameserver.result.hostid
if($hostid){
Write-Output \"Host $Zhost found with ID: $hostid\"
}
else{
Write-Error \"Host $Zhost not found, or user not authorized for this host - please contact your system administrator!\"
exit
}
###ADD NEW MAINTENANCE###
if ($action -eq \"create\"){
###REMOVE MAINTENANCE IF ALREADY EXISTS WITH THE SAME NAME###
$remove='{\"jsonrpc\": \"2.0\", \"method\": \"maintenance.get\", \"params\": {\"output\": \"extend\", \"selectHosts\": \"extend\", \"selectTimeperiods\": \"extend\"},\"auth\": \"'+\"$auth\"+'\",\"id\": 1}'
$maintenace=Invoke-WebRequest -Uri \"$Zurl\" -ContentType application/json-rpc -Body $remove -Method Put -UseBasicParsing
$select= ($maintenace.Content | ConvertFrom-Json).result | where{$_.name -like \"$maintenancename\"}
if(!$select){
Write-Output \"No maintenance with the same name is already registered\"
}
else{
Remove-Maintenance -srvr $Zserver -usr $Zuser -pswd $Zpassword -uri $Zurl -mname $maintenancename
}
###START TO CREATE NEW MAINTENANCE###
$since=[int][double]::Parse((get-date -UFormat %s))
$till=0
###ATTENTION ON GMT - THIS WORK FOR ITALIAN ZONE AND TAKES DAYLIGHT SAVINGSTIME FROM###
###start check your ZABBIX configuration###
$workdate=(Get-Date)
if (![int32]::TryParse($setgmt, [ref] $gmt)) { $gmt=([TimeZoneInfo]::Local.BaseUtcOffset).Hours }
if ($workdate.IsDaylightSavingTime()) { $gmt+=1 }
$min=$workdate.AddHours(-$gmt).Minute
$h=$workdate.AddHours(-$gmt).Hour
$minutetoseconds=$min*=60
$hourstoseconds=$h*=3600
$starttime=$minutetoseconds+=$hourstoseconds
$seconds=$hours*=3600
$sincesum=$since
$till=$sincesum+=$seconds
$since=$since-=(60*60*$gmt)
$till=$till-=(60*60*$gmt)
###stop check your ZABBIX configuration###
$add='{\"jsonrpc\": \"2.0\", \"method\": \"maintenance.create\", \"params\": {\"name\": \"'+\"$maintenancename\"+'\", \"active_since\": \"'+\"$since\"+'\", \"active_till\": '+\"$till\"+', \"hostids\": [\"'+$hostid+'\"], \"timeperiods\": [{\"timeperiod_type\": 0, \"start_time\": '+$starttime+', \"period\": '+$seconds+'}]}, \"auth\": \"'+$auth+'\", \"id\": 1}'
$maintenance=Invoke-WebRequest -Uri \"$Zurl\" -ContentType application/json-rpc -Body $add -Method Put -UseBasicParsing
$check=(($maintenance.Content | ConvertFrom-Json).result).maintenanceids
if($check){
Write-Output \"Maintenance $check created\"
}
else{
Write-Error \"Something wrong. Please contact your system administrator\"
}
}
else{
if($action -eq \"remove\"){
Remove-Maintenance -srvr $Zserver -usr $Zuser -pswd $Zpassword -uri $Zurl -mname $maintenancename
}
else{
Write-Error \"Action $action not possible\"
}
}"
},
"Parameters": [
{
"Id": "2d679eba-f403-4548-a57d-dacacfb5b299",
"Name": "zserver",
"Label": "Zabbix Server",
"HelpText": null,
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
},
"Links": {}
},
{
"Id": "898bbce8-28d3-43d1-9d19-a659ffce3865",
"Name": "zuser",
"Label": "Zabbix Username",
"HelpText": null,
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
},
"Links": {}
},
{
"Id": "86e4c608-e37f-4a0a-a432-e1c07b8f2b6b",
"Name": "zpass",
"Label": "Zabbix Password",
"HelpText": null,
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "Sensitive"
},
"Links": {}
},
{
"Id": "1f51ac2a-852a-4f88-be40-a1304eb11cb2",
"Name": "zhost",
"Label": "Host",
"HelpText": "host in maintenance - single host",
"DefaultValue": "#{Octopus.Machine.Name}",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
},
"Links": {}
},
{
"Id": "50e5fbdd-949e-4ce8-8a10-94a403c3fb2d",
"Name": "action",
"Label": "",
"HelpText": null,
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "Select",
"Octopus.SelectOptions": "create|Create maintenance
remove|Remove maintenance"
},
"Links": {}
},
{
"Id": "b1ef0d32-ec8e-44f3-a7f4-a34af6cd252c",
"Name": "gmt",
"Label": "GMT",
"HelpText": "This option depends on Zabbix server configuration.
If it is not necessary, set GMT=0 (UTC).
If UTC+2, insert 2. If UTC-4, insert -4.",
"DefaultValue": "0",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
},
"Links": {}
},
{
"Id": "6fa1f56d-b9ee-4830-9e88-a434c6b82774",
"Name": "hours",
"Label": "Maintenance Period",
"HelpText": "in hours",
"DefaultValue": "4",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
},
"Links": {}
},
{
"Id": "ca825d16-f0ff-4bcc-b0e7-abcde1f2188e",
"Name": "number",
"Label": "ID for maintenance name",
"HelpText": "maintenance name: Octo-[number]-[host]",
"DefaultValue": "#{Octopus.Release.Number}",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
},
"Links": {}
}
],
"LastModifiedBy": "dpijl10",
"$Meta": {
"ExportedAt": "2018-04-11T11:33:40.330Z",
"OctopusVersion": "2018.2.7",
"Type": "ActionTemplate"
},
"Category": "zabbix"
}