Code

fixes from richard brodie (tracker id 1216576)
[nagiosplug.git] / plugins / check_mysql.c
index ffd96ed4743dc509745bdf83dbdf6f6821efd005..20901e639d908afd7f211469d8e50fe69f95a56b 100644 (file)
@@ -16,7 +16,7 @@
 
 const char *progname = "check_mysql";
 const char *revision = "$Revision$";
-const char *copyright = "1999-2002";
+const char *copyright = "1999-2004";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #define SLAVERESULTSIZE 40
@@ -24,6 +24,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
 #include "common.h"
 #include "utils.h"
 #include "netutils.h"
+
 #include <mysql/mysql.h>
 #include <mysql/errmsg.h>
 
@@ -39,6 +40,8 @@ int validate_arguments (void);
 void print_help (void);
 void print_usage (void);
 
+
+
 int
 main (int argc, char **argv)
 {
@@ -46,6 +49,9 @@ main (int argc, char **argv)
        MYSQL mysql;
        MYSQL_RES *res;
        MYSQL_ROW row;
+       
+       /* should be status */
+       
        char *result = NULL;
        char slaveresult[SLAVERESULTSIZE];
 
@@ -53,8 +59,8 @@ main (int argc, char **argv)
        bindtextdomain (PACKAGE, LOCALEDIR);
        textdomain (PACKAGE);
 
-       if (process_arguments (argc, argv) != OK)
-               usage (_("check_mysql: could not parse arguments\n"));
+       if (process_arguments (argc, argv) == ERROR)
+               usage4 (_("Could not parse arguments"));
 
        /* initialize mysql  */
        mysql_init (&mysql);
@@ -92,25 +98,25 @@ main (int argc, char **argv)
                /* check the slave status */
                if (mysql_query (&mysql, "show slave status") != 0) {
                        mysql_close (&mysql);
-                       die (STATE_CRITICAL, "slave query error: %s\n", mysql_error (&mysql));
+                       die (STATE_CRITICAL, _("slave query error: %s\n"), mysql_error (&mysql));
                }
 
                /* store the result */
                if ( (res = mysql_store_result (&mysql)) == NULL) {
                        mysql_close (&mysql);
-                       die (STATE_CRITICAL, "slave store_result error: %s\n", mysql_error (&mysql));
+                       die (STATE_CRITICAL, _("slave store_result error: %s\n"), mysql_error (&mysql));
                }
 
                /* fetch the first row */
                if ( (row = mysql_fetch_row (res)) == NULL) {
                        mysql_free_result (res);
                        mysql_close (&mysql);
-                       die (STATE_CRITICAL, "slave fetch row error: %s\n", mysql_error (&mysql));
+                       die (STATE_CRITICAL, _("slave fetch row error: %s\n"), mysql_error (&mysql));
                }
 
                if (mysql_field_count (&mysql) == 12) {
                        /* mysql 3.23.x */
-                       snprintf (slaveresult, SLAVERESULTSIZE, "Slave running: %s", row[6]);
+                       snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
                        if (strcmp (row[6], "Yes") != 0) {
                                mysql_free_result (res);
                                mysql_close (&mysql);
@@ -119,8 +125,33 @@ main (int argc, char **argv)
 
                } else {
                        /* mysql 4.x.x */
-                       snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[9], row[10]);
-                       if (strcmp (row[9], "Yes") != 0 || strcmp (row[10], "Yes") != 0) {
+                       int slave_io_field = -1 , slave_sql_field = -1, i, num_fields;
+                       MYSQL_FIELD* fields;
+
+                       num_fields = mysql_num_fields(res);
+                       fields = mysql_fetch_fields(res);
+                       for(i = 0; i < num_fields; i++)
+                       {
+                               if (0 == strcmp(fields[i].name, "Slave_IO_Running"))
+                               {
+                                       slave_io_field = i;
+                                       continue;
+                               }
+                               if (0 == strcmp(fields[i].name, "Slave_SQL_Running"))
+                               {
+                                       slave_sql_field = i;
+                                       continue;
+                               }
+                       }
+                       if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0))
+                       {
+                               mysql_free_result (res);
+                               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]);
+                       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);
@@ -145,7 +176,6 @@ main (int argc, char **argv)
 }
 
 
-
 /* process command-line arguments */
 int
 process_arguments (int argc, char **argv)
@@ -206,9 +236,7 @@ process_arguments (int argc, char **argv)
                        print_help ();
                        exit (STATE_OK);
                case '?':                                                                       /* help */
-                       printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
-                       print_usage ();
-                       exit (STATE_UNKNOWN);
+                       usage2 (_("Unknown argument"), optarg);
                }
        }
 
@@ -239,7 +267,6 @@ process_arguments (int argc, char **argv)
 }
 
 
-
 int
 validate_arguments (void)
 {
@@ -259,7 +286,6 @@ validate_arguments (void)
 }
 
 
-
 void
 print_help (void)
 {
@@ -298,12 +324,10 @@ a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
 }
 
 
-
 void
 print_usage (void)
 {
-       printf (_("\
-Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n"),
+       printf ("\
+Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n",
                progname);
-       printf (_(UT_HLP_VRS), progname, progname);
 }