diff options
Diffstat (limited to 'plugins/check_mysql_query.c')
-rw-r--r-- | plugins/check_mysql_query.c | 156 |
1 files changed, 92 insertions, 64 deletions
diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c index 79b6e2f4..c7e84deb 100644 --- a/plugins/check_mysql_query.c +++ b/plugins/check_mysql_query.c | |||
@@ -37,27 +37,22 @@ const char *email = "devel@monitoring-plugins.org"; | |||
37 | #include "utils.h" | 37 | #include "utils.h" |
38 | #include "utils_base.h" | 38 | #include "utils_base.h" |
39 | #include "netutils.h" | 39 | #include "netutils.h" |
40 | #include "check_mysql_query.d/config.h" | ||
40 | 41 | ||
41 | #include <mysql.h> | 42 | #include <mysql.h> |
42 | #include <errmsg.h> | 43 | #include <errmsg.h> |
43 | 44 | ||
44 | static char *db_user = NULL; | 45 | typedef struct { |
45 | static char *db_host = NULL; | 46 | int errorcode; |
46 | static char *db_socket = NULL; | 47 | check_mysql_query_config config; |
47 | static char *db_pass = NULL; | 48 | } check_mysql_query_config_wrapper; |
48 | static char *db = NULL; | 49 | static check_mysql_query_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
49 | static char *opt_file = NULL; | 50 | static check_mysql_query_config_wrapper |
50 | static char *opt_group = NULL; | 51 | validate_arguments(check_mysql_query_config_wrapper /*config_wrapper*/); |
51 | static unsigned int db_port = MYSQL_PORT; | ||
52 | |||
53 | static int process_arguments(int /*argc*/, char ** /*argv*/); | ||
54 | static int validate_arguments(void); | ||
55 | static void print_help(void); | 52 | static void print_help(void); |
56 | void print_usage(void); | 53 | void print_usage(void); |
57 | 54 | ||
58 | static char *sql_query = NULL; | ||
59 | static int verbose = 0; | 55 | static int verbose = 0; |
60 | static thresholds *my_thresholds = NULL; | ||
61 | 56 | ||
62 | int main(int argc, char **argv) { | 57 | int main(int argc, char **argv) { |
63 | setlocale(LC_ALL, ""); | 58 | setlocale(LC_ALL, ""); |
@@ -67,39 +62,47 @@ int main(int argc, char **argv) { | |||
67 | /* Parse extra opts if any */ | 62 | /* Parse extra opts if any */ |
68 | argv = np_extra_opts(&argc, argv, progname); | 63 | argv = np_extra_opts(&argc, argv, progname); |
69 | 64 | ||
70 | if (process_arguments(argc, argv) == ERROR) | 65 | check_mysql_query_config_wrapper tmp_config = process_arguments(argc, argv); |
66 | if (tmp_config.errorcode == ERROR) { | ||
71 | usage4(_("Could not parse arguments")); | 67 | usage4(_("Could not parse arguments")); |
68 | } | ||
69 | |||
70 | const check_mysql_query_config config = tmp_config.config; | ||
72 | 71 | ||
73 | MYSQL mysql; | 72 | MYSQL mysql; |
74 | /* initialize mysql */ | 73 | /* initialize mysql */ |
75 | mysql_init(&mysql); | 74 | mysql_init(&mysql); |
76 | 75 | ||
77 | if (opt_file != NULL) | 76 | if (config.opt_file != NULL) { |
78 | mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, opt_file); | 77 | mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, config.opt_file); |
78 | } | ||
79 | 79 | ||
80 | if (opt_group != NULL) | 80 | if (config.opt_group != NULL) { |
81 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, opt_group); | 81 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, config.opt_group); |
82 | else | 82 | } else { |
83 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client"); | 83 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client"); |
84 | } | ||
84 | 85 | ||
85 | /* establish a connection to the server and error checking */ | 86 | /* establish a connection to the server and error checking */ |
86 | if (!mysql_real_connect(&mysql, db_host, db_user, db_pass, db, db_port, db_socket, 0)) { | 87 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, |
87 | if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) | 88 | config.db_port, config.db_socket, 0)) { |
89 | if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { | ||
88 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); | 90 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); |
89 | else if (mysql_errno(&mysql) == CR_VERSION_ERROR) | 91 | } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { |
90 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); | 92 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); |
91 | else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY) | 93 | } else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY) { |
92 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); | 94 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); |
93 | else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR) | 95 | } else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR) { |
94 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); | 96 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); |
95 | else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR) | 97 | } else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR) { |
96 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); | 98 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); |
97 | else | 99 | } else { |
98 | die(STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error(&mysql)); | 100 | die(STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error(&mysql)); |
101 | } | ||
99 | } | 102 | } |
100 | 103 | ||
101 | char *error = NULL; | 104 | char *error = NULL; |
102 | if (mysql_query(&mysql, sql_query) != 0) { | 105 | if (mysql_query(&mysql, config.sql_query) != 0) { |
103 | error = strdup(mysql_error(&mysql)); | 106 | error = strdup(mysql_error(&mysql)); |
104 | mysql_close(&mysql); | 107 | mysql_close(&mysql); |
105 | die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); | 108 | die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); |
@@ -140,10 +143,11 @@ int main(int argc, char **argv) { | |||
140 | /* close the connection */ | 143 | /* close the connection */ |
141 | mysql_close(&mysql); | 144 | mysql_close(&mysql); |
142 | 145 | ||
143 | if (verbose >= 3) | 146 | if (verbose >= 3) { |
144 | printf("mysql result: %f\n", value); | 147 | printf("mysql result: %f\n", value); |
148 | } | ||
145 | 149 | ||
146 | int status = get_status(value, my_thresholds); | 150 | int status = get_status(value, config.my_thresholds); |
147 | 151 | ||
148 | if (status == STATE_OK) { | 152 | if (status == STATE_OK) { |
149 | printf("QUERY %s: ", _("OK")); | 153 | printf("QUERY %s: ", _("OK")); |
@@ -152,26 +156,44 @@ int main(int argc, char **argv) { | |||
152 | } else if (status == STATE_CRITICAL) { | 156 | } else if (status == STATE_CRITICAL) { |
153 | printf("QUERY %s: ", _("CRITICAL")); | 157 | printf("QUERY %s: ", _("CRITICAL")); |
154 | } | 158 | } |
155 | printf(_("'%s' returned %f | %s"), sql_query, value, | 159 | printf(_("'%s' returned %f | %s"), config.sql_query, value, |
156 | fperfdata("result", value, "", my_thresholds->warning ? true : false, my_thresholds->warning ? my_thresholds->warning->end : 0, | 160 | fperfdata("result", value, "", config.my_thresholds->warning, |
157 | my_thresholds->critical ? true : false, my_thresholds->critical ? my_thresholds->critical->end : 0, false, 0, false, | 161 | config.my_thresholds->warning ? config.my_thresholds->warning->end : 0, |
158 | 0)); | 162 | config.my_thresholds->critical, |
163 | config.my_thresholds->critical ? config.my_thresholds->critical->end : 0, | ||
164 | false, 0, false, 0)); | ||
159 | printf("\n"); | 165 | printf("\n"); |
160 | 166 | ||
161 | return status; | 167 | return status; |
162 | } | 168 | } |
163 | 169 | ||
164 | /* process command-line arguments */ | 170 | /* process command-line arguments */ |
165 | int process_arguments(int argc, char **argv) { | 171 | check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { |
166 | static struct option longopts[] = { | 172 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
167 | {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'}, | 173 | {"socket", required_argument, 0, 's'}, |
168 | {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'}, | 174 | {"database", required_argument, 0, 'd'}, |
169 | {"group", required_argument, 0, 'g'}, {"port", required_argument, 0, 'P'}, {"verbose", no_argument, 0, 'v'}, | 175 | {"username", required_argument, 0, 'u'}, |
170 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"query", required_argument, 0, 'q'}, | 176 | {"password", required_argument, 0, 'p'}, |
171 | {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {0, 0, 0, 0}}; | 177 | {"file", required_argument, 0, 'f'}, |
172 | 178 | {"group", required_argument, 0, 'g'}, | |
173 | if (argc < 1) | 179 | {"port", required_argument, 0, 'P'}, |
174 | return ERROR; | 180 | {"verbose", no_argument, 0, 'v'}, |
181 | {"version", no_argument, 0, 'V'}, | ||
182 | {"help", no_argument, 0, 'h'}, | ||
183 | {"query", required_argument, 0, 'q'}, | ||
184 | {"warning", required_argument, 0, 'w'}, | ||
185 | {"critical", required_argument, 0, 'c'}, | ||
186 | {0, 0, 0, 0}}; | ||
187 | |||
188 | check_mysql_query_config_wrapper result = { | ||
189 | .errorcode = OK, | ||
190 | .config = check_mysql_query_config_init(), | ||
191 | }; | ||
192 | |||
193 | if (argc < 1) { | ||
194 | result.errorcode = ERROR; | ||
195 | return result; | ||
196 | } | ||
175 | 197 | ||
176 | char *warning = NULL; | 198 | char *warning = NULL; |
177 | char *critical = NULL; | 199 | char *critical = NULL; |
@@ -180,28 +202,29 @@ int process_arguments(int argc, char **argv) { | |||
180 | int option = 0; | 202 | int option = 0; |
181 | int option_char = getopt_long(argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option); | 203 | int option_char = getopt_long(argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option); |
182 | 204 | ||
183 | if (option_char == -1 || option_char == EOF) | 205 | if (option_char == -1 || option_char == EOF) { |
184 | break; | 206 | break; |
207 | } | ||
185 | 208 | ||
186 | switch (option_char) { | 209 | switch (option_char) { |
187 | case 'H': /* hostname */ | 210 | case 'H': /* hostname */ |
188 | if (is_host(optarg)) { | 211 | if (is_host(optarg)) { |
189 | db_host = optarg; | 212 | result.config.db_host = optarg; |
190 | } else { | 213 | } else { |
191 | usage2(_("Invalid hostname/address"), optarg); | 214 | usage2(_("Invalid hostname/address"), optarg); |
192 | } | 215 | } |
193 | break; | 216 | break; |
194 | case 's': /* socket */ | 217 | case 's': /* socket */ |
195 | db_socket = optarg; | 218 | result.config.db_socket = optarg; |
196 | break; | 219 | break; |
197 | case 'd': /* database */ | 220 | case 'd': /* database */ |
198 | db = optarg; | 221 | result.config.db = optarg; |
199 | break; | 222 | break; |
200 | case 'u': /* username */ | 223 | case 'u': /* username */ |
201 | db_user = optarg; | 224 | result.config.db_user = optarg; |
202 | break; | 225 | break; |
203 | case 'p': /* authentication information: password */ | 226 | case 'p': /* authentication information: password */ |
204 | db_pass = strdup(optarg); | 227 | result.config.db_pass = strdup(optarg); |
205 | 228 | ||
206 | /* Delete the password from process list */ | 229 | /* Delete the password from process list */ |
207 | while (*optarg != '\0') { | 230 | while (*optarg != '\0') { |
@@ -210,13 +233,13 @@ int process_arguments(int argc, char **argv) { | |||
210 | } | 233 | } |
211 | break; | 234 | break; |
212 | case 'f': /* client options file */ | 235 | case 'f': /* client options file */ |
213 | opt_file = optarg; | 236 | result.config.opt_file = optarg; |
214 | break; | 237 | break; |
215 | case 'g': /* client options group */ | 238 | case 'g': /* client options group */ |
216 | opt_group = optarg; | 239 | result.config.opt_group = optarg; |
217 | break; | 240 | break; |
218 | case 'P': /* critical time threshold */ | 241 | case 'P': /* critical time threshold */ |
219 | db_port = atoi(optarg); | 242 | result.config.db_port = atoi(optarg); |
220 | break; | 243 | break; |
221 | case 'v': | 244 | case 'v': |
222 | verbose++; | 245 | verbose++; |
@@ -228,7 +251,7 @@ int process_arguments(int argc, char **argv) { | |||
228 | print_help(); | 251 | print_help(); |
229 | exit(STATE_UNKNOWN); | 252 | exit(STATE_UNKNOWN); |
230 | case 'q': | 253 | case 'q': |
231 | xasprintf(&sql_query, "%s", optarg); | 254 | xasprintf(&result.config.sql_query, "%s", optarg); |
232 | break; | 255 | break; |
233 | case 'w': | 256 | case 'w': |
234 | warning = optarg; | 257 | warning = optarg; |
@@ -241,25 +264,30 @@ int process_arguments(int argc, char **argv) { | |||
241 | } | 264 | } |
242 | } | 265 | } |
243 | 266 | ||
244 | set_thresholds(&my_thresholds, warning, critical); | 267 | set_thresholds(&result.config.my_thresholds, warning, critical); |
245 | 268 | ||
246 | return validate_arguments(); | 269 | return validate_arguments(result); |
247 | } | 270 | } |
248 | 271 | ||
249 | int validate_arguments(void) { | 272 | check_mysql_query_config_wrapper |
250 | if (sql_query == NULL) | 273 | validate_arguments(check_mysql_query_config_wrapper config_wrapper) { |
274 | if (config_wrapper.config.sql_query == NULL) { | ||
251 | usage("Must specify a SQL query to run"); | 275 | usage("Must specify a SQL query to run"); |
276 | } | ||
252 | 277 | ||
253 | if (db_user == NULL) | 278 | if (config_wrapper.config.db_user == NULL) { |
254 | db_user = strdup(""); | 279 | config_wrapper.config.db_user = strdup(""); |
280 | } | ||
255 | 281 | ||
256 | if (db_host == NULL) | 282 | if (config_wrapper.config.db_host == NULL) { |
257 | db_host = strdup(""); | 283 | config_wrapper.config.db_host = strdup(""); |
284 | } | ||
258 | 285 | ||
259 | if (db == NULL) | 286 | if (config_wrapper.config.db == NULL) { |
260 | db = strdup(""); | 287 | config_wrapper.config.db = strdup(""); |
288 | } | ||
261 | 289 | ||
262 | return OK; | 290 | return config_wrapper; |
263 | } | 291 | } |
264 | 292 | ||
265 | void print_help(void) { | 293 | void print_help(void) { |