diff options
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/Makefile.am | 1 | ||||
| -rw-r--r-- | plugins/check_cluster.c | 149 | ||||
| -rw-r--r-- | plugins/check_cluster.d/config.h | 27 |
3 files changed, 101 insertions, 76 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index ce00c392..3ce24325 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -58,6 +58,7 @@ EXTRA_DIST = t \ | |||
| 58 | check_by_ssh.d \ | 58 | check_by_ssh.d \ |
| 59 | check_smtp.d \ | 59 | check_smtp.d \ |
| 60 | check_dig.d \ | 60 | check_dig.d \ |
| 61 | check_cluster.d \ | ||
| 61 | check_curl.d | 62 | check_curl.d |
| 62 | 63 | ||
| 63 | PLUGINHDRS = common.h | 64 | PLUGINHDRS = common.h |
diff --git a/plugins/check_cluster.c b/plugins/check_cluster.c index b40c38c7..9b695499 100644 --- a/plugins/check_cluster.c +++ b/plugins/check_cluster.c | |||
| @@ -29,42 +29,20 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 29 | #include "common.h" | 29 | #include "common.h" |
| 30 | #include "utils.h" | 30 | #include "utils.h" |
| 31 | #include "utils_base.h" | 31 | #include "utils_base.h" |
| 32 | 32 | #include "check_cluster.d/config.h" | |
| 33 | enum { | ||
| 34 | CHECK_SERVICES = 1, | ||
| 35 | CHECK_HOSTS = 2 | ||
| 36 | }; | ||
| 37 | 33 | ||
| 38 | static void print_help(void); | 34 | static void print_help(void); |
| 39 | void print_usage(void); | 35 | void print_usage(void); |
| 40 | 36 | ||
| 41 | static int total_services_ok = 0; | ||
| 42 | static int total_services_warning = 0; | ||
| 43 | static int total_services_unknown = 0; | ||
| 44 | static int total_services_critical = 0; | ||
| 45 | |||
| 46 | static int total_hosts_up = 0; | ||
| 47 | static int total_hosts_down = 0; | ||
| 48 | static int total_hosts_unreachable = 0; | ||
| 49 | |||
| 50 | static char *warn_threshold; | ||
| 51 | static char *crit_threshold; | ||
| 52 | |||
| 53 | static int check_type = CHECK_SERVICES; | ||
| 54 | |||
| 55 | static char *data_vals = NULL; | ||
| 56 | static char *label = NULL; | ||
| 57 | |||
| 58 | static int verbose = 0; | 37 | static int verbose = 0; |
| 59 | 38 | ||
| 60 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 39 | typedef struct { |
| 40 | int errorcode; | ||
| 41 | check_cluster_config config; | ||
| 42 | } check_cluster_config_wrapper; | ||
| 43 | static check_cluster_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 61 | 44 | ||
| 62 | int main(int argc, char **argv) { | 45 | int main(int argc, char **argv) { |
| 63 | char *ptr; | ||
| 64 | int data_val; | ||
| 65 | int return_code = STATE_OK; | ||
| 66 | thresholds *thresholds = NULL; | ||
| 67 | |||
| 68 | setlocale(LC_ALL, ""); | 46 | setlocale(LC_ALL, ""); |
| 69 | bindtextdomain(PACKAGE, LOCALEDIR); | 47 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 70 | textdomain(PACKAGE); | 48 | textdomain(PACKAGE); |
| @@ -72,20 +50,32 @@ int main(int argc, char **argv) { | |||
| 72 | /* Parse extra opts if any */ | 50 | /* Parse extra opts if any */ |
| 73 | argv = np_extra_opts(&argc, argv, progname); | 51 | argv = np_extra_opts(&argc, argv, progname); |
| 74 | 52 | ||
| 75 | if (process_arguments(argc, argv) == ERROR) | 53 | check_cluster_config_wrapper tmp_config = process_arguments(argc, argv); |
| 54 | if (tmp_config.errorcode == ERROR) { | ||
| 76 | usage(_("Could not parse arguments")); | 55 | usage(_("Could not parse arguments")); |
| 56 | } | ||
| 57 | |||
| 58 | const check_cluster_config config = tmp_config.config; | ||
| 77 | 59 | ||
| 78 | /* Initialize the thresholds */ | 60 | /* Initialize the thresholds */ |
| 79 | set_thresholds(&thresholds, warn_threshold, crit_threshold); | 61 | if (verbose) { |
| 80 | if (verbose) | 62 | print_thresholds("check_cluster", config.thresholds); |
| 81 | print_thresholds("check_cluster", thresholds); | 63 | } |
| 82 | 64 | ||
| 65 | int data_val; | ||
| 66 | int total_services_ok = 0; | ||
| 67 | int total_services_warning = 0; | ||
| 68 | int total_services_unknown = 0; | ||
| 69 | int total_services_critical = 0; | ||
| 70 | int total_hosts_up = 0; | ||
| 71 | int total_hosts_down = 0; | ||
| 72 | int total_hosts_unreachable = 0; | ||
| 83 | /* check the data values */ | 73 | /* check the data values */ |
| 84 | for (ptr = strtok(data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) { | 74 | for (char *ptr = strtok(config.data_vals, ","); ptr != NULL; ptr = strtok(NULL, ",")) { |
| 85 | 75 | ||
| 86 | data_val = atoi(ptr); | 76 | data_val = atoi(ptr); |
| 87 | 77 | ||
| 88 | if (check_type == CHECK_SERVICES) { | 78 | if (config.check_type == CHECK_SERVICES) { |
| 89 | switch (data_val) { | 79 | switch (data_val) { |
| 90 | case 0: | 80 | case 0: |
| 91 | total_services_ok++; | 81 | total_services_ok++; |
| @@ -119,101 +109,108 @@ int main(int argc, char **argv) { | |||
| 119 | } | 109 | } |
| 120 | } | 110 | } |
| 121 | 111 | ||
| 112 | int return_code = STATE_OK; | ||
| 122 | /* return the status of the cluster */ | 113 | /* return the status of the cluster */ |
| 123 | if (check_type == CHECK_SERVICES) { | 114 | if (config.check_type == CHECK_SERVICES) { |
| 124 | return_code = get_status(total_services_warning + total_services_unknown + total_services_critical, thresholds); | 115 | return_code = get_status(total_services_warning + total_services_unknown + total_services_critical, config.thresholds); |
| 125 | printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", state_text(return_code), | 116 | printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", state_text(return_code), |
| 126 | (label == NULL) ? "Service cluster" : label, total_services_ok, total_services_warning, total_services_unknown, | 117 | (config.label == NULL) ? "Service cluster" : config.label, total_services_ok, total_services_warning, total_services_unknown, |
| 127 | total_services_critical); | 118 | total_services_critical); |
| 128 | } else { | 119 | } else { |
| 129 | return_code = get_status(total_hosts_down + total_hosts_unreachable, thresholds); | 120 | return_code = get_status(total_hosts_down + total_hosts_unreachable, config.thresholds); |
| 130 | printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), (label == NULL) ? "Host cluster" : label, | 121 | printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), |
| 131 | total_hosts_up, total_hosts_down, total_hosts_unreachable); | 122 | (config.label == NULL) ? "Host cluster" : config.label, total_hosts_up, total_hosts_down, total_hosts_unreachable); |
| 132 | } | 123 | } |
| 133 | 124 | ||
| 134 | return return_code; | 125 | exit(return_code); |
| 135 | } | 126 | } |
| 136 | 127 | ||
| 137 | int process_arguments(int argc, char **argv) { | 128 | check_cluster_config_wrapper process_arguments(int argc, char **argv) { |
| 138 | int c; | ||
| 139 | char *ptr; | ||
| 140 | int option = 0; | ||
| 141 | static struct option longopts[] = {{"data", required_argument, 0, 'd'}, {"warning", required_argument, 0, 'w'}, | 129 | static struct option longopts[] = {{"data", required_argument, 0, 'd'}, {"warning", required_argument, 0, 'w'}, |
| 142 | {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, | 130 | {"critical", required_argument, 0, 'c'}, {"label", required_argument, 0, 'l'}, |
| 143 | {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, | 131 | {"host", no_argument, 0, 'h'}, {"service", no_argument, 0, 's'}, |
| 144 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 132 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, |
| 145 | {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; | 133 | {"help", no_argument, 0, 'H'}, {0, 0, 0, 0}}; |
| 146 | 134 | ||
| 147 | /* no options were supplied */ | 135 | check_cluster_config_wrapper result = { |
| 148 | if (argc < 2) | 136 | .errorcode = OK, |
| 149 | return ERROR; | 137 | .config = check_cluster_config_init(), |
| 138 | }; | ||
| 150 | 139 | ||
| 151 | while (1) { | 140 | /* no options were supplied */ |
| 141 | if (argc < 2) { | ||
| 142 | result.errorcode = ERROR; | ||
| 143 | return result; | ||
| 144 | } | ||
| 152 | 145 | ||
| 153 | c = getopt_long(argc, argv, "hHsvVw:c:d:l:", longopts, &option); | 146 | int option = 0; |
| 147 | char *warn_threshold = NULL; | ||
| 148 | char *crit_threshold = NULL; | ||
| 149 | while (true) { | ||
| 150 | int option_index = getopt_long(argc, argv, "hHsvVw:c:d:l:", longopts, &option); | ||
| 154 | 151 | ||
| 155 | if (c == -1 || c == EOF || c == 1) | 152 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
| 156 | break; | 153 | break; |
| 154 | } | ||
| 157 | 155 | ||
| 158 | switch (c) { | 156 | switch (option_index) { |
| 159 | |||
| 160 | case 'h': /* host cluster */ | 157 | case 'h': /* host cluster */ |
| 161 | check_type = CHECK_HOSTS; | 158 | result.config.check_type = CHECK_HOSTS; |
| 162 | break; | 159 | break; |
| 163 | |||
| 164 | case 's': /* service cluster */ | 160 | case 's': /* service cluster */ |
| 165 | check_type = CHECK_SERVICES; | 161 | result.config.check_type = CHECK_SERVICES; |
| 166 | break; | 162 | break; |
| 167 | |||
| 168 | case 'w': /* warning threshold */ | 163 | case 'w': /* warning threshold */ |
| 169 | warn_threshold = strdup(optarg); | 164 | warn_threshold = strdup(optarg); |
| 170 | break; | 165 | break; |
| 171 | |||
| 172 | case 'c': /* warning threshold */ | 166 | case 'c': /* warning threshold */ |
| 173 | crit_threshold = strdup(optarg); | 167 | crit_threshold = strdup(optarg); |
| 174 | break; | 168 | break; |
| 175 | |||
| 176 | case 'd': /* data values */ | 169 | case 'd': /* data values */ |
| 177 | data_vals = (char *)strdup(optarg); | 170 | result.config.data_vals = strdup(optarg); |
| 178 | /* validate data */ | 171 | /* validate data */ |
| 179 | for (ptr = data_vals; ptr != NULL; ptr += 2) { | 172 | for (char *ptr = result.config.data_vals; ptr != NULL; ptr += 2) { |
| 180 | if (ptr[0] < '0' || ptr[0] > '3') | 173 | if (ptr[0] < '0' || ptr[0] > '3') { |
| 181 | return ERROR; | 174 | result.errorcode = ERROR; |
| 182 | if (ptr[1] == '\0') | 175 | return result; |
| 176 | } | ||
| 177 | if (ptr[1] == '\0') { | ||
| 183 | break; | 178 | break; |
| 184 | if (ptr[1] != ',') | 179 | } |
| 185 | return ERROR; | 180 | if (ptr[1] != ',') { |
| 181 | result.errorcode = ERROR; | ||
| 182 | return result; | ||
| 183 | } | ||
| 186 | } | 184 | } |
| 187 | break; | 185 | break; |
| 188 | |||
| 189 | case 'l': /* text label */ | 186 | case 'l': /* text label */ |
| 190 | label = (char *)strdup(optarg); | 187 | result.config.label = strdup(optarg); |
| 191 | break; | 188 | break; |
| 192 | |||
| 193 | case 'v': /* verbose */ | 189 | case 'v': /* verbose */ |
| 194 | verbose++; | 190 | verbose++; |
| 195 | break; | 191 | break; |
| 196 | |||
| 197 | case 'V': /* version */ | 192 | case 'V': /* version */ |
| 198 | print_revision(progname, NP_VERSION); | 193 | print_revision(progname, NP_VERSION); |
| 199 | exit(STATE_UNKNOWN); | 194 | exit(STATE_UNKNOWN); |
| 200 | break; | 195 | break; |
| 201 | |||
| 202 | case 'H': /* help */ | 196 | case 'H': /* help */ |
| 203 | print_help(); | 197 | print_help(); |
| 204 | exit(STATE_UNKNOWN); | 198 | exit(STATE_UNKNOWN); |
| 205 | break; | 199 | break; |
| 206 | |||
| 207 | default: | 200 | default: |
| 208 | return ERROR; | 201 | result.errorcode = ERROR; |
| 202 | return result; | ||
| 209 | break; | 203 | break; |
| 210 | } | 204 | } |
| 211 | } | 205 | } |
| 212 | 206 | ||
| 213 | if (data_vals == NULL) | 207 | if (result.config.data_vals == NULL) { |
| 214 | return ERROR; | 208 | result.errorcode = ERROR; |
| 209 | return result; | ||
| 210 | } | ||
| 215 | 211 | ||
| 216 | return OK; | 212 | set_thresholds(&result.config.thresholds, warn_threshold, crit_threshold); |
| 213 | return result; | ||
| 217 | } | 214 | } |
| 218 | 215 | ||
| 219 | void print_help(void) { | 216 | void print_help(void) { |
diff --git a/plugins/check_cluster.d/config.h b/plugins/check_cluster.d/config.h new file mode 100644 index 00000000..fc386415 --- /dev/null +++ b/plugins/check_cluster.d/config.h | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include "../../lib/thresholds.h" | ||
| 5 | #include <stddef.h> | ||
| 6 | |||
| 7 | enum { | ||
| 8 | CHECK_SERVICES = 1, | ||
| 9 | CHECK_HOSTS = 2 | ||
| 10 | }; | ||
| 11 | |||
| 12 | typedef struct { | ||
| 13 | char *data_vals; | ||
| 14 | thresholds *thresholds; | ||
| 15 | int check_type; | ||
| 16 | char *label; | ||
| 17 | } check_cluster_config; | ||
| 18 | |||
| 19 | check_cluster_config check_cluster_config_init() { | ||
| 20 | check_cluster_config tmp = { | ||
| 21 | .data_vals = NULL, | ||
| 22 | .thresholds = NULL, | ||
| 23 | .check_type = CHECK_SERVICES, | ||
| 24 | .label = NULL, | ||
| 25 | }; | ||
| 26 | return tmp; | ||
| 27 | } | ||
