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