summaryrefslogtreecommitdiffstats
path: root/plugins/check_mysql.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_mysql.c')
-rw-r--r--plugins/check_mysql.c689
1 files changed, 358 insertions, 331 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index 8a73772d..2b6cfeaf 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -1,40 +1,40 @@
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";
35const char *email = "devel@monitoring-plugins.org"; 35const char *email = "devel@monitoring-plugins.org";
36 36
37#define SLAVERESULTSIZE 96 37#define REPLICA_RESULTSIZE 96
38 38
39#include "common.h" 39#include "common.h"
40#include "utils.h" 40#include "utils.h"
@@ -59,7 +59,7 @@ static bool ssl = false;
59static char *opt_file = NULL; 59static char *opt_file = NULL;
60static char *opt_group = NULL; 60static char *opt_group = NULL;
61static unsigned int db_port = MYSQL_PORT; 61static unsigned int db_port = MYSQL_PORT;
62static bool check_slave = false; 62static bool check_replica = false;
63static bool ignore_auth = false; 63static bool ignore_auth = false;
64static int verbose = 0; 64static int verbose = 0;
65 65
@@ -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,158 +95,206 @@ 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 replica_result[REPLICA_RESULTSIZE] = {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_replica) {
214 /* check the slave status */ 199
215 if (mysql_query (&mysql, "show slave status") != 0) { 200 // Detect which version we are, on older version
201 // "show slave status" should work, on newer ones
202 // "show replica status"
203 // But first we have to find out whether this is
204 // MySQL or MariaDB since the version numbering scheme
205 // is different
206 bool use_deprecated_slave_status = false;
207 const char *server_version = mysql_get_server_info(&mysql);
208 unsigned long server_verion_int = mysql_get_server_version(&mysql);
209 unsigned long major_version = server_verion_int / 10000;
210 unsigned long minor_version = (server_verion_int % 10000) / 100;
211 unsigned long patch_version = (server_verion_int % 100);
212 if (verbose) {
213 printf("Found MariaDB: %s, main version: %lu, minor version: %lu, patch version: %lu\n", server_version, major_version,
214 minor_version, patch_version);
215 }
216
217 if (strstr(server_version, "MariaDB") != NULL) {
218 // Looks like MariaDB, new commands should be available after 10.5.1
219 if (major_version < 10) {
220 use_deprecated_slave_status = true;
221 } else if (major_version == 10) {
222 if (minor_version < 5) {
223 use_deprecated_slave_status = true;
224 } else if (minor_version == 5 && patch_version < 1) {
225 use_deprecated_slave_status = true;
226 }
227 }
228 } else if (strstr(server_version, "MySQL") != NULL) {
229 // Looks like MySQL
230 if (major_version < 8) {
231 use_deprecated_slave_status = true;
232 } else if (major_version == 10 && minor_version < 4) {
233 use_deprecated_slave_status = true;
234 }
235 } else {
236 printf("Not a known sever implementation: %s\n", server_version);
237 exit(STATE_UNKNOWN);
238 }
239
240 char *replica_query = NULL;
241 if (use_deprecated_slave_status) {
242 replica_query = "show slave status";
243 } else {
244 replica_query = "show replica status";
245 }
246
247 /* check the replica status */
248 if (mysql_query(&mysql, replica_query) != 0) {
216 error = strdup(mysql_error(&mysql)); 249 error = strdup(mysql_error(&mysql));
217 mysql_close (&mysql); 250 mysql_close(&mysql);
218 die (STATE_CRITICAL, _("slave query error: %s\n"), error); 251 die(STATE_CRITICAL, _("replica query error: %s\n"), error);
219 } 252 }
220 253
221 /* store the result */ 254 /* store the result */
222 if ( (res = mysql_store_result (&mysql)) == NULL) { 255 if ((res = mysql_store_result(&mysql)) == NULL) {
223 error = strdup(mysql_error(&mysql)); 256 error = strdup(mysql_error(&mysql));
224 mysql_close (&mysql); 257 mysql_close(&mysql);
225 die (STATE_CRITICAL, _("slave store_result error: %s\n"), error); 258 die(STATE_CRITICAL, _("replica store_result error: %s\n"), error);
226 } 259 }
227 260
228 /* Check there is some data */ 261 /* Check there is some data */
229 if (mysql_num_rows(res) == 0) { 262 if (mysql_num_rows(res) == 0) {
230 mysql_close(&mysql); 263 mysql_close(&mysql);
231 die (STATE_WARNING, "%s\n", _("No slaves defined")); 264 die(STATE_WARNING, "%s\n", _("No replicas defined"));
232 } 265 }
233 266
234 /* fetch the first row */ 267 /* fetch the first row */
235 if ( (row = mysql_fetch_row (res)) == NULL) { 268 if ((row = mysql_fetch_row(res)) == NULL) {
236 error = strdup(mysql_error(&mysql)); 269 error = strdup(mysql_error(&mysql));
237 mysql_free_result (res); 270 mysql_free_result(res);
238 mysql_close (&mysql); 271 mysql_close(&mysql);
239 die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error); 272 die(STATE_CRITICAL, _("replica fetch row error: %s\n"), error);
240 } 273 }
241 274
242 if (mysql_field_count (&mysql) == 12) { 275 if (mysql_field_count(&mysql) == 12) {
243 /* mysql 3.23.x */ 276 /* mysql 3.23.x */
244 snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]); 277 snprintf(replica_result, REPLICA_RESULTSIZE, _("Replica running: %s"), row[6]);
245 if (strcmp (row[6], "Yes") != 0) { 278 if (strcmp(row[6], "Yes") != 0) {
246 mysql_free_result (res); 279 mysql_free_result(res);
247 mysql_close (&mysql); 280 mysql_close(&mysql);
248 die (STATE_CRITICAL, "%s\n", slaveresult); 281 die(STATE_CRITICAL, "%s\n", replica_result);
249 } 282 }
250 283
251 } else { 284 } else {
252 /* mysql 4.x.x and mysql 5.x.x */ 285 /* 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; 286 int replica_io_field = -1, replica_sql_field = -1, seconds_behind_field = -1, i, num_fields;
254 MYSQL_FIELD* fields; 287 MYSQL_FIELD *fields;
255 288
256 num_fields = mysql_num_fields(res); 289 num_fields = mysql_num_fields(res);
257 fields = mysql_fetch_fields(res); 290 fields = mysql_fetch_fields(res);
258 for(i = 0; i < num_fields; i++) { 291 for (i = 0; i < num_fields; i++) {
259 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { 292 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
260 slave_io_field = i; 293 replica_io_field = i;
261 continue; 294 continue;
262 } 295 }
263 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) { 296 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
264 slave_sql_field = i; 297 replica_sql_field = i;
265 continue; 298 continue;
266 } 299 }
267 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) { 300 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
@@ -270,150 +303,147 @@ main (int argc, char **argv)
270 } 303 }
271 } 304 }
272 305
273 /* Check if slave status is available */ 306 /* Check if replica status is available */
274 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) { 307 if ((replica_io_field < 0) || (replica_sql_field < 0) || (num_fields == 0)) {
275 mysql_free_result (res); 308 mysql_free_result(res);
276 mysql_close (&mysql); 309 mysql_close(&mysql);
277 die (STATE_CRITICAL, "Slave status unavailable\n"); 310 die(STATE_CRITICAL, "Replica status unavailable\n");
278 } 311 }
279 312
280 /* Save slave status in slaveresult */ 313 /* Save replica status in replica_result */
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"); 314 snprintf(replica_result, REPLICA_RESULTSIZE, "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s", row[replica_io_field],
315 row[replica_sql_field], seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown");
282 316
283 /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */ 317 /* 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) { 318 if (strcmp(row[replica_io_field], "Yes") != 0 || strcmp(row[replica_sql_field], "Yes") != 0) {
285 MYSQL_RES *res_mysqldump; 319 MYSQL_RES *res_mysqldump;
286 MYSQL_ROW row_mysqldump; 320 MYSQL_ROW row_mysqldump;
287 unsigned int mysqldump_threads = 0; 321 unsigned int mysqldump_threads = 0;
288 322
289 if (mysql_query (&mysql, MYSQLDUMP_THREADS_QUERY) == 0) { 323 if (mysql_query(&mysql, MYSQLDUMP_THREADS_QUERY) == 0) {
290 /* store the result */ 324 /* store the result */
291 if ( (res_mysqldump = mysql_store_result (&mysql)) != NULL) { 325 if ((res_mysqldump = mysql_store_result(&mysql)) != NULL) {
292 if (mysql_num_rows(res_mysqldump) == 1) { 326 if (mysql_num_rows(res_mysqldump) == 1) {
293 if ( (row_mysqldump = mysql_fetch_row (res_mysqldump)) != NULL) { 327 if ((row_mysqldump = mysql_fetch_row(res_mysqldump)) != NULL) {
294 mysqldump_threads = atoi(row_mysqldump[0]); 328 mysqldump_threads = atoi(row_mysqldump[0]);
295 } 329 }
296 } 330 }
297 /* free the result */ 331 /* free the result */
298 mysql_free_result (res_mysqldump); 332 mysql_free_result(res_mysqldump);
299 } 333 }
300 mysql_close (&mysql); 334 mysql_close(&mysql);
301 } 335 }
302 if (mysqldump_threads == 0) { 336 if (mysqldump_threads == 0) {
303 die (STATE_CRITICAL, "%s\n", slaveresult); 337 die(STATE_CRITICAL, "%s\n", replica_result);
304 } else { 338 } else {
305 strncat(slaveresult, " Mysqldump: in progress", SLAVERESULTSIZE-1); 339 strncat(replica_result, " Mysqldump: in progress", REPLICA_RESULTSIZE - 1);
306 } 340 }
307 } 341 }
308 342
309 if (verbose >=3) { 343 if (verbose >= 3) {
310 if (seconds_behind_field == -1) { 344 if (seconds_behind_field == -1) {
311 printf("seconds_behind_field not found\n"); 345 printf("seconds_behind_field not found\n");
312 } else { 346 } else {
313 printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]); 347 printf("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
314 } 348 }
315 } 349 }
316 350
317 /* Check Seconds Behind against threshold */ 351 /* Check Seconds Behind against threshold */
318 if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && strcmp (row[seconds_behind_field], "NULL") != 0)) { 352 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]); 353 double value = atof(row[seconds_behind_field]);
320 int status; 354 int status;
321 355
322 status = get_status(value, my_threshold); 356 status = get_status(value, my_threshold);
323 357
324 xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s", 358 xasprintf(&perf, "%s %s", perf,
325 true, (double) warning_time, 359 fperfdata("seconds behind master", value, "s", true, (double)warning_time, true, (double)critical_time, false, 0,
326 true, (double) critical_time, 360 false, 0));
327 false, 0,
328 false, 0));
329 361
330 if (status == STATE_WARNING) { 362 if (status == STATE_WARNING) {
331 printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf); 363 printf("SLOW_REPLICA %s: %s|%s\n", _("WARNING"), replica_result, perf);
332 exit(STATE_WARNING); 364 exit(STATE_WARNING);
333 } else if (status == STATE_CRITICAL) { 365 } else if (status == STATE_CRITICAL) {
334 printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf); 366 printf("SLOW_REPLICA %s: %s|%s\n", _("CRITICAL"), replica_result, perf);
335 exit(STATE_CRITICAL); 367 exit(STATE_CRITICAL);
336 } 368 }
337 } 369 }
338 } 370 }
339 371
340 /* free the result */ 372 /* free the result */
341 mysql_free_result (res); 373 mysql_free_result(res);
342 } 374 }
343 375
344 /* close the connection */ 376 /* close the connection */
345 mysql_close (&mysql); 377 mysql_close(&mysql);
346 378
347 /* print out the result of stats */ 379 /* print out the result of stats */
348 if (check_slave) { 380 if (check_replica) {
349 printf ("%s %s|%s\n", result, slaveresult, perf); 381 printf("%s %s|%s\n", result, replica_result, perf);
350 } else { 382 } else {
351 printf ("%s|%s\n", result, perf); 383 printf("%s|%s\n", result, perf);
352 } 384 }
353 385
354 return STATE_OK; 386 return STATE_OK;
355} 387}
356 388
389#define CHECK_REPLICA_OPT CHAR_MAX + 1
357 390
358/* process command-line arguments */ 391/* process command-line arguments */
359int 392int process_arguments(int argc, char **argv) {
360process_arguments (int argc, char **argv)
361{
362 int c; 393 int c;
363 char *warning = NULL; 394 char *warning = NULL;
364 char *critical = NULL; 395 char *critical = NULL;
365 396
366 int option = 0; 397 int option = 0;
367 static struct option longopts[] = { 398 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
368 {"hostname", required_argument, 0, 'H'}, 399 {"socket", required_argument, 0, 's'},
369 {"socket", required_argument, 0, 's'}, 400 {"database", required_argument, 0, 'd'},
370 {"database", required_argument, 0, 'd'}, 401 {"username", required_argument, 0, 'u'},
371 {"username", required_argument, 0, 'u'}, 402 {"password", required_argument, 0, 'p'},
372 {"password", required_argument, 0, 'p'}, 403 {"file", required_argument, 0, 'f'},
373 {"file", required_argument, 0, 'f'}, 404 {"group", required_argument, 0, 'g'},
374 {"group", required_argument, 0, 'g'}, 405 {"port", required_argument, 0, 'P'},
375 {"port", required_argument, 0, 'P'}, 406 {"critical", required_argument, 0, 'c'},
376 {"critical", required_argument, 0, 'c'}, 407 {"warning", required_argument, 0, 'w'},
377 {"warning", required_argument, 0, 'w'}, 408 {"check-slave", no_argument, 0, 'S'},
378 {"check-slave", no_argument, 0, 'S'}, 409 {"check-replica", no_argument, 0, CHECK_REPLICA_OPT},
379 {"ignore-auth", no_argument, 0, 'n'}, 410 {"ignore-auth", no_argument, 0, 'n'},
380 {"verbose", no_argument, 0, 'v'}, 411 {"verbose", no_argument, 0, 'v'},
381 {"version", no_argument, 0, 'V'}, 412 {"version", no_argument, 0, 'V'},
382 {"help", no_argument, 0, 'h'}, 413 {"help", no_argument, 0, 'h'},
383 {"ssl", no_argument, 0, 'l'}, 414 {"ssl", no_argument, 0, 'l'},
384 {"ca-cert", optional_argument, 0, 'C'}, 415 {"ca-cert", optional_argument, 0, 'C'},
385 {"key", required_argument,0,'k'}, 416 {"key", required_argument, 0, 'k'},
386 {"cert", required_argument,0,'a'}, 417 {"cert", required_argument, 0, 'a'},
387 {"ca-dir", required_argument, 0, 'D'}, 418 {"ca-dir", required_argument, 0, 'D'},
388 {"ciphers", required_argument, 0, 'L'}, 419 {"ciphers", required_argument, 0, 'L'},
389 {0, 0, 0, 0} 420 {0, 0, 0, 0}};
390 }; 421
391 422 if (argc < 1) {
392 if (argc < 1)
393 return ERROR; 423 return ERROR;
424 }
394 425
395 while (1) { 426 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); 427 c = getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option);
397 428
398 if (c == -1 || c == EOF) 429 if (c == -1 || c == EOF) {
399 break; 430 break;
431 }
400 432
401 switch (c) { 433 switch (c) {
402 case 'H': /* hostname */ 434 case 'H': /* hostname */
403 if (is_host (optarg)) { 435 if (is_host(optarg)) {
404 db_host = optarg; 436 db_host = optarg;
405 } 437 } else if (*optarg == '/') {
406 else if (*optarg == '/') {
407 db_socket = optarg; 438 db_socket = optarg;
408 } 439 } else {
409 else { 440 usage2(_("Invalid hostname/address"), optarg);
410 usage2 (_("Invalid hostname/address"), optarg);
411 } 441 }
412 break; 442 break;
413 case 's': /* socket */ 443 case 's': /* socket */
414 db_socket = optarg; 444 db_socket = optarg;
415 break; 445 break;
416 case 'd': /* database */ 446 case 'd': /* database */
417 db = optarg; 447 db = optarg;
418 break; 448 break;
419 case 'l': 449 case 'l':
@@ -434,10 +464,10 @@ process_arguments (int argc, char **argv)
434 case 'L': 464 case 'L':
435 ciphers = optarg; 465 ciphers = optarg;
436 break; 466 break;
437 case 'u': /* username */ 467 case 'u': /* username */
438 db_user = optarg; 468 db_user = optarg;
439 break; 469 break;
440 case 'p': /* authentication information: password */ 470 case 'p': /* authentication information: password */
441 db_pass = strdup(optarg); 471 db_pass = strdup(optarg);
442 472
443 /* Delete the password from process list */ 473 /* Delete the password from process list */
@@ -446,40 +476,41 @@ process_arguments (int argc, char **argv)
446 optarg++; 476 optarg++;
447 } 477 }
448 break; 478 break;
449 case 'f': /* client options file */ 479 case 'f': /* client options file */
450 opt_file = optarg; 480 opt_file = optarg;
451 break; 481 break;
452 case 'g': /* client options group */ 482 case 'g': /* client options group */
453 opt_group = optarg; 483 opt_group = optarg;
454 break; 484 break;
455 case 'P': /* critical time threshold */ 485 case 'P': /* critical time threshold */
456 db_port = atoi (optarg); 486 db_port = atoi(optarg);
457 break; 487 break;
458 case 'S': 488 case 'S':
459 check_slave = true; /* check-slave */ 489 case CHECK_REPLICA_OPT:
490 check_replica = true; /* check-slave */
460 break; 491 break;
461 case 'n': 492 case 'n':
462 ignore_auth = true; /* ignore-auth */ 493 ignore_auth = true; /* ignore-auth */
463 break; 494 break;
464 case 'w': 495 case 'w':
465 warning = optarg; 496 warning = optarg;
466 warning_time = strtod (warning, NULL); 497 warning_time = strtod(warning, NULL);
467 break; 498 break;
468 case 'c': 499 case 'c':
469 critical = optarg; 500 critical = optarg;
470 critical_time = strtod (critical, NULL); 501 critical_time = strtod(critical, NULL);
471 break; 502 break;
472 case 'V': /* version */ 503 case 'V': /* version */
473 print_revision (progname, NP_VERSION); 504 print_revision(progname, NP_VERSION);
474 exit (STATE_UNKNOWN); 505 exit(STATE_UNKNOWN);
475 case 'h': /* help */ 506 case 'h': /* help */
476 print_help (); 507 print_help();
477 exit (STATE_UNKNOWN); 508 exit(STATE_UNKNOWN);
478 case 'v': 509 case 'v':
479 verbose++; 510 verbose++;
480 break; 511 break;
481 case '?': /* help */ 512 case '?': /* help */
482 usage5 (); 513 usage5();
483 } 514 }
484 } 515 }
485 516
@@ -487,126 +518,122 @@ process_arguments (int argc, char **argv)
487 518
488 set_thresholds(&my_threshold, warning, critical); 519 set_thresholds(&my_threshold, warning, critical);
489 520
490 while ( argc > c ) { 521 while (argc > c) {
491 522
492 if (db_host == NULL) 523 if (db_host == NULL) {
493 if (is_host (argv[c])) { 524 if (is_host(argv[c])) {
494 db_host = argv[c++]; 525 db_host = argv[c++];
526 } else {
527 usage2(_("Invalid hostname/address"), argv[c]);
495 } 528 }
496 else { 529 } else if (db_user == NULL) {
497 usage2 (_("Invalid hostname/address"), argv[c]);
498 }
499 else if (db_user == NULL)
500 db_user = argv[c++]; 530 db_user = argv[c++];
501 else if (db_pass == NULL) 531 } else if (db_pass == NULL) {
502 db_pass = argv[c++]; 532 db_pass = argv[c++];
503 else if (db == NULL) 533 } else if (db == NULL) {
504 db = argv[c++]; 534 db = argv[c++];
505 else if (is_intnonneg (argv[c])) 535 } else if (is_intnonneg(argv[c])) {
506 db_port = atoi (argv[c++]); 536 db_port = atoi(argv[c++]);
507 else 537 } else {
508 break; 538 break;
539 }
509 } 540 }
510 541
511 return validate_arguments (); 542 return validate_arguments();
512} 543}
513 544
514 545int validate_arguments(void) {
515int 546 if (db_user == NULL) {
516validate_arguments (void)
517{
518 if (db_user == NULL)
519 db_user = strdup(""); 547 db_user = strdup("");
548 }
520 549
521 if (db_host == NULL) 550 if (db_host == NULL) {
522 db_host = strdup(""); 551 db_host = strdup("");
552 }
523 553
524 if (db == NULL) 554 if (db == NULL) {
525 db = strdup(""); 555 db = strdup("");
556 }
526 557
527 return OK; 558 return OK;
528} 559}
529 560
530 561void print_help(void) {
531void
532print_help (void)
533{
534 char *myport; 562 char *myport;
535 xasprintf (&myport, "%d", MYSQL_PORT); 563 xasprintf(&myport, "%d", MYSQL_PORT);
536 564
537 print_revision (progname, NP_VERSION); 565 print_revision(progname, NP_VERSION);
538 566
539 printf (_(COPYRIGHT), copyright, email); 567 printf(_(COPYRIGHT), copyright, email);
540 568
541 printf ("%s\n", _("This program tests connections to a MySQL server")); 569 printf("%s\n", _("This program tests connections to a MySQL server"));
542 570
543 printf ("\n\n"); 571 printf("\n\n");
544 572
545 print_usage (); 573 print_usage();
546 574
547 printf (UT_HELP_VRSN); 575 printf(UT_HELP_VRSN);
548 printf (UT_EXTRA_OPTS); 576 printf(UT_EXTRA_OPTS);
549 577
550 printf (UT_HOST_PORT, 'P', myport); 578 printf(UT_HOST_PORT, 'P', myport);
551 printf (" %s\n", "-n, --ignore-auth"); 579 printf(" %s\n", "-n, --ignore-auth");
552 printf (" %s\n", _("Ignore authentication failure and check for mysql connectivity only")); 580 printf(" %s\n", _("Ignore authentication failure and check for mysql connectivity only"));
553 581
554 printf (" %s\n", "-s, --socket=STRING"); 582 printf(" %s\n", "-s, --socket=STRING");
555 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)")); 583 printf(" %s\n", _("Use the specified socket (has no effect if -H is used)"));
556 584
557 printf (" %s\n", "-d, --database=STRING"); 585 printf(" %s\n", "-d, --database=STRING");
558 printf (" %s\n", _("Check database with indicated name")); 586 printf(" %s\n", _("Check database with indicated name"));
559 printf (" %s\n", "-f, --file=STRING"); 587 printf(" %s\n", "-f, --file=STRING");
560 printf (" %s\n", _("Read from the specified client options file")); 588 printf(" %s\n", _("Read from the specified client options file"));
561 printf (" %s\n", "-g, --group=STRING"); 589 printf(" %s\n", "-g, --group=STRING");
562 printf (" %s\n", _("Use a client options group")); 590 printf(" %s\n", _("Use a client options group"));
563 printf (" %s\n", "-u, --username=STRING"); 591 printf(" %s\n", "-u, --username=STRING");
564 printf (" %s\n", _("Connect using the indicated username")); 592 printf(" %s\n", _("Connect using the indicated username"));
565 printf (" %s\n", "-p, --password=STRING"); 593 printf(" %s\n", "-p, --password=STRING");
566 printf (" %s\n", _("Use the indicated password to authenticate the connection")); 594 printf(" %s\n", _("Use the indicated password to authenticate the connection"));
567 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); 595 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")); 596 printf(" %s\n", _("Your clear-text password could be visible as a process table entry"));
569 printf (" %s\n", "-S, --check-slave"); 597 printf(" %s\n", "-S, --check-slave");
570 printf (" %s\n", _("Check if the slave thread is running properly.")); 598 printf(" %s\n",
571 printf (" %s\n", "-w, --warning"); 599 _("Check if the slave thread is running properly. This option is deprecated in favour of check-replica, which does the same"));
572 printf (" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds")); 600 printf(" %s\n", "--check-replica");
573 printf (" %s\n", _("behind master")); 601 printf(" %s\n", _("Check if the replica thread is running properly."));
574 printf (" %s\n", "-c, --critical"); 602 printf(" %s\n", "-w, --warning");
575 printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds")); 603 printf(" %s\n", _("Exit with WARNING status if replica server is more than INTEGER seconds"));
576 printf (" %s\n", _("behind master")); 604 printf(" %s\n", _("behind master"));
577 printf (" %s\n", "-l, --ssl"); 605 printf(" %s\n", "-c, --critical");
578 printf (" %s\n", _("Use ssl encryption")); 606 printf(" %s\n", _("Exit with CRITICAL status if replica server is more then INTEGER seconds"));
579 printf (" %s\n", "-C, --ca-cert=STRING"); 607 printf(" %s\n", _("behind master"));
580 printf (" %s\n", _("Path to CA signing the cert")); 608 printf(" %s\n", "-l, --ssl");
581 printf (" %s\n", "-a, --cert=STRING"); 609 printf(" %s\n", _("Use ssl encryption"));
582 printf (" %s\n", _("Path to SSL certificate")); 610 printf(" %s\n", "-C, --ca-cert=STRING");
583 printf (" %s\n", "-k, --key=STRING"); 611 printf(" %s\n", _("Path to CA signing the cert"));
584 printf (" %s\n", _("Path to private SSL key")); 612 printf(" %s\n", "-a, --cert=STRING");
585 printf (" %s\n", "-D, --ca-dir=STRING"); 613 printf(" %s\n", _("Path to SSL certificate"));
586 printf (" %s\n", _("Path to CA directory")); 614 printf(" %s\n", "-k, --key=STRING");
587 printf (" %s\n", "-L, --ciphers=STRING"); 615 printf(" %s\n", _("Path to private SSL key"));
588 printf (" %s\n", _("List of valid SSL ciphers")); 616 printf(" %s\n", "-D, --ca-dir=STRING");
589 617 printf(" %s\n", _("Path to CA directory"));
590 618 printf(" %s\n", "-L, --ciphers=STRING");
591 printf ("\n"); 619 printf(" %s\n", _("List of valid SSL ciphers"));
592 printf (" %s\n", _("There are no required arguments. By default, the local database is checked")); 620
593 printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); 621 printf("\n");
594 printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); 622 printf(" %s\n", _("There are no required arguments. By default, the local database is checked"));
595 623 printf(" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
596 printf ("\n"); 624 printf(" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
597 printf ("%s\n", _("Notes:")); 625
598 printf (" %s\n", _("You must specify -p with an empty string to force an empty password,")); 626 printf("\n");
599 printf (" %s\n", _("overriding any my.cnf settings.")); 627 printf("%s\n", _("Notes:"));
600 628 printf(" %s\n", _("You must specify -p with an empty string to force an empty password,"));
601 printf (UT_SUPPORT); 629 printf(" %s\n", _("overriding any my.cnf settings."));
630
631 printf(UT_SUPPORT);
602} 632}
603 633
604 634void print_usage(void) {
605void 635 printf("%s\n", _("Usage:"));
606print_usage (void) 636 printf(" %s [-d database] [-H host] [-P port] [-s socket]\n", progname);
607{ 637 printf(" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n");
608 printf ("%s\n", _("Usage:")); 638 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} 639}