diff options
-rw-r--r-- | plugins/check_mysql_query.c | 38 |
1 files changed, 15 insertions, 23 deletions
diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c index 52753461..d4b67bef 100644 --- a/plugins/check_mysql_query.c +++ b/plugins/check_mysql_query.c | |||
@@ -50,7 +50,7 @@ static char *opt_file = NULL; | |||
50 | static char *opt_group = NULL; | 50 | static char *opt_group = NULL; |
51 | static unsigned int db_port = MYSQL_PORT; | 51 | static unsigned int db_port = MYSQL_PORT; |
52 | 52 | ||
53 | static int process_arguments(int, char **); | 53 | static int process_arguments(int /*argc*/, char ** /*argv*/); |
54 | static int validate_arguments(void); | 54 | static int validate_arguments(void); |
55 | static void print_help(void); | 55 | static void print_help(void); |
56 | void print_usage(void); | 56 | void print_usage(void); |
@@ -60,15 +60,6 @@ static int verbose = 0; | |||
60 | static thresholds *my_thresholds = NULL; | 60 | static thresholds *my_thresholds = NULL; |
61 | 61 | ||
62 | int main(int argc, char **argv) { | 62 | int main(int argc, char **argv) { |
63 | |||
64 | MYSQL mysql; | ||
65 | MYSQL_RES *res; | ||
66 | MYSQL_ROW row; | ||
67 | |||
68 | double value; | ||
69 | char *error = NULL; | ||
70 | int status; | ||
71 | |||
72 | setlocale(LC_ALL, ""); | 63 | setlocale(LC_ALL, ""); |
73 | bindtextdomain(PACKAGE, LOCALEDIR); | 64 | bindtextdomain(PACKAGE, LOCALEDIR); |
74 | textdomain(PACKAGE); | 65 | textdomain(PACKAGE); |
@@ -79,6 +70,7 @@ int main(int argc, char **argv) { | |||
79 | if (process_arguments(argc, argv) == ERROR) | 70 | if (process_arguments(argc, argv) == ERROR) |
80 | usage4(_("Could not parse arguments")); | 71 | usage4(_("Could not parse arguments")); |
81 | 72 | ||
73 | MYSQL mysql; | ||
82 | /* initialize mysql */ | 74 | /* initialize mysql */ |
83 | mysql_init(&mysql); | 75 | mysql_init(&mysql); |
84 | 76 | ||
@@ -106,12 +98,14 @@ int main(int argc, char **argv) { | |||
106 | die(STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error(&mysql)); | 98 | die(STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error(&mysql)); |
107 | } | 99 | } |
108 | 100 | ||
101 | char *error = NULL; | ||
109 | if (mysql_query(&mysql, sql_query) != 0) { | 102 | if (mysql_query(&mysql, sql_query) != 0) { |
110 | error = strdup(mysql_error(&mysql)); | 103 | error = strdup(mysql_error(&mysql)); |
111 | mysql_close(&mysql); | 104 | mysql_close(&mysql); |
112 | die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); | 105 | die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); |
113 | } | 106 | } |
114 | 107 | ||
108 | MYSQL_RES *res; | ||
115 | /* store the result */ | 109 | /* store the result */ |
116 | if ((res = mysql_store_result(&mysql)) == NULL) { | 110 | if ((res = mysql_store_result(&mysql)) == NULL) { |
117 | error = strdup(mysql_error(&mysql)); | 111 | error = strdup(mysql_error(&mysql)); |
@@ -125,6 +119,7 @@ int main(int argc, char **argv) { | |||
125 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned")); | 119 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned")); |
126 | } | 120 | } |
127 | 121 | ||
122 | MYSQL_ROW row; | ||
128 | /* fetch the first row */ | 123 | /* fetch the first row */ |
129 | if ((row = mysql_fetch_row(res)) == NULL) { | 124 | if ((row = mysql_fetch_row(res)) == NULL) { |
130 | error = strdup(mysql_error(&mysql)); | 125 | error = strdup(mysql_error(&mysql)); |
@@ -137,7 +132,7 @@ int main(int argc, char **argv) { | |||
137 | die(STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]); | 132 | die(STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]); |
138 | } | 133 | } |
139 | 134 | ||
140 | value = strtod(row[0], NULL); | 135 | double value = strtod(row[0], NULL); |
141 | 136 | ||
142 | /* free the result */ | 137 | /* free the result */ |
143 | mysql_free_result(res); | 138 | mysql_free_result(res); |
@@ -148,7 +143,7 @@ int main(int argc, char **argv) { | |||
148 | if (verbose >= 3) | 143 | if (verbose >= 3) |
149 | printf("mysql result: %f\n", value); | 144 | printf("mysql result: %f\n", value); |
150 | 145 | ||
151 | status = get_status(value, my_thresholds); | 146 | int status = get_status(value, my_thresholds); |
152 | 147 | ||
153 | if (status == STATE_OK) { | 148 | if (status == STATE_OK) { |
154 | printf("QUERY %s: ", _("OK")); | 149 | printf("QUERY %s: ", _("OK")); |
@@ -168,11 +163,6 @@ int main(int argc, char **argv) { | |||
168 | 163 | ||
169 | /* process command-line arguments */ | 164 | /* process command-line arguments */ |
170 | int process_arguments(int argc, char **argv) { | 165 | int process_arguments(int argc, char **argv) { |
171 | int c; | ||
172 | char *warning = NULL; | ||
173 | char *critical = NULL; | ||
174 | |||
175 | int option = 0; | ||
176 | static struct option longopts[] = { | 166 | static struct option longopts[] = { |
177 | {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'}, | 167 | {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'}, |
178 | {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'}, | 168 | {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'}, |
@@ -183,13 +173,17 @@ int process_arguments(int argc, char **argv) { | |||
183 | if (argc < 1) | 173 | if (argc < 1) |
184 | return ERROR; | 174 | return ERROR; |
185 | 175 | ||
186 | while (1) { | 176 | char *warning = NULL; |
187 | c = getopt_long(argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option); | 177 | char *critical = NULL; |
188 | 178 | ||
189 | if (c == -1 || c == EOF) | 179 | while (true) { |
180 | int option = 0; | ||
181 | int option_char = getopt_long(argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option); | ||
182 | |||
183 | if (option_char == -1 || option_char == EOF) | ||
190 | break; | 184 | break; |
191 | 185 | ||
192 | switch (c) { | 186 | switch (option_char) { |
193 | case 'H': /* hostname */ | 187 | case 'H': /* hostname */ |
194 | if (is_host(optarg)) { | 188 | if (is_host(optarg)) { |
195 | db_host = optarg; | 189 | db_host = optarg; |
@@ -247,8 +241,6 @@ int process_arguments(int argc, char **argv) { | |||
247 | } | 241 | } |
248 | } | 242 | } |
249 | 243 | ||
250 | c = optind; | ||
251 | |||
252 | set_thresholds(&my_thresholds, warning, critical); | 244 | set_thresholds(&my_thresholds, warning, critical); |
253 | 245 | ||
254 | return validate_arguments(); | 246 | return validate_arguments(); |