Back to index

nagios-plugins  1.4.16
Functions | Variables
check_mysql_query.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.

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_query"
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
char * sql_query = NULL
int verbose = 0
thresholdsmy_thresholds = NULL

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 62 of file check_mysql_query.c.

{

       MYSQL mysql;
       MYSQL_RES *res;
       MYSQL_ROW row;
       
       double value;
       char *error = NULL;
       int status;

       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, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
              else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
                     die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
              else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
                     die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
              else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
                     die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
              else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
                     die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
              else
                     die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql));
       }

       if (mysql_query (&mysql, sql_query) != 0) {
              error = strdup(mysql_error(&mysql));
              mysql_close (&mysql);
              die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error);
       }

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

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

       /* 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, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error);
       }

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

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

       if (! is_numeric(row[0])) {
              die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]);
       }

       value = strtod(row[0], NULL);

       if (verbose >= 3)
              printf("mysql result: %f\n", value);

       status = get_status(value, my_thresholds);

       if (status == STATE_OK) {
              printf("QUERY %s: ", _("OK"));
       } else if (status == STATE_WARNING) {
              printf("QUERY %s: ", _("WARNING"));
       } else if (status == STATE_CRITICAL) {
              printf("QUERY %s: ", _("CRITICAL"));
       }
       printf(_("'%s' returned %f"), sql_query, value);
       printf("\n");

       return status;
}

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

const char* copyright = "1999-2007"

Definition at line 33 of file check_mysql_query.c.

char* db = NULL

Definition at line 48 of file check_mysql_query.c.

char* db_host = NULL

Definition at line 45 of file check_mysql_query.c.

char* db_pass = NULL

Definition at line 47 of file check_mysql_query.c.

unsigned int db_port = MYSQL_PORT

Definition at line 49 of file check_mysql_query.c.

char* db_socket = NULL

Definition at line 46 of file check_mysql_query.c.

char* db_user = NULL

Definition at line 44 of file check_mysql_query.c.

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

Definition at line 34 of file check_mysql_query.c.

Definition at line 58 of file check_mysql_query.c.

const char* progname = "check_mysql_query"

Definition at line 32 of file check_mysql_query.c.

char* sql_query = NULL

Definition at line 56 of file check_mysql_query.c.

int verbose = 0

Definition at line 57 of file check_mysql_query.c.