Code

semicolon needed where progname define was replaced
[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 #define REVISION "$Revision$"
19 #define COPYRIGHT "1999-2002"
21 #include "common.h"
22 #include "utils.h"
24 #include <mysql/mysql.h>
25 #include <mysql/errmsg.h>
27 char *db_user = "";
28 char *db_host = "";
29 char *db_pass = "";
30 char *db = "";
31 unsigned int db_port = MYSQL_PORT;
33 int process_arguments (int, char **);
34 int validate_arguments (void);
35 int check_disk (int usp, int free_disk);
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[1024];
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
54                         (&mysql, db_host, db_user, db_pass, db, db_port, NULL, 0)) {
56                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) {
57                         printf ("%s\n", mysql_error (&mysql));
58                         return STATE_WARNING;
60                 }
61                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR) {
62                         printf ("%s\n", mysql_error (&mysql));
63                         return STATE_WARNING;
65                 }
66                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) {
67                         printf ("%s\n", mysql_error (&mysql));
68                         return STATE_WARNING;
70                 }
71                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) {
72                         printf ("%s\n", mysql_error (&mysql));
73                         return STATE_WARNING;
75                 }
76                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) {
77                         printf ("%s\n", mysql_error (&mysql));
78                         return STATE_WARNING;
80                 }
81                 else {
82                         printf ("%s\n", mysql_error (&mysql));
83                         return STATE_CRITICAL;
84                 }
86         }
88         /* get the server stats */
89         sprintf (result, mysql_stat (&mysql));
91         /* error checking once more */
92         if (mysql_error (&mysql)) {
94                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) {
95                         printf ("%s\n", mysql_error (&mysql));
96                         return STATE_CRITICAL;
98                 }
99                 else if (mysql_errno (&mysql) == CR_SERVER_LOST) {
100                         printf ("%s\n", mysql_error (&mysql));
101                         return STATE_CRITICAL;
103                 }
104                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) {
105                         printf ("%s\n", mysql_error (&mysql));
106                         return STATE_UNKNOWN;
107                 }
109         }
111         /* close the connection */
112         mysql_close (&mysql);
114         /* print out the result of stats */
115         printf ("%s\n", result);
117         return STATE_OK;
124 /* process command-line arguments */
125 int
126 process_arguments (int argc, char **argv)
128         int c;
130 #ifdef HAVE_GETOPT_H
131         int option_index = 0;
132         static struct option long_options[] = {
133                 {"hostname", required_argument, 0, 'H'},
134                 {"database", required_argument, 0, 'd'},
135                 {"username", required_argument, 0, 'u'},
136                 {"password", required_argument, 0, 'p'},
137                 {"port", required_argument, 0, 'P'},
138                 {"verbose", no_argument, 0, 'v'},
139                 {"version", no_argument, 0, 'V'},
140                 {"help", no_argument, 0, 'h'},
141                 {0, 0, 0, 0}
142         };
143 #endif
145         if (argc < 1)
146                 return ERROR;
148         while (1) {
149 #ifdef HAVE_GETOPT_H
150                 c =
151                         getopt_long (argc, argv, "hVP:p:u:d:H:", long_options, &option_index);
152 #else
153                 c = getopt (argc, argv, "hVP:p:u:d:H:");
154 #endif
156                 if (c == -1 || c == EOF)
157                         break;
159                 switch (c) {
160                 case 'H':                                                                       /* hostname */
161                         if (is_host (optarg)) {
162                                 db_host = optarg;
163                         }
164                         else {
165                                 usage ("Invalid host name\n");
166                         }
167                         break;
168                 case 'd':                                                                       /* hostname */
169                         db = optarg;
170                         break;
171                 case 'u':                                                                       /* username */
172                         db_user = optarg;
173                         break;
174                 case 'p':                                                                       /* authentication information: password */
175                         db_pass = optarg;
176                         break;
177                 case 'P':                                                                       /* critical time threshold */
178                         db_port = atoi (optarg);
179                         break;
180                 case 'V':                                                                       /* version */
181                         print_revision (progname, REVISION);
182                         exit (STATE_OK);
183                 case 'h':                                                                       /* help */
184                         print_help ();
185                         exit (STATE_OK);
186                 case '?':                                                                       /* help */
187                         usage ("Invalid argument\n");
188                 }
189         }
191         c = optind;
193         if (strlen(db_host) == 0 && argc > c)
194                 if (is_host (argv[c])) {
195                         db_host = argv[c++];
196                 }
197                 else {
198                         usage ("Invalid host name");
199                 }
201         if (strlen(db_user) == 0 && argc > c)
202                 db_user = argv[c++];
204         if (strlen(db_pass) == 0 && argc > c)
205                 db_pass = argv[c++];
207         if (strlen(db) == 0 && argc > c)
208                 db = argv[c++];
210         if (is_intnonneg (argv[c]))
211                 db_port = atoi (argv[c++]);
213         return validate_arguments ();
220 int
221 validate_arguments (void)
223         return OK;
230 void
231 print_help (void)
233         print_revision (progname, REVISION);
234         printf
235                 ("Copyright (c) 2000 Didi Rieder/Karl DeBisschop\n\n"
236                  "This plugin is for testing a mysql server.\n");
237         print_usage ();
238         printf
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"
241                  "Options:\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"
254                  " -h, --help\n"
255                  "    Print detailed help screen\n"
256                  " -V, --version\n" "    Print version information\n\n", MYSQL_PORT);
257         support ();
264 void
265 print_usage (void)
267         printf
268                 ("Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"
269                  "       %s --help\n"
270                  "       %s --version\n", progname, progname, progname);