-
Notifications
You must be signed in to change notification settings - Fork 506
Expand file tree
/
Copy pathzabbix-node-api-maintenance.json
More file actions
110 lines (110 loc) · 8.97 KB
/
zabbix-node-api-maintenance.json
File metadata and controls
110 lines (110 loc) · 8.97 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
{
"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\"\n$Zserver=\"#{zserver}\"\n$Zuser=\"#{zuser}\"\n$Zpassword=\"#{zpass}\"\n$Zhost=\"#{zhost}\"\n$setgmt=#{gmt}\n$hours=#{hours}\n$action=\"#{action}\"\n$number=\"#{number}\"\n\n[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;\n\nfunction Get-Auth{\n param(\n $server,\n $user,\n $pass,\n $url\n )\n $body='{\"jsonrpc\": \"2.0\", \"method\": \"user.login\", \"params\": {\"user\": \"'+\"$user\"+'\", \"password\": \"'+\"$pass\"+'\"}, \"id\": 1, \"auth\": null}'\n try {\n $key=Invoke-WebRequest -Uri \"$url\" -ContentType application/json-rpc -Body $body -Method Put -UseBasicParsing\n } catch [Exception] {\n 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\n }\n $token=($key.Content | ConvertFrom-Json).result\n return $token\n}\n\nfunction Remove-Maintenance{\n param(\n $srvr,\n $usr,\n $pswd,\n $uri,\n $mname\n )\n $remove='{\"jsonrpc\": \"2.0\", \"method\": \"maintenance.get\", \"params\": {\"output\": \"extend\", \"selectHosts\": \"extend\", \"selectTimeperiods\": \"extend\"},\"auth\": \"'+\"$auth\"+'\",\"id\": 1}'\n\n $maintenace=Invoke-WebRequest -Uri \"$uri\" -ContentType application/json-rpc -Body $remove -Method Put -UseBasicParsing\n $select= ($maintenace.Content | ConvertFrom-Json).result | where{$_.name -like \"$mname\"}\n $id=$select.maintenanceid\n if($id){\n Write-Output \"Remove maintenance ID: $id\"\n $rmv='{\"jsonrpc\": \"2.0\", \"method\": \"maintenance.delete\", \"params\": [\"'+\"$id\"+'\"], \"auth\": \"'+\"$auth\"+'\",\"id\": 1}'\n $actionremove=Invoke-WebRequest -Uri \"$uri\" -ContentType application/json-rpc -Body $rmv -Method Put -UseBasicParsing\n $check=(($actionremove.Content | ConvertFrom-Json).result).maintenanceids\n if($check -like $id){\n Write-Output \"Maintenance $id removed\"\n }\n else{\n Write-Error \"Something wrong. Please contact your system administrator\"\n }\n }\n else{\n Write-Error \"NO Maintenance ID - contact your system administrator\"\n }\n}\n\n###GLOBAL VARIABLES###\nif (!$Zserver.StartsWith(\"http\")) { $Zserver=\"http://$Zserver\" } \n$Zurl=\"$Zserver/api_jsonrpc.php\"\n$maintenancename=\"Octo-$number-$Zhost\"\n\n###GET AUTH FROM ZABBIX SERVER###\n$auth=Get-Auth -server $Zserver -user $Zuser -pass $Zpassword -url $Zurl\nif ($auth -eq $null) { \n Write-Error \"Authentication failure for user $Zuser on server $Zserver!\" -ErrorAction Stop \n exit\n}\n\n###GET HOST ID###\n$content='{\"jsonrpc\": \"2.0\", \"method\": \"host.get\", \"params\": {\"output\": \"extend\", \"filter\": {\"host\": \"'+\"$Zhost\"+'\"}},\"auth\": \"'+\"$auth\"+'\",\"id\": 1}'\n$zabbixhost=Invoke-WebRequest -Uri \"$Zurl\" -ContentType application/json-rpc -Body $content -Method Put -UseBasicParsing\n$nameserver=$zabbixhost.Content | ConvertFrom-Json\n$hostid=$nameserver.result.hostid\nif($hostid){\n Write-Output \"Host $Zhost found with ID: $hostid\"\n}\nelse{\n Write-Error \"Host $Zhost not found, or user not authorized for this host - please contact your system administrator!\"\n exit\n}\n\n###ADD NEW MAINTENANCE###\nif ($action -eq \"create\"){\n ###REMOVE MAINTENANCE IF ALREADY EXISTS WITH THE SAME NAME###\n $remove='{\"jsonrpc\": \"2.0\", \"method\": \"maintenance.get\", \"params\": {\"output\": \"extend\", \"selectHosts\": \"extend\", \"selectTimeperiods\": \"extend\"},\"auth\": \"'+\"$auth\"+'\",\"id\": 1}'\n $maintenace=Invoke-WebRequest -Uri \"$Zurl\" -ContentType application/json-rpc -Body $remove -Method Put -UseBasicParsing\n\n $select= ($maintenace.Content | ConvertFrom-Json).result | where{$_.name -like \"$maintenancename\"}\n if(!$select){\n Write-Output \"No maintenance with the same name is already registered\"\n }\n else{\n Remove-Maintenance -srvr $Zserver -usr $Zuser -pswd $Zpassword -uri $Zurl -mname $maintenancename\n }\n\n ###START TO CREATE NEW MAINTENANCE###\n $since=[int][double]::Parse((get-date -UFormat %s))\n $till=0\n\n ###ATTENTION ON GMT - THIS WORK FOR ITALIAN ZONE AND TAKES DAYLIGHT SAVINGSTIME FROM###\n ###start check your ZABBIX configuration###\n $workdate=(Get-Date)\n if (![int32]::TryParse($setgmt, [ref] $gmt)) { $gmt=([TimeZoneInfo]::Local.BaseUtcOffset).Hours }\n if ($workdate.IsDaylightSavingTime()) { $gmt+=1 }\n\n $min=$workdate.AddHours(-$gmt).Minute\n $h=$workdate.AddHours(-$gmt).Hour\n $minutetoseconds=$min*=60\n $hourstoseconds=$h*=3600\n $starttime=$minutetoseconds+=$hourstoseconds\n $seconds=$hours*=3600\n\n $sincesum=$since\n $till=$sincesum+=$seconds\n $since=$since-=(60*60*$gmt)\n $till=$till-=(60*60*$gmt)\n\n ###stop check your ZABBIX configuration###\n $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}'\n $maintenance=Invoke-WebRequest -Uri \"$Zurl\" -ContentType application/json-rpc -Body $add -Method Put -UseBasicParsing\n $check=(($maintenance.Content | ConvertFrom-Json).result).maintenanceids\n if($check){\n Write-Output \"Maintenance $check created\"\n }\n else{\n Write-Error \"Something wrong. Please contact your system administrator\"\n }\n}\nelse{\n if($action -eq \"remove\"){\n Remove-Maintenance -srvr $Zserver -usr $Zuser -pswd $Zpassword -uri $Zurl -mname $maintenancename \n }\n else{\n Write-Error \"Action $action not possible\"\n }\n}"
},
"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\nremove|Remove maintenance"
},
"Links": {}
},
{
"Id": "b1ef0d32-ec8e-44f3-a7f4-a34af6cd252c",
"Name": "gmt",
"Label": "GMT",
"HelpText": "This option depends on Zabbix server configuration.\nIf it is not necessary, set GMT=0 (UTC).\nIf 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"
}