summaryrefslogtreecommitdiffstats
path: root/plugins/check_mysql.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-02-20 17:11:06 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-02-20 17:11:06 +0100
commit599c137fd3d522793dc022644b8b6566b75628cb (patch)
treeb4d82eda5550b74962baa3de7b224b13dfd410f1 /plugins/check_mysql.c
parentb38dec3e9b45efa6a6631acc38ada853e69fc547 (diff)
downloadmonitoring-plugins-599c137fd3d522793dc022644b8b6566b75628cb.tar.gz
clang-format
Diffstat (limited to 'plugins/check_mysql.c')
-rw-r--r--plugins/check_mysql.c603
1 files changed, 282 insertions, 321 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index 8a73772d..09c0b7dd 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -1,34 +1,34 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_mysql plugin 3 * Monitoring check_mysql plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) 6 * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
7* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) 7 * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
8* Copyright (c) 1999-2024 Monitoring Plugins Development Team 8 * Copyright (c) 1999-2024 Monitoring Plugins Development Team
9* 9 *
10* Description: 10 * Description:
11* 11 *
12* This file contains the check_mysql plugin 12 * This file contains the check_mysql plugin
13* 13 *
14* This program tests connections to a mysql server 14 * This program tests connections to a mysql server
15* 15 *
16* 16 *
17* This program is free software: you can redistribute it and/or modify 17 * This program is free software: you can redistribute it and/or modify
18* it under the terms of the GNU General Public License as published by 18 * it under the terms of the GNU General Public License as published by
19* the Free Software Foundation, either version 3 of the License, or 19 * the Free Software Foundation, either version 3 of the License, or
20* (at your option) any later version. 20 * (at your option) any later version.
21* 21 *
22* This program is distributed in the hope that it will be useful, 22 * This program is distributed in the hope that it will be useful,
23* but WITHOUT ANY WARRANTY; without even the implied warranty of 23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25* GNU General Public License for more details. 25 * GNU General Public License for more details.
26* 26 *
27* You should have received a copy of the GNU General Public License 27 * You should have received a copy of the GNU General Public License
28* along with this program. If not, see <http://www.gnu.org/licenses/>. 28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29* 29 *
30* 30 *
31*****************************************************************************/ 31 *****************************************************************************/
32 32
33const char *progname = "check_mysql"; 33const char *progname = "check_mysql";
34const char *copyright = "1999-2024"; 34const char *copyright = "1999-2024";
@@ -68,39 +68,24 @@ static double critical_time = 0;
68 68
69#define LENGTH_METRIC_UNIT 6 69#define LENGTH_METRIC_UNIT 6
70static const char *metric_unit[LENGTH_METRIC_UNIT] = { 70static const char *metric_unit[LENGTH_METRIC_UNIT] = {
71 "Open_files", 71 "Open_files", "Open_tables", "Qcache_free_memory", "Qcache_queries_in_cache", "Threads_connected", "Threads_running"};
72 "Open_tables",
73 "Qcache_free_memory",
74 "Qcache_queries_in_cache",
75 "Threads_connected",
76 "Threads_running"
77};
78 72
79#define LENGTH_METRIC_COUNTER 9 73#define LENGTH_METRIC_COUNTER 9
80static const char *metric_counter[LENGTH_METRIC_COUNTER] = { 74static const char *metric_counter[LENGTH_METRIC_COUNTER] = {
81 "Connections", 75 "Connections", "Qcache_hits", "Qcache_inserts", "Qcache_lowmem_prunes", "Qcache_not_cached", "Queries",
82 "Qcache_hits", 76 "Questions", "Table_locks_waited", "Uptime"};
83 "Qcache_inserts", 77
84 "Qcache_lowmem_prunes", 78#define MYSQLDUMP_THREADS_QUERY \
85 "Qcache_not_cached", 79 "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE 'SELECT /*!40001 SQL_NO_CACHE */%'"
86 "Queries",
87 "Questions",
88 "Table_locks_waited",
89 "Uptime"
90};
91
92#define MYSQLDUMP_THREADS_QUERY "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE 'SELECT /*!40001 SQL_NO_CACHE */%'"
93 80
94static thresholds *my_threshold = NULL; 81static thresholds *my_threshold = NULL;
95 82
96static int process_arguments (int, char **); 83static int process_arguments(int, char **);
97static int validate_arguments (void); 84static int validate_arguments(void);
98static void print_help (void); 85static void print_help(void);
99void print_usage (void); 86void print_usage(void);
100 87
101int 88int main(int argc, char **argv) {
102main (int argc, char **argv)
103{
104 89
105 MYSQL mysql; 90 MYSQL mysql;
106 MYSQL_RES *res; 91 MYSQL_RES *res;
@@ -110,152 +95,152 @@ main (int argc, char **argv)
110 95
111 char *result = NULL; 96 char *result = NULL;
112 char *error = NULL; 97 char *error = NULL;
113 char slaveresult[SLAVERESULTSIZE] = { 0 }; 98 char slaveresult[SLAVERESULTSIZE] = {0};
114 char* perf; 99 char *perf;
115 100
116 perf = strdup (""); 101 perf = strdup("");
117 102
118 setlocale (LC_ALL, ""); 103 setlocale(LC_ALL, "");
119 bindtextdomain (PACKAGE, LOCALEDIR); 104 bindtextdomain(PACKAGE, LOCALEDIR);
120 textdomain (PACKAGE); 105 textdomain(PACKAGE);
121 106
122 /* Parse extra opts if any */ 107 /* Parse extra opts if any */
123 argv=np_extra_opts (&argc, argv, progname); 108 argv = np_extra_opts(&argc, argv, progname);
124 109
125 if (process_arguments (argc, argv) == ERROR) 110 if (process_arguments(argc, argv) == ERROR) {
126 usage4 (_("Could not parse arguments")); 111 usage4(_("Could not parse arguments"));
112 }
127 113
128 /* initialize mysql */ 114 /* initialize mysql */
129 mysql_init (&mysql); 115 mysql_init(&mysql);
130 116
131 if (opt_file != NULL) 117 if (opt_file != NULL) {
132 mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file); 118 mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, opt_file);
133 119 }
134 if (opt_group != NULL) 120
135 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group); 121 if (opt_group != NULL) {
136 else 122 mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, opt_group);
137 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); 123 } else {
138 124 mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client");
139 if (ssl) 125 }
140 mysql_ssl_set(&mysql,key,cert,ca_cert,ca_dir,ciphers); 126
127 if (ssl) {
128 mysql_ssl_set(&mysql, key, cert, ca_cert, ca_dir, ciphers);
129 }
141 /* establish a connection to the server and error checking */ 130 /* establish a connection to the server and error checking */
142 if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) { 131 if (!mysql_real_connect(&mysql, db_host, db_user, db_pass, db, db_port, db_socket, 0)) {
143 /* Depending on internally-selected auth plugin MySQL might return */ 132 /* Depending on internally-selected auth plugin MySQL might return */
144 /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */ 133 /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */
145 /* Semantically these errors are the same. */ 134 /* Semantically these errors are the same. */
146 if (ignore_auth && (mysql_errno (&mysql) == ER_ACCESS_DENIED_ERROR || mysql_errno (&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) 135 if (ignore_auth && (mysql_errno(&mysql) == ER_ACCESS_DENIED_ERROR || mysql_errno(&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) {
147 { 136 printf("MySQL OK - Version: %s (protocol %d)\n", mysql_get_server_info(&mysql), mysql_get_proto_info(&mysql));
148 printf("MySQL OK - Version: %s (protocol %d)\n", 137 mysql_close(&mysql);
149 mysql_get_server_info(&mysql),
150 mysql_get_proto_info(&mysql)
151 );
152 mysql_close (&mysql);
153 return STATE_OK; 138 return STATE_OK;
139 } else if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) {
140 die(STATE_WARNING, "%s\n", mysql_error(&mysql));
141 } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) {
142 die(STATE_WARNING, "%s\n", mysql_error(&mysql));
143 } else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY) {
144 die(STATE_WARNING, "%s\n", mysql_error(&mysql));
145 } else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR) {
146 die(STATE_WARNING, "%s\n", mysql_error(&mysql));
147 } else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR) {
148 die(STATE_WARNING, "%s\n", mysql_error(&mysql));
149 } else {
150 die(STATE_CRITICAL, "%s\n", mysql_error(&mysql));
154 } 151 }
155 else if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
156 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
157 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
158 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
159 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
160 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
161 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
162 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
163 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
164 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
165 else
166 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
167 } 152 }
168 153
169 /* get the server stats */ 154 /* get the server stats */
170 result = strdup (mysql_stat (&mysql)); 155 result = strdup(mysql_stat(&mysql));
171 156
172 /* error checking once more */ 157 /* error checking once more */
173 if (mysql_error (&mysql)) { 158 if (mysql_error(&mysql)) {
174 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) 159 if (mysql_errno(&mysql) == CR_SERVER_GONE_ERROR) {
175 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); 160 die(STATE_CRITICAL, "%s\n", mysql_error(&mysql));
176 else if (mysql_errno (&mysql) == CR_SERVER_LOST) 161 } else if (mysql_errno(&mysql) == CR_SERVER_LOST) {
177 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); 162 die(STATE_CRITICAL, "%s\n", mysql_error(&mysql));
178 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) 163 } else if (mysql_errno(&mysql) == CR_UNKNOWN_ERROR) {
179 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); 164 die(STATE_CRITICAL, "%s\n", mysql_error(&mysql));
165 }
180 } 166 }
181 167
182 /* try to fetch some perf data */ 168 /* try to fetch some perf data */
183 if (mysql_query (&mysql, "show global status") == 0) { 169 if (mysql_query(&mysql, "show global status") == 0) {
184 if ( (res = mysql_store_result (&mysql)) == NULL) { 170 if ((res = mysql_store_result(&mysql)) == NULL) {
185 error = strdup(mysql_error(&mysql)); 171 error = strdup(mysql_error(&mysql));
186 mysql_close (&mysql); 172 mysql_close(&mysql);
187 die (STATE_CRITICAL, _("status store_result error: %s\n"), error); 173 die(STATE_CRITICAL, _("status store_result error: %s\n"), error);
188 } 174 }
189 175
190 while ( (row = mysql_fetch_row (res)) != NULL) { 176 while ((row = mysql_fetch_row(res)) != NULL) {
191 int i; 177 int i;
192 178
193 for(i = 0; i < LENGTH_METRIC_UNIT; i++) { 179 for (i = 0; i < LENGTH_METRIC_UNIT; i++) {
194 if (strcmp(row[0], metric_unit[i]) == 0) { 180 if (strcmp(row[0], metric_unit[i]) == 0) {
195 xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], 181 xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], atol(row[1]), "", false, 0, false, 0, false, 0, false, 0));
196 atol(row[1]), "", false, 0, false, 0, false, 0, false, 0));
197 continue; 182 continue;
198 } 183 }
199 } 184 }
200 for(i = 0; i < LENGTH_METRIC_COUNTER; i++) { 185 for (i = 0; i < LENGTH_METRIC_COUNTER; i++) {
201 if (strcmp(row[0], metric_counter[i]) == 0) { 186 if (strcmp(row[0], metric_counter[i]) == 0) {
202 xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], 187 xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], atol(row[1]), "c", false, 0, false, 0, false, 0, false, 0));
203 atol(row[1]), "c", false, 0, false, 0, false, 0, false, 0));
204 continue; 188 continue;
205 } 189 }
206 } 190 }
207 } 191 }
208 /* remove trailing space */ 192 /* remove trailing space */
209 if (strlen(perf) > 0) 193 if (strlen(perf) > 0) {
210 perf[strlen(perf) - 1] = '\0'; 194 perf[strlen(perf) - 1] = '\0';
195 }
211 } 196 }
212 197
213 if(check_slave) { 198 if (check_slave) {
214 /* check the slave status */ 199 /* check the slave status */
215 if (mysql_query (&mysql, "show slave status") != 0) { 200 if (mysql_query(&mysql, "show slave status") != 0) {
216 error = strdup(mysql_error(&mysql)); 201 error = strdup(mysql_error(&mysql));
217 mysql_close (&mysql); 202 mysql_close(&mysql);
218 die (STATE_CRITICAL, _("slave query error: %s\n"), error); 203 die(STATE_CRITICAL, _("slave query error: %s\n"), error);
219 } 204 }
220 205
221 /* store the result */ 206 /* store the result */
222 if ( (res = mysql_store_result (&mysql)) == NULL) { 207 if ((res = mysql_store_result(&mysql)) == NULL) {
223 error = strdup(mysql_error(&mysql)); 208 error = strdup(mysql_error(&mysql));
224 mysql_close (&mysql); 209 mysql_close(&mysql);
225 die (STATE_CRITICAL, _("slave store_result error: %s\n"), error); 210 die(STATE_CRITICAL, _("slave store_result error: %s\n"), error);
226 } 211 }
227 212
228 /* Check there is some data */ 213 /* Check there is some data */
229 if (mysql_num_rows(res) == 0) { 214 if (mysql_num_rows(res) == 0) {
230 mysql_close(&mysql); 215 mysql_close(&mysql);
231 die (STATE_WARNING, "%s\n", _("No slaves defined")); 216 die(STATE_WARNING, "%s\n", _("No slaves defined"));
232 } 217 }
233 218
234 /* fetch the first row */ 219 /* fetch the first row */
235 if ( (row = mysql_fetch_row (res)) == NULL) { 220 if ((row = mysql_fetch_row(res)) == NULL) {
236 error = strdup(mysql_error(&mysql)); 221 error = strdup(mysql_error(&mysql));
237 mysql_free_result (res); 222 mysql_free_result(res);
238 mysql_close (&mysql); 223 mysql_close(&mysql);
239 die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error); 224 die(STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
240 } 225 }
241 226
242 if (mysql_field_count (&mysql) == 12) { 227 if (mysql_field_count(&mysql) == 12) {
243 /* mysql 3.23.x */ 228 /* mysql 3.23.x */
244 snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]); 229 snprintf(slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
245 if (strcmp (row[6], "Yes") != 0) { 230 if (strcmp(row[6], "Yes") != 0) {
246 mysql_free_result (res); 231 mysql_free_result(res);
247 mysql_close (&mysql); 232 mysql_close(&mysql);
248 die (STATE_CRITICAL, "%s\n", slaveresult); 233 die(STATE_CRITICAL, "%s\n", slaveresult);
249 } 234 }
250 235
251 } else { 236 } else {
252 /* mysql 4.x.x and mysql 5.x.x */ 237 /* mysql 4.x.x and mysql 5.x.x */
253 int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields; 238 int slave_io_field = -1, slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
254 MYSQL_FIELD* fields; 239 MYSQL_FIELD *fields;
255 240
256 num_fields = mysql_num_fields(res); 241 num_fields = mysql_num_fields(res);
257 fields = mysql_fetch_fields(res); 242 fields = mysql_fetch_fields(res);
258 for(i = 0; i < num_fields; i++) { 243 for (i = 0; i < num_fields; i++) {
259 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { 244 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
260 slave_io_field = i; 245 slave_io_field = i;
261 continue; 246 continue;
@@ -272,60 +257,59 @@ main (int argc, char **argv)
272 257
273 /* Check if slave status is available */ 258 /* Check if slave status is available */
274 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) { 259 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
275 mysql_free_result (res); 260 mysql_free_result(res);
276 mysql_close (&mysql); 261 mysql_close(&mysql);
277 die (STATE_CRITICAL, "Slave status unavailable\n"); 262 die(STATE_CRITICAL, "Slave status unavailable\n");
278 } 263 }
279 264
280 /* Save slave status in slaveresult */ 265 /* Save slave status in slaveresult */
281 snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], seconds_behind_field!=-1?row[seconds_behind_field]:"Unknown"); 266 snprintf(slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field],
267 row[slave_sql_field], seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown");
282 268
283 /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */ 269 /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */
284 if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) { 270 if (strcmp(row[slave_io_field], "Yes") != 0 || strcmp(row[slave_sql_field], "Yes") != 0) {
285 MYSQL_RES *res_mysqldump; 271 MYSQL_RES *res_mysqldump;
286 MYSQL_ROW row_mysqldump; 272 MYSQL_ROW row_mysqldump;
287 unsigned int mysqldump_threads = 0; 273 unsigned int mysqldump_threads = 0;
288 274
289 if (mysql_query (&mysql, MYSQLDUMP_THREADS_QUERY) == 0) { 275 if (mysql_query(&mysql, MYSQLDUMP_THREADS_QUERY) == 0) {
290 /* store the result */ 276 /* store the result */
291 if ( (res_mysqldump = mysql_store_result (&mysql)) != NULL) { 277 if ((res_mysqldump = mysql_store_result(&mysql)) != NULL) {
292 if (mysql_num_rows(res_mysqldump) == 1) { 278 if (mysql_num_rows(res_mysqldump) == 1) {
293 if ( (row_mysqldump = mysql_fetch_row (res_mysqldump)) != NULL) { 279 if ((row_mysqldump = mysql_fetch_row(res_mysqldump)) != NULL) {
294 mysqldump_threads = atoi(row_mysqldump[0]); 280 mysqldump_threads = atoi(row_mysqldump[0]);
295 } 281 }
296 } 282 }
297 /* free the result */ 283 /* free the result */
298 mysql_free_result (res_mysqldump); 284 mysql_free_result(res_mysqldump);
299 } 285 }
300 mysql_close (&mysql); 286 mysql_close(&mysql);
301 } 287 }
302 if (mysqldump_threads == 0) { 288 if (mysqldump_threads == 0) {
303 die (STATE_CRITICAL, "%s\n", slaveresult); 289 die(STATE_CRITICAL, "%s\n", slaveresult);
304 } else { 290 } else {
305 strncat(slaveresult, " Mysqldump: in progress", SLAVERESULTSIZE-1); 291 strncat(slaveresult, " Mysqldump: in progress", SLAVERESULTSIZE - 1);
306 } 292 }
307 } 293 }
308 294
309 if (verbose >=3) { 295 if (verbose >= 3) {
310 if (seconds_behind_field == -1) { 296 if (seconds_behind_field == -1) {
311 printf("seconds_behind_field not found\n"); 297 printf("seconds_behind_field not found\n");
312 } else { 298 } else {
313 printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]); 299 printf("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
314 } 300 }
315 } 301 }
316 302
317 /* Check Seconds Behind against threshold */ 303 /* Check Seconds Behind against threshold */
318 if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && strcmp (row[seconds_behind_field], "NULL") != 0)) { 304 if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && strcmp(row[seconds_behind_field], "NULL") != 0)) {
319 double value = atof(row[seconds_behind_field]); 305 double value = atof(row[seconds_behind_field]);
320 int status; 306 int status;
321 307
322 status = get_status(value, my_threshold); 308 status = get_status(value, my_threshold);
323 309
324 xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s", 310 xasprintf(&perf, "%s %s", perf,
325 true, (double) warning_time, 311 fperfdata("seconds behind master", value, "s", true, (double)warning_time, true, (double)critical_time, false, 0,
326 true, (double) critical_time, 312 false, 0));
327 false, 0,
328 false, 0));
329 313
330 if (status == STATE_WARNING) { 314 if (status == STATE_WARNING) {
331 printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf); 315 printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf);
@@ -338,82 +322,66 @@ main (int argc, char **argv)
338 } 322 }
339 323
340 /* free the result */ 324 /* free the result */
341 mysql_free_result (res); 325 mysql_free_result(res);
342 } 326 }
343 327
344 /* close the connection */ 328 /* close the connection */
345 mysql_close (&mysql); 329 mysql_close(&mysql);
346 330
347 /* print out the result of stats */ 331 /* print out the result of stats */
348 if (check_slave) { 332 if (check_slave) {
349 printf ("%s %s|%s\n", result, slaveresult, perf); 333 printf("%s %s|%s\n", result, slaveresult, perf);
350 } else { 334 } else {
351 printf ("%s|%s\n", result, perf); 335 printf("%s|%s\n", result, perf);
352 } 336 }
353 337
354 return STATE_OK; 338 return STATE_OK;
355} 339}
356 340
357
358/* process command-line arguments */ 341/* process command-line arguments */
359int 342int process_arguments(int argc, char **argv) {
360process_arguments (int argc, char **argv)
361{
362 int c; 343 int c;
363 char *warning = NULL; 344 char *warning = NULL;
364 char *critical = NULL; 345 char *critical = NULL;
365 346
366 int option = 0; 347 int option = 0;
367 static struct option longopts[] = { 348 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'},
368 {"hostname", required_argument, 0, 'H'}, 349 {"database", required_argument, 0, 'd'}, {"username", required_argument, 0, 'u'},
369 {"socket", required_argument, 0, 's'}, 350 {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'},
370 {"database", required_argument, 0, 'd'}, 351 {"group", required_argument, 0, 'g'}, {"port", required_argument, 0, 'P'},
371 {"username", required_argument, 0, 'u'}, 352 {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'},
372 {"password", required_argument, 0, 'p'}, 353 {"check-slave", no_argument, 0, 'S'}, {"ignore-auth", no_argument, 0, 'n'},
373 {"file", required_argument, 0, 'f'}, 354 {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'},
374 {"group", required_argument, 0, 'g'}, 355 {"help", no_argument, 0, 'h'}, {"ssl", no_argument, 0, 'l'},
375 {"port", required_argument, 0, 'P'}, 356 {"ca-cert", optional_argument, 0, 'C'}, {"key", required_argument, 0, 'k'},
376 {"critical", required_argument, 0, 'c'}, 357 {"cert", required_argument, 0, 'a'}, {"ca-dir", required_argument, 0, 'D'},
377 {"warning", required_argument, 0, 'w'}, 358 {"ciphers", required_argument, 0, 'L'}, {0, 0, 0, 0}};
378 {"check-slave", no_argument, 0, 'S'}, 359
379 {"ignore-auth", no_argument, 0, 'n'}, 360 if (argc < 1) {
380 {"verbose", no_argument, 0, 'v'},
381 {"version", no_argument, 0, 'V'},
382 {"help", no_argument, 0, 'h'},
383 {"ssl", no_argument, 0, 'l'},
384 {"ca-cert", optional_argument, 0, 'C'},
385 {"key", required_argument,0,'k'},
386 {"cert", required_argument,0,'a'},
387 {"ca-dir", required_argument, 0, 'D'},
388 {"ciphers", required_argument, 0, 'L'},
389 {0, 0, 0, 0}
390 };
391
392 if (argc < 1)
393 return ERROR; 361 return ERROR;
362 }
394 363
395 while (1) { 364 while (1) {
396 c = getopt_long (argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option); 365 c = getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option);
397 366
398 if (c == -1 || c == EOF) 367 if (c == -1 || c == EOF) {
399 break; 368 break;
369 }
400 370
401 switch (c) { 371 switch (c) {
402 case 'H': /* hostname */ 372 case 'H': /* hostname */
403 if (is_host (optarg)) { 373 if (is_host(optarg)) {
404 db_host = optarg; 374 db_host = optarg;
405 } 375 } else if (*optarg == '/') {
406 else if (*optarg == '/') {
407 db_socket = optarg; 376 db_socket = optarg;
408 } 377 } else {
409 else { 378 usage2(_("Invalid hostname/address"), optarg);
410 usage2 (_("Invalid hostname/address"), optarg);
411 } 379 }
412 break; 380 break;
413 case 's': /* socket */ 381 case 's': /* socket */
414 db_socket = optarg; 382 db_socket = optarg;
415 break; 383 break;
416 case 'd': /* database */ 384 case 'd': /* database */
417 db = optarg; 385 db = optarg;
418 break; 386 break;
419 case 'l': 387 case 'l':
@@ -434,10 +402,10 @@ process_arguments (int argc, char **argv)
434 case 'L': 402 case 'L':
435 ciphers = optarg; 403 ciphers = optarg;
436 break; 404 break;
437 case 'u': /* username */ 405 case 'u': /* username */
438 db_user = optarg; 406 db_user = optarg;
439 break; 407 break;
440 case 'p': /* authentication information: password */ 408 case 'p': /* authentication information: password */
441 db_pass = strdup(optarg); 409 db_pass = strdup(optarg);
442 410
443 /* Delete the password from process list */ 411 /* Delete the password from process list */
@@ -446,40 +414,40 @@ process_arguments (int argc, char **argv)
446 optarg++; 414 optarg++;
447 } 415 }
448 break; 416 break;
449 case 'f': /* client options file */ 417 case 'f': /* client options file */
450 opt_file = optarg; 418 opt_file = optarg;
451 break; 419 break;
452 case 'g': /* client options group */ 420 case 'g': /* client options group */
453 opt_group = optarg; 421 opt_group = optarg;
454 break; 422 break;
455 case 'P': /* critical time threshold */ 423 case 'P': /* critical time threshold */
456 db_port = atoi (optarg); 424 db_port = atoi(optarg);
457 break; 425 break;
458 case 'S': 426 case 'S':
459 check_slave = true; /* check-slave */ 427 check_slave = true; /* check-slave */
460 break; 428 break;
461 case 'n': 429 case 'n':
462 ignore_auth = true; /* ignore-auth */ 430 ignore_auth = true; /* ignore-auth */
463 break; 431 break;
464 case 'w': 432 case 'w':
465 warning = optarg; 433 warning = optarg;
466 warning_time = strtod (warning, NULL); 434 warning_time = strtod(warning, NULL);
467 break; 435 break;
468 case 'c': 436 case 'c':
469 critical = optarg; 437 critical = optarg;
470 critical_time = strtod (critical, NULL); 438 critical_time = strtod(critical, NULL);
471 break; 439 break;
472 case 'V': /* version */ 440 case 'V': /* version */
473 print_revision (progname, NP_VERSION); 441 print_revision(progname, NP_VERSION);
474 exit (STATE_UNKNOWN); 442 exit(STATE_UNKNOWN);
475 case 'h': /* help */ 443 case 'h': /* help */
476 print_help (); 444 print_help();
477 exit (STATE_UNKNOWN); 445 exit(STATE_UNKNOWN);
478 case 'v': 446 case 'v':
479 verbose++; 447 verbose++;
480 break; 448 break;
481 case '?': /* help */ 449 case '?': /* help */
482 usage5 (); 450 usage5();
483 } 451 }
484 } 452 }
485 453
@@ -487,126 +455,119 @@ process_arguments (int argc, char **argv)
487 455
488 set_thresholds(&my_threshold, warning, critical); 456 set_thresholds(&my_threshold, warning, critical);
489 457
490 while ( argc > c ) { 458 while (argc > c) {
491 459
492 if (db_host == NULL) 460 if (db_host == NULL) {
493 if (is_host (argv[c])) { 461 if (is_host(argv[c])) {
494 db_host = argv[c++]; 462 db_host = argv[c++];
463 } else {
464 usage2(_("Invalid hostname/address"), argv[c]);
495 } 465 }
496 else { 466 } else if (db_user == NULL) {
497 usage2 (_("Invalid hostname/address"), argv[c]);
498 }
499 else if (db_user == NULL)
500 db_user = argv[c++]; 467 db_user = argv[c++];
501 else if (db_pass == NULL) 468 } else if (db_pass == NULL) {
502 db_pass = argv[c++]; 469 db_pass = argv[c++];
503 else if (db == NULL) 470 } else if (db == NULL) {
504 db = argv[c++]; 471 db = argv[c++];
505 else if (is_intnonneg (argv[c])) 472 } else if (is_intnonneg(argv[c])) {
506 db_port = atoi (argv[c++]); 473 db_port = atoi(argv[c++]);
507 else 474 } else {
508 break; 475 break;
476 }
509 } 477 }
510 478
511 return validate_arguments (); 479 return validate_arguments();
512} 480}
513 481
514 482int validate_arguments(void) {
515int 483 if (db_user == NULL) {
516validate_arguments (void)
517{
518 if (db_user == NULL)
519 db_user = strdup(""); 484 db_user = strdup("");
485 }
520 486
521 if (db_host == NULL) 487 if (db_host == NULL) {
522 db_host = strdup(""); 488 db_host = strdup("");
489 }
523 490
524 if (db == NULL) 491 if (db == NULL) {
525 db = strdup(""); 492 db = strdup("");
493 }
526 494
527 return OK; 495 return OK;
528} 496}
529 497
530 498void print_help(void) {
531void
532print_help (void)
533{
534 char *myport; 499 char *myport;
535 xasprintf (&myport, "%d", MYSQL_PORT); 500 xasprintf(&myport, "%d", MYSQL_PORT);
536 501
537 print_revision (progname, NP_VERSION); 502 print_revision(progname, NP_VERSION);
538 503
539 printf (_(COPYRIGHT), copyright, email); 504 printf(_(COPYRIGHT), copyright, email);
540 505
541 printf ("%s\n", _("This program tests connections to a MySQL server")); 506 printf("%s\n", _("This program tests connections to a MySQL server"));
542 507
543 printf ("\n\n"); 508 printf("\n\n");
544 509
545 print_usage (); 510 print_usage();
546 511
547 printf (UT_HELP_VRSN); 512 printf(UT_HELP_VRSN);
548 printf (UT_EXTRA_OPTS); 513 printf(UT_EXTRA_OPTS);
549 514
550 printf (UT_HOST_PORT, 'P', myport); 515 printf(UT_HOST_PORT, 'P', myport);
551 printf (" %s\n", "-n, --ignore-auth"); 516 printf(" %s\n", "-n, --ignore-auth");
552 printf (" %s\n", _("Ignore authentication failure and check for mysql connectivity only")); 517 printf(" %s\n", _("Ignore authentication failure and check for mysql connectivity only"));
553 518
554 printf (" %s\n", "-s, --socket=STRING"); 519 printf(" %s\n", "-s, --socket=STRING");
555 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)")); 520 printf(" %s\n", _("Use the specified socket (has no effect if -H is used)"));
556 521
557 printf (" %s\n", "-d, --database=STRING"); 522 printf(" %s\n", "-d, --database=STRING");
558 printf (" %s\n", _("Check database with indicated name")); 523 printf(" %s\n", _("Check database with indicated name"));
559 printf (" %s\n", "-f, --file=STRING"); 524 printf(" %s\n", "-f, --file=STRING");
560 printf (" %s\n", _("Read from the specified client options file")); 525 printf(" %s\n", _("Read from the specified client options file"));
561 printf (" %s\n", "-g, --group=STRING"); 526 printf(" %s\n", "-g, --group=STRING");
562 printf (" %s\n", _("Use a client options group")); 527 printf(" %s\n", _("Use a client options group"));
563 printf (" %s\n", "-u, --username=STRING"); 528 printf(" %s\n", "-u, --username=STRING");
564 printf (" %s\n", _("Connect using the indicated username")); 529 printf(" %s\n", _("Connect using the indicated username"));
565 printf (" %s\n", "-p, --password=STRING"); 530 printf(" %s\n", "-p, --password=STRING");
566 printf (" %s\n", _("Use the indicated password to authenticate the connection")); 531 printf(" %s\n", _("Use the indicated password to authenticate the connection"));
567 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); 532 printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
568 printf (" %s\n", _("Your clear-text password could be visible as a process table entry")); 533 printf(" %s\n", _("Your clear-text password could be visible as a process table entry"));
569 printf (" %s\n", "-S, --check-slave"); 534 printf(" %s\n", "-S, --check-slave");
570 printf (" %s\n", _("Check if the slave thread is running properly.")); 535 printf(" %s\n", _("Check if the slave thread is running properly."));
571 printf (" %s\n", "-w, --warning"); 536 printf(" %s\n", "-w, --warning");
572 printf (" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds")); 537 printf(" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds"));
573 printf (" %s\n", _("behind master")); 538 printf(" %s\n", _("behind master"));
574 printf (" %s\n", "-c, --critical"); 539 printf(" %s\n", "-c, --critical");
575 printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds")); 540 printf(" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
576 printf (" %s\n", _("behind master")); 541 printf(" %s\n", _("behind master"));
577 printf (" %s\n", "-l, --ssl"); 542 printf(" %s\n", "-l, --ssl");
578 printf (" %s\n", _("Use ssl encryption")); 543 printf(" %s\n", _("Use ssl encryption"));
579 printf (" %s\n", "-C, --ca-cert=STRING"); 544 printf(" %s\n", "-C, --ca-cert=STRING");
580 printf (" %s\n", _("Path to CA signing the cert")); 545 printf(" %s\n", _("Path to CA signing the cert"));
581 printf (" %s\n", "-a, --cert=STRING"); 546 printf(" %s\n", "-a, --cert=STRING");
582 printf (" %s\n", _("Path to SSL certificate")); 547 printf(" %s\n", _("Path to SSL certificate"));
583 printf (" %s\n", "-k, --key=STRING"); 548 printf(" %s\n", "-k, --key=STRING");
584 printf (" %s\n", _("Path to private SSL key")); 549 printf(" %s\n", _("Path to private SSL key"));
585 printf (" %s\n", "-D, --ca-dir=STRING"); 550 printf(" %s\n", "-D, --ca-dir=STRING");
586 printf (" %s\n", _("Path to CA directory")); 551 printf(" %s\n", _("Path to CA directory"));
587 printf (" %s\n", "-L, --ciphers=STRING"); 552 printf(" %s\n", "-L, --ciphers=STRING");
588 printf (" %s\n", _("List of valid SSL ciphers")); 553 printf(" %s\n", _("List of valid SSL ciphers"));
589 554
590 555 printf("\n");
591 printf ("\n"); 556 printf(" %s\n", _("There are no required arguments. By default, the local database is checked"));
592 printf (" %s\n", _("There are no required arguments. By default, the local database is checked")); 557 printf(" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
593 printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); 558 printf(" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
594 printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); 559
595 560 printf("\n");
596 printf ("\n"); 561 printf("%s\n", _("Notes:"));
597 printf ("%s\n", _("Notes:")); 562 printf(" %s\n", _("You must specify -p with an empty string to force an empty password,"));
598 printf (" %s\n", _("You must specify -p with an empty string to force an empty password,")); 563 printf(" %s\n", _("overriding any my.cnf settings."));
599 printf (" %s\n", _("overriding any my.cnf settings.")); 564
600 565 printf(UT_SUPPORT);
601 printf (UT_SUPPORT);
602} 566}
603 567
604 568void print_usage(void) {
605void 569 printf("%s\n", _("Usage:"));
606print_usage (void) 570 printf(" %s [-d database] [-H host] [-P port] [-s socket]\n", progname);
607{ 571 printf(" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n");
608 printf ("%s\n", _("Usage:")); 572 printf(" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n");
609 printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
610 printf (" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n");
611 printf (" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n");
612} 573}