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