Code

fixes for using POSIX return codes
[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 #define PROGNAME "check_mysql"
19 #include "common.h"
20 #include "utils.h"
22 #include <mysql/mysql.h>
23 #include <mysql/errmsg.h>
25 char *db_user = NULL;
26 char *db_host = NULL;
27 char *db_pass = NULL;
28 char *db = NULL;
29 unsigned int db_port = MYSQL_PORT;
31 int process_arguments (int, char **);
32 int call_getopt (int, char **);
33 int validate_arguments (void);
34 int check_disk (int usp, int free_disk);
35 void print_help (void);
36 void print_usage (void);
38 int
39 main (int argc, char **argv)
40 {
42         MYSQL mysql;
43         char result[1024];
45         if (process_arguments (argc, argv) != OK)
46                 usage ("Invalid command arguments supplied\n");
48         /* initialize mysql  */
49         mysql_init (&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));
57                         return STATE_WARNING;
59                 }
60                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR) {
61                         printf ("%s\n", mysql_error (&mysql));
62                         return STATE_WARNING;
64                 }
65                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) {
66                         printf ("%s\n", mysql_error (&mysql));
67                         return STATE_WARNING;
69                 }
70                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) {
71                         printf ("%s\n", mysql_error (&mysql));
72                         return STATE_WARNING;
74                 }
75                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) {
76                         printf ("%s\n", mysql_error (&mysql));
77                         return STATE_WARNING;
79                 }
80                 else {
81                         printf ("%s\n", mysql_error (&mysql));
82                         return STATE_CRITICAL;
83                 }
85         }
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;
97                 }
98                 else if (mysql_errno (&mysql) == CR_SERVER_LOST) {
99                         printf ("%s\n", mysql_error (&mysql));
100                         return STATE_CRITICAL;
102                 }
103                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) {
104                         printf ("%s\n", mysql_error (&mysql));
105                         return STATE_UNKNOWN;
106                 }
108         }
110         /* close the connection */
111         mysql_close (&mysql);
113         /* print out the result of stats */
114         printf ("%s\n", result);
116         return STATE_OK;
123 /* process command-line arguments */
124 int
125 process_arguments (int argc, char **argv)
127         int c;
129         if (argc < 1)
130                 return ERROR;
132         c = 0;
133         while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
135                 if (is_option (argv[c]))
136                         continue;
138                 if (db_host == NULL)
139                         if (is_host (argv[c])) {
140                                 db_host = argv[c];
141                         }
142                         else {
143                                 usage ("Invalid host name");
144                         }
145                 else if (db_user == NULL)
146                         db_user = argv[c];
147                 else if (db_pass == NULL)
148                         db_pass = argv[c];
149                 else if (db == NULL)
150                         db = argv[c];
151                 else if (is_intnonneg (argv[c]))
152                         db_port = atoi (argv[c]);
153         }
155         if (db_host == NULL)
156                 db_host = strscpy (db_host, "127.0.0.1");
158         return validate_arguments ();
166 int
167 call_getopt (int argc, char **argv)
169         int c, i = 0;
171 #ifdef HAVE_GETOPT_H
172         int option_index = 0;
173         static struct option long_options[] = {
174                 {"hostname", required_argument, 0, 'H'},
175                 {"database", required_argument, 0, 'd'},
176                 {"username", required_argument, 0, 'u'},
177                 {"password", required_argument, 0, 'p'},
178                 {"port", required_argument, 0, 'P'},
179                 {"verbose", no_argument, 0, 'v'},
180                 {"version", no_argument, 0, 'V'},
181                 {"help", no_argument, 0, 'h'},
182                 {0, 0, 0, 0}
183         };
184 #endif
186         while (1) {
187 #ifdef HAVE_GETOPT_H
188                 c =
189                         getopt_long (argc, argv, "+hVP:p:u:d:H:", long_options, &option_index);
190 #else
191                 c = getopt (argc, argv, "+?hVP:p:u:d:H:");
192 #endif
194                 i++;
196                 if (c == -1 || c == EOF || c == 1)
197                         break;
199                 switch (c) {
200                 case 'P':
201                 case 'p':
202                 case 'u':
203                 case 'd':
204                 case 'H':
205                         i++;
206                 }
208                 switch (c) {
209                 case 'H':                                                                       /* hostname */
210                         if (is_host (optarg)) {
211                                 db_host = optarg;
212                         }
213                         else {
214                                 usage ("Invalid host name\n");
215                         }
216                         break;
217                 case 'd':                                                                       /* hostname */
218                         db = optarg;
219                         break;
220                 case 'u':                                                                       /* username */
221                         db_user = optarg;
222                         break;
223                 case 'p':                                                                       /* authentication information: password */
224                         db_pass = optarg;
225                         break;
226                 case 'P':                                                                       /* critical time threshold */
227                         db_port = atoi (optarg);
228                         break;
229                 case 'V':                                                                       /* version */
230                         print_revision (my_basename (argv[0]), "$Revision$");
231                         exit (STATE_OK);
232                 case 'h':                                                                       /* help */
233                         print_help ();
234                         exit (STATE_OK);
235                 case '?':                                                                       /* help */
236                         usage ("Invalid argument\n");
237                 }
238         }
239         return i;
246 int
247 validate_arguments (void)
249         return OK;
256 void
257 print_help (void)
259         print_revision (PROGNAME, "$Revision$");
260         printf
261                 ("Copyright (c) 2000 Didi Rieder/Karl DeBisschop\n\n"
262                  "This plugin is for testing a mysql server.\n");
263         print_usage ();
264         printf
265                 ("\nThere are no required arguments. By default, the local database with\n"
266                  "a server listening on MySQL standard port %d will be checked\n\n"
267                  "Options:\n"
268                  " -d, --database=STRING\n"
269                  "   Check database with indicated name\n"
270                  " -H, --hostname=STRING or IPADDRESS\n"
271                  "   Check server on the indicated host\n"
272                  " -P, --port=INTEGER\n"
273                  "   Make connection on the indicated port\n"
274                  " -u, --username=STRING\n"
275                  "   Connect using the indicated username\n"
276                  " -p, --password=STRING\n"
277                  "   Use the indicated password to authenticate the connection\n"
278                  "   ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n"
279                  "   Your clear-text password will be visible as a process table entry\n"
280                  " -h, --help\n"
281                  "    Print detailed help screen\n"
282                  " -V, --version\n" "    Print version information\n\n", MYSQL_PORT);
283         support ();
290 void
291 print_usage (void)
293         printf
294                 ("Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"
295                  "       %s --help\n"
296                  "       %s --version\n", PROGNAME, PROGNAME, PROGNAME);