Code

remove incorrect check_disk() declaration
[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 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 #ifdef HAVE_GETOPT_H
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'},
140                 {0, 0, 0, 0}
141         };
142 #endif
144         if (argc < 1)
145                 return ERROR;
147         while (1) {
148 #ifdef HAVE_GETOPT_H
149                 c =
150                         getopt_long (argc, argv, "hVP:p:u:d:H:", long_options, &option_index);
151 #else
152                 c = getopt (argc, argv, "hVP:p:u:d:H:");
153 #endif
155                 if (c == -1 || c == EOF)
156                         break;
158                 switch (c) {
159                 case 'H':                                                                       /* hostname */
160                         if (is_host (optarg)) {
161                                 db_host = optarg;
162                         }
163                         else {
164                                 usage ("Invalid host name\n");
165                         }
166                         break;
167                 case 'd':                                                                       /* hostname */
168                         db = optarg;
169                         break;
170                 case 'u':                                                                       /* username */
171                         db_user = optarg;
172                         break;
173                 case 'p':                                                                       /* authentication information: password */
174                         db_pass = optarg;
175                         break;
176                 case 'P':                                                                       /* critical time threshold */
177                         db_port = atoi (optarg);
178                         break;
179                 case 'V':                                                                       /* version */
180                         print_revision (progname, REVISION);
181                         exit (STATE_OK);
182                 case 'h':                                                                       /* help */
183                         print_help ();
184                         exit (STATE_OK);
185                 case '?':                                                                       /* help */
186                         usage ("Invalid argument\n");
187                 }
188         }
190         c = optind;
192         if (strlen(db_host) == 0 && argc > c)
193                 if (is_host (argv[c])) {
194                         db_host = argv[c++];
195                 }
196                 else {
197                         usage ("Invalid host name");
198                 }
200         if (strlen(db_user) == 0 && argc > c)
201                 db_user = argv[c++];
203         if (strlen(db_pass) == 0 && argc > c)
204                 db_pass = argv[c++];
206         if (strlen(db) == 0 && argc > c)
207                 db = argv[c++];
209         if (is_intnonneg (argv[c]) && argc > c)
210                 db_port = atoi (argv[c++]);
212         return validate_arguments ();
219 int
220 validate_arguments (void)
222         return OK;
229 void
230 print_help (void)
232         print_revision (progname, REVISION);
233         printf
234                 ("Copyright (c) 2000 Didi Rieder/Karl DeBisschop\n\n"
235                  "This plugin is for testing a mysql server.\n");
236         print_usage ();
237         printf
238                 ("\nThere are no required arguments. By default, the local database with\n"
239                  "a server listening on MySQL standard port %d will be checked\n\n"
240                  "Options:\n"
241                  " -d, --database=STRING\n"
242                  "   Check database with indicated name\n"
243                  " -H, --hostname=STRING or IPADDRESS\n"
244                  "   Check server on the indicated host\n"
245                  " -P, --port=INTEGER\n"
246                  "   Make connection on the indicated port\n"
247                  " -u, --username=STRING\n"
248                  "   Connect using the indicated username\n"
249                  " -p, --password=STRING\n"
250                  "   Use the indicated password to authenticate the connection\n"
251                  "   ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n"
252                  "   Your clear-text password will be visible as a process table entry\n"
253                  " -h, --help\n"
254                  "    Print detailed help screen\n"
255                  " -V, --version\n" "    Print version information\n\n", MYSQL_PORT);
256         support ();
263 void
264 print_usage (void)
266         printf
267                 ("Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"
268                  "       %s --help\n"
269                  "       %s --version\n", progname, progname, progname);