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.c838
1 files changed, 438 insertions, 400 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index 6a7daf11..3d7ec4cd 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -1,267 +1,296 @@
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-2011 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-2011"; 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"
41#include "utils_base.h" 41#include "utils_base.h"
42#include "netutils.h" 42#include "netutils.h"
43#include "check_mysql.d/config.h"
43 44
44#include <mysql.h> 45#include <mysql.h>
45#include <mysqld_error.h> 46#include <mysqld_error.h>
46#include <errmsg.h> 47#include <errmsg.h>
47 48
48char *db_user = NULL; 49static int verbose = 0;
49char *db_host = NULL;
50char *db_socket = NULL;
51char *db_pass = NULL;
52char *db = NULL;
53char *ca_cert = NULL;
54char *ca_dir = NULL;
55char *cert = NULL;
56char *key = NULL;
57char *ciphers = NULL;
58bool ssl = false;
59char *opt_file = NULL;
60char *opt_group = NULL;
61unsigned int db_port = MYSQL_PORT;
62int check_slave = 0, warn_sec = 0, crit_sec = 0;
63int ignore_auth = 0;
64int verbose = 0;
65
66static double warning_time = 0;
67static double critical_time = 0;
68 50
69#define LENGTH_METRIC_UNIT 6 51#define LENGTH_METRIC_UNIT 6
70static const char *metric_unit[LENGTH_METRIC_UNIT] = { 52static const char *metric_unit[LENGTH_METRIC_UNIT] = {
71 "Open_files", 53 "Open_files", "Open_tables", "Qcache_free_memory", "Qcache_queries_in_cache",
72 "Open_tables", 54 "Threads_connected", "Threads_running"};
73 "Qcache_free_memory",
74 "Qcache_queries_in_cache",
75 "Threads_connected",
76 "Threads_running"
77};
78 55
79#define LENGTH_METRIC_COUNTER 9 56#define LENGTH_METRIC_COUNTER 9
80static const char *metric_counter[LENGTH_METRIC_COUNTER] = { 57static const char *metric_counter[LENGTH_METRIC_COUNTER] = {"Connections",
81 "Connections", 58 "Qcache_hits",
82 "Qcache_hits", 59 "Qcache_inserts",
83 "Qcache_inserts", 60 "Qcache_lowmem_prunes",
84 "Qcache_lowmem_prunes", 61 "Qcache_not_cached",
85 "Qcache_not_cached", 62 "Queries",
86 "Queries", 63 "Questions",
87 "Questions", 64 "Table_locks_waited",
88 "Table_locks_waited", 65 "Uptime"};
89 "Uptime" 66
90}; 67#define MYSQLDUMP_THREADS_QUERY \
91 68 "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE " \
92#define MYSQLDUMP_THREADS_QUERY "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE 'SELECT /*!40001 SQL_NO_CACHE */%'" 69 "'SELECT /*!40001 SQL_NO_CACHE */%'"
93 70
94thresholds *my_threshold = NULL; 71typedef struct {
95 72 int errorcode;
96int process_arguments (int, char **); 73 check_mysql_config config;
97int validate_arguments (void); 74} check_mysql_config_wrapper;
98void print_help (void); 75static check_mysql_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
99void print_usage (void); 76static check_mysql_config_wrapper validate_arguments(check_mysql_config_wrapper /*config_wrapper*/);
100 77static void print_help(void);
101int 78void print_usage(void);
102main (int argc, char **argv) 79
103{ 80int main(int argc, char **argv) {
81 setlocale(LC_ALL, "");
82 bindtextdomain(PACKAGE, LOCALEDIR);
83 textdomain(PACKAGE);
104 84
105 MYSQL mysql; 85 /* Parse extra opts if any */
106 MYSQL_RES *res; 86 argv = np_extra_opts(&argc, argv, progname);
107 MYSQL_ROW row;
108
109 /* should be status */
110 87
111 char *result = NULL; 88 check_mysql_config_wrapper tmp_config = process_arguments(argc, argv);
112 char *error = NULL; 89 if (tmp_config.errorcode == ERROR) {
113 char slaveresult[SLAVERESULTSIZE] = { 0 }; 90 usage4(_("Could not parse arguments"));
114 char* perf; 91 }
115 92
116 perf = strdup (""); 93 const check_mysql_config config = tmp_config.config;
117 94
118 setlocale (LC_ALL, ""); 95 MYSQL mysql;
119 bindtextdomain (PACKAGE, LOCALEDIR); 96 /* initialize mysql */
120 textdomain (PACKAGE); 97 mysql_init(&mysql);
121 98
122 /* Parse extra opts if any */ 99 if (config.opt_file != NULL) {
123 argv=np_extra_opts (&argc, argv, progname); 100 mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, config.opt_file);
101 }
124 102
125 if (process_arguments (argc, argv) == ERROR) 103 if (config.opt_group != NULL) {
126 usage4 (_("Could not parse arguments")); 104 mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, config.opt_group);
105 } else {
106 mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client");
107 }
127 108
128 /* initialize mysql */ 109 if (config.ssl) {
129 mysql_init (&mysql); 110 mysql_ssl_set(&mysql, config.key, config.cert, config.ca_cert, config.ca_dir,
130 111 config.ciphers);
131 if (opt_file != NULL) 112 }
132 mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file);
133
134 if (opt_group != NULL)
135 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group);
136 else
137 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
138
139 if (ssl)
140 mysql_ssl_set(&mysql,key,cert,ca_cert,ca_dir,ciphers);
141 /* establish a connection to the server and error checking */ 113 /* 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)) { 114 if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db,
115 config.db_port, config.db_socket, 0)) {
143 /* Depending on internally-selected auth plugin MySQL might return */ 116 /* Depending on internally-selected auth plugin MySQL might return */
144 /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */ 117 /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */
145 /* Semantically these errors are the same. */ 118 /* 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)) 119 if (config.ignore_auth && (mysql_errno(&mysql) == ER_ACCESS_DENIED_ERROR ||
147 { 120 mysql_errno(&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) {
148 printf("MySQL OK - Version: %s (protocol %d)\n", 121 printf("MySQL OK - Version: %s (protocol %d)\n", mysql_get_server_info(&mysql),
149 mysql_get_server_info(&mysql), 122 mysql_get_proto_info(&mysql));
150 mysql_get_proto_info(&mysql) 123 mysql_close(&mysql);
151 );
152 mysql_close (&mysql);
153 return STATE_OK; 124 return STATE_OK;
154 } 125 }
155 else if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) 126
156 die (STATE_WARNING, "%s\n", mysql_error (&mysql)); 127 if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) {
157 else if (mysql_errno (&mysql) == CR_VERSION_ERROR) 128 die(STATE_WARNING, "%s\n", mysql_error(&mysql));
158 die (STATE_WARNING, "%s\n", mysql_error (&mysql)); 129 } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) {
159 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) 130 die(STATE_WARNING, "%s\n", mysql_error(&mysql));
160 die (STATE_WARNING, "%s\n", mysql_error (&mysql)); 131 } else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY) {
161 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) 132 die(STATE_WARNING, "%s\n", mysql_error(&mysql));
162 die (STATE_WARNING, "%s\n", mysql_error (&mysql)); 133 } else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR) {
163 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) 134 die(STATE_WARNING, "%s\n", mysql_error(&mysql));
164 die (STATE_WARNING, "%s\n", mysql_error (&mysql)); 135 } else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR) {
165 else 136 die(STATE_WARNING, "%s\n", mysql_error(&mysql));
166 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); 137 } else {
138 die(STATE_CRITICAL, "%s\n", mysql_error(&mysql));
139 }
167 } 140 }
168 141
169 /* get the server stats */ 142 /* get the server stats */
170 result = strdup (mysql_stat (&mysql)); 143 char *result = strdup(mysql_stat(&mysql));
171 144
172 /* error checking once more */ 145 /* error checking once more */
173 if (mysql_error (&mysql)) { 146 if (mysql_error(&mysql)) {
174 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) 147 if (mysql_errno(&mysql) == CR_SERVER_GONE_ERROR) {
175 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); 148 die(STATE_CRITICAL, "%s\n", mysql_error(&mysql));
176 else if (mysql_errno (&mysql) == CR_SERVER_LOST) 149 } else if (mysql_errno(&mysql) == CR_SERVER_LOST) {
177 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); 150 die(STATE_CRITICAL, "%s\n", mysql_error(&mysql));
178 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) 151 } else if (mysql_errno(&mysql) == CR_UNKNOWN_ERROR) {
179 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); 152 die(STATE_CRITICAL, "%s\n", mysql_error(&mysql));
153 }
180 } 154 }
181 155
156 char *perf = strdup("");
157 char *error = NULL;
158 MYSQL_RES *res;
159 MYSQL_ROW row;
182 /* try to fetch some perf data */ 160 /* try to fetch some perf data */
183 if (mysql_query (&mysql, "show global status") == 0) { 161 if (mysql_query(&mysql, "show global status") == 0) {
184 if ( (res = mysql_store_result (&mysql)) == NULL) { 162 if ((res = mysql_store_result(&mysql)) == NULL) {
185 error = strdup(mysql_error(&mysql)); 163 error = strdup(mysql_error(&mysql));
186 mysql_close (&mysql); 164 mysql_close(&mysql);
187 die (STATE_CRITICAL, _("status store_result error: %s\n"), error); 165 die(STATE_CRITICAL, _("status store_result error: %s\n"), error);
188 } 166 }
189 167
190 while ( (row = mysql_fetch_row (res)) != NULL) { 168 while ((row = mysql_fetch_row(res)) != NULL) {
191 int i; 169 for (int i = 0; i < LENGTH_METRIC_UNIT; i++) {
192
193 for(i = 0; i < LENGTH_METRIC_UNIT; i++) {
194 if (strcmp(row[0], metric_unit[i]) == 0) { 170 if (strcmp(row[0], metric_unit[i]) == 0) {
195 xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], 171 xasprintf(&perf, "%s%s ", perf,
196 atol(row[1]), "", false, 0, false, 0, false, 0, false, 0)); 172 perfdata(metric_unit[i], atol(row[1]), "", false, 0, false, 0, false,
173 0, false, 0));
197 continue; 174 continue;
198 } 175 }
199 } 176 }
200 for(i = 0; i < LENGTH_METRIC_COUNTER; i++) { 177 for (int i = 0; i < LENGTH_METRIC_COUNTER; i++) {
201 if (strcmp(row[0], metric_counter[i]) == 0) { 178 if (strcmp(row[0], metric_counter[i]) == 0) {
202 xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], 179 xasprintf(&perf, "%s%s ", perf,
203 atol(row[1]), "c", false, 0, false, 0, false, 0, false, 0)); 180 perfdata(metric_counter[i], atol(row[1]), "c", false, 0, false, 0,
181 false, 0, false, 0));
204 continue; 182 continue;
205 } 183 }
206 } 184 }
207 } 185 }
208 /* remove trailing space */ 186 /* remove trailing space */
209 if (strlen(perf) > 0) 187 if (strlen(perf) > 0) {
210 perf[strlen(perf) - 1] = '\0'; 188 perf[strlen(perf) - 1] = '\0';
189 }
211 } 190 }
212 191
213 if(check_slave) { 192 char replica_result[REPLICA_RESULTSIZE] = {0};
214 /* check the slave status */ 193 if (config.check_replica) {
215 if (mysql_query (&mysql, "show slave status") != 0) { 194 // Detect which version we are, on older version
195 // "show slave status" should work, on newer ones
196 // "show replica status"
197 // But first we have to find out whether this is
198 // MySQL or MariaDB since the version numbering scheme
199 // is different
200 bool use_deprecated_slave_status = false;
201 const char *server_version = mysql_get_server_info(&mysql);
202 unsigned long server_verion_int = mysql_get_server_version(&mysql);
203 unsigned long major_version = server_verion_int / 10000;
204 unsigned long minor_version = (server_verion_int % 10000) / 100;
205 unsigned long patch_version = (server_verion_int % 100);
206 if (verbose) {
207 printf("Found MariaDB: %s, main version: %lu, minor version: %lu, patch version: %lu\n",
208 server_version, major_version, minor_version, patch_version);
209 }
210
211 if (strstr(server_version, "MariaDB") != NULL) {
212 // Looks like MariaDB, new commands should be available after 10.5.1
213 if (major_version < 10) {
214 use_deprecated_slave_status = true;
215 } else if (major_version == 10) {
216 if (minor_version < 5) {
217 use_deprecated_slave_status = true;
218 } else if (minor_version == 5 && patch_version < 1) {
219 use_deprecated_slave_status = true;
220 }
221 }
222 } else if (strstr(server_version, "MySQL") != NULL) {
223 // Looks like MySQL
224 if (major_version < 8) {
225 use_deprecated_slave_status = true;
226 } else if (major_version == 10 && minor_version < 4) {
227 use_deprecated_slave_status = true;
228 }
229 } else {
230 printf("Not a known sever implementation: %s\n", server_version);
231 exit(STATE_UNKNOWN);
232 }
233
234 char *replica_query = NULL;
235 if (use_deprecated_slave_status) {
236 replica_query = "show slave status";
237 } else {
238 replica_query = "show replica status";
239 }
240
241 /* check the replica status */
242 if (mysql_query(&mysql, replica_query) != 0) {
216 error = strdup(mysql_error(&mysql)); 243 error = strdup(mysql_error(&mysql));
217 mysql_close (&mysql); 244 mysql_close(&mysql);
218 die (STATE_CRITICAL, _("slave query error: %s\n"), error); 245 die(STATE_CRITICAL, _("replica query error: %s\n"), error);
219 } 246 }
220 247
221 /* store the result */ 248 /* store the result */
222 if ( (res = mysql_store_result (&mysql)) == NULL) { 249 if ((res = mysql_store_result(&mysql)) == NULL) {
223 error = strdup(mysql_error(&mysql)); 250 error = strdup(mysql_error(&mysql));
224 mysql_close (&mysql); 251 mysql_close(&mysql);
225 die (STATE_CRITICAL, _("slave store_result error: %s\n"), error); 252 die(STATE_CRITICAL, _("replica store_result error: %s\n"), error);
226 } 253 }
227 254
228 /* Check there is some data */ 255 /* Check there is some data */
229 if (mysql_num_rows(res) == 0) { 256 if (mysql_num_rows(res) == 0) {
230 mysql_close(&mysql); 257 mysql_close(&mysql);
231 die (STATE_WARNING, "%s\n", _("No slaves defined")); 258 die(STATE_WARNING, "%s\n", _("No replicas defined"));
232 } 259 }
233 260
234 /* fetch the first row */ 261 /* fetch the first row */
235 if ( (row = mysql_fetch_row (res)) == NULL) { 262 if ((row = mysql_fetch_row(res)) == NULL) {
236 error = strdup(mysql_error(&mysql)); 263 error = strdup(mysql_error(&mysql));
237 mysql_free_result (res); 264 mysql_free_result(res);
238 mysql_close (&mysql); 265 mysql_close(&mysql);
239 die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error); 266 die(STATE_CRITICAL, _("replica fetch row error: %s\n"), error);
240 } 267 }
241 268
242 if (mysql_field_count (&mysql) == 12) { 269 if (mysql_field_count(&mysql) == 12) {
243 /* mysql 3.23.x */ 270 /* mysql 3.23.x */
244 snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]); 271 snprintf(replica_result, REPLICA_RESULTSIZE, _("Replica running: %s"), row[6]);
245 if (strcmp (row[6], "Yes") != 0) { 272 if (strcmp(row[6], "Yes") != 0) {
246 mysql_free_result (res); 273 mysql_free_result(res);
247 mysql_close (&mysql); 274 mysql_close(&mysql);
248 die (STATE_CRITICAL, "%s\n", slaveresult); 275 die(STATE_CRITICAL, "%s\n", replica_result);
249 } 276 }
250 277
251 } else { 278 } else {
252 /* mysql 4.x.x and mysql 5.x.x */ 279 /* 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; 280 int replica_io_field = -1;
254 MYSQL_FIELD* fields; 281 int replica_sql_field = -1;
255 282 int seconds_behind_field = -1;
283 int num_fields;
284 MYSQL_FIELD *fields;
256 num_fields = mysql_num_fields(res); 285 num_fields = mysql_num_fields(res);
257 fields = mysql_fetch_fields(res); 286 fields = mysql_fetch_fields(res);
258 for(i = 0; i < num_fields; i++) { 287 for (int i = 0; i < num_fields; i++) {
259 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { 288 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
260 slave_io_field = i; 289 replica_io_field = i;
261 continue; 290 continue;
262 } 291 }
263 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) { 292 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
264 slave_sql_field = i; 293 replica_sql_field = i;
265 continue; 294 continue;
266 } 295 }
267 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) { 296 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
@@ -270,175 +299,185 @@ main (int argc, char **argv)
270 } 299 }
271 } 300 }
272 301
273 /* Check if slave status is available */ 302 /* Check if replica status is available */
274 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) { 303 if ((replica_io_field < 0) || (replica_sql_field < 0) || (num_fields == 0)) {
275 mysql_free_result (res); 304 mysql_free_result(res);
276 mysql_close (&mysql); 305 mysql_close(&mysql);
277 die (STATE_CRITICAL, "Slave status unavailable\n"); 306 die(STATE_CRITICAL, "Replica status unavailable\n");
278 } 307 }
279 308
280 /* Save slave status in slaveresult */ 309 /* 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"); 310 snprintf(replica_result, REPLICA_RESULTSIZE,
311 "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s",
312 row[replica_io_field], row[replica_sql_field],
313 seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown");
282 314
283 /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */ 315 /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no
284 if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) { 316 * mysqldump threads running */
317 if (strcmp(row[replica_io_field], "Yes") != 0 ||
318 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,
348 row[seconds_behind_field]);
314 } 349 }
315 } 350 }
316 351
317 /* Check Seconds Behind against threshold */ 352 /* Check Seconds Behind against threshold */
318 if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && strcmp (row[seconds_behind_field], "NULL") != 0)) { 353 if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL &&
354 strcmp(row[seconds_behind_field], "NULL") != 0)) {
319 double value = atof(row[seconds_behind_field]); 355 double value = atof(row[seconds_behind_field]);
320 int status; 356 int status;
321 357
322 status = get_status(value, my_threshold); 358 status = get_status(value, config.my_threshold);
323 359
324 xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s", 360 xasprintf(&perf, "%s %s", perf,
325 true, (double) warning_time, 361 fperfdata("seconds behind master", value, "s", true,
326 true, (double) critical_time, 362 (double)config.warning_time, true, (double)config.critical_time,
327 false, 0, 363 false, 0, false, 0));
328 false, 0));
329 364
330 if (status == STATE_WARNING) { 365 if (status == STATE_WARNING) {
331 printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf); 366 printf("SLOW_REPLICA %s: %s|%s\n", _("WARNING"), replica_result, perf);
332 exit(STATE_WARNING); 367 exit(STATE_WARNING);
333 } else if (status == STATE_CRITICAL) { 368 } else if (status == STATE_CRITICAL) {
334 printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf); 369 printf("SLOW_REPLICA %s: %s|%s\n", _("CRITICAL"), replica_result, perf);
335 exit(STATE_CRITICAL); 370 exit(STATE_CRITICAL);
336 } 371 }
337 } 372 }
338 } 373 }
339 374
340 /* free the result */ 375 /* free the result */
341 mysql_free_result (res); 376 mysql_free_result(res);
342 } 377 }
343 378
344 /* close the connection */ 379 /* close the connection */
345 mysql_close (&mysql); 380 mysql_close(&mysql);
346 381
347 /* print out the result of stats */ 382 /* print out the result of stats */
348 if (check_slave) { 383 if (config.check_replica) {
349 printf ("%s %s|%s\n", result, slaveresult, perf); 384 printf("%s %s|%s\n", result, replica_result, perf);
350 } else { 385 } else {
351 printf ("%s|%s\n", result, perf); 386 printf("%s|%s\n", result, perf);
352 } 387 }
353 388
354 return STATE_OK; 389 return STATE_OK;
355} 390}
356 391
392#define CHECK_REPLICA_OPT CHAR_MAX + 1
357 393
358/* process command-line arguments */ 394/* process command-line arguments */
359int 395check_mysql_config_wrapper process_arguments(int argc, char **argv) {
360process_arguments (int argc, char **argv) 396 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
361{ 397 {"socket", required_argument, 0, 's'},
362 int c; 398 {"database", required_argument, 0, 'd'},
399 {"username", required_argument, 0, 'u'},
400 {"password", required_argument, 0, 'p'},
401 {"file", required_argument, 0, 'f'},
402 {"group", required_argument, 0, 'g'},
403 {"port", required_argument, 0, 'P'},
404 {"critical", required_argument, 0, 'c'},
405 {"warning", required_argument, 0, 'w'},
406 {"check-slave", no_argument, 0, 'S'},
407 {"check-replica", no_argument, 0, CHECK_REPLICA_OPT},
408 {"ignore-auth", no_argument, 0, 'n'},
409 {"verbose", no_argument, 0, 'v'},
410 {"version", no_argument, 0, 'V'},
411 {"help", no_argument, 0, 'h'},
412 {"ssl", no_argument, 0, 'l'},
413 {"ca-cert", optional_argument, 0, 'C'},
414 {"key", required_argument, 0, 'k'},
415 {"cert", required_argument, 0, 'a'},
416 {"ca-dir", required_argument, 0, 'D'},
417 {"ciphers", required_argument, 0, 'L'},
418 {0, 0, 0, 0}};
419
420 check_mysql_config_wrapper result = {
421 .errorcode = OK,
422 .config = check_mysql_config_init(),
423 };
424
425 if (argc < 1) {
426 result.errorcode = ERROR;
427 return result;
428 }
429
363 char *warning = NULL; 430 char *warning = NULL;
364 char *critical = NULL; 431 char *critical = NULL;
365 432
366 int option = 0; 433 int option = 0;
367 static struct option longopts[] = { 434 while (true) {
368 {"hostname", required_argument, 0, 'H'}, 435 int option_index =
369 {"socket", required_argument, 0, 's'}, 436 getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option);
370 {"database", required_argument, 0, 'd'},
371 {"username", required_argument, 0, 'u'},
372 {"password", required_argument, 0, 'p'},
373 {"file", required_argument, 0, 'f'},
374 {"group", required_argument, 0, 'g'},
375 {"port", required_argument, 0, 'P'},
376 {"critical", required_argument, 0, 'c'},
377 {"warning", required_argument, 0, 'w'},
378 {"check-slave", no_argument, 0, 'S'},
379 {"ignore-auth", no_argument, 0, 'n'},
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 437
392 if (argc < 1) 438 if (option_index == -1 || option_index == EOF) {
393 return ERROR;
394
395 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);
397
398 if (c == -1 || c == EOF)
399 break; 439 break;
440 }
400 441
401 switch (c) { 442 switch (option_index) {
402 case 'H': /* hostname */ 443 case 'H': /* hostname */
403 if (is_host (optarg)) { 444 if (is_host(optarg)) {
404 db_host = optarg; 445 result.config.db_host = optarg;
405 } 446 } else if (*optarg == '/') {
406 else if (*optarg == '/') { 447 result.config.db_socket = optarg;
407 db_socket = optarg; 448 } else {
408 } 449 usage2(_("Invalid hostname/address"), optarg);
409 else {
410 usage2 (_("Invalid hostname/address"), optarg);
411 } 450 }
412 break; 451 break;
413 case 's': /* socket */ 452 case 's': /* socket */
414 db_socket = optarg; 453 result.config.db_socket = optarg;
415 break; 454 break;
416 case 'd': /* database */ 455 case 'd': /* database */
417 db = optarg; 456 result.config.db = optarg;
418 break; 457 break;
419 case 'l': 458 case 'l':
420 ssl = true; 459 result.config.ssl = true;
421 break; 460 break;
422 case 'C': 461 case 'C':
423 ca_cert = optarg; 462 result.config.ca_cert = optarg;
424 break; 463 break;
425 case 'a': 464 case 'a':
426 cert = optarg; 465 result.config.cert = optarg;
427 break; 466 break;
428 case 'k': 467 case 'k':
429 key = optarg; 468 result.config.key = optarg;
430 break; 469 break;
431 case 'D': 470 case 'D':
432 ca_dir = optarg; 471 result.config.ca_dir = optarg;
433 break; 472 break;
434 case 'L': 473 case 'L':
435 ciphers = optarg; 474 result.config.ciphers = optarg;
436 break; 475 break;
437 case 'u': /* username */ 476 case 'u': /* username */
438 db_user = optarg; 477 result.config.db_user = optarg;
439 break; 478 break;
440 case 'p': /* authentication information: password */ 479 case 'p': /* authentication information: password */
441 db_pass = strdup(optarg); 480 result.config.db_pass = strdup(optarg);
442 481
443 /* Delete the password from process list */ 482 /* Delete the password from process list */
444 while (*optarg != '\0') { 483 while (*optarg != '\0') {
@@ -446,167 +485,166 @@ process_arguments (int argc, char **argv)
446 optarg++; 485 optarg++;
447 } 486 }
448 break; 487 break;
449 case 'f': /* client options file */ 488 case 'f': /* client options file */
450 opt_file = optarg; 489 result.config.opt_file = optarg;
451 break; 490 break;
452 case 'g': /* client options group */ 491 case 'g': /* client options group */
453 opt_group = optarg; 492 result.config.opt_group = optarg;
454 break; 493 break;
455 case 'P': /* critical time threshold */ 494 case 'P': /* critical time threshold */
456 db_port = atoi (optarg); 495 result.config.db_port = atoi(optarg);
457 break; 496 break;
458 case 'S': 497 case 'S':
459 check_slave = 1; /* check-slave */ 498 case CHECK_REPLICA_OPT:
499 result.config.check_replica = true; /* check-slave */
460 break; 500 break;
461 case 'n': 501 case 'n':
462 ignore_auth = 1; /* ignore-auth */ 502 result.config.ignore_auth = true; /* ignore-auth */
463 break; 503 break;
464 case 'w': 504 case 'w':
465 warning = optarg; 505 warning = optarg;
466 warning_time = strtod (warning, NULL); 506 result.config.warning_time = strtod(warning, NULL);
467 break; 507 break;
468 case 'c': 508 case 'c':
469 critical = optarg; 509 critical = optarg;
470 critical_time = strtod (critical, NULL); 510 result.config.critical_time = strtod(critical, NULL);
471 break; 511 break;
472 case 'V': /* version */ 512 case 'V': /* version */
473 print_revision (progname, NP_VERSION); 513 print_revision(progname, NP_VERSION);
474 exit (STATE_UNKNOWN); 514 exit(STATE_UNKNOWN);
475 case 'h': /* help */ 515 case 'h': /* help */
476 print_help (); 516 print_help();
477 exit (STATE_UNKNOWN); 517 exit(STATE_UNKNOWN);
478 case 'v': 518 case 'v':
479 verbose++; 519 verbose++;
480 break; 520 break;
481 case '?': /* help */ 521 case '?': /* help */
482 usage5 (); 522 usage5();
483 } 523 }
484 } 524 }
485 525
486 c = optind; 526 int index = optind;
487
488 set_thresholds(&my_threshold, warning, critical);
489 527
490 while ( argc > c ) { 528 set_thresholds(&result.config.my_threshold, warning, critical);
491 529
492 if (db_host == NULL) 530 while (argc > index) {
493 if (is_host (argv[c])) { 531 if (result.config.db_host == NULL) {
494 db_host = argv[c++]; 532 if (is_host(argv[index])) {
533 result.config.db_host = argv[index++];
534 } else {
535 usage2(_("Invalid hostname/address"), argv[index]);
495 } 536 }
496 else { 537 } else if (result.config.db_user == NULL) {
497 usage2 (_("Invalid hostname/address"), argv[c]); 538 result.config.db_user = argv[index++];
498 } 539 } else if (result.config.db_pass == NULL) {
499 else if (db_user == NULL) 540 result.config.db_pass = argv[index++];
500 db_user = argv[c++]; 541 } else if (result.config.db == NULL) {
501 else if (db_pass == NULL) 542 result.config.db = argv[index++];
502 db_pass = argv[c++]; 543 } else if (is_intnonneg(argv[index])) {
503 else if (db == NULL) 544 result.config.db_port = atoi(argv[index++]);
504 db = argv[c++]; 545 } else {
505 else if (is_intnonneg (argv[c]))
506 db_port = atoi (argv[c++]);
507 else
508 break; 546 break;
547 }
509 } 548 }
510 549
511 return validate_arguments (); 550 return validate_arguments(result);
512} 551}
513 552
553check_mysql_config_wrapper validate_arguments(check_mysql_config_wrapper config_wrapper) {
554 if (config_wrapper.config.db_user == NULL) {
555 config_wrapper.config.db_user = strdup("");
556 }
514 557
515int 558 if (config_wrapper.config.db_host == NULL) {
516validate_arguments (void) 559 config_wrapper.config.db_host = strdup("");
517{ 560 }
518 if (db_user == NULL)
519 db_user = strdup("");
520
521 if (db_host == NULL)
522 db_host = strdup("");
523 561
524 if (db == NULL) 562 if (config_wrapper.config.db == NULL) {
525 db = strdup(""); 563 config_wrapper.config.db = strdup("");
564 }
526 565
527 return OK; 566 return config_wrapper;
528} 567}
529 568
530 569void print_help(void) {
531void
532print_help (void)
533{
534 char *myport; 570 char *myport;
535 xasprintf (&myport, "%d", MYSQL_PORT); 571 xasprintf(&myport, "%d", MYSQL_PORT);
536 572
537 print_revision (progname, NP_VERSION); 573 print_revision(progname, NP_VERSION);
538 574
539 printf (_(COPYRIGHT), copyright, email); 575 printf(_(COPYRIGHT), copyright, email);
540 576
541 printf ("%s\n", _("This program tests connections to a MySQL server")); 577 printf("%s\n", _("This program tests connections to a MySQL server"));
542 578
543 printf ("\n\n"); 579 printf("\n\n");
544 580
545 print_usage (); 581 print_usage();
546 582
547 printf (UT_HELP_VRSN); 583 printf(UT_HELP_VRSN);
548 printf (UT_EXTRA_OPTS); 584 printf(UT_EXTRA_OPTS);
549 585
550 printf (UT_HOST_PORT, 'P', myport); 586 printf(UT_HOST_PORT, 'P', myport);
551 printf (" %s\n", "-n, --ignore-auth"); 587 printf(" %s\n", "-n, --ignore-auth");
552 printf (" %s\n", _("Ignore authentication failure and check for mysql connectivity only")); 588 printf(" %s\n", _("Ignore authentication failure and check for mysql connectivity only"));
553 589
554 printf (" %s\n", "-s, --socket=STRING"); 590 printf(" %s\n", "-s, --socket=STRING");
555 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)")); 591 printf(" %s\n", _("Use the specified socket (has no effect if -H is used)"));
556 592
557 printf (" %s\n", "-d, --database=STRING"); 593 printf(" %s\n", "-d, --database=STRING");
558 printf (" %s\n", _("Check database with indicated name")); 594 printf(" %s\n", _("Check database with indicated name"));
559 printf (" %s\n", "-f, --file=STRING"); 595 printf(" %s\n", "-f, --file=STRING");
560 printf (" %s\n", _("Read from the specified client options file")); 596 printf(" %s\n", _("Read from the specified client options file"));
561 printf (" %s\n", "-g, --group=STRING"); 597 printf(" %s\n", "-g, --group=STRING");
562 printf (" %s\n", _("Use a client options group")); 598 printf(" %s\n", _("Use a client options group"));
563 printf (" %s\n", "-u, --username=STRING"); 599 printf(" %s\n", "-u, --username=STRING");
564 printf (" %s\n", _("Connect using the indicated username")); 600 printf(" %s\n", _("Connect using the indicated username"));
565 printf (" %s\n", "-p, --password=STRING"); 601 printf(" %s\n", "-p, --password=STRING");
566 printf (" %s\n", _("Use the indicated password to authenticate the connection")); 602 printf(" %s\n", _("Use the indicated password to authenticate the connection"));
567 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); 603 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")); 604 printf(" %s\n", _("Your clear-text password could be visible as a process table entry"));
569 printf (" %s\n", "-S, --check-slave"); 605 printf(" %s\n", "-S, --check-slave");
570 printf (" %s\n", _("Check if the slave thread is running properly.")); 606 printf(" %s\n", _("Check if the slave thread is running properly. This option is deprecated "
571 printf (" %s\n", "-w, --warning"); 607 "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")); 608 printf(" %s\n", "--check-replica");
573 printf (" %s\n", _("behind master")); 609 printf(" %s\n", _("Check if the replica thread is running properly."));
574 printf (" %s\n", "-c, --critical"); 610 printf(" %s\n", "-w, --warning");
575 printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds")); 611 printf(" %s\n",
576 printf (" %s\n", _("behind master")); 612 _("Exit with WARNING status if replica server is more than INTEGER seconds"));
577 printf (" %s\n", "-l, --ssl"); 613 printf(" %s\n", _("behind master"));
578 printf (" %s\n", _("Use ssl encryption")); 614 printf(" %s\n", "-c, --critical");
579 printf (" %s\n", "-C, --ca-cert=STRING"); 615 printf(" %s\n",
580 printf (" %s\n", _("Path to CA signing the cert")); 616 _("Exit with CRITICAL status if replica server is more then INTEGER seconds"));
581 printf (" %s\n", "-a, --cert=STRING"); 617 printf(" %s\n", _("behind master"));
582 printf (" %s\n", _("Path to SSL certificate")); 618 printf(" %s\n", "-l, --ssl");
583 printf (" %s\n", "-k, --key=STRING"); 619 printf(" %s\n", _("Use ssl encryption"));
584 printf (" %s\n", _("Path to private SSL key")); 620 printf(" %s\n", "-C, --ca-cert=STRING");
585 printf (" %s\n", "-D, --ca-dir=STRING"); 621 printf(" %s\n", _("Path to CA signing the cert"));
586 printf (" %s\n", _("Path to CA directory")); 622 printf(" %s\n", "-a, --cert=STRING");
587 printf (" %s\n", "-L, --ciphers=STRING"); 623 printf(" %s\n", _("Path to SSL certificate"));
588 printf (" %s\n", _("List of valid SSL ciphers")); 624 printf(" %s\n", "-k, --key=STRING");
589 625 printf(" %s\n", _("Path to private SSL key"));
590 626 printf(" %s\n", "-D, --ca-dir=STRING");
591 printf ("\n"); 627 printf(" %s\n", _("Path to CA directory"));
592 printf (" %s\n", _("There are no required arguments. By default, the local database is checked")); 628 printf(" %s\n", "-L, --ciphers=STRING");
593 printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); 629 printf(" %s\n", _("List of valid SSL ciphers"));
594 printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); 630
595 631 printf("\n");
596 printf ("\n"); 632 printf(" %s\n",
597 printf ("%s\n", _("Notes:")); 633 _("There are no required arguments. By default, the local database is checked"));
598 printf (" %s\n", _("You must specify -p with an empty string to force an empty password,")); 634 printf(" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
599 printf (" %s\n", _("overriding any my.cnf settings.")); 635 printf(" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
600 636
601 printf (UT_SUPPORT); 637 printf("\n");
638 printf("%s\n", _("Notes:"));
639 printf(" %s\n", _("You must specify -p with an empty string to force an empty password,"));
640 printf(" %s\n", _("overriding any my.cnf settings."));
641
642 printf(UT_SUPPORT);
602} 643}
603 644
604 645void print_usage(void) {
605void 646 printf("%s\n", _("Usage:"));
606print_usage (void) 647 printf(" %s [-d database] [-H host] [-P port] [-s socket]\n", progname);
607{ 648 printf(" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n");
608 printf ("%s\n", _("Usage:")); 649 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} 650}