/***************************************************************** * * Program: check_mysql.c * License: GPL * * Written by Tim Weippert * (based on plugins by Ethan Galstad and MySQL example code) * * Adapted by J. Javier Sianes - skyo@rotxa.org * * * Command line: check_mysql [user] [passwd] [db] [port] * can be the FQDN or the IP-Adress * [user], [passwd], [db] and [port] are optional * * Description: * * This plugin attempts to connect to an MySQL Server * with the optional specified parameters user, passwd and db. * Normaly the host and a user HAVE to assigned. * * The plugin returns * STATE_OK and the Version Number of the Server when all is fine * STATE_CRITICAL if the Connection can't be esablished * STATE_WARNING if the connection was established but the * program can't get the Versoin Number * STATE_UNKNOWN if to many parameters are given * * Copyright (c) 1999 by Tim Weippert * * Changes: * 16.12.1999: Changed the return codes from numbers to statements * 20.06.2006: Included new db and port parameters * 20.06.2006: Adapted for using new MySQL5 API * *******************************************************************/ /***************************************************************** * * Note that all includes are related to Nagios and MySQL default installation. * If you have installed them on a different location, you may change * the following include lines in order to make it works. * * To compile the agent, in a shell use the following command, for example: * * gcc -lmysqlclient -o check_mysql check_mysql.c * *******************************************************************/ #include #include #include "/usr/include/nagios/config.h" #include "/usr/include/nagios/common.h" #include "/usr/include/nagios/nagios.h" #include "/usr/include/mysql/mysql.h" MYSQL mysql; int main(int argc, char **argv) { uint i = 0; int mport; char *host; char *user; char *passwd; char *db; char *status; char *version; if ( ( argc > 6 ) || ( argc < 2 ) ) { printf("Incorrect number of arguments supplied - %i .\n", argc); printf("Usage: %s [user] [passwd] [db] [port]\n", argv[0]); return STATE_UNKNOWN; } (host = argv[1]) || (host = NULL); (user = argv[2]) || (user = NULL); (passwd = argv[3]) || (passwd = NULL); (db = argv[4]) || (db = "mysql"); if (argc==6) { mport = atoi(argv[5]); } else { mport = 3306; } mysql_init(&mysql); mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"check_mysql"); if (!mysql_real_connect(&mysql,host,user,passwd,db,(unsigned int)mport,NULL,0)) { printf("Connect ERROR, Failed to connect to database '%s': %s\n",db,mysql_error(&mysql)); return STATE_CRITICAL; } if ( !(version = mysql_get_server_info(&mysql)) ) { printf("Connect OK, but can't get Serverinfo ... something wrong !\n"); return STATE_WARNING; } printf("MYSQL OK - Running Version: %s\n", version); mysql_close(&mysql); return STATE_OK; }