diff options
| -rw-r--r-- | plugins/Makefile.am | 2 | ||||
| -rw-r--r-- | plugins/check_time.c | 191 | ||||
| -rw-r--r-- | plugins/check_time.d/config.h | 42 | ||||
| -rw-r--r-- | plugins/check_ups.c | 274 | ||||
| -rw-r--r-- | plugins/check_ups.d/config.h | 55 |
5 files changed, 325 insertions, 239 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index a420d23d..9e4924c3 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -55,6 +55,7 @@ EXTRA_DIST = t \ | |||
| 55 | check_hpjd.d \ | 55 | check_hpjd.d \ |
| 56 | check_game.d \ | 56 | check_game.d \ |
| 57 | check_radius.d \ | 57 | check_radius.d \ |
| 58 | check_time.d \ | ||
| 58 | check_nagios.d \ | 59 | check_nagios.d \ |
| 59 | check_dbi.d \ | 60 | check_dbi.d \ |
| 60 | check_real.d \ | 61 | check_real.d \ |
| @@ -74,6 +75,7 @@ EXTRA_DIST = t \ | |||
| 74 | check_ntp_time.d \ | 75 | check_ntp_time.d \ |
| 75 | check_dig.d \ | 76 | check_dig.d \ |
| 76 | check_cluster.d \ | 77 | check_cluster.d \ |
| 78 | check_ups.d \ | ||
| 77 | check_fping.d | 79 | check_fping.d |
| 78 | 80 | ||
| 79 | PLUGINHDRS = common.h | 81 | PLUGINHDRS = common.h |
diff --git a/plugins/check_time.c b/plugins/check_time.c index d1f50683..debf59f3 100644 --- a/plugins/check_time.c +++ b/plugins/check_time.c | |||
| @@ -28,6 +28,7 @@ | |||
| 28 | * | 28 | * |
| 29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
| 30 | 30 | ||
| 31 | #include "states.h" | ||
| 31 | const char *progname = "check_time"; | 32 | const char *progname = "check_time"; |
| 32 | const char *copyright = "1999-2024"; | 33 | const char *copyright = "1999-2024"; |
| 33 | const char *email = "devel@monitoring-plugins.org"; | 34 | const char *email = "devel@monitoring-plugins.org"; |
| @@ -35,28 +36,15 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 35 | #include "common.h" | 36 | #include "common.h" |
| 36 | #include "netutils.h" | 37 | #include "netutils.h" |
| 37 | #include "utils.h" | 38 | #include "utils.h" |
| 38 | 39 | #include "check_time.d/config.h" | |
| 39 | enum { | ||
| 40 | TIME_PORT = 37 | ||
| 41 | }; | ||
| 42 | 40 | ||
| 43 | #define UNIX_EPOCH 2208988800UL | 41 | #define UNIX_EPOCH 2208988800UL |
| 44 | 42 | ||
| 45 | static uint32_t raw_server_time; | 43 | typedef struct { |
| 46 | static unsigned long server_time, diff_time; | 44 | int errorcode; |
| 47 | static int warning_time = 0; | 45 | check_time_config config; |
| 48 | static bool check_warning_time = false; | 46 | } check_time_config_wrapper; |
| 49 | static int critical_time = 0; | 47 | static check_time_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
| 50 | static bool check_critical_time = false; | ||
| 51 | static unsigned long warning_diff = 0; | ||
| 52 | static bool check_warning_diff = false; | ||
| 53 | static unsigned long critical_diff = 0; | ||
| 54 | static bool check_critical_diff = false; | ||
| 55 | static int server_port = TIME_PORT; | ||
| 56 | static char *server_address = NULL; | ||
| 57 | static bool use_udp = false; | ||
| 58 | |||
| 59 | static int process_arguments(int, char **); | ||
| 60 | static void print_help(void); | 48 | static void print_help(void); |
| 61 | void print_usage(void); | 49 | void print_usage(void); |
| 62 | 50 | ||
| @@ -68,8 +56,12 @@ int main(int argc, char **argv) { | |||
| 68 | /* Parse extra opts if any */ | 56 | /* Parse extra opts if any */ |
| 69 | argv = np_extra_opts(&argc, argv, progname); | 57 | argv = np_extra_opts(&argc, argv, progname); |
| 70 | 58 | ||
| 71 | if (process_arguments(argc, argv) == ERROR) | 59 | check_time_config_wrapper tmp_config = process_arguments(argc, argv); |
| 60 | if (tmp_config.errorcode == ERROR) { | ||
| 72 | usage4(_("Could not parse arguments")); | 61 | usage4(_("Could not parse arguments")); |
| 62 | } | ||
| 63 | |||
| 64 | const check_time_config config = tmp_config.config; | ||
| 73 | 65 | ||
| 74 | /* initialize alarm signal handling */ | 66 | /* initialize alarm signal handling */ |
| 75 | signal(SIGALRM, socket_timeout_alarm_handler); | 67 | signal(SIGALRM, socket_timeout_alarm_handler); |
| @@ -79,37 +71,40 @@ int main(int argc, char **argv) { | |||
| 79 | time(&start_time); | 71 | time(&start_time); |
| 80 | 72 | ||
| 81 | int socket; | 73 | int socket; |
| 82 | int result = STATE_UNKNOWN; | 74 | mp_state_enum result = STATE_UNKNOWN; |
| 83 | /* try to connect to the host at the given port number */ | 75 | /* try to connect to the host at the given port number */ |
| 84 | if (use_udp) { | 76 | if (config.use_udp) { |
| 85 | result = my_udp_connect(server_address, server_port, &socket); | 77 | result = my_udp_connect(config.server_address, config.server_port, &socket); |
| 86 | } else { | 78 | } else { |
| 87 | result = my_tcp_connect(server_address, server_port, &socket); | 79 | result = my_tcp_connect(config.server_address, config.server_port, &socket); |
| 88 | } | 80 | } |
| 89 | 81 | ||
| 90 | if (result != STATE_OK) { | 82 | if (result != STATE_OK) { |
| 91 | if (check_critical_time) | 83 | if (config.check_critical_time) { |
| 92 | result = STATE_CRITICAL; | 84 | result = STATE_CRITICAL; |
| 93 | else if (check_warning_time) | 85 | } else if (config.check_warning_time) { |
| 94 | result = STATE_WARNING; | 86 | result = STATE_WARNING; |
| 95 | else | 87 | } else { |
| 96 | result = STATE_UNKNOWN; | 88 | result = STATE_UNKNOWN; |
| 97 | die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), server_address, server_port); | 89 | } |
| 90 | die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), config.server_address, config.server_port); | ||
| 98 | } | 91 | } |
| 99 | 92 | ||
| 100 | if (use_udp) { | 93 | if (config.use_udp) { |
| 101 | if (send(socket, "", 0, 0) < 0) { | 94 | if (send(socket, "", 0, 0) < 0) { |
| 102 | if (check_critical_time) | 95 | if (config.check_critical_time) { |
| 103 | result = STATE_CRITICAL; | 96 | result = STATE_CRITICAL; |
| 104 | else if (check_warning_time) | 97 | } else if (config.check_warning_time) { |
| 105 | result = STATE_WARNING; | 98 | result = STATE_WARNING; |
| 106 | else | 99 | } else { |
| 107 | result = STATE_UNKNOWN; | 100 | result = STATE_UNKNOWN; |
| 108 | die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), server_address, server_port); | 101 | } |
| 102 | die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), config.server_address, config.server_port); | ||
| 109 | } | 103 | } |
| 110 | } | 104 | } |
| 111 | 105 | ||
| 112 | /* watch for the connection string */ | 106 | /* watch for the connection string */ |
| 107 | uint32_t raw_server_time; | ||
| 113 | result = recv(socket, (void *)&raw_server_time, sizeof(raw_server_time), 0); | 108 | result = recv(socket, (void *)&raw_server_time, sizeof(raw_server_time), 0); |
| 114 | 109 | ||
| 115 | /* close the connection */ | 110 | /* close the connection */ |
| @@ -121,48 +116,56 @@ int main(int argc, char **argv) { | |||
| 121 | 116 | ||
| 122 | /* return a WARNING status if we couldn't read any data */ | 117 | /* return a WARNING status if we couldn't read any data */ |
| 123 | if (result <= 0) { | 118 | if (result <= 0) { |
| 124 | if (check_critical_time) | 119 | if (config.check_critical_time) { |
| 125 | result = STATE_CRITICAL; | 120 | result = STATE_CRITICAL; |
| 126 | else if (check_warning_time) | 121 | } else if (config.check_warning_time) { |
| 127 | result = STATE_WARNING; | 122 | result = STATE_WARNING; |
| 128 | else | 123 | } else { |
| 129 | result = STATE_UNKNOWN; | 124 | result = STATE_UNKNOWN; |
| 130 | die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), server_address, server_port); | 125 | } |
| 126 | die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), config.server_address, config.server_port); | ||
| 131 | } | 127 | } |
| 132 | 128 | ||
| 133 | result = STATE_OK; | 129 | result = STATE_OK; |
| 134 | 130 | ||
| 135 | time_t conntime = (end_time - start_time); | 131 | time_t conntime = (end_time - start_time); |
| 136 | if (check_critical_time && conntime > critical_time) | 132 | if (config.check_critical_time && conntime > config.critical_time) { |
| 137 | result = STATE_CRITICAL; | 133 | result = STATE_CRITICAL; |
| 138 | else if (check_warning_time && conntime > warning_time) | 134 | } else if (config.check_warning_time && conntime > config.warning_time) { |
| 139 | result = STATE_WARNING; | 135 | result = STATE_WARNING; |
| 136 | } | ||
| 140 | 137 | ||
| 141 | if (result != STATE_OK) | 138 | if (result != STATE_OK) { |
| 142 | die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime, | 139 | die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime, |
| 143 | perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0, | 140 | perfdata("time", (long)conntime, "s", config.check_warning_time, (long)config.warning_time, config.check_critical_time, |
| 144 | false, 0)); | 141 | (long)config.critical_time, true, 0, false, 0)); |
| 142 | } | ||
| 145 | 143 | ||
| 144 | unsigned long server_time; | ||
| 145 | unsigned long diff_time; | ||
| 146 | server_time = ntohl(raw_server_time) - UNIX_EPOCH; | 146 | server_time = ntohl(raw_server_time) - UNIX_EPOCH; |
| 147 | if (server_time > (unsigned long)end_time) | 147 | if (server_time > (unsigned long)end_time) { |
| 148 | diff_time = server_time - (unsigned long)end_time; | 148 | diff_time = server_time - (unsigned long)end_time; |
| 149 | else | 149 | } else { |
| 150 | diff_time = (unsigned long)end_time - server_time; | 150 | diff_time = (unsigned long)end_time - server_time; |
| 151 | } | ||
| 151 | 152 | ||
| 152 | if (check_critical_diff && diff_time > critical_diff) | 153 | if (config.check_critical_diff && diff_time > config.critical_diff) { |
| 153 | result = STATE_CRITICAL; | 154 | result = STATE_CRITICAL; |
| 154 | else if (check_warning_diff && diff_time > warning_diff) | 155 | } else if (config.check_warning_diff && diff_time > config.warning_diff) { |
| 155 | result = STATE_WARNING; | 156 | result = STATE_WARNING; |
| 157 | } | ||
| 156 | 158 | ||
| 157 | printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time, | 159 | printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time, |
| 158 | perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0, | 160 | perfdata("time", (long)conntime, "s", config.check_warning_time, (long)config.warning_time, config.check_critical_time, |
| 159 | false, 0), | 161 | (long)config.critical_time, true, 0, false, 0), |
| 160 | perfdata("offset", diff_time, "s", check_warning_diff, warning_diff, check_critical_diff, critical_diff, true, 0, false, 0)); | 162 | perfdata("offset", diff_time, "s", config.check_warning_diff, config.warning_diff, config.check_critical_diff, |
| 163 | config.critical_diff, true, 0, false, 0)); | ||
| 161 | return result; | 164 | return result; |
| 162 | } | 165 | } |
| 163 | 166 | ||
| 164 | /* process command-line arguments */ | 167 | /* process command-line arguments */ |
| 165 | int process_arguments(int argc, char **argv) { | 168 | check_time_config_wrapper process_arguments(int argc, char **argv) { |
| 166 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | 169 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 167 | {"warning-variance", required_argument, 0, 'w'}, | 170 | {"warning-variance", required_argument, 0, 'w'}, |
| 168 | {"critical-variance", required_argument, 0, 'c'}, | 171 | {"critical-variance", required_argument, 0, 'c'}, |
| @@ -175,29 +178,37 @@ int process_arguments(int argc, char **argv) { | |||
| 175 | {"help", no_argument, 0, 'h'}, | 178 | {"help", no_argument, 0, 'h'}, |
| 176 | {0, 0, 0, 0}}; | 179 | {0, 0, 0, 0}}; |
| 177 | 180 | ||
| 178 | if (argc < 2) | 181 | if (argc < 2) { |
| 179 | usage("\n"); | 182 | usage("\n"); |
| 183 | } | ||
| 180 | 184 | ||
| 181 | for (int i = 1; i < argc; i++) { | 185 | for (int i = 1; i < argc; i++) { |
| 182 | if (strcmp("-to", argv[i]) == 0) | 186 | if (strcmp("-to", argv[i]) == 0) { |
| 183 | strcpy(argv[i], "-t"); | 187 | strcpy(argv[i], "-t"); |
| 184 | else if (strcmp("-wd", argv[i]) == 0) | 188 | } else if (strcmp("-wd", argv[i]) == 0) { |
| 185 | strcpy(argv[i], "-w"); | 189 | strcpy(argv[i], "-w"); |
| 186 | else if (strcmp("-cd", argv[i]) == 0) | 190 | } else if (strcmp("-cd", argv[i]) == 0) { |
| 187 | strcpy(argv[i], "-c"); | 191 | strcpy(argv[i], "-c"); |
| 188 | else if (strcmp("-wt", argv[i]) == 0) | 192 | } else if (strcmp("-wt", argv[i]) == 0) { |
| 189 | strcpy(argv[i], "-W"); | 193 | strcpy(argv[i], "-W"); |
| 190 | else if (strcmp("-ct", argv[i]) == 0) | 194 | } else if (strcmp("-ct", argv[i]) == 0) { |
| 191 | strcpy(argv[i], "-C"); | 195 | strcpy(argv[i], "-C"); |
| 196 | } | ||
| 192 | } | 197 | } |
| 193 | 198 | ||
| 199 | check_time_config_wrapper result = { | ||
| 200 | .errorcode = OK, | ||
| 201 | .config = check_time_config_init(), | ||
| 202 | }; | ||
| 203 | |||
| 194 | int option_char; | 204 | int option_char; |
| 195 | while (true) { | 205 | while (true) { |
| 196 | int option = 0; | 206 | int option = 0; |
| 197 | option_char = getopt_long(argc, argv, "hVH:w:c:W:C:p:t:u", longopts, &option); | 207 | option_char = getopt_long(argc, argv, "hVH:w:c:W:C:p:t:u", longopts, &option); |
| 198 | 208 | ||
| 199 | if (option_char == -1 || option_char == EOF) | 209 | if (option_char == -1 || option_char == EOF) { |
| 200 | break; | 210 | break; |
| 211 | } | ||
| 201 | 212 | ||
| 202 | switch (option_char) { | 213 | switch (option_char) { |
| 203 | case '?': /* print short usage statement if args not parsable */ | 214 | case '?': /* print short usage statement if args not parsable */ |
| @@ -209,18 +220,19 @@ int process_arguments(int argc, char **argv) { | |||
| 209 | print_revision(progname, NP_VERSION); | 220 | print_revision(progname, NP_VERSION); |
| 210 | exit(STATE_UNKNOWN); | 221 | exit(STATE_UNKNOWN); |
| 211 | case 'H': /* hostname */ | 222 | case 'H': /* hostname */ |
| 212 | if (!is_host(optarg)) | 223 | if (!is_host(optarg)) { |
| 213 | usage2(_("Invalid hostname/address"), optarg); | 224 | usage2(_("Invalid hostname/address"), optarg); |
| 214 | server_address = optarg; | 225 | } |
| 226 | result.config.server_address = optarg; | ||
| 215 | break; | 227 | break; |
| 216 | case 'w': /* warning-variance */ | 228 | case 'w': /* warning-variance */ |
| 217 | if (is_intnonneg(optarg)) { | 229 | if (is_intnonneg(optarg)) { |
| 218 | warning_diff = strtoul(optarg, NULL, 10); | 230 | result.config.warning_diff = strtoul(optarg, NULL, 10); |
| 219 | check_warning_diff = true; | 231 | result.config.check_warning_diff = true; |
| 220 | } else if (strspn(optarg, "0123456789:,") > 0) { | 232 | } else if (strspn(optarg, "0123456789:,") > 0) { |
| 221 | if (sscanf(optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) { | 233 | if (sscanf(optarg, "%lu%*[:,]%d", &result.config.warning_diff, &result.config.warning_time) == 2) { |
| 222 | check_warning_diff = true; | 234 | result.config.check_warning_diff = true; |
| 223 | check_warning_time = true; | 235 | result.config.check_warning_time = true; |
| 224 | } else { | 236 | } else { |
| 225 | usage4(_("Warning thresholds must be a positive integer")); | 237 | usage4(_("Warning thresholds must be a positive integer")); |
| 226 | } | 238 | } |
| @@ -230,12 +242,12 @@ int process_arguments(int argc, char **argv) { | |||
| 230 | break; | 242 | break; |
| 231 | case 'c': /* critical-variance */ | 243 | case 'c': /* critical-variance */ |
| 232 | if (is_intnonneg(optarg)) { | 244 | if (is_intnonneg(optarg)) { |
| 233 | critical_diff = strtoul(optarg, NULL, 10); | 245 | result.config.critical_diff = strtoul(optarg, NULL, 10); |
| 234 | check_critical_diff = true; | 246 | result.config.check_critical_diff = true; |
| 235 | } else if (strspn(optarg, "0123456789:,") > 0) { | 247 | } else if (strspn(optarg, "0123456789:,") > 0) { |
| 236 | if (sscanf(optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) == 2) { | 248 | if (sscanf(optarg, "%lu%*[:,]%d", &result.config.critical_diff, &result.config.critical_time) == 2) { |
| 237 | check_critical_diff = true; | 249 | result.config.check_critical_diff = true; |
| 238 | check_critical_time = true; | 250 | result.config.check_critical_time = true; |
| 239 | } else { | 251 | } else { |
| 240 | usage4(_("Critical thresholds must be a positive integer")); | 252 | usage4(_("Critical thresholds must be a positive integer")); |
| 241 | } | 253 | } |
| @@ -244,48 +256,53 @@ int process_arguments(int argc, char **argv) { | |||
| 244 | } | 256 | } |
| 245 | break; | 257 | break; |
| 246 | case 'W': /* warning-connect */ | 258 | case 'W': /* warning-connect */ |
| 247 | if (!is_intnonneg(optarg)) | 259 | if (!is_intnonneg(optarg)) { |
| 248 | usage4(_("Warning threshold must be a positive integer")); | 260 | usage4(_("Warning threshold must be a positive integer")); |
| 249 | else | 261 | } else { |
| 250 | warning_time = atoi(optarg); | 262 | result.config.warning_time = atoi(optarg); |
| 251 | check_warning_time = true; | 263 | } |
| 264 | result.config.check_warning_time = true; | ||
| 252 | break; | 265 | break; |
| 253 | case 'C': /* critical-connect */ | 266 | case 'C': /* critical-connect */ |
| 254 | if (!is_intnonneg(optarg)) | 267 | if (!is_intnonneg(optarg)) { |
| 255 | usage4(_("Critical threshold must be a positive integer")); | 268 | usage4(_("Critical threshold must be a positive integer")); |
| 256 | else | 269 | } else { |
| 257 | critical_time = atoi(optarg); | 270 | result.config.critical_time = atoi(optarg); |
| 258 | check_critical_time = true; | 271 | } |
| 272 | result.config.check_critical_time = true; | ||
| 259 | break; | 273 | break; |
| 260 | case 'p': /* port */ | 274 | case 'p': /* port */ |
| 261 | if (!is_intnonneg(optarg)) | 275 | if (!is_intnonneg(optarg)) { |
| 262 | usage4(_("Port must be a positive integer")); | 276 | usage4(_("Port must be a positive integer")); |
| 263 | else | 277 | } else { |
| 264 | server_port = atoi(optarg); | 278 | result.config.server_port = atoi(optarg); |
| 279 | } | ||
| 265 | break; | 280 | break; |
| 266 | case 't': /* timeout */ | 281 | case 't': /* timeout */ |
| 267 | if (!is_intnonneg(optarg)) | 282 | if (!is_intnonneg(optarg)) { |
| 268 | usage2(_("Timeout interval must be a positive integer"), optarg); | 283 | usage2(_("Timeout interval must be a positive integer"), optarg); |
| 269 | else | 284 | } else { |
| 270 | socket_timeout = atoi(optarg); | 285 | socket_timeout = atoi(optarg); |
| 286 | } | ||
| 271 | break; | 287 | break; |
| 272 | case 'u': /* udp */ | 288 | case 'u': /* udp */ |
| 273 | use_udp = true; | 289 | result.config.use_udp = true; |
| 274 | } | 290 | } |
| 275 | } | 291 | } |
| 276 | 292 | ||
| 277 | option_char = optind; | 293 | option_char = optind; |
| 278 | if (server_address == NULL) { | 294 | if (result.config.server_address == NULL) { |
| 279 | if (argc > option_char) { | 295 | if (argc > option_char) { |
| 280 | if (!is_host(argv[option_char])) | 296 | if (!is_host(argv[option_char])) { |
| 281 | usage2(_("Invalid hostname/address"), optarg); | 297 | usage2(_("Invalid hostname/address"), optarg); |
| 282 | server_address = argv[option_char]; | 298 | } |
| 299 | result.config.server_address = argv[option_char]; | ||
| 283 | } else { | 300 | } else { |
| 284 | usage4(_("Hostname was not supplied")); | 301 | usage4(_("Hostname was not supplied")); |
| 285 | } | 302 | } |
| 286 | } | 303 | } |
| 287 | 304 | ||
| 288 | return OK; | 305 | return result; |
| 289 | } | 306 | } |
| 290 | 307 | ||
| 291 | void print_help(void) { | 308 | void print_help(void) { |
diff --git a/plugins/check_time.d/config.h b/plugins/check_time.d/config.h new file mode 100644 index 00000000..09bd7c45 --- /dev/null +++ b/plugins/check_time.d/config.h | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include <stddef.h> | ||
| 5 | |||
| 6 | enum { | ||
| 7 | TIME_PORT = 37 | ||
| 8 | }; | ||
| 9 | |||
| 10 | typedef struct { | ||
| 11 | char *server_address; | ||
| 12 | int server_port; | ||
| 13 | bool use_udp; | ||
| 14 | |||
| 15 | int warning_time; | ||
| 16 | bool check_warning_time; | ||
| 17 | int critical_time; | ||
| 18 | bool check_critical_time; | ||
| 19 | unsigned long warning_diff; | ||
| 20 | bool check_warning_diff; | ||
| 21 | unsigned long critical_diff; | ||
| 22 | bool check_critical_diff; | ||
| 23 | } check_time_config; | ||
| 24 | |||
| 25 | check_time_config check_time_config_init() { | ||
| 26 | check_time_config tmp = { | ||
| 27 | .server_address = NULL, | ||
| 28 | .server_port = TIME_PORT, | ||
| 29 | .use_udp = false, | ||
| 30 | |||
| 31 | .warning_time = 0, | ||
| 32 | .check_warning_time = false, | ||
| 33 | .critical_time = 0, | ||
| 34 | .check_critical_time = false, | ||
| 35 | |||
| 36 | .warning_diff = 0, | ||
| 37 | .check_warning_diff = false, | ||
| 38 | .critical_diff = 0, | ||
| 39 | .check_critical_diff = false, | ||
| 40 | }; | ||
| 41 | return tmp; | ||
| 42 | } | ||
diff --git a/plugins/check_ups.c b/plugins/check_ups.c index 526a29df..ecc0760f 100644 --- a/plugins/check_ups.c +++ b/plugins/check_ups.c | |||
| @@ -39,69 +39,29 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 39 | #include "common.h" | 39 | #include "common.h" |
| 40 | #include "netutils.h" | 40 | #include "netutils.h" |
| 41 | #include "utils.h" | 41 | #include "utils.h" |
| 42 | 42 | #include "check_ups.d/config.h" | |
| 43 | enum { | 43 | #include "states.h" |
| 44 | PORT = 3493 | ||
| 45 | }; | ||
| 46 | |||
| 47 | #define UPS_NONE 0 /* no supported options */ | ||
| 48 | #define UPS_UTILITY 1 /* supports utility line */ | ||
| 49 | #define UPS_BATTPCT 2 /* supports percent battery remaining */ | ||
| 50 | #define UPS_STATUS 4 /* supports UPS status */ | ||
| 51 | #define UPS_TEMP 8 /* supports UPS temperature */ | ||
| 52 | #define UPS_LOADPCT 16 /* supports load percent */ | ||
| 53 | #define UPS_REALPOWER 32 /* supports real power */ | ||
| 54 | |||
| 55 | #define UPSSTATUS_NONE 0 | ||
| 56 | #define UPSSTATUS_OFF 1 | ||
| 57 | #define UPSSTATUS_OL 2 | ||
| 58 | #define UPSSTATUS_OB 4 | ||
| 59 | #define UPSSTATUS_LB 8 | ||
| 60 | #define UPSSTATUS_CAL 16 | ||
| 61 | #define UPSSTATUS_RB 32 /*Replace Battery */ | ||
| 62 | #define UPSSTATUS_BYPASS 64 | ||
| 63 | #define UPSSTATUS_OVER 128 | ||
| 64 | #define UPSSTATUS_TRIM 256 | ||
| 65 | #define UPSSTATUS_BOOST 512 | ||
| 66 | #define UPSSTATUS_CHRG 1024 | ||
| 67 | #define UPSSTATUS_DISCHRG 2048 | ||
| 68 | #define UPSSTATUS_UNKNOWN 4096 | ||
| 69 | #define UPSSTATUS_ALARM 8192 | ||
| 70 | 44 | ||
| 71 | enum { | 45 | enum { |
| 72 | NOSUCHVAR = ERROR - 1 | 46 | NOSUCHVAR = ERROR - 1 |
| 73 | }; | 47 | }; |
| 74 | 48 | ||
| 75 | typedef struct ups_config { | ||
| 76 | unsigned int server_port; | ||
| 77 | char *server_address; | ||
| 78 | char *ups_name; | ||
| 79 | double warning_value; | ||
| 80 | double critical_value; | ||
| 81 | bool check_warn; | ||
| 82 | bool check_crit; | ||
| 83 | int check_variable; | ||
| 84 | int status; | ||
| 85 | bool temp_output_c; | ||
| 86 | } ups_config; | ||
| 87 | |||
| 88 | ups_config ups_config_init(void) { | ||
| 89 | ups_config tmp = {0}; | ||
| 90 | tmp.server_port = PORT; | ||
| 91 | tmp.server_address = NULL; | ||
| 92 | tmp.ups_name = NULL; | ||
| 93 | tmp.check_variable = UPS_NONE; | ||
| 94 | tmp.status = UPSSTATUS_NONE; | ||
| 95 | |||
| 96 | return tmp; | ||
| 97 | } | ||
| 98 | |||
| 99 | // Forward declarations | 49 | // Forward declarations |
| 100 | static int determine_status(ups_config * /*config*/, int *supported_options); | 50 | typedef struct { |
| 101 | static int get_ups_variable(const char * /*varname*/, char * /*buf*/, ups_config config); | 51 | int errorcode; |
| 52 | int ups_status; | ||
| 53 | int supported_options; | ||
| 54 | } determine_status_result; | ||
| 55 | static determine_status_result determine_status(check_ups_config /*config*/); | ||
| 56 | static int get_ups_variable(const char * /*varname*/, char * /*buf*/, check_ups_config config); | ||
| 57 | |||
| 58 | typedef struct { | ||
| 59 | int errorcode; | ||
| 60 | check_ups_config config; | ||
| 61 | } check_ups_config_wrapper; | ||
| 62 | static check_ups_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 63 | static check_ups_config_wrapper validate_arguments(check_ups_config_wrapper /*config_wrapper*/); | ||
| 102 | 64 | ||
| 103 | static int process_arguments(int /*argc*/, char ** /*argv*/, ups_config * /*config*/); | ||
| 104 | static int validate_arguments(ups_config /*config*/); | ||
| 105 | static void print_help(void); | 65 | static void print_help(void); |
| 106 | void print_usage(void); | 66 | void print_usage(void); |
| 107 | 67 | ||
| @@ -109,28 +69,16 @@ int main(int argc, char **argv) { | |||
| 109 | setlocale(LC_ALL, ""); | 69 | setlocale(LC_ALL, ""); |
| 110 | bindtextdomain(PACKAGE, LOCALEDIR); | 70 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 111 | textdomain(PACKAGE); | 71 | textdomain(PACKAGE); |
| 112 | |||
| 113 | char *ups_status; | ||
| 114 | ups_status = strdup("N/A"); | ||
| 115 | |||
| 116 | char *data; | ||
| 117 | data = strdup(""); | ||
| 118 | |||
| 119 | char *message; | ||
| 120 | message = strdup(""); | ||
| 121 | |||
| 122 | // Exit result | ||
| 123 | int result = STATE_UNKNOWN; | ||
| 124 | |||
| 125 | /* Parse extra opts if any */ | 72 | /* Parse extra opts if any */ |
| 126 | argv = np_extra_opts(&argc, argv, progname); | 73 | argv = np_extra_opts(&argc, argv, progname); |
| 127 | 74 | ||
| 128 | // Config from commandline | 75 | check_ups_config_wrapper tmp_config = process_arguments(argc, argv); |
| 129 | ups_config config = ups_config_init(); | ||
| 130 | 76 | ||
| 131 | if (process_arguments(argc, argv, &config) == ERROR) { | 77 | if (tmp_config.errorcode == ERROR) { |
| 132 | usage4(_("Could not parse arguments")); | 78 | usage4(_("Could not parse arguments")); |
| 133 | } | 79 | } |
| 80 | // Config from commandline | ||
| 81 | check_ups_config config = tmp_config.config; | ||
| 134 | 82 | ||
| 135 | /* initialize alarm signal handling */ | 83 | /* initialize alarm signal handling */ |
| 136 | signal(SIGALRM, socket_timeout_alarm_handler); | 84 | signal(SIGALRM, socket_timeout_alarm_handler); |
| @@ -138,71 +86,75 @@ int main(int argc, char **argv) { | |||
| 138 | /* set socket timeout */ | 86 | /* set socket timeout */ |
| 139 | alarm(socket_timeout); | 87 | alarm(socket_timeout); |
| 140 | 88 | ||
| 141 | int supported_options = UPS_NONE; | ||
| 142 | |||
| 143 | /* get the ups status if possible */ | 89 | /* get the ups status if possible */ |
| 144 | if (determine_status(&config, &supported_options) != OK) { | 90 | determine_status_result query_result = determine_status(config); |
| 91 | if (query_result.errorcode != OK) { | ||
| 145 | return STATE_CRITICAL; | 92 | return STATE_CRITICAL; |
| 146 | } | 93 | } |
| 147 | 94 | ||
| 148 | if (supported_options & UPS_STATUS) { | 95 | int ups_status_flags = query_result.ups_status; |
| 96 | int supported_options = query_result.supported_options; | ||
| 149 | 97 | ||
| 150 | ups_status = strdup(""); | 98 | // Exit result |
| 99 | mp_state_enum result = STATE_UNKNOWN; | ||
| 100 | char *message = NULL; | ||
| 151 | 101 | ||
| 102 | if (supported_options & UPS_STATUS) { | ||
| 103 | char *ups_status = strdup(""); | ||
| 152 | result = STATE_OK; | 104 | result = STATE_OK; |
| 153 | 105 | ||
| 154 | if (config.status & UPSSTATUS_OFF) { | 106 | if (ups_status_flags & UPSSTATUS_OFF) { |
| 155 | xasprintf(&ups_status, "Off"); | 107 | xasprintf(&ups_status, "Off"); |
| 156 | result = STATE_CRITICAL; | 108 | result = STATE_CRITICAL; |
| 157 | } else if ((config.status & (UPSSTATUS_OB | UPSSTATUS_LB)) == (UPSSTATUS_OB | UPSSTATUS_LB)) { | 109 | } else if ((ups_status_flags & (UPSSTATUS_OB | UPSSTATUS_LB)) == (UPSSTATUS_OB | UPSSTATUS_LB)) { |
| 158 | xasprintf(&ups_status, _("On Battery, Low Battery")); | 110 | xasprintf(&ups_status, _("On Battery, Low Battery")); |
| 159 | result = STATE_CRITICAL; | 111 | result = STATE_CRITICAL; |
| 160 | } else { | 112 | } else { |
| 161 | if (config.status & UPSSTATUS_OL) { | 113 | if (ups_status_flags & UPSSTATUS_OL) { |
| 162 | xasprintf(&ups_status, "%s%s", ups_status, _("Online")); | 114 | xasprintf(&ups_status, "%s%s", ups_status, _("Online")); |
| 163 | } | 115 | } |
| 164 | if (config.status & UPSSTATUS_OB) { | 116 | if (ups_status_flags & UPSSTATUS_OB) { |
| 165 | xasprintf(&ups_status, "%s%s", ups_status, _("On Battery")); | 117 | xasprintf(&ups_status, "%s%s", ups_status, _("On Battery")); |
| 166 | result = max_state(result, STATE_WARNING); | 118 | result = max_state(result, STATE_WARNING); |
| 167 | } | 119 | } |
| 168 | if (config.status & UPSSTATUS_LB) { | 120 | if (ups_status_flags & UPSSTATUS_LB) { |
| 169 | xasprintf(&ups_status, "%s%s", ups_status, _(", Low Battery")); | 121 | xasprintf(&ups_status, "%s%s", ups_status, _(", Low Battery")); |
| 170 | result = max_state(result, STATE_WARNING); | 122 | result = max_state(result, STATE_WARNING); |
| 171 | } | 123 | } |
| 172 | if (config.status & UPSSTATUS_CAL) { | 124 | if (ups_status_flags & UPSSTATUS_CAL) { |
| 173 | xasprintf(&ups_status, "%s%s", ups_status, _(", Calibrating")); | 125 | xasprintf(&ups_status, "%s%s", ups_status, _(", Calibrating")); |
| 174 | } | 126 | } |
| 175 | if (config.status & UPSSTATUS_RB) { | 127 | if (ups_status_flags & UPSSTATUS_RB) { |
| 176 | xasprintf(&ups_status, "%s%s", ups_status, _(", Replace Battery")); | 128 | xasprintf(&ups_status, "%s%s", ups_status, _(", Replace Battery")); |
| 177 | result = max_state(result, STATE_WARNING); | 129 | result = max_state(result, STATE_WARNING); |
| 178 | } | 130 | } |
| 179 | if (config.status & UPSSTATUS_BYPASS) { | 131 | if (ups_status_flags & UPSSTATUS_BYPASS) { |
| 180 | xasprintf(&ups_status, "%s%s", ups_status, _(", On Bypass")); | 132 | xasprintf(&ups_status, "%s%s", ups_status, _(", On Bypass")); |
| 181 | // Bypassing the battery is likely a bad thing | 133 | // Bypassing the battery is likely a bad thing |
| 182 | result = STATE_CRITICAL; | 134 | result = STATE_CRITICAL; |
| 183 | } | 135 | } |
| 184 | if (config.status & UPSSTATUS_OVER) { | 136 | if (ups_status_flags & UPSSTATUS_OVER) { |
| 185 | xasprintf(&ups_status, "%s%s", ups_status, _(", Overload")); | 137 | xasprintf(&ups_status, "%s%s", ups_status, _(", Overload")); |
| 186 | result = max_state(result, STATE_WARNING); | 138 | result = max_state(result, STATE_WARNING); |
| 187 | } | 139 | } |
| 188 | if (config.status & UPSSTATUS_TRIM) { | 140 | if (ups_status_flags & UPSSTATUS_TRIM) { |
| 189 | xasprintf(&ups_status, "%s%s", ups_status, _(", Trimming")); | 141 | xasprintf(&ups_status, "%s%s", ups_status, _(", Trimming")); |
| 190 | } | 142 | } |
| 191 | if (config.status & UPSSTATUS_BOOST) { | 143 | if (ups_status_flags & UPSSTATUS_BOOST) { |
| 192 | xasprintf(&ups_status, "%s%s", ups_status, _(", Boosting")); | 144 | xasprintf(&ups_status, "%s%s", ups_status, _(", Boosting")); |
| 193 | } | 145 | } |
| 194 | if (config.status & UPSSTATUS_CHRG) { | 146 | if (ups_status_flags & UPSSTATUS_CHRG) { |
| 195 | xasprintf(&ups_status, "%s%s", ups_status, _(", Charging")); | 147 | xasprintf(&ups_status, "%s%s", ups_status, _(", Charging")); |
| 196 | } | 148 | } |
| 197 | if (config.status & UPSSTATUS_DISCHRG) { | 149 | if (ups_status_flags & UPSSTATUS_DISCHRG) { |
| 198 | xasprintf(&ups_status, "%s%s", ups_status, _(", Discharging")); | 150 | xasprintf(&ups_status, "%s%s", ups_status, _(", Discharging")); |
| 199 | result = max_state(result, STATE_WARNING); | 151 | result = max_state(result, STATE_WARNING); |
| 200 | } | 152 | } |
| 201 | if (config.status & UPSSTATUS_ALARM) { | 153 | if (ups_status_flags & UPSSTATUS_ALARM) { |
| 202 | xasprintf(&ups_status, "%s%s", ups_status, _(", ALARM")); | 154 | xasprintf(&ups_status, "%s%s", ups_status, _(", ALARM")); |
| 203 | result = STATE_CRITICAL; | 155 | result = STATE_CRITICAL; |
| 204 | } | 156 | } |
| 205 | if (config.status & UPSSTATUS_UNKNOWN) { | 157 | if (ups_status_flags & UPSSTATUS_UNKNOWN) { |
| 206 | xasprintf(&ups_status, "%s%s", ups_status, _(", Unknown")); | 158 | xasprintf(&ups_status, "%s%s", ups_status, _(", Unknown")); |
| 207 | } | 159 | } |
| 208 | } | 160 | } |
| @@ -211,7 +163,7 @@ int main(int argc, char **argv) { | |||
| 211 | 163 | ||
| 212 | int res; | 164 | int res; |
| 213 | char temp_buffer[MAX_INPUT_BUFFER]; | 165 | char temp_buffer[MAX_INPUT_BUFFER]; |
| 214 | 166 | char *performance_data = strdup(""); | |
| 215 | /* get the ups utility voltage if possible */ | 167 | /* get the ups utility voltage if possible */ |
| 216 | res = get_ups_variable("input.voltage", temp_buffer, config); | 168 | res = get_ups_variable("input.voltage", temp_buffer, config); |
| 217 | if (res == NOSUCHVAR) { | 169 | if (res == NOSUCHVAR) { |
| @@ -239,11 +191,12 @@ int main(int argc, char **argv) { | |||
| 239 | } else if (config.check_warn && ups_utility_deviation >= config.warning_value) { | 191 | } else if (config.check_warn && ups_utility_deviation >= config.warning_value) { |
| 240 | result = max_state(result, STATE_WARNING); | 192 | result = max_state(result, STATE_WARNING); |
| 241 | } | 193 | } |
| 242 | xasprintf(&data, "%s", | 194 | xasprintf(&performance_data, "%s", |
| 243 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", config.check_warn, (long)(1000 * config.warning_value), | 195 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", config.check_warn, (long)(1000 * config.warning_value), |
| 244 | config.check_crit, (long)(1000 * config.critical_value), true, 0, false, 0)); | 196 | config.check_crit, (long)(1000 * config.critical_value), true, 0, false, 0)); |
| 245 | } else { | 197 | } else { |
| 246 | xasprintf(&data, "%s", perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", false, 0, false, 0, true, 0, false, 0)); | 198 | xasprintf(&performance_data, "%s", |
| 199 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", false, 0, false, 0, true, 0, false, 0)); | ||
| 247 | } | 200 | } |
| 248 | } | 201 | } |
| 249 | 202 | ||
| @@ -266,11 +219,12 @@ int main(int argc, char **argv) { | |||
| 266 | } else if (config.check_warn && ups_battery_percent <= config.warning_value) { | 219 | } else if (config.check_warn && ups_battery_percent <= config.warning_value) { |
| 267 | result = max_state(result, STATE_WARNING); | 220 | result = max_state(result, STATE_WARNING); |
| 268 | } | 221 | } |
| 269 | xasprintf(&data, "%s %s", data, | 222 | xasprintf(&performance_data, "%s %s", performance_data, |
| 270 | perfdata("battery", (long)ups_battery_percent, "%", config.check_warn, (long)(config.warning_value), | 223 | perfdata("battery", (long)ups_battery_percent, "%", config.check_warn, (long)(config.warning_value), |
| 271 | config.check_crit, (long)(config.critical_value), true, 0, true, 100)); | 224 | config.check_crit, (long)(config.critical_value), true, 0, true, 100)); |
| 272 | } else { | 225 | } else { |
| 273 | xasprintf(&data, "%s %s", data, perfdata("battery", (long)ups_battery_percent, "%", false, 0, false, 0, true, 0, true, 100)); | 226 | xasprintf(&performance_data, "%s %s", performance_data, |
| 227 | perfdata("battery", (long)ups_battery_percent, "%", false, 0, false, 0, true, 0, true, 100)); | ||
| 274 | } | 228 | } |
| 275 | } | 229 | } |
| 276 | 230 | ||
| @@ -293,11 +247,12 @@ int main(int argc, char **argv) { | |||
| 293 | } else if (config.check_warn && ups_load_percent >= config.warning_value) { | 247 | } else if (config.check_warn && ups_load_percent >= config.warning_value) { |
| 294 | result = max_state(result, STATE_WARNING); | 248 | result = max_state(result, STATE_WARNING); |
| 295 | } | 249 | } |
| 296 | xasprintf(&data, "%s %s", data, | 250 | xasprintf(&performance_data, "%s %s", performance_data, |
| 297 | perfdata("load", (long)ups_load_percent, "%", config.check_warn, (long)(config.warning_value), config.check_crit, | 251 | perfdata("load", (long)ups_load_percent, "%", config.check_warn, (long)(config.warning_value), config.check_crit, |
| 298 | (long)(config.critical_value), true, 0, true, 100)); | 252 | (long)(config.critical_value), true, 0, true, 100)); |
| 299 | } else { | 253 | } else { |
| 300 | xasprintf(&data, "%s %s", data, perfdata("load", (long)ups_load_percent, "%", false, 0, false, 0, true, 0, true, 100)); | 254 | xasprintf(&performance_data, "%s %s", performance_data, |
| 255 | perfdata("load", (long)ups_load_percent, "%", false, 0, false, 0, true, 0, true, 100)); | ||
| 301 | } | 256 | } |
| 302 | } | 257 | } |
| 303 | 258 | ||
| @@ -329,11 +284,12 @@ int main(int argc, char **argv) { | |||
| 329 | } else if (config.check_warn && ups_temperature >= config.warning_value) { | 284 | } else if (config.check_warn && ups_temperature >= config.warning_value) { |
| 330 | result = max_state(result, STATE_WARNING); | 285 | result = max_state(result, STATE_WARNING); |
| 331 | } | 286 | } |
| 332 | xasprintf(&data, "%s %s", data, | 287 | xasprintf(&performance_data, "%s %s", performance_data, |
| 333 | perfdata("temp", (long)ups_temperature, tunits, config.check_warn, (long)(config.warning_value), config.check_crit, | 288 | perfdata("temp", (long)ups_temperature, tunits, config.check_warn, (long)(config.warning_value), config.check_crit, |
| 334 | (long)(config.critical_value), true, 0, false, 0)); | 289 | (long)(config.critical_value), true, 0, false, 0)); |
| 335 | } else { | 290 | } else { |
| 336 | xasprintf(&data, "%s %s", data, perfdata("temp", (long)ups_temperature, tunits, false, 0, false, 0, true, 0, false, 0)); | 291 | xasprintf(&performance_data, "%s %s", performance_data, |
| 292 | perfdata("temp", (long)ups_temperature, tunits, false, 0, false, 0, true, 0, false, 0)); | ||
| 337 | } | 293 | } |
| 338 | } | 294 | } |
| 339 | 295 | ||
| @@ -355,11 +311,12 @@ int main(int argc, char **argv) { | |||
| 355 | } else if (config.check_warn && ups_realpower >= config.warning_value) { | 311 | } else if (config.check_warn && ups_realpower >= config.warning_value) { |
| 356 | result = max_state(result, STATE_WARNING); | 312 | result = max_state(result, STATE_WARNING); |
| 357 | } | 313 | } |
| 358 | xasprintf(&data, "%s %s", data, | 314 | xasprintf(&performance_data, "%s %s", performance_data, |
| 359 | perfdata("realpower", (long)ups_realpower, "W", config.check_warn, (long)(config.warning_value), config.check_crit, | 315 | perfdata("realpower", (long)ups_realpower, "W", config.check_warn, (long)(config.warning_value), config.check_crit, |
| 360 | (long)(config.critical_value), true, 0, false, 0)); | 316 | (long)(config.critical_value), true, 0, false, 0)); |
| 361 | } else { | 317 | } else { |
| 362 | xasprintf(&data, "%s %s", data, perfdata("realpower", (long)ups_realpower, "W", false, 0, false, 0, true, 0, false, 0)); | 318 | xasprintf(&performance_data, "%s %s", performance_data, |
| 319 | perfdata("realpower", (long)ups_realpower, "W", false, 0, false, 0, true, 0, false, 0)); | ||
| 363 | } | 320 | } |
| 364 | } | 321 | } |
| 365 | 322 | ||
| @@ -373,66 +330,73 @@ int main(int argc, char **argv) { | |||
| 373 | /* reset timeout */ | 330 | /* reset timeout */ |
| 374 | alarm(0); | 331 | alarm(0); |
| 375 | 332 | ||
| 376 | printf("UPS %s - %s|%s\n", state_text(result), message, data); | 333 | printf("UPS %s - %s|%s\n", state_text(result), message, performance_data); |
| 377 | return result; | 334 | exit(result); |
| 378 | } | 335 | } |
| 379 | 336 | ||
| 380 | /* determines what options are supported by the UPS */ | 337 | /* determines what options are supported by the UPS */ |
| 381 | int determine_status(ups_config *config, int *supported_options) { | 338 | determine_status_result determine_status(const check_ups_config config) { |
| 382 | char recv_buffer[MAX_INPUT_BUFFER]; | ||
| 383 | 339 | ||
| 384 | int res = get_ups_variable("ups.status", recv_buffer, *config); | 340 | determine_status_result result = { |
| 341 | .errorcode = OK, | ||
| 342 | .ups_status = UPSSTATUS_NONE, | ||
| 343 | .supported_options = 0, | ||
| 344 | }; | ||
| 345 | |||
| 346 | char recv_buffer[MAX_INPUT_BUFFER]; | ||
| 347 | int res = get_ups_variable("ups.status", recv_buffer, config); | ||
| 385 | if (res == NOSUCHVAR) { | 348 | if (res == NOSUCHVAR) { |
| 386 | return OK; | 349 | return result; |
| 387 | } | 350 | } |
| 388 | 351 | ||
| 389 | if (res != STATE_OK) { | 352 | if (res != STATE_OK) { |
| 390 | printf("%s\n", _("Invalid response received from host")); | 353 | printf("%s\n", _("Invalid response received from host")); |
| 391 | return ERROR; | 354 | result.errorcode = ERROR; |
| 355 | return result; | ||
| 392 | } | 356 | } |
| 393 | 357 | ||
| 394 | *supported_options |= UPS_STATUS; | 358 | result.supported_options |= UPS_STATUS; |
| 395 | 359 | ||
| 396 | char temp_buffer[MAX_INPUT_BUFFER]; | 360 | char temp_buffer[MAX_INPUT_BUFFER]; |
| 397 | 361 | ||
| 398 | strcpy(temp_buffer, recv_buffer); | 362 | strcpy(temp_buffer, recv_buffer); |
| 399 | for (char *ptr = (char *)strtok(temp_buffer, " "); ptr != NULL; ptr = (char *)strtok(NULL, " ")) { | 363 | for (char *ptr = strtok(temp_buffer, " "); ptr != NULL; ptr = strtok(NULL, " ")) { |
| 400 | if (!strcmp(ptr, "OFF")) { | 364 | if (!strcmp(ptr, "OFF")) { |
| 401 | config->status |= UPSSTATUS_OFF; | 365 | result.ups_status |= UPSSTATUS_OFF; |
| 402 | } else if (!strcmp(ptr, "OL")) { | 366 | } else if (!strcmp(ptr, "OL")) { |
| 403 | config->status |= UPSSTATUS_OL; | 367 | result.ups_status |= UPSSTATUS_OL; |
| 404 | } else if (!strcmp(ptr, "OB")) { | 368 | } else if (!strcmp(ptr, "OB")) { |
| 405 | config->status |= UPSSTATUS_OB; | 369 | result.ups_status |= UPSSTATUS_OB; |
| 406 | } else if (!strcmp(ptr, "LB")) { | 370 | } else if (!strcmp(ptr, "LB")) { |
| 407 | config->status |= UPSSTATUS_LB; | 371 | result.ups_status |= UPSSTATUS_LB; |
| 408 | } else if (!strcmp(ptr, "CAL")) { | 372 | } else if (!strcmp(ptr, "CAL")) { |
| 409 | config->status |= UPSSTATUS_CAL; | 373 | result.ups_status |= UPSSTATUS_CAL; |
| 410 | } else if (!strcmp(ptr, "RB")) { | 374 | } else if (!strcmp(ptr, "RB")) { |
| 411 | config->status |= UPSSTATUS_RB; | 375 | result.ups_status |= UPSSTATUS_RB; |
| 412 | } else if (!strcmp(ptr, "BYPASS")) { | 376 | } else if (!strcmp(ptr, "BYPASS")) { |
| 413 | config->status |= UPSSTATUS_BYPASS; | 377 | result.ups_status |= UPSSTATUS_BYPASS; |
| 414 | } else if (!strcmp(ptr, "OVER")) { | 378 | } else if (!strcmp(ptr, "OVER")) { |
| 415 | config->status |= UPSSTATUS_OVER; | 379 | result.ups_status |= UPSSTATUS_OVER; |
| 416 | } else if (!strcmp(ptr, "TRIM")) { | 380 | } else if (!strcmp(ptr, "TRIM")) { |
| 417 | config->status |= UPSSTATUS_TRIM; | 381 | result.ups_status |= UPSSTATUS_TRIM; |
| 418 | } else if (!strcmp(ptr, "BOOST")) { | 382 | } else if (!strcmp(ptr, "BOOST")) { |
| 419 | config->status |= UPSSTATUS_BOOST; | 383 | result.ups_status |= UPSSTATUS_BOOST; |
| 420 | } else if (!strcmp(ptr, "CHRG")) { | 384 | } else if (!strcmp(ptr, "CHRG")) { |
| 421 | config->status |= UPSSTATUS_CHRG; | 385 | result.ups_status |= UPSSTATUS_CHRG; |
| 422 | } else if (!strcmp(ptr, "DISCHRG")) { | 386 | } else if (!strcmp(ptr, "DISCHRG")) { |
| 423 | config->status |= UPSSTATUS_DISCHRG; | 387 | result.ups_status |= UPSSTATUS_DISCHRG; |
| 424 | } else if (!strcmp(ptr, "ALARM")) { | 388 | } else if (!strcmp(ptr, "ALARM")) { |
| 425 | config->status |= UPSSTATUS_ALARM; | 389 | result.ups_status |= UPSSTATUS_ALARM; |
| 426 | } else { | 390 | } else { |
| 427 | config->status |= UPSSTATUS_UNKNOWN; | 391 | result.ups_status |= UPSSTATUS_UNKNOWN; |
| 428 | } | 392 | } |
| 429 | } | 393 | } |
| 430 | 394 | ||
| 431 | return OK; | 395 | return result; |
| 432 | } | 396 | } |
| 433 | 397 | ||
| 434 | /* gets a variable value for a specific UPS */ | 398 | /* gets a variable value for a specific UPS */ |
| 435 | int get_ups_variable(const char *varname, char *buf, const ups_config config) { | 399 | int get_ups_variable(const char *varname, char *buf, const check_ups_config config) { |
| 436 | char send_buffer[MAX_INPUT_BUFFER]; | 400 | char send_buffer[MAX_INPUT_BUFFER]; |
| 437 | 401 | ||
| 438 | /* create the command string to send to the UPS daemon */ | 402 | /* create the command string to send to the UPS daemon */ |
| @@ -500,7 +464,7 @@ int get_ups_variable(const char *varname, char *buf, const ups_config config) { | |||
| 500 | [-wv warn_value] [-cv crit_value] [-to to_sec] */ | 464 | [-wv warn_value] [-cv crit_value] [-to to_sec] */ |
| 501 | 465 | ||
| 502 | /* process command-line arguments */ | 466 | /* process command-line arguments */ |
| 503 | int process_arguments(int argc, char **argv, ups_config *config) { | 467 | check_ups_config_wrapper process_arguments(int argc, char **argv) { |
| 504 | 468 | ||
| 505 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | 469 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 506 | {"ups", required_argument, 0, 'u'}, | 470 | {"ups", required_argument, 0, 'u'}, |
| @@ -514,8 +478,14 @@ int process_arguments(int argc, char **argv, ups_config *config) { | |||
| 514 | {"help", no_argument, 0, 'h'}, | 478 | {"help", no_argument, 0, 'h'}, |
| 515 | {0, 0, 0, 0}}; | 479 | {0, 0, 0, 0}}; |
| 516 | 480 | ||
| 481 | check_ups_config_wrapper result = { | ||
| 482 | .errorcode = OK, | ||
| 483 | .config = check_ups_config_init(), | ||
| 484 | }; | ||
| 485 | |||
| 517 | if (argc < 2) { | 486 | if (argc < 2) { |
| 518 | return ERROR; | 487 | result.errorcode = ERROR; |
| 488 | return result; | ||
| 519 | } | 489 | } |
| 520 | 490 | ||
| 521 | int c; | 491 | int c; |
| @@ -542,52 +512,52 @@ int process_arguments(int argc, char **argv, ups_config *config) { | |||
| 542 | usage5(); | 512 | usage5(); |
| 543 | case 'H': /* hostname */ | 513 | case 'H': /* hostname */ |
| 544 | if (is_host(optarg)) { | 514 | if (is_host(optarg)) { |
| 545 | config->server_address = optarg; | 515 | result.config.server_address = optarg; |
| 546 | } else { | 516 | } else { |
| 547 | usage2(_("Invalid hostname/address"), optarg); | 517 | usage2(_("Invalid hostname/address"), optarg); |
| 548 | } | 518 | } |
| 549 | break; | 519 | break; |
| 550 | case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for | 520 | case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for |
| 551 | Fahrenheit) */ | 521 | Fahrenheit) */ |
| 552 | config->temp_output_c = true; | 522 | result.config.temp_output_c = true; |
| 553 | break; | 523 | break; |
| 554 | case 'u': /* ups name */ | 524 | case 'u': /* ups name */ |
| 555 | config->ups_name = optarg; | 525 | result.config.ups_name = optarg; |
| 556 | break; | 526 | break; |
| 557 | case 'p': /* port */ | 527 | case 'p': /* port */ |
| 558 | if (is_intpos(optarg)) { | 528 | if (is_intpos(optarg)) { |
| 559 | config->server_port = atoi(optarg); | 529 | result.config.server_port = atoi(optarg); |
| 560 | } else { | 530 | } else { |
| 561 | usage2(_("Port must be a positive integer"), optarg); | 531 | usage2(_("Port must be a positive integer"), optarg); |
| 562 | } | 532 | } |
| 563 | break; | 533 | break; |
| 564 | case 'c': /* critical time threshold */ | 534 | case 'c': /* critical time threshold */ |
| 565 | if (is_intnonneg(optarg)) { | 535 | if (is_intnonneg(optarg)) { |
| 566 | config->critical_value = atoi(optarg); | 536 | result.config.critical_value = atoi(optarg); |
| 567 | config->check_crit = true; | 537 | result.config.check_crit = true; |
| 568 | } else { | 538 | } else { |
| 569 | usage2(_("Critical time must be a positive integer"), optarg); | 539 | usage2(_("Critical time must be a positive integer"), optarg); |
| 570 | } | 540 | } |
| 571 | break; | 541 | break; |
| 572 | case 'w': /* warning time threshold */ | 542 | case 'w': /* warning time threshold */ |
| 573 | if (is_intnonneg(optarg)) { | 543 | if (is_intnonneg(optarg)) { |
| 574 | config->warning_value = atoi(optarg); | 544 | result.config.warning_value = atoi(optarg); |
| 575 | config->check_warn = true; | 545 | result.config.check_warn = true; |
| 576 | } else { | 546 | } else { |
| 577 | usage2(_("Warning time must be a positive integer"), optarg); | 547 | usage2(_("Warning time must be a positive integer"), optarg); |
| 578 | } | 548 | } |
| 579 | break; | 549 | break; |
| 580 | case 'v': /* variable */ | 550 | case 'v': /* variable */ |
| 581 | if (!strcmp(optarg, "LINE")) { | 551 | if (!strcmp(optarg, "LINE")) { |
| 582 | config->check_variable = UPS_UTILITY; | 552 | result.config.check_variable = UPS_UTILITY; |
| 583 | } else if (!strcmp(optarg, "TEMP")) { | 553 | } else if (!strcmp(optarg, "TEMP")) { |
| 584 | config->check_variable = UPS_TEMP; | 554 | result.config.check_variable = UPS_TEMP; |
| 585 | } else if (!strcmp(optarg, "BATTPCT")) { | 555 | } else if (!strcmp(optarg, "BATTPCT")) { |
| 586 | config->check_variable = UPS_BATTPCT; | 556 | result.config.check_variable = UPS_BATTPCT; |
| 587 | } else if (!strcmp(optarg, "LOADPCT")) { | 557 | } else if (!strcmp(optarg, "LOADPCT")) { |
| 588 | config->check_variable = UPS_LOADPCT; | 558 | result.config.check_variable = UPS_LOADPCT; |
| 589 | } else if (!strcmp(optarg, "REALPOWER")) { | 559 | } else if (!strcmp(optarg, "REALPOWER")) { |
| 590 | config->check_variable = UPS_REALPOWER; | 560 | result.config.check_variable = UPS_REALPOWER; |
| 591 | } else { | 561 | } else { |
| 592 | usage2(_("Unrecognized UPS variable"), optarg); | 562 | usage2(_("Unrecognized UPS variable"), optarg); |
| 593 | } | 563 | } |
| @@ -608,27 +578,27 @@ int process_arguments(int argc, char **argv, ups_config *config) { | |||
| 608 | } | 578 | } |
| 609 | } | 579 | } |
| 610 | 580 | ||
| 611 | if (config->server_address == NULL && argc > optind) { | 581 | if (result.config.server_address == NULL && argc > optind) { |
| 612 | if (is_host(argv[optind])) { | 582 | if (is_host(argv[optind])) { |
| 613 | config->server_address = argv[optind++]; | 583 | result.config.server_address = argv[optind++]; |
| 614 | } else { | 584 | } else { |
| 615 | usage2(_("Invalid hostname/address"), optarg); | 585 | usage2(_("Invalid hostname/address"), optarg); |
| 616 | } | 586 | } |
| 617 | } | 587 | } |
| 618 | 588 | ||
| 619 | if (config->server_address == NULL) { | 589 | if (result.config.server_address == NULL) { |
| 620 | config->server_address = strdup("127.0.0.1"); | 590 | result.config.server_address = strdup("127.0.0.1"); |
| 621 | } | 591 | } |
| 622 | 592 | ||
| 623 | return validate_arguments(*config); | 593 | return validate_arguments(result); |
| 624 | } | 594 | } |
| 625 | 595 | ||
| 626 | int validate_arguments(ups_config config) { | 596 | check_ups_config_wrapper validate_arguments(check_ups_config_wrapper config_wrapper) { |
| 627 | if (!config.ups_name) { | 597 | if (config_wrapper.config.ups_name) { |
| 628 | printf("%s\n", _("Error : no UPS indicated")); | 598 | printf("%s\n", _("Error : no UPS indicated")); |
| 629 | return ERROR; | 599 | config_wrapper.errorcode = ERROR; |
| 630 | } | 600 | } |
| 631 | return OK; | 601 | return config_wrapper; |
| 632 | } | 602 | } |
| 633 | 603 | ||
| 634 | void print_help(void) { | 604 | void print_help(void) { |
diff --git a/plugins/check_ups.d/config.h b/plugins/check_ups.d/config.h new file mode 100644 index 00000000..353104f2 --- /dev/null +++ b/plugins/check_ups.d/config.h | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include <stddef.h> | ||
| 5 | |||
| 6 | #define UPS_NONE 0 /* no supported options */ | ||
| 7 | #define UPS_UTILITY 1 /* supports utility line */ | ||
| 8 | #define UPS_BATTPCT 2 /* supports percent battery remaining */ | ||
| 9 | #define UPS_STATUS 4 /* supports UPS status */ | ||
| 10 | #define UPS_TEMP 8 /* supports UPS temperature */ | ||
| 11 | #define UPS_LOADPCT 16 /* supports load percent */ | ||
| 12 | #define UPS_REALPOWER 32 /* supports real power */ | ||
| 13 | |||
| 14 | #define UPSSTATUS_NONE 0 | ||
| 15 | #define UPSSTATUS_OFF 1 | ||
| 16 | #define UPSSTATUS_OL 2 | ||
| 17 | #define UPSSTATUS_OB 4 | ||
| 18 | #define UPSSTATUS_LB 8 | ||
| 19 | #define UPSSTATUS_CAL 16 | ||
| 20 | #define UPSSTATUS_RB 32 /*Replace Battery */ | ||
| 21 | #define UPSSTATUS_BYPASS 64 | ||
| 22 | #define UPSSTATUS_OVER 128 | ||
| 23 | #define UPSSTATUS_TRIM 256 | ||
| 24 | #define UPSSTATUS_BOOST 512 | ||
| 25 | #define UPSSTATUS_CHRG 1024 | ||
| 26 | #define UPSSTATUS_DISCHRG 2048 | ||
| 27 | #define UPSSTATUS_UNKNOWN 4096 | ||
| 28 | #define UPSSTATUS_ALARM 8192 | ||
| 29 | |||
| 30 | enum { | ||
| 31 | PORT = 3493 | ||
| 32 | }; | ||
| 33 | |||
| 34 | typedef struct ups_config { | ||
| 35 | unsigned int server_port; | ||
| 36 | char *server_address; | ||
| 37 | char *ups_name; | ||
| 38 | double warning_value; | ||
| 39 | double critical_value; | ||
| 40 | bool check_warn; | ||
| 41 | bool check_crit; | ||
| 42 | int check_variable; | ||
| 43 | bool temp_output_c; | ||
| 44 | } check_ups_config; | ||
| 45 | |||
| 46 | check_ups_config check_ups_config_init(void) { | ||
| 47 | check_ups_config tmp = {0}; | ||
| 48 | tmp.server_port = PORT; | ||
| 49 | tmp.server_address = NULL; | ||
| 50 | tmp.ups_name = NULL; | ||
| 51 | tmp.check_variable = UPS_NONE; | ||
| 52 | |||
| 53 | return tmp; | ||
| 54 | } | ||
| 55 | |||
