1
/******************************************************************************
5
* Program: Mysql plugin for Nagios
7
* Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
8
* portions (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
10
* $Id: check_mysql.c,v 1.7 2003/02/11 00:47:47 tonvoon Exp $
14
* This plugin is for testing a mysql server.
15
******************************************************************************/
17
const char *progname = "check_mysql";
18
#define REVISION "$Revision: 1.7 $"
19
#define COPYRIGHT "1999-2002"
24
#include <mysql/mysql.h>
25
#include <mysql/errmsg.h>
31
unsigned int db_port = MYSQL_PORT;
33
int process_arguments (int, char **);
34
int validate_arguments (void);
35
void print_help (void);
36
void print_usage (void);
39
main (int argc, char **argv)
45
if (process_arguments (argc, argv) != OK)
46
usage ("Invalid command arguments supplied\n");
48
/* initialize mysql */
51
/* establish a connection to the server and error checking */
52
if (!mysql_real_connect
53
(&mysql, db_host, db_user, db_pass, db, db_port, NULL, 0)) {
55
if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) {
56
printf ("%s\n", mysql_error (&mysql));
60
else if (mysql_errno (&mysql) == CR_VERSION_ERROR) {
61
printf ("%s\n", mysql_error (&mysql));
65
else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) {
66
printf ("%s\n", mysql_error (&mysql));
70
else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) {
71
printf ("%s\n", mysql_error (&mysql));
75
else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) {
76
printf ("%s\n", mysql_error (&mysql));
81
printf ("%s\n", mysql_error (&mysql));
82
return STATE_CRITICAL;
87
/* get the server stats */
88
sprintf (result, mysql_stat (&mysql));
90
/* error checking once more */
91
if (mysql_error (&mysql)) {
93
if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) {
94
printf ("%s\n", mysql_error (&mysql));
95
return STATE_CRITICAL;
98
else if (mysql_errno (&mysql) == CR_SERVER_LOST) {
99
printf ("%s\n", mysql_error (&mysql));
100
return STATE_CRITICAL;
103
else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) {
104
printf ("%s\n", mysql_error (&mysql));
105
return STATE_UNKNOWN;
110
/* close the connection */
111
mysql_close (&mysql);
113
/* print out the result of stats */
114
printf ("%s\n", result);
123
/* process command-line arguments */
125
process_arguments (int argc, char **argv)
130
int option_index = 0;
131
static struct option long_options[] = {
132
{"hostname", required_argument, 0, 'H'},
133
{"database", required_argument, 0, 'd'},
134
{"username", required_argument, 0, 'u'},
135
{"password", required_argument, 0, 'p'},
136
{"port", required_argument, 0, 'P'},
137
{"verbose", no_argument, 0, 'v'},
138
{"version", no_argument, 0, 'V'},
139
{"help", no_argument, 0, 'h'},
150
getopt_long (argc, argv, "hVP:p:u:d:H:", long_options, &option_index);
152
c = getopt (argc, argv, "hVP:p:u:d:H:");
155
if (c == -1 || c == EOF)
159
case 'H': /* hostname */
160
if (is_host (optarg)) {
164
usage ("Invalid host name\n");
167
case 'd': /* hostname */
170
case 'u': /* username */
173
case 'p': /* authentication information: password */
176
case 'P': /* critical time threshold */
177
db_port = atoi (optarg);
179
case 'V': /* version */
180
print_revision (progname, REVISION);
186
usage ("Invalid argument\n");
194
if (strlen(db_host) == 0)
195
if (is_host (argv[c])) {
199
usage ("Invalid host name");
201
else if (strlen(db_user) == 0)
203
else if (strlen(db_pass) == 0)
205
else if (strlen(db) == 0)
207
else if (is_intnonneg (argv[c]))
208
db_port = atoi (argv[c++]);
213
return validate_arguments ();
221
validate_arguments (void)
233
print_revision (progname, REVISION);
235
("Copyright (c) 2000 Didi Rieder/Karl DeBisschop\n\n"
236
"This plugin is for testing a mysql server.\n");
239
("\nThere are no required arguments. By default, the local database with\n"
240
"a server listening on MySQL standard port %d will be checked\n\n"
242
" -d, --database=STRING\n"
243
" Check database with indicated name\n"
244
" -H, --hostname=STRING or IPADDRESS\n"
245
" Check server on the indicated host\n"
246
" -P, --port=INTEGER\n"
247
" Make connection on the indicated port\n"
248
" -u, --username=STRING\n"
249
" Connect using the indicated username\n"
250
" -p, --password=STRING\n"
251
" Use the indicated password to authenticate the connection\n"
252
" ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n"
253
" Your clear-text password will be visible as a process table entry\n"
255
" Print detailed help screen\n"
256
" -V, --version\n" " Print version information\n\n", MYSQL_PORT);
268
("Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"
270
" %s --version\n", progname, progname, progname);