Code

Bug from code-clean (Antony Simmonds - 846311)
[nagiosplug.git] / plugins / check_mysql.c
1 /******************************************************************************
2 *
3 * CHECK_MYSQL.C
4 *
5 * Program: Mysql plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
8 *  portions (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
9
10 * $Id$
11 *
12 * Description:
13 *
14 * This plugin is for testing a mysql server.
15 ******************************************************************************/
17 const char *progname = "check_mysql";
18 const char *revision = "$Revision$";
19 const char *copyright = "1999-2002";
20 const char *email = "nagiosplug-devel@lists.sourceforge.net";
22 #include "common.h"
23 #include "utils.h"
24 #include "netutils.h"
25 #include <mysql/mysql.h>
26 #include <mysql/errmsg.h>
28 char *db_user = NULL;
29 char *db_host = NULL;
30 char *db_pass = NULL;
31 char *db = NULL;
32 unsigned int db_port = MYSQL_PORT;
34 int process_arguments (int, char **);
35 int validate_arguments (void);
36 void print_help (void);
37 void print_usage (void);
39 int
40 main (int argc, char **argv)
41 {
43         MYSQL mysql;
44         char *result = NULL;
46         setlocale (LC_ALL, "");
47         bindtextdomain (PACKAGE, LOCALEDIR);
48         textdomain (PACKAGE);
50         if (process_arguments (argc, argv) != OK)
51                 usage (_("Invalid command arguments supplied\n"));
53         /* initialize mysql  */
54         mysql_init (&mysql);
56         /* establish a connection to the server and error checking */
57         if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
58                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
59                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
60                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
61                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
62                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
63                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
64                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
65                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
66                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
67                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
68                 else
69                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
70         }
72         /* get the server stats */
73         result = strdup (mysql_stat (&mysql));
75         /* error checking once more */
76         if (mysql_error (&mysql)) {
77                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
78                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
79                 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
80                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
81                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
82                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
83         }
85         /* close the connection */
86         mysql_close (&mysql);
88         /* print out the result of stats */
89         printf ("%s\n", result);
91         return STATE_OK;
92 }
98 /* process command-line arguments */
99 int
100 process_arguments (int argc, char **argv)
102         int c;
104         int option = 0;
105         static struct option longopts[] = {
106                 {"hostname", required_argument, 0, 'H'},
107                 {"database", required_argument, 0, 'd'},
108                 {"username", required_argument, 0, 'u'},
109                 {"password", required_argument, 0, 'p'},
110                 {"port", required_argument, 0, 'P'},
111                 {"verbose", no_argument, 0, 'v'},
112                 {"version", no_argument, 0, 'V'},
113                 {"help", no_argument, 0, 'h'},
114                 {0, 0, 0, 0}
115         };
117         if (argc < 1)
118                 return ERROR;
120         while (1) {
121                 c = getopt_long (argc, argv, "hVP:p:u:d:H:", longopts, &option);
123                 if (c == -1 || c == EOF)
124                         break;
126                 switch (c) {
127                 case 'H':                                                                       /* hostname */
128                         if (is_host (optarg)) {
129                                 db_host = optarg;
130                         }
131                         else {
132                                 usage (_("Invalid host name\n"));
133                         }
134                         break;
135                 case 'd':                                                                       /* hostname */
136                         db = optarg;
137                         break;
138                 case 'u':                                                                       /* username */
139                         db_user = optarg;
140                         break;
141                 case 'p':                                                                       /* authentication information: password */
142                         db_pass = optarg;
143                         break;
144                 case 'P':                                                                       /* critical time threshold */
145                         db_port = atoi (optarg);
146                         break;
147                 case 'V':                                                                       /* version */
148                         print_revision (progname, revision);
149                         exit (STATE_OK);
150                 case 'h':                                                                       /* help */
151                         print_help ();
152                         exit (STATE_OK);
153                 case '?':                                                                       /* help */
154                         usage (_("Invalid argument\n"));
155                 }
156         }
158         c = optind;
160         while ( argc > c ) {
162                 if (strlen(db_host) == 0)
163                         if (is_host (argv[c])) {
164                                 db_host = argv[c++];
165                         }
166                         else {
167                                 usage ("Invalid host name");
168                         }
169                 else if (strlen(db_user) == 0)
170                         db_user = argv[c++];
171                 else if (strlen(db_pass) == 0)
172                         db_pass = argv[c++];
173                 else if (strlen(db) == 0)
174                         db = argv[c++];
175                 else if (is_intnonneg (argv[c]))
176                         db_port = atoi (argv[c++]);
177                 else
178                         break;
179         }
181         return validate_arguments ();
188 int
189 validate_arguments (void)
191         if (db_user == NULL)
192                 db_user = strdup("");
194         if (db_host == NULL)
195                 db_host = strdup("");
197         if (db_pass == NULL)
198                 db_pass == strdup("");
200         if (db == NULL)
201                 db = strdup("");
203         return OK;
210 \f
211 void
212 print_help (void)
214         char *myport;
215         asprintf (&myport, "%d", MYSQL_PORT);
217         print_revision (progname, revision);
219         printf (_(COPYRIGHT), copyright, email);
221         printf (_("This program tests connections to a mysql server\n"));
223         print_usage ();
225         printf (_(UT_HELP_VRSN));
227         printf (_(UT_HOST_PORT), 'P', myport);
229         printf (_("\
230  -d, --database=STRING\n\
231    Check database with indicated name\n\
232  -u, --username=STRING\n\
233    Connect using the indicated username\n\
234  -p, --password=STRING\n\
235    Use the indicated password to authenticate the connection\n\
236    ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
237    Your clear-text password will be visible as a process table entry\n"));
239         printf (_("\n\
240 There are no required arguments. By default, the local database with\n\
241 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
243         printf (_(UT_SUPPORT));
249 void
250 print_usage (void)
252         printf (_("\
253 Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"),
254                 progname);
255         printf (_(UT_HLP_VRS), progname, progname);