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 unsigned int db_port = MYSQL_PORT;
30 void
31 print_usage (void)
32 {
33 printf (_("\
34 Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"),
35 progname);
36 printf (_(UT_HLP_VRS), progname, progname);
37 }
39 void
40 print_help (void)
41 {
42 print_revision (progname, revision);
44 printf (_(COPYRIGHT), copyright, email);
46 printf (_("This program tests connections to a mysql server\n"));
48 print_usage ();
50 printf (_(UT_HELP_VRSN));
52 printf (_(UT_HOST_PORT), 'P', atoi(MYSQL_PORT));
54 printf (_("\
55 -d, --database=STRING\n\
56 Check database with indicated name\n\
57 -u, --username=STRING\n\
58 Connect using the indicated username\n\
59 -p, --password=STRING\n\
60 Use the indicated password to authenticate the connection\n\
61 ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
62 Your clear-text password will be visible as a process table entry\n"));
64 printf (_("\n\
65 There are no required arguments. By default, the local database with\n\
66 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
68 printf (_(UT_SUPPORT));
69 }
70 \f
71 char *db_user = "";
72 char *db_host = "";
73 char *db_pass = "";
74 char *db = "";
76 int process_arguments (int, char **);
77 int validate_arguments (void);
79 int
80 main (int argc, char **argv)
81 {
83 MYSQL mysql;
84 char result[1024];
86 if (process_arguments (argc, argv) != OK)
87 usage (_("Invalid command arguments supplied\n"));
89 /* initialize mysql */
90 mysql_init (&mysql);
92 /* establish a connection to the server and error checking */
93 if (!mysql_real_connect
94 (&mysql, db_host, db_user, db_pass, db, db_port, NULL, 0)) {
96 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) {
97 printf ("%s\n", mysql_error (&mysql));
98 return STATE_WARNING;
100 }
101 else if (mysql_errno (&mysql) == CR_VERSION_ERROR) {
102 printf ("%s\n", mysql_error (&mysql));
103 return STATE_WARNING;
105 }
106 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) {
107 printf ("%s\n", mysql_error (&mysql));
108 return STATE_WARNING;
110 }
111 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) {
112 printf ("%s\n", mysql_error (&mysql));
113 return STATE_WARNING;
115 }
116 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) {
117 printf ("%s\n", mysql_error (&mysql));
118 return STATE_WARNING;
120 }
121 else {
122 printf ("%s\n", mysql_error (&mysql));
123 return STATE_CRITICAL;
124 }
126 }
128 /* get the server stats */
129 sprintf (result, mysql_stat (&mysql));
131 /* error checking once more */
132 if (mysql_error (&mysql)) {
134 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) {
135 printf ("%s\n", mysql_error (&mysql));
136 return STATE_CRITICAL;
138 }
139 else if (mysql_errno (&mysql) == CR_SERVER_LOST) {
140 printf ("%s\n", mysql_error (&mysql));
141 return STATE_CRITICAL;
143 }
144 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) {
145 printf ("%s\n", mysql_error (&mysql));
146 return STATE_UNKNOWN;
147 }
149 }
151 /* close the connection */
152 mysql_close (&mysql);
154 /* print out the result of stats */
155 printf ("%s\n", result);
157 return STATE_OK;
158 }
164 /* process command-line arguments */
165 int
166 process_arguments (int argc, char **argv)
167 {
168 int c;
170 int option_index = 0;
171 static struct option long_options[] = {
172 {"hostname", required_argument, 0, 'H'},
173 {"database", required_argument, 0, 'd'},
174 {"username", required_argument, 0, 'u'},
175 {"password", required_argument, 0, 'p'},
176 {"port", required_argument, 0, 'P'},
177 {"verbose", no_argument, 0, 'v'},
178 {"version", no_argument, 0, 'V'},
179 {"help", no_argument, 0, 'h'},
180 {0, 0, 0, 0}
181 };
183 if (argc < 1)
184 return ERROR;
186 while (1) {
187 c = getopt_long (argc, argv, "hVP:p:u:d:H:", long_options, &option_index);
189 if (c == -1 || c == EOF)
190 break;
192 switch (c) {
193 case 'H': /* hostname */
194 if (is_host (optarg)) {
195 db_host = optarg;
196 }
197 else {
198 usage (_("Invalid host name\n"));
199 }
200 break;
201 case 'd': /* hostname */
202 db = optarg;
203 break;
204 case 'u': /* username */
205 db_user = optarg;
206 break;
207 case 'p': /* authentication information: password */
208 db_pass = optarg;
209 break;
210 case 'P': /* critical time threshold */
211 db_port = atoi (optarg);
212 break;
213 case 'V': /* version */
214 print_revision (progname, revision);
215 exit (STATE_OK);
216 case 'h': /* help */
217 print_help ();
218 exit (STATE_OK);
219 case '?': /* help */
220 usage (_("Invalid argument\n"));
221 }
222 }
224 c = optind;
226 while ( argc > c ) {
228 if (strlen(db_host) == 0)
229 if (is_host (argv[c])) {
230 db_host = argv[c++];
231 }
232 else {
233 usage ("Invalid host name");
234 }
235 else if (strlen(db_user) == 0)
236 db_user = argv[c++];
237 else if (strlen(db_pass) == 0)
238 db_pass = argv[c++];
239 else if (strlen(db) == 0)
240 db = argv[c++];
241 else if (is_intnonneg (argv[c]))
242 db_port = atoi (argv[c++]);
243 else
244 break;
245 }
247 return validate_arguments ();
248 }
254 int
255 validate_arguments (void)
256 {
257 return OK;
258 }