diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-12-05 12:28:26 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-12-05 12:28:26 +0100 |
| commit | 74d61bbf8ab622c22a08b5460475535860f95d91 (patch) | |
| tree | b6dfbf7e68461e68fbc152d976fc34617e30073d /plugins | |
| parent | 3ae751ed67bb59c5073e0a160f4125ba2a5ff1e4 (diff) | |
| download | monitoring-plugins-74d61bbf8ab622c22a08b5460475535860f95d91.tar.gz | |
check_real: implement modern output
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/check_real.c | 318 | ||||
| -rw-r--r-- | plugins/check_real.d/config.h | 23 | ||||
| -rw-r--r-- | plugins/check_time.c | 2 | ||||
| -rw-r--r-- | plugins/utils.c | 1 | ||||
| -rw-r--r-- | plugins/utils.h | 2 |
5 files changed, 215 insertions, 131 deletions
diff --git a/plugins/check_real.c b/plugins/check_real.c index 66d07f8f..15c8a20c 100644 --- a/plugins/check_real.c +++ b/plugins/check_real.c | |||
| @@ -28,19 +28,21 @@ | |||
| 28 | * | 28 | * |
| 29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
| 30 | 30 | ||
| 31 | #include "output.h" | ||
| 32 | #include "perfdata.h" | ||
| 31 | #include "states.h" | 33 | #include "states.h" |
| 32 | #include <stdio.h> | 34 | #include <stdio.h> |
| 33 | const char *progname = "check_real"; | ||
| 34 | const char *copyright = "2000-2024"; | ||
| 35 | const char *email = "devel@monitoring-plugins.org"; | ||
| 36 | |||
| 37 | #include "common.h" | 35 | #include "common.h" |
| 38 | #include "netutils.h" | 36 | #include "netutils.h" |
| 37 | #include "thresholds.h" | ||
| 39 | #include "utils.h" | 38 | #include "utils.h" |
| 40 | #include "check_real.d/config.h" | 39 | #include "check_real.d/config.h" |
| 41 | 40 | ||
| 42 | #define EXPECT "RTSP/1." | 41 | const char *progname = "check_real"; |
| 43 | #define URL "" | 42 | const char *copyright = "2000-2024"; |
| 43 | const char *email = "devel@monitoring-plugins.org"; | ||
| 44 | |||
| 45 | #define URL "" | ||
| 44 | 46 | ||
| 45 | typedef struct { | 47 | typedef struct { |
| 46 | int errorcode; | 48 | int errorcode; |
| @@ -68,42 +70,68 @@ int main(int argc, char **argv) { | |||
| 68 | 70 | ||
| 69 | const check_real_config config = tmp_config.config; | 71 | const check_real_config config = tmp_config.config; |
| 70 | 72 | ||
| 73 | if (config.output_format_is_set) { | ||
| 74 | mp_set_format(config.output_format); | ||
| 75 | } | ||
| 76 | |||
| 71 | /* initialize alarm signal handling */ | 77 | /* initialize alarm signal handling */ |
| 72 | signal(SIGALRM, socket_timeout_alarm_handler); | 78 | signal(SIGALRM, socket_timeout_alarm_handler); |
| 73 | 79 | ||
| 74 | /* set socket timeout */ | 80 | /* set socket timeout */ |
| 75 | alarm(socket_timeout); | 81 | alarm(socket_timeout); |
| 82 | time_t start_time; | ||
| 76 | time(&start_time); | 83 | time(&start_time); |
| 77 | 84 | ||
| 85 | mp_check overall = mp_check_init(); | ||
| 86 | mp_subcheck sc_connect = mp_subcheck_init(); | ||
| 87 | |||
| 78 | /* try to connect to the host at the given port number */ | 88 | /* try to connect to the host at the given port number */ |
| 79 | int socket; | 89 | int socket; |
| 80 | if (my_tcp_connect(config.server_address, config.server_port, &socket) != STATE_OK) { | 90 | if (my_tcp_connect(config.server_address, config.server_port, &socket) != STATE_OK) { |
| 81 | die(STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), config.server_address, | 91 | xasprintf(&sc_connect.output, _("unable to connect to %s on port %d"), |
| 82 | config.server_port); | 92 | config.server_address, config.server_port); |
| 93 | sc_connect = mp_set_subcheck_state(sc_connect, STATE_CRITICAL); | ||
| 94 | mp_add_subcheck_to_check(&overall, sc_connect); | ||
| 95 | mp_exit(overall); | ||
| 83 | } | 96 | } |
| 84 | 97 | ||
| 98 | xasprintf(&sc_connect.output, _("connected to %s on port %d"), config.server_address, | ||
| 99 | config.server_port); | ||
| 100 | sc_connect = mp_set_subcheck_state(sc_connect, STATE_OK); | ||
| 101 | mp_add_subcheck_to_check(&overall, sc_connect); | ||
| 102 | |||
| 85 | /* Part I - Server Check */ | 103 | /* Part I - Server Check */ |
| 104 | mp_subcheck sc_send = mp_subcheck_init(); | ||
| 86 | 105 | ||
| 87 | /* send the OPTIONS request */ | 106 | /* send the OPTIONS request */ |
| 88 | char buffer[MAX_INPUT_BUFFER]; | 107 | char buffer[MAX_INPUT_BUFFER]; |
| 89 | sprintf(buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", config.host_name, config.server_port); | 108 | sprintf(buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", config.host_name, config.server_port); |
| 90 | ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0); | 109 | ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0); |
| 91 | if (sent_bytes == -1) { | 110 | if (sent_bytes == -1) { |
| 92 | die(STATE_CRITICAL, _("Sending options to %s failed\n"), config.host_name); | 111 | xasprintf(&sc_send.output, _("Sending options to %s failed"), config.host_name); |
| 112 | sc_send = mp_set_subcheck_state(sc_send, STATE_CRITICAL); | ||
| 113 | mp_add_subcheck_to_check(&overall, sc_send); | ||
| 114 | mp_exit(overall); | ||
| 93 | } | 115 | } |
| 94 | 116 | ||
| 95 | /* send the header sync */ | 117 | /* send the header sync */ |
| 96 | sprintf(buffer, "CSeq: 1\r\n"); | 118 | sprintf(buffer, "CSeq: 1\r\n"); |
| 97 | sent_bytes = send(socket, buffer, strlen(buffer), 0); | 119 | sent_bytes = send(socket, buffer, strlen(buffer), 0); |
| 98 | if (sent_bytes == -1) { | 120 | if (sent_bytes == -1) { |
| 99 | die(STATE_CRITICAL, _("Sending header sync to %s failed\n"), config.host_name); | 121 | xasprintf(&sc_send.output, _("Sending header sync to %s failed"), config.host_name); |
| 122 | sc_send = mp_set_subcheck_state(sc_send, STATE_CRITICAL); | ||
| 123 | mp_add_subcheck_to_check(&overall, sc_send); | ||
| 124 | mp_exit(overall); | ||
| 100 | } | 125 | } |
| 101 | 126 | ||
| 102 | /* send a newline so the server knows we're done with the request */ | 127 | /* send a newline so the server knows we're done with the request */ |
| 103 | sprintf(buffer, "\r\n"); | 128 | sprintf(buffer, "\r\n"); |
| 104 | sent_bytes = send(socket, buffer, strlen(buffer), 0); | 129 | sent_bytes = send(socket, buffer, strlen(buffer), 0); |
| 105 | if (sent_bytes == -1) { | 130 | if (sent_bytes == -1) { |
| 106 | die(STATE_CRITICAL, _("Sending newline to %s failed\n"), config.host_name); | 131 | xasprintf(&sc_send.output, _("Sending newline to %s failed"), config.host_name); |
| 132 | sc_send = mp_set_subcheck_state(sc_send, STATE_CRITICAL); | ||
| 133 | mp_add_subcheck_to_check(&overall, sc_send); | ||
| 134 | mp_exit(overall); | ||
| 107 | } | 135 | } |
| 108 | 136 | ||
| 109 | /* watch for the REAL connection string */ | 137 | /* watch for the REAL connection string */ |
| @@ -111,60 +139,75 @@ int main(int argc, char **argv) { | |||
| 111 | 139 | ||
| 112 | /* return a CRITICAL status if we couldn't read any data */ | 140 | /* return a CRITICAL status if we couldn't read any data */ |
| 113 | if (received_bytes == -1) { | 141 | if (received_bytes == -1) { |
| 114 | die(STATE_CRITICAL, _("No data received from %s\n"), config.host_name); | 142 | xasprintf(&sc_send.output, _("No data received from %s"), config.host_name); |
| 143 | sc_send = mp_set_subcheck_state(sc_send, STATE_CRITICAL); | ||
| 144 | mp_add_subcheck_to_check(&overall, sc_send); | ||
| 145 | mp_exit(overall); | ||
| 115 | } | 146 | } |
| 116 | 147 | ||
| 117 | mp_state_enum result = STATE_OK; | 148 | time_t end_time; |
| 118 | char *status_line = NULL; | 149 | { |
| 119 | /* make sure we find the response we are looking for */ | 150 | mp_subcheck sc_options_request = mp_subcheck_init(); |
| 120 | if (!strstr(buffer, config.server_expect)) { | 151 | mp_state_enum options_result = STATE_OK; |
| 121 | if (config.server_port == PORT) { | 152 | /* make sure we find the response we are looking for */ |
| 122 | printf("%s\n", _("Invalid REAL response received from host")); | 153 | if (!strstr(buffer, config.server_expect)) { |
| 154 | if (config.server_port == PORT) { | ||
| 155 | xasprintf(&sc_options_request.output, "invalid REAL response received from host"); | ||
| 156 | } else { | ||
| 157 | xasprintf(&sc_options_request.output, | ||
| 158 | "invalid REAL response received from host on port %d", | ||
| 159 | config.server_port); | ||
| 160 | } | ||
| 123 | } else { | 161 | } else { |
| 124 | printf(_("Invalid REAL response received from host on port %d\n"), config.server_port); | 162 | /* else we got the REAL string, so check the return code */ |
| 125 | } | 163 | time(&end_time); |
| 126 | } else { | ||
| 127 | /* else we got the REAL string, so check the return code */ | ||
| 128 | 164 | ||
| 129 | time(&end_time); | 165 | options_result = STATE_OK; |
| 130 | 166 | ||
| 131 | result = STATE_OK; | 167 | char *status_line = strtok(buffer, "\n"); |
| 168 | xasprintf(&sc_options_request.output, "status line: %s", status_line); | ||
| 132 | 169 | ||
| 133 | status_line = strtok(buffer, "\n"); | 170 | if (strstr(status_line, "200")) { |
| 134 | 171 | options_result = STATE_OK; | |
| 135 | if (strstr(status_line, "200")) { | 172 | } |
| 136 | result = STATE_OK; | 173 | /* client errors options_result in a warning state */ |
| 174 | else if (strstr(status_line, "400")) { | ||
| 175 | options_result = STATE_WARNING; | ||
| 176 | } else if (strstr(status_line, "401")) { | ||
| 177 | options_result = STATE_WARNING; | ||
| 178 | } else if (strstr(status_line, "402")) { | ||
| 179 | options_result = STATE_WARNING; | ||
| 180 | } else if (strstr(status_line, "403")) { | ||
| 181 | options_result = STATE_WARNING; | ||
| 182 | } else if (strstr(status_line, "404")) { | ||
| 183 | options_result = STATE_WARNING; | ||
| 184 | } else if (strstr(status_line, "500")) { | ||
| 185 | /* server errors options_result in a critical state */ | ||
| 186 | options_result = STATE_CRITICAL; | ||
| 187 | } else if (strstr(status_line, "501")) { | ||
| 188 | options_result = STATE_CRITICAL; | ||
| 189 | } else if (strstr(status_line, "502")) { | ||
| 190 | options_result = STATE_CRITICAL; | ||
| 191 | } else if (strstr(status_line, "503")) { | ||
| 192 | options_result = STATE_CRITICAL; | ||
| 193 | } else { | ||
| 194 | options_result = STATE_UNKNOWN; | ||
| 195 | } | ||
| 137 | } | 196 | } |
| 138 | 197 | ||
| 139 | /* client errors result in a warning state */ | 198 | sc_options_request = mp_set_subcheck_state(sc_options_request, options_result); |
| 140 | else if (strstr(status_line, "400")) { | 199 | mp_add_subcheck_to_check(&overall, sc_options_request); |
| 141 | result = STATE_WARNING; | 200 | |
| 142 | } else if (strstr(status_line, "401")) { | 201 | if (options_result != STATE_OK) { |
| 143 | result = STATE_WARNING; | 202 | // exit here if Setting options already failed |
| 144 | } else if (strstr(status_line, "402")) { | 203 | mp_exit(overall); |
| 145 | result = STATE_WARNING; | ||
| 146 | } else if (strstr(status_line, "403")) { | ||
| 147 | result = STATE_WARNING; | ||
| 148 | } else if (strstr(status_line, "404")) { | ||
| 149 | result = STATE_WARNING; | ||
| 150 | } else if (strstr(status_line, "500")) { | ||
| 151 | /* server errors result in a critical state */ | ||
| 152 | result = STATE_CRITICAL; | ||
| 153 | } else if (strstr(status_line, "501")) { | ||
| 154 | result = STATE_CRITICAL; | ||
| 155 | } else if (strstr(status_line, "502")) { | ||
| 156 | result = STATE_CRITICAL; | ||
| 157 | } else if (strstr(status_line, "503")) { | ||
| 158 | result = STATE_CRITICAL; | ||
| 159 | } else { | ||
| 160 | result = STATE_UNKNOWN; | ||
| 161 | } | 204 | } |
| 162 | } | 205 | } |
| 163 | 206 | ||
| 164 | /* Part II - Check stream exists and is ok */ | 207 | /* Part II - Check stream exists and is ok */ |
| 165 | if ((result == STATE_OK) && (config.server_url != NULL)) { | 208 | if (config.server_url != NULL) { |
| 166 | |||
| 167 | /* Part I - Server Check */ | 209 | /* Part I - Server Check */ |
| 210 | mp_subcheck sc_describe = mp_subcheck_init(); | ||
| 168 | 211 | ||
| 169 | /* send the DESCRIBE request */ | 212 | /* send the DESCRIBE request */ |
| 170 | sprintf(buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", config.host_name, | 213 | sprintf(buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", config.host_name, |
| @@ -172,98 +215,115 @@ int main(int argc, char **argv) { | |||
| 172 | 215 | ||
| 173 | ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0); | 216 | ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0); |
| 174 | if (sent_bytes == -1) { | 217 | if (sent_bytes == -1) { |
| 175 | die(STATE_CRITICAL, _("Sending DESCRIBE request to %s failed\n"), config.host_name); | 218 | sc_describe = mp_set_subcheck_state(sc_describe, STATE_CRITICAL); |
| 219 | xasprintf(&sc_describe.output, "sending DESCRIBE request to %s failed", | ||
| 220 | config.host_name); | ||
| 221 | mp_add_subcheck_to_check(&overall, sc_describe); | ||
| 222 | mp_exit(overall); | ||
| 176 | } | 223 | } |
| 177 | 224 | ||
| 178 | /* send the header sync */ | 225 | /* send the header sync */ |
| 179 | sprintf(buffer, "CSeq: 2\r\n"); | 226 | sprintf(buffer, "CSeq: 2\r\n"); |
| 180 | sent_bytes = send(socket, buffer, strlen(buffer), 0); | 227 | sent_bytes = send(socket, buffer, strlen(buffer), 0); |
| 181 | if (sent_bytes == -1) { | 228 | if (sent_bytes == -1) { |
| 182 | die(STATE_CRITICAL, _("Sending DESCRIBE request to %s failed\n"), config.host_name); | 229 | sc_describe = mp_set_subcheck_state(sc_describe, STATE_CRITICAL); |
| 230 | xasprintf(&sc_describe.output, "sending DESCRIBE request to %s failed", | ||
| 231 | config.host_name); | ||
| 232 | mp_add_subcheck_to_check(&overall, sc_describe); | ||
| 233 | mp_exit(overall); | ||
| 183 | } | 234 | } |
| 184 | 235 | ||
| 185 | /* send a newline so the server knows we're done with the request */ | 236 | /* send a newline so the server knows we're done with the request */ |
| 186 | sprintf(buffer, "\r\n"); | 237 | sprintf(buffer, "\r\n"); |
| 187 | sent_bytes = send(socket, buffer, strlen(buffer), 0); | 238 | sent_bytes = send(socket, buffer, strlen(buffer), 0); |
| 188 | if (sent_bytes == -1) { | 239 | if (sent_bytes == -1) { |
| 189 | die(STATE_CRITICAL, _("Sending DESCRIBE request to %s failed\n"), config.host_name); | 240 | sc_describe = mp_set_subcheck_state(sc_describe, STATE_CRITICAL); |
| 241 | xasprintf(&sc_describe.output, "sending DESCRIBE request to %s failed", | ||
| 242 | config.host_name); | ||
| 243 | mp_add_subcheck_to_check(&overall, sc_describe); | ||
| 244 | mp_exit(overall); | ||
| 190 | } | 245 | } |
| 191 | 246 | ||
| 192 | /* watch for the REAL connection string */ | 247 | /* watch for the REAL connection string */ |
| 193 | ssize_t recv_bytes = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0); | 248 | ssize_t recv_bytes = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0); |
| 194 | if (recv_bytes == -1) { | 249 | if (recv_bytes == -1) { |
| 195 | /* return a CRITICAL status if we couldn't read any data */ | 250 | /* return a CRITICAL status if we couldn't read any data */ |
| 196 | printf(_("No data received from host\n")); | 251 | sc_describe = mp_set_subcheck_state(sc_describe, STATE_CRITICAL); |
| 197 | result = STATE_CRITICAL; | 252 | xasprintf(&sc_describe.output, "No data received from host on DESCRIBE request"); |
| 253 | mp_add_subcheck_to_check(&overall, sc_describe); | ||
| 254 | mp_exit(overall); | ||
| 198 | } else { | 255 | } else { |
| 199 | buffer[result] = '\0'; /* null terminate received buffer */ | 256 | buffer[recv_bytes] = '\0'; /* null terminate received buffer */ |
| 200 | /* make sure we find the response we are looking for */ | 257 | /* make sure we find the response we are looking for */ |
| 201 | if (!strstr(buffer, config.server_expect)) { | 258 | if (!strstr(buffer, config.server_expect)) { |
| 202 | if (config.server_port == PORT) { | 259 | if (config.server_port == PORT) { |
| 203 | printf("%s\n", _("Invalid REAL response received from host")); | 260 | xasprintf(&sc_describe.output, "invalid REAL response received from host"); |
| 204 | } else { | 261 | } else { |
| 205 | printf(_("Invalid REAL response received from host on port %d\n"), | 262 | xasprintf(&sc_describe.output, |
| 206 | config.server_port); | 263 | "invalid REAL response received from host on port %d", |
| 264 | config.server_port); | ||
| 207 | } | 265 | } |
| 208 | } else { | ||
| 209 | 266 | ||
| 267 | sc_describe = mp_set_subcheck_state(sc_describe, STATE_UNKNOWN); | ||
| 268 | mp_add_subcheck_to_check(&overall, sc_describe); | ||
| 269 | mp_exit(overall); | ||
| 270 | } else { | ||
| 210 | /* else we got the REAL string, so check the return code */ | 271 | /* else we got the REAL string, so check the return code */ |
| 211 | 272 | ||
| 212 | time(&end_time); | 273 | time(&end_time); |
| 213 | 274 | ||
| 214 | result = STATE_OK; | 275 | char *status_line = strtok(buffer, "\n"); |
| 215 | 276 | xasprintf(&sc_describe.output, "status line: %s", status_line); | |
| 216 | status_line = strtok(buffer, "\n"); | ||
| 217 | 277 | ||
| 278 | mp_state_enum describe_result; | ||
| 218 | if (strstr(status_line, "200")) { | 279 | if (strstr(status_line, "200")) { |
| 219 | result = STATE_OK; | 280 | describe_result = STATE_OK; |
| 220 | } | 281 | } |
| 221 | 282 | /* client errors describe_result in a warning state */ | |
| 222 | /* client errors result in a warning state */ | ||
| 223 | else if (strstr(status_line, "400")) { | 283 | else if (strstr(status_line, "400")) { |
| 224 | result = STATE_WARNING; | 284 | describe_result = STATE_WARNING; |
| 225 | } else if (strstr(status_line, "401")) { | 285 | } else if (strstr(status_line, "401")) { |
| 226 | result = STATE_WARNING; | 286 | describe_result = STATE_WARNING; |
| 227 | } else if (strstr(status_line, "402")) { | 287 | } else if (strstr(status_line, "402")) { |
| 228 | result = STATE_WARNING; | 288 | describe_result = STATE_WARNING; |
| 229 | } else if (strstr(status_line, "403")) { | 289 | } else if (strstr(status_line, "403")) { |
| 230 | result = STATE_WARNING; | 290 | describe_result = STATE_WARNING; |
| 231 | } else if (strstr(status_line, "404")) { | 291 | } else if (strstr(status_line, "404")) { |
| 232 | result = STATE_WARNING; | 292 | describe_result = STATE_WARNING; |
| 233 | } | 293 | } |
| 234 | 294 | /* server errors describe_result in a critical state */ | |
| 235 | /* server errors result in a critical state */ | ||
| 236 | else if (strstr(status_line, "500")) { | 295 | else if (strstr(status_line, "500")) { |
| 237 | result = STATE_CRITICAL; | 296 | describe_result = STATE_CRITICAL; |
| 238 | } else if (strstr(status_line, "501")) { | 297 | } else if (strstr(status_line, "501")) { |
| 239 | result = STATE_CRITICAL; | 298 | describe_result = STATE_CRITICAL; |
| 240 | } else if (strstr(status_line, "502")) { | 299 | } else if (strstr(status_line, "502")) { |
| 241 | result = STATE_CRITICAL; | 300 | describe_result = STATE_CRITICAL; |
| 242 | } else if (strstr(status_line, "503")) { | 301 | } else if (strstr(status_line, "503")) { |
| 243 | result = STATE_CRITICAL; | 302 | describe_result = STATE_CRITICAL; |
| 303 | } else { | ||
| 304 | describe_result = STATE_UNKNOWN; | ||
| 244 | } | 305 | } |
| 245 | 306 | ||
| 246 | else { | 307 | sc_describe = mp_set_subcheck_state(sc_describe, describe_result); |
| 247 | result = STATE_UNKNOWN; | 308 | mp_add_subcheck_to_check(&overall, sc_describe); |
| 248 | } | ||
| 249 | } | 309 | } |
| 250 | } | 310 | } |
| 251 | } | 311 | } |
| 252 | 312 | ||
| 253 | /* Return results */ | 313 | /* Return results */ |
| 254 | if (result == STATE_OK) { | 314 | mp_subcheck sc_timing = mp_subcheck_init(); |
| 255 | if (config.check_critical_time && (end_time - start_time) > config.critical_time) { | 315 | xasprintf(&sc_timing.output, "response time: %lds", end_time - start_time); |
| 256 | result = STATE_CRITICAL; | 316 | sc_timing = mp_set_subcheck_default_state(sc_timing, STATE_OK); |
| 257 | } else if (config.check_warning_time && (end_time - start_time) > config.warning_time) { | ||
| 258 | result = STATE_WARNING; | ||
| 259 | } | ||
| 260 | 317 | ||
| 261 | /* Put some HTML in here to create a dynamic link */ | 318 | mp_perfdata pd_response_time = perfdata_init(); |
| 262 | printf(_("REAL %s - %d second response time\n"), state_text(result), | 319 | pd_response_time = mp_set_pd_value(pd_response_time, (end_time - start_time)); |
| 263 | (int)(end_time - start_time)); | 320 | pd_response_time.label = "response_time"; |
| 264 | } else { | 321 | pd_response_time.uom = "s"; |
| 265 | printf("%s\n", status_line); | 322 | pd_response_time = mp_pd_set_thresholds(pd_response_time, config.time_thresholds); |
| 266 | } | 323 | mp_add_perfdata_to_subcheck(&sc_connect, pd_response_time); |
| 324 | sc_timing = mp_set_subcheck_state(sc_timing, mp_get_pd_status(pd_response_time)); | ||
| 325 | |||
| 326 | mp_add_subcheck_to_check(&overall, sc_timing); | ||
| 267 | 327 | ||
| 268 | /* close the connection */ | 328 | /* close the connection */ |
| 269 | close(socket); | 329 | close(socket); |
| @@ -271,18 +331,28 @@ int main(int argc, char **argv) { | |||
| 271 | /* reset the alarm */ | 331 | /* reset the alarm */ |
| 272 | alarm(0); | 332 | alarm(0); |
| 273 | 333 | ||
| 274 | exit(result); | 334 | mp_exit(overall); |
| 275 | } | 335 | } |
| 276 | 336 | ||
| 277 | /* process command-line arguments */ | 337 | /* process command-line arguments */ |
| 278 | check_real_config_wrapper process_arguments(int argc, char **argv) { | 338 | check_real_config_wrapper process_arguments(int argc, char **argv) { |
| 279 | static struct option longopts[] = { | 339 | enum { |
| 280 | {"hostname", required_argument, 0, 'H'}, {"IPaddress", required_argument, 0, 'I'}, | 340 | output_format_index = CHAR_MAX + 1, |
| 281 | {"expect", required_argument, 0, 'e'}, {"url", required_argument, 0, 'u'}, | 341 | }; |
| 282 | {"port", required_argument, 0, 'p'}, {"critical", required_argument, 0, 'c'}, | 342 | |
| 283 | {"warning", required_argument, 0, 'w'}, {"timeout", required_argument, 0, 't'}, | 343 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 284 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 344 | {"IPaddress", required_argument, 0, 'I'}, |
| 285 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | 345 | {"expect", required_argument, 0, 'e'}, |
| 346 | {"url", required_argument, 0, 'u'}, | ||
| 347 | {"port", required_argument, 0, 'p'}, | ||
| 348 | {"critical", required_argument, 0, 'c'}, | ||
| 349 | {"warning", required_argument, 0, 'w'}, | ||
| 350 | {"timeout", required_argument, 0, 't'}, | ||
| 351 | {"verbose", no_argument, 0, 'v'}, | ||
| 352 | {"version", no_argument, 0, 'V'}, | ||
| 353 | {"help", no_argument, 0, 'h'}, | ||
| 354 | {"output-format", required_argument, 0, output_format_index}, | ||
| 355 | {0, 0, 0, 0}}; | ||
| 286 | 356 | ||
| 287 | check_real_config_wrapper result = { | 357 | check_real_config_wrapper result = { |
| 288 | .errorcode = OK, | 358 | .errorcode = OK, |
| @@ -337,21 +407,23 @@ check_real_config_wrapper process_arguments(int argc, char **argv) { | |||
| 337 | } | 407 | } |
| 338 | break; | 408 | break; |
| 339 | case 'w': /* warning time threshold */ | 409 | case 'w': /* warning time threshold */ |
| 340 | if (is_intnonneg(optarg)) { | 410 | { |
| 341 | result.config.warning_time = atoi(optarg); | 411 | mp_range_parsed critical_range = mp_parse_range_string(optarg); |
| 342 | result.config.check_warning_time = true; | 412 | if (critical_range.error != MP_PARSING_SUCCES) { |
| 343 | } else { | 413 | die(STATE_UNKNOWN, "failed to parse warning threshold: %s", optarg); |
| 344 | usage4(_("Warning time must be a positive integer")); | ||
| 345 | } | 414 | } |
| 346 | break; | 415 | result.config.time_thresholds = |
| 416 | mp_thresholds_set_warn(result.config.time_thresholds, critical_range.range); | ||
| 417 | } break; | ||
| 347 | case 'c': /* critical time threshold */ | 418 | case 'c': /* critical time threshold */ |
| 348 | if (is_intnonneg(optarg)) { | 419 | { |
| 349 | result.config.critical_time = atoi(optarg); | 420 | mp_range_parsed critical_range = mp_parse_range_string(optarg); |
| 350 | result.config.check_critical_time = true; | 421 | if (critical_range.error != MP_PARSING_SUCCES) { |
| 351 | } else { | 422 | die(STATE_UNKNOWN, "failed to parse critical threshold: %s", optarg); |
| 352 | usage4(_("Critical time must be a positive integer")); | ||
| 353 | } | 423 | } |
| 354 | break; | 424 | result.config.time_thresholds = |
| 425 | mp_thresholds_set_crit(result.config.time_thresholds, critical_range.range); | ||
| 426 | } break; | ||
| 355 | case 'v': /* verbose */ | 427 | case 'v': /* verbose */ |
| 356 | verbose = true; | 428 | verbose = true; |
| 357 | break; | 429 | break; |
| @@ -368,6 +440,18 @@ check_real_config_wrapper process_arguments(int argc, char **argv) { | |||
| 368 | case 'h': /* help */ | 440 | case 'h': /* help */ |
| 369 | print_help(); | 441 | print_help(); |
| 370 | exit(STATE_UNKNOWN); | 442 | exit(STATE_UNKNOWN); |
| 443 | case output_format_index: { | ||
| 444 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
| 445 | if (!parser.parsing_success) { | ||
| 446 | // TODO List all available formats here, maybe add anothoer usage function | ||
| 447 | printf("Invalid output format: %s\n", optarg); | ||
| 448 | exit(STATE_UNKNOWN); | ||
| 449 | } | ||
| 450 | |||
| 451 | result.config.output_format_is_set = true; | ||
| 452 | result.config.output_format = parser.output_format; | ||
| 453 | break; | ||
| 454 | } | ||
| 371 | case '?': /* usage */ | 455 | case '?': /* usage */ |
| 372 | usage5(); | 456 | usage5(); |
| 373 | } | 457 | } |
| @@ -390,10 +474,6 @@ check_real_config_wrapper process_arguments(int argc, char **argv) { | |||
| 390 | result.config.host_name = strdup(result.config.server_address); | 474 | result.config.host_name = strdup(result.config.server_address); |
| 391 | } | 475 | } |
| 392 | 476 | ||
| 393 | if (result.config.server_expect == NULL) { | ||
| 394 | result.config.server_expect = strdup(EXPECT); | ||
| 395 | } | ||
| 396 | |||
| 397 | return result; | 477 | return result; |
| 398 | } | 478 | } |
| 399 | 479 | ||
| @@ -420,7 +500,7 @@ void print_help(void) { | |||
| 420 | printf(" %s\n", "-u, --url=STRING"); | 500 | printf(" %s\n", "-u, --url=STRING"); |
| 421 | printf(" %s\n", _("Connect to this url")); | 501 | printf(" %s\n", _("Connect to this url")); |
| 422 | printf(" %s\n", "-e, --expect=STRING"); | 502 | printf(" %s\n", "-e, --expect=STRING"); |
| 423 | printf(_("String to expect in first line of server response (default: %s)\n"), EXPECT); | 503 | printf(_("String to expect in first line of server response (default: %s)\n"), default_expect); |
| 424 | 504 | ||
| 425 | printf(UT_WARN_CRIT); | 505 | printf(UT_WARN_CRIT); |
| 426 | 506 | ||
diff --git a/plugins/check_real.d/config.h b/plugins/check_real.d/config.h index c4663cf9..2d99ad49 100644 --- a/plugins/check_real.d/config.h +++ b/plugins/check_real.d/config.h | |||
| @@ -1,12 +1,16 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include "../../config.h" | 3 | #include "../../config.h" |
| 4 | #include "output.h" | ||
| 5 | #include "thresholds.h" | ||
| 4 | #include <stddef.h> | 6 | #include <stddef.h> |
| 5 | 7 | ||
| 6 | enum { | 8 | enum { |
| 7 | PORT = 554 | 9 | PORT = 554 |
| 8 | }; | 10 | }; |
| 9 | 11 | ||
| 12 | const char *default_expect = "RTSP/1."; | ||
| 13 | |||
| 10 | typedef struct { | 14 | typedef struct { |
| 11 | char *server_address; | 15 | char *server_address; |
| 12 | char *host_name; | 16 | char *host_name; |
| @@ -14,10 +18,11 @@ typedef struct { | |||
| 14 | char *server_url; | 18 | char *server_url; |
| 15 | 19 | ||
| 16 | char *server_expect; | 20 | char *server_expect; |
| 17 | int warning_time; | 21 | |
| 18 | bool check_warning_time; | 22 | mp_thresholds time_thresholds; |
| 19 | int critical_time; | 23 | |
| 20 | bool check_critical_time; | 24 | bool output_format_is_set; |
| 25 | mp_output_format output_format; | ||
| 21 | } check_real_config; | 26 | } check_real_config; |
| 22 | 27 | ||
| 23 | check_real_config check_real_config_init() { | 28 | check_real_config check_real_config_init() { |
| @@ -27,11 +32,11 @@ check_real_config check_real_config_init() { | |||
| 27 | .server_port = PORT, | 32 | .server_port = PORT, |
| 28 | .server_url = NULL, | 33 | .server_url = NULL, |
| 29 | 34 | ||
| 30 | .server_expect = NULL, | 35 | .server_expect = default_expect, |
| 31 | .warning_time = 0, | 36 | |
| 32 | .check_warning_time = false, | 37 | .time_thresholds = mp_thresholds_init(), |
| 33 | .critical_time = 0, | 38 | |
| 34 | .check_critical_time = false, | 39 | .output_format_is_set = false, |
| 35 | }; | 40 | }; |
| 36 | return tmp; | 41 | return tmp; |
| 37 | } | 42 | } |
diff --git a/plugins/check_time.c b/plugins/check_time.c index fc9ba3f9..99708ad3 100644 --- a/plugins/check_time.c +++ b/plugins/check_time.c | |||
| @@ -68,6 +68,7 @@ int main(int argc, char **argv) { | |||
| 68 | 68 | ||
| 69 | /* set socket timeout */ | 69 | /* set socket timeout */ |
| 70 | alarm(socket_timeout); | 70 | alarm(socket_timeout); |
| 71 | time_t start_time; | ||
| 71 | time(&start_time); | 72 | time(&start_time); |
| 72 | 73 | ||
| 73 | int socket; | 74 | int socket; |
| @@ -113,6 +114,7 @@ int main(int argc, char **argv) { | |||
| 113 | close(socket); | 114 | close(socket); |
| 114 | 115 | ||
| 115 | /* reset the alarm */ | 116 | /* reset the alarm */ |
| 117 | time_t end_time; | ||
| 116 | time(&end_time); | 118 | time(&end_time); |
| 117 | alarm(0); | 119 | alarm(0); |
| 118 | 120 | ||
diff --git a/plugins/utils.c b/plugins/utils.c index 41fe5fcf..dc6f5a85 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
| @@ -40,7 +40,6 @@ extern const char *progname; | |||
| 40 | #define STRLEN 64 | 40 | #define STRLEN 64 |
| 41 | #define TXTBLK 128 | 41 | #define TXTBLK 128 |
| 42 | 42 | ||
| 43 | time_t start_time, end_time; | ||
| 44 | 43 | ||
| 45 | void usage(const char *msg) { | 44 | void usage(const char *msg) { |
| 46 | printf("%s\n", msg); | 45 | printf("%s\n", msg); |
diff --git a/plugins/utils.h b/plugins/utils.h index 1f0e021b..68ff1630 100644 --- a/plugins/utils.h +++ b/plugins/utils.h | |||
| @@ -32,8 +32,6 @@ suite of plugins. */ | |||
| 32 | void support(void); | 32 | void support(void); |
| 33 | void print_revision(const char *, const char *); | 33 | void print_revision(const char *, const char *); |
| 34 | 34 | ||
| 35 | extern time_t start_time, end_time; | ||
| 36 | |||
| 37 | /* Test input types */ | 35 | /* Test input types */ |
| 38 | 36 | ||
| 39 | bool is_integer(char *); | 37 | bool is_integer(char *); |
