· 2 years ago · Oct 02, 2023, 01:35 PM
1#!/bin/bash
2
3API_USER=""
4API_PASS=""
5API_BASE=""
6
7
8usage() {
9 echo "Usage: $0 -H <HOST> [-s <Service>] [-t <Time><m|h|d|w|M|y>] [-c Comment] [-a Author] -o <downtime|remove-downtime|reschedule-check|acknowledge-problem>"
10}
11
12downtime() {
13
14 # Check that all Variables are set
15 if [ -z "${MYHOST}" ]
16 then
17 echo "host parameter (-h or --host) is missing. Aborting."
18 usage
19 exit 1
20 elif [ -z "${TIME}" ]
21 then
22 echo "time parameter (-t or --time) is missing. Aborting."
23 usage
24 exit 1
25 fi
26
27 if [ -z "${COMMENT}" ]
28 then
29 COMMENT="Planned downtime"
30 fi
31
32 if [ -z "${AUTHOR}" ]
33 then
34 AUTHOR="LBA Admin"
35 fi
36
37 # Prepare the JSON Object
38 JSON=$(jo -p type=Host filter="host.name==\"${MYHOST}\"" all_services=true author="${AUTHOR}" comment="${COMMENT}" fixed=true start_time="$(date +%s -d "+0 hour")" end_time="$(date +%s -d "${TIME}")")
39
40 # Use the icinga2 API
41 curl -k -s -u ${API_USER}:${API_PASS} -H "Accept: application/json" -X POST ${API_BASE}actions/schedule-downtime -d "${JSON}" | jq
42
43}
44
45remove-downtime() {
46
47 # Check that all Variables are set
48 if [ -z "${MYHOST}" ]
49 then
50 echo "host parameter (-H or --host) is missing. Aborting."
51 usage
52 exit 1
53 fi
54
55 if [ -z "${COMMENT}" ]
56 then
57 COMMENT="Removed downtime"
58 fi
59
60 if [ -z "${AUTHOR}" ]
61 then
62 AUTHOR="LBA Admin"
63 fi
64
65 # Prepare the JSON object
66 JSON=$(jo -p type=Host filter="host.name==\"${MYHOST}\"" author="${AUTHOR}" comment="${COMMENT}")
67
68 # Use the icinga2 API
69 curl -k -s -u ${API_USER}:${API_PASS} -H 'Accept: application/json' -X POST ${API_BASE}'actions/remove-downtime' -d "${JSON}" | jq
70
71}
72
73reschedule-check() {
74 # Check that all Variables are set
75 if [ -z "${MYHOST}" ]
76 then
77 echo "host parameter (-H or --host) is missing. Aborting."
78 usage
79 exit 1
80 elif [ -z "${SERVICE}" ]
81 then
82 echo "service paramter (-s or --service) is missing. Aborting."
83 usage
84 exit 1
85 fi
86
87 JSON=$(jo -p type=Service filter="service.name==\"${SERVICE}\" && host.name==\"${MYHOST}\"" force=true)
88
89 curl -k -s -u ${API_USER}:${API_PASS} -H "Accept: application/json" -X POST ${API_BASE}'actions/reschedule-check' -d "$JSON" | jq
90}
91
92
93
94acknowledge-problem() {
95 # Check that all Variables are set
96 if [ -z "${MYHOST}" ]
97 then
98 echo "host parameter (-H or --host) is missing. Aborting."
99 usage
100 exit 1
101 elif [ -z "${SERVICE}" ]
102 then
103 echo "service parameter (-s or --service) is missing. Aborting."
104 usage
105 exit 1
106 elif [ -z "${COMMENT}" ]
107 then
108 echo "comment parameter (-c or --comment) is missing. Aborting."
109 usage
110 exit 1
111 elif [ -z "${TIME}" ]
112 then
113 echo "time parameter (-t or --time) is missing. Aborting."
114 usage
115 exit 1
116 fi
117
118 if [ -z "${AUTHOR}" ]
119 then
120 AUTHOR="LBA Admin"
121 fi
122
123 # Prepare the JSON object
124 JSON=$(jo -p type=Service filter="service.name==\"${SERVICE}\" && host.name==\"${MYHOST}\"" author="${AUTHOR}" comment="$COMMENT" notify=true expiry="$(date +%s -d "${TIME}")")
125
126 curl -k -s -u ${API_USER}:${API_PASS} -H 'Accept: application/json' -X POST ${API_BASE}'actions/acknowledge-problem' -d "$JSON" | jq
127
128
129}
130
131##### MAIN #####
132
133if [ $# -eq 0 ]
134then
135 usage
136else
137
138 while [[ $# -gt 0 ]]; do
139 key="$1"
140 case "$key" in
141 -h | --help)
142 usage
143 exit 0
144 ;;
145 -H | --host)
146 MYHOST="$2"
147 shift
148 shift
149 ;;
150 -s | --service)
151 SERVICE="$2"
152 shift
153 shift
154 ;;
155 -t | --time)
156 TIME="$2"
157 shift
158 shift
159 ;;
160 -c | --comment)
161 COMMENT="$2"
162 shift
163 shift
164 ;;
165 -a | --author)
166 AUTHOR="$2"
167 shift
168 shift
169 ;;
170 -o | --option)
171 OPTION="$2"
172 shift
173 shift
174 ;;
175 *)
176 usage
177 exit 0
178 ;;
179 esac
180 done
181
182
183 # Translate TIME into a nice date format
184 if [ ! -z "${TIME}" ]
185 then
186 case "${TIME:0-1}" in
187 m)
188 TIME="+${TIME%?} min"
189 ;;
190 h)
191 TIME="+${TIME%?} hour"
192 ;;
193 d)
194 TIME="+${TIME%?} day"
195 ;;
196 w)
197 TIME="+${TIME%?} week"
198 ;;
199 M)
200 TIME="+${TIME%?} month"
201 ;;
202 y)
203 TIME="+${TIME%?} year"
204 ;;
205 *)
206 echo "Sorry, ${TIME:0-1} is not supported."
207 exit 1
208 ;;
209 esac
210 fi
211
212 case $OPTION in
213 downtime)
214 downtime
215 ;;
216 remove-downtime)
217 remove-downtime
218 ;;
219 reschedule-check)
220 reschedule-check
221 ;;
222 acknowledge-problem)
223 acknowledge-problem
224 ;;
225 *)
226 echo "$OPTION is unknown"
227 usage
228 exit 1
229 ;;
230 esac
231fi