Code

the last round of pedantic compiler warnings
[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         if (process_arguments (argc, argv) != OK)
47                 usage (_("Invalid command arguments supplied\n"));
49         /* initialize mysql  */
50         mysql_init (&mysql);
52         /* establish a connection to the server and error checking */
53         if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
54                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
55                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
56                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
57                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
58                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
59                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
60                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
61                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
62                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
63                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
64                 else
65                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
66         }
68         /* get the server stats */
69         result = strdup (mysql_stat (&mysql));
71         /* error checking once more */
72         if (mysql_error (&mysql)) {
73                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
74                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
75                 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
76                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
77                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
78                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
79         }
81         /* close the connection */
82         mysql_close (&mysql);
84         /* print out the result of stats */
85         printf ("%s\n", result);
87         return STATE_OK;
88 }
94 /* process command-line arguments */
95 int
96 process_arguments (int argc, char **argv)
97 {
98         int c;
100         int option = 0;
101         static struct option longopts[] = {
102                 {"hostname", required_argument, 0, 'H'},
103                 {"database", required_argument, 0, 'd'},
104                 {"username", required_argument, 0, 'u'},
105                 {"password", required_argument, 0, 'p'},
106                 {"port", required_argument, 0, 'P'},
107                 {"verbose", no_argument, 0, 'v'},
108                 {"version", no_argument, 0, 'V'},
109                 {"help", no_argument, 0, 'h'},
110                 {0, 0, 0, 0}
111         };
113         if (argc < 1)
114                 return ERROR;
116         while (1) {
117                 c = getopt_long (argc, argv, "hVP:p:u:d:H:", longopts, &option);
119                 if (c == -1 || c == EOF)
120                         break;
122                 switch (c) {
123                 case 'H':                                                                       /* hostname */
124                         if (is_host (optarg)) {
125                                 db_host = optarg;
126                         }
127                         else {
128                                 usage (_("Invalid host name\n"));
129                         }
130                         break;
131                 case 'd':                                                                       /* hostname */
132                         db = optarg;
133                         break;
134                 case 'u':                                                                       /* username */
135                         db_user = optarg;
136                         break;
137                 case 'p':                                                                       /* authentication information: password */
138                         db_pass = optarg;
139                         break;
140                 case 'P':                                                                       /* critical time threshold */
141                         db_port = atoi (optarg);
142                         break;
143                 case 'V':                                                                       /* version */
144                         print_revision (progname, revision);
145                         exit (STATE_OK);
146                 case 'h':                                                                       /* help */
147                         print_help ();
148                         exit (STATE_OK);
149                 case '?':                                                                       /* help */
150                         usage (_("Invalid argument\n"));
151                 }
152         }
154         c = optind;
156         while ( argc > c ) {
158                 if (strlen(db_host) == 0)
159                         if (is_host (argv[c])) {
160                                 db_host = argv[c++];
161                         }
162                         else {
163                                 usage ("Invalid host name");
164                         }
165                 else if (strlen(db_user) == 0)
166                         db_user = argv[c++];
167                 else if (strlen(db_pass) == 0)
168                         db_pass = argv[c++];
169                 else if (strlen(db) == 0)
170                         db = argv[c++];
171                 else if (is_intnonneg (argv[c]))
172                         db_port = atoi (argv[c++]);
173                 else
174                         break;
175         }
177         return validate_arguments ();
184 int
185 validate_arguments (void)
187         if (db_user == NULL)
188                 db_user = strdup("");
190         if (db_host == NULL)
191                 db_host = strdup("");
193         if (db_pass == NULL)
194                 db_pass == strdup("");
196         if (db == NULL)
197                 db = strdup("");
199         return OK;
206 \f
207 void
208 print_help (void)
210         char *myport;
211         asprintf (&myport, "%d", MYSQL_PORT);
213         print_revision (progname, revision);
215         printf (_(COPYRIGHT), copyright, email);
217         printf (_("This program tests connections to a mysql server\n"));
219         print_usage ();
221         printf (_(UT_HELP_VRSN));
223         printf (_(UT_HOST_PORT), 'P', myport);
225         printf (_("\
226  -d, --database=STRING\n\
227    Check database with indicated name\n\
228  -u, --username=STRING\n\
229    Connect using the indicated username\n\
230  -p, --password=STRING\n\
231    Use the indicated password to authenticate the connection\n\
232    ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
233    Your clear-text password will be visible as a process table entry\n"));
235         printf (_("\n\
236 There are no required arguments. By default, the local database with\n\
237 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
239         printf (_(UT_SUPPORT));
245 void
246 print_usage (void)
248         printf (_("\
249 Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"),
250                 progname);
251         printf (_(UT_HLP_VRS), progname, progname);