Back to index

nagios-plugins  1.4.16
Defines | Functions | Variables
check_mysql.c File Reference
#include "common.h"
#include "utils.h"
#include "utils_base.h"
#include "netutils.h"
#include <mysql.h>
#include <errmsg.h>

Go to the source code of this file.

Defines

#define SLAVERESULTSIZE   70

Functions

int process_arguments (int, char **)
int validate_arguments (void)
void print_help (void)
void print_usage (void)
int main (int argc, char **argv)

Variables

const char * progname = "check_mysql"
const char * copyright = "1999-2007"
const char * email = "nagiosplug-devel@lists.sourceforge.net"
char * db_user = NULL
char * db_host = NULL
char * db_socket = NULL
char * db_pass = NULL
char * db = NULL
unsigned int db_port = MYSQL_PORT
int check_slave = 0
int warn_sec = 0
int crit_sec = 0
int verbose = 0
thresholdsmy_threshold = NULL

Define Documentation

#define SLAVERESULTSIZE   70

Definition at line 37 of file check_mysql.c.


Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 64 of file check_mysql.c.

{

       MYSQL mysql;
       MYSQL_RES *res;
       MYSQL_ROW row;

       /* should be status */

       char *result = NULL;
       char *error = NULL;
       char slaveresult[SLAVERESULTSIZE];

       setlocale (LC_ALL, "");
       bindtextdomain (PACKAGE, LOCALEDIR);
       textdomain (PACKAGE);

       /* Parse extra opts if any */
       argv=np_extra_opts (&argc, argv, progname);

       if (process_arguments (argc, argv) == ERROR)
              usage4 (_("Could not parse arguments"));

       /* initialize mysql  */
       mysql_init (&mysql);

       mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");

       /* establish a connection to the server and error checking */
       if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
              if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
                     die (STATE_WARNING, "%s\n", mysql_error (&mysql));
              else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
                     die (STATE_WARNING, "%s\n", mysql_error (&mysql));
              else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
                     die (STATE_WARNING, "%s\n", mysql_error (&mysql));
              else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
                     die (STATE_WARNING, "%s\n", mysql_error (&mysql));
              else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
                     die (STATE_WARNING, "%s\n", mysql_error (&mysql));
              else
                     die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
       }

       /* get the server stats */
       result = strdup (mysql_stat (&mysql));

       /* error checking once more */
       if (mysql_error (&mysql)) {
              if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
                     die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
              else if (mysql_errno (&mysql) == CR_SERVER_LOST)
                     die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
              else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
                     die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
       }

       if(check_slave) {
              /* check the slave status */
              if (mysql_query (&mysql, "show slave status") != 0) {
                     error = strdup(mysql_error(&mysql));
                     mysql_close (&mysql);
                     die (STATE_CRITICAL, _("slave query error: %s\n"), error);
              }

              /* store the result */
              if ( (res = mysql_store_result (&mysql)) == NULL) {
                     error = strdup(mysql_error(&mysql));
                     mysql_close (&mysql);
                     die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
              }

              /* Check there is some data */
              if (mysql_num_rows(res) == 0) {
                     mysql_close(&mysql);
                     die (STATE_WARNING, "%s\n", _("No slaves defined"));
              }

              /* fetch the first row */
              if ( (row = mysql_fetch_row (res)) == NULL) {
                     error = strdup(mysql_error(&mysql));
                     mysql_free_result (res);
                     mysql_close (&mysql);
                     die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
              }

              if (mysql_field_count (&mysql) == 12) {
                     /* mysql 3.23.x */
                     snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
                     if (strcmp (row[6], "Yes") != 0) {
                            mysql_free_result (res);
                            mysql_close (&mysql);
                            die (STATE_CRITICAL, "%s\n", slaveresult);
                     }

              } else {
                     /* mysql 4.x.x */
                     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);
                     fields = mysql_fetch_fields(res);
                     for(i = 0; i < num_fields; i++) {
                            if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
                                   slave_io_field = i;
                                   continue;
                            }
                            if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
                                   slave_sql_field = i;
                                   continue;
                            }
                            if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
                                   seconds_behind_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 Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], seconds_behind_field!=-1?row[seconds_behind_field]:"Unknown");
                     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 (verbose >=3) {
                            if (seconds_behind_field == -1) {
                                   printf("seconds_behind_field not found\n");
                            } else {
                                   printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
                            }
                     }

                     if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
                            double value = atof(row[seconds_behind_field]);
                            int status;

                            status = get_status(value, my_threshold);

                            if (status == STATE_WARNING) {
                                   printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult);
                                   exit(STATE_WARNING);
                            } else if (status == STATE_CRITICAL) {
                                   printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult);
                                   exit(STATE_CRITICAL);
                            }
                     }
              }

              /* free the result */
              mysql_free_result (res);
       }

       /* close the connection */
       mysql_close (&mysql);

       /* print out the result of stats */
       if (check_slave) {
              printf ("%s %s\n", result, slaveresult);
       } else {
              printf ("%s\n", result);
       }

       return STATE_OK;
}

Here is the call graph for this function:

void print_help ( void  )
void print_usage ( void  )

Definition at line 389 of file check_cpqarray.c.

{
  printf("cpqarrayd [options]\n");
  printf("   -h         prints this text\n");
  printf("   -d         enables debugging\n");
}
int process_arguments ( int  argc,
char **  argv 
)
int validate_arguments ( void  )

Variable Documentation

int check_slave = 0

Definition at line 53 of file check_mysql.c.

const char* copyright = "1999-2007"

Definition at line 34 of file check_mysql.c.

int crit_sec = 0

Definition at line 53 of file check_mysql.c.

char* db = NULL

Definition at line 51 of file check_mysql.c.

char* db_host = NULL

Definition at line 48 of file check_mysql.c.

char* db_pass = NULL

Definition at line 50 of file check_mysql.c.

unsigned int db_port = MYSQL_PORT

Definition at line 52 of file check_mysql.c.

char* db_socket = NULL

Definition at line 49 of file check_mysql.c.

char* db_user = NULL

Definition at line 47 of file check_mysql.c.

const char* email = "nagiosplug-devel@lists.sourceforge.net"

Definition at line 35 of file check_mysql.c.

Definition at line 56 of file check_mysql.c.

const char* progname = "check_mysql"

Definition at line 33 of file check_mysql.c.

int verbose = 0

Definition at line 54 of file check_mysql.c.

int warn_sec = 0

Definition at line 53 of file check_mysql.c.