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.c81
1 files changed, 62 insertions, 19 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index 45f86a9..92ac7ff 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -6,7 +6,7 @@
6* License: GPL 6* License: GPL
7* Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) 7* Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
8* portions (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) 8* portions (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
9* 9*
10* $Id$ 10* $Id$
11* 11*
12* Description: 12* Description:
@@ -16,10 +16,10 @@
16 16
17const char *progname = "check_mysql"; 17const char *progname = "check_mysql";
18const char *revision = "$Revision$"; 18const char *revision = "$Revision$";
19const char *copyright = "1999-2004"; 19const char *copyright = "1999-2006";
20const char *email = "nagiosplug-devel@lists.sourceforge.net"; 20const char *email = "nagiosplug-devel@lists.sourceforge.net";
21 21
22#define SLAVERESULTSIZE 40 22#define SLAVERESULTSIZE 70
23 23
24#include "common.h" 24#include "common.h"
25#include "utils.h" 25#include "utils.h"
@@ -33,15 +33,16 @@ char *db_host = NULL;
33char *db_pass = NULL; 33char *db_pass = NULL;
34char *db = NULL; 34char *db = NULL;
35unsigned int db_port = MYSQL_PORT; 35unsigned int db_port = MYSQL_PORT;
36int check_slave = 0; 36int check_slave = 0, warn_sec = 0, crit_sec = 0;
37int verbose = 0;
38
39thresholds *my_threshold = NULL;
37 40
38int process_arguments (int, char **); 41int process_arguments (int, char **);
39int validate_arguments (void); 42int validate_arguments (void);
40void print_help (void); 43void print_help (void);
41void print_usage (void); 44void print_usage (void);
42 45
43
44
45int 46int
46main (int argc, char **argv) 47main (int argc, char **argv)
47{ 48{
@@ -137,37 +138,60 @@ main (int argc, char **argv)
137 138
138 } else { 139 } else {
139 /* mysql 4.x.x */ 140 /* mysql 4.x.x */
140 int slave_io_field = -1 , slave_sql_field = -1, i, num_fields; 141 int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
141 MYSQL_FIELD* fields; 142 MYSQL_FIELD* fields;
142 143
143 num_fields = mysql_num_fields(res); 144 num_fields = mysql_num_fields(res);
144 fields = mysql_fetch_fields(res); 145 fields = mysql_fetch_fields(res);
145 for(i = 0; i < num_fields; i++) 146 for(i = 0; i < num_fields; i++) {
146 { 147 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
147 if (0 == strcmp(fields[i].name, "Slave_IO_Running"))
148 {
149 slave_io_field = i; 148 slave_io_field = i;
150 continue; 149 continue;
151 } 150 }
152 if (0 == strcmp(fields[i].name, "Slave_SQL_Running")) 151 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
153 {
154 slave_sql_field = i; 152 slave_sql_field = i;
155 continue; 153 continue;
156 } 154 }
155 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
156 seconds_behind_field = i;
157 continue;
158 }
157 } 159 }
158 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) 160 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
159 {
160 mysql_free_result (res); 161 mysql_free_result (res);
161 mysql_close (&mysql); 162 mysql_close (&mysql);
162 die (STATE_CRITICAL, "Slave status unavailable\n"); 163 die (STATE_CRITICAL, "Slave status unavailable\n");
163 } 164 }
164 165
165 snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[slave_io_field], row[slave_sql_field]); 166 snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], row[seconds_behind_field]);
166 if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) { 167 if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
167 mysql_free_result (res); 168 mysql_free_result (res);
168 mysql_close (&mysql); 169 mysql_close (&mysql);
169 die (STATE_CRITICAL, "%s\n", slaveresult); 170 die (STATE_CRITICAL, "%s\n", slaveresult);
170 } 171 }
172
173 if (verbose >=3) {
174 if (seconds_behind_field == -1) {
175 printf("seconds_behind_field not found\n");
176 } else {
177 printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
178 }
179 }
180
181 if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
182 double value = atof(row[seconds_behind_field]);
183 int status;
184
185 status = get_status(value, my_threshold);
186
187 if (status == STATE_WARNING) {
188 printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult);
189 exit(STATE_WARNING);
190 } else if (status == STATE_CRITICAL) {
191 printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult);
192 exit(STATE_CRITICAL);
193 }
194 }
171 } 195 }
172 196
173 /* free the result */ 197 /* free the result */
@@ -193,6 +217,8 @@ int
193process_arguments (int argc, char **argv) 217process_arguments (int argc, char **argv)
194{ 218{
195 int c; 219 int c;
220 char *warning = NULL;
221 char *critical = NULL;
196 222
197 int option = 0; 223 int option = 0;
198 static struct option longopts[] = { 224 static struct option longopts[] = {
@@ -201,6 +227,8 @@ process_arguments (int argc, char **argv)
201 {"username", required_argument, 0, 'u'}, 227 {"username", required_argument, 0, 'u'},
202 {"password", required_argument, 0, 'p'}, 228 {"password", required_argument, 0, 'p'},
203 {"port", required_argument, 0, 'P'}, 229 {"port", required_argument, 0, 'P'},
230 {"critical", required_argument, 0, 'c'},
231 {"warning", required_argument, 0, 'w'},
204 {"check-slave", no_argument, 0, 'S'}, 232 {"check-slave", no_argument, 0, 'S'},
205 {"verbose", no_argument, 0, 'v'}, 233 {"verbose", no_argument, 0, 'v'},
206 {"version", no_argument, 0, 'V'}, 234 {"version", no_argument, 0, 'V'},
@@ -212,7 +240,7 @@ process_arguments (int argc, char **argv)
212 return ERROR; 240 return ERROR;
213 241
214 while (1) { 242 while (1) {
215 c = getopt_long (argc, argv, "hVSP:p:u:d:H:", longopts, &option); 243 c = getopt_long (argc, argv, "hvVSP:p:u:d:H:c:w:", longopts, &option);
216 244
217 if (c == -1 || c == EOF) 245 if (c == -1 || c == EOF)
218 break; 246 break;
@@ -241,12 +269,21 @@ process_arguments (int argc, char **argv)
241 case 'S': 269 case 'S':
242 check_slave = 1; /* check-slave */ 270 check_slave = 1; /* check-slave */
243 break; 271 break;
272 case 'w':
273 warning = optarg;
274 break;
275 case 'c':
276 critical = optarg;
277 break;
244 case 'V': /* version */ 278 case 'V': /* version */
245 print_revision (progname, revision); 279 print_revision (progname, revision);
246 exit (STATE_OK); 280 exit (STATE_OK);
247 case 'h': /* help */ 281 case 'h': /* help */
248 print_help (); 282 print_help ();
249 exit (STATE_OK); 283 exit (STATE_OK);
284 case 'v':
285 verbose++;
286 break;
250 case '?': /* help */ 287 case '?': /* help */
251 usage2 (_("Unknown argument"), optarg); 288 usage2 (_("Unknown argument"), optarg);
252 } 289 }
@@ -254,6 +291,8 @@ process_arguments (int argc, char **argv)
254 291
255 c = optind; 292 c = optind;
256 293
294 set_thresholds(&my_threshold, warning, critical);
295
257 while ( argc > c ) { 296 while ( argc > c ) {
258 297
259 if (strlen(db_host) == 0) 298 if (strlen(db_host) == 0)
@@ -326,7 +365,11 @@ print_help (void)
326 ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\ 365 ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
327 Your clear-text password will be visible as a process table entry\n\ 366 Your clear-text password will be visible as a process table entry\n\
328 -S, --check-slave\n\ 367 -S, --check-slave\n\
329 Check if the slave thread is running properly.\n")); 368 Check if the slave thread is running properly.\n\
369 -w, --warning\n\
370 Exit with WARNING status if slave server is more then INTEGER seconds behind master\n\
371 -c, --critical\n\
372 Exit with CRITICAL status if slave server is more then INTEGER seconds behind master\n"));
330 373
331 printf (_("\n\ 374 printf (_("\n\
332There are no required arguments. By default, the local database with\n\ 375There are no required arguments. By default, the local database with\n\