Skip to content

Commit

Permalink
Modify a template
Browse files Browse the repository at this point in the history
  • Loading branch information
tothegills committed Aug 6, 2024
1 parent 4e724e1 commit 473bacd
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion step-templates/zabbix-node-api-maintenance.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"Properties": {
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.ScriptBody": "$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}"
"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": [
{
Expand Down

0 comments on commit 473bacd

Please sign in to comment.