--- check_mysql.c-orig	Tue Feb 21 15:53:10 2006
+++ check_mysql.c	Wed Feb 22 15:06:24 2006
@@ -6,7 +6,7 @@
 * License: GPL
 * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
 *  portions (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
-* 
+*  portions (c) 2006 Steven Kreuzer (skreuzer@f2o.org) 
 * $Id: check_mysql.c,v 1.28 2005/12/15 17:06:54 tonvoon Exp $
 *
 * Description:
@@ -16,10 +16,10 @@
 
 const char *progname = "check_mysql";
 const char *revision = "$Revision: 1.28 $";
-const char *copyright = "1999-2004";
+const char *copyright = "1999-2006";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
-#define SLAVERESULTSIZE 40
+#define SLAVERESULTSIZE 70
 
 #include "common.h"
 #include "utils.h"
@@ -33,7 +33,7 @@
 char *db_pass = NULL;
 char *db = NULL;
 unsigned int db_port = MYSQL_PORT;
-int check_slave = 0;
+int check_slave = 0, warn_sec = 0, crit_sec = 0;
 
 int process_arguments (int, char **);
 int validate_arguments (void);
@@ -40,8 +40,6 @@
 void print_help (void);
 void print_usage (void);
 
-
-
 int
 main (int argc, char **argv)
 {
@@ -137,7 +135,7 @@
 
 		} else {
 			/* mysql 4.x.x */
-			int slave_io_field = -1 , slave_sql_field = -1, i, num_fields;
+			int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
 			MYSQL_FIELD* fields;
 
 			num_fields = mysql_num_fields(res);
@@ -154,6 +152,11 @@
 					slave_sql_field = i;
 					continue;
 				}
+				if (0 == strcmp(fields[i].name, "Seconds_Behind_Master"))
+				{
+					seconds_behind_field = i;
+					continue;
+				}
 			}
 			if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0))
 			{
@@ -161,13 +164,28 @@
 				mysql_close (&mysql);
 				die (STATE_CRITICAL, "Slave status unavailable\n");
 			}
-			 
-			snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[slave_io_field], row[slave_sql_field]);
+			
+			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]);
 			if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
 				mysql_free_result (res);
 				mysql_close (&mysql);
 				die (STATE_CRITICAL, "%s\n", slaveresult);
 			}
+			if ((strcmp (row[seconds_behind_field], "NULL") != 0) && (crit_sec != 0) && (warn_sec != 0))
+			{
+				if ((atoi (row[seconds_behind_field]) > warn_sec) && (atoi (row[seconds_behind_field]) < crit_sec)) 
+				{
+					mysql_free_result (res);
+					mysql_close (&mysql);
+					die (STATE_WARNING, "%s\n", slaveresult);
+				}
+				if (atoi(row[seconds_behind_field]) > crit_sec)
+				{
+					mysql_free_result (res);
+					mysql_close (&mysql);
+					die (STATE_CRITICAL, "%s\n", slaveresult);
+				}
+			}
 		}
 
 		/* free the result */
@@ -201,6 +219,8 @@
 		{"username", required_argument, 0, 'u'},
 		{"password", required_argument, 0, 'p'},
 		{"port", required_argument, 0, 'P'},
+		{"critical", required_argument, 0, 'c'},
+		{"warning", required_argument, 0, 'w'},
 		{"check-slave", no_argument, 0, 'S'},
 		{"verbose", no_argument, 0, 'v'},
 		{"version", no_argument, 0, 'V'},
@@ -212,7 +232,7 @@
 		return ERROR;
 
 	while (1) {
-		c = getopt_long (argc, argv, "hVSP:p:u:d:H:", longopts, &option);
+		c = getopt_long (argc, argv, "hVSP:p:u:d:H:c:w:", longopts, &option);
 
 		if (c == -1 || c == EOF)
 			break;
@@ -241,6 +261,16 @@
 		case 'S':
 			check_slave = 1;							/* check-slave */
 			break;
+		case 'w':
+			if (is_intnonneg (optarg)) {
+				warn_sec = atoi (optarg);
+			}	
+			break;
+		case 'c':
+			if (is_intnonneg (optarg)) {
+				crit_sec = atoi (optarg);
+			}
+			break;
 		case 'V':									/* version */
 			print_revision (progname, revision);
 			exit (STATE_OK);
@@ -326,7 +356,11 @@
    ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
    Your clear-text password will be visible as a process table entry\n\
  -S, --check-slave\n\
-   Check if the slave thread is running properly.\n"));
+   Check if the slave thread is running properly.\n\
+ -w, --warning\n\
+   Exit with WARNING status if slave server is more then INTEGER seconds behind master\n\
+ -c, --critical\n\
+   Exit with CRITICAL status if slave server is more then INTEGER seconds behind master\n"));
 
 	printf (_("\n\
 There are no required arguments. By default, the local database with\n\
