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