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;
117 }
123 /* process command-line arguments */
124 int
125 process_arguments (int argc, char **argv)
126 {
127 int c;
129 int option_index = 0;
130 static struct option long_options[] = {
131 {"hostname", required_argument, 0, 'H'},
132 {"database", required_argument, 0, 'd'},
133 {"username", required_argument, 0, 'u'},
134 {"password", required_argument, 0, 'p'},
135 {"port", required_argument, 0, 'P'},
136 {"verbose", no_argument, 0, 'v'},
137 {"version", no_argument, 0, 'V'},
138 {"help", no_argument, 0, 'h'},
139 {0, 0, 0, 0}
140 };
142 if (argc < 1)
143 return ERROR;
145 while (1) {
146 c = getopt_long (argc, argv, "hVP:p:u:d:H:", long_options, &option_index);
148 if (c == -1 || c == EOF)
149 break;
151 switch (c) {
152 case 'H': /* hostname */
153 if (is_host (optarg)) {
154 db_host = optarg;
155 }
156 else {
157 usage ("Invalid host name\n");
158 }
159 break;
160 case 'd': /* hostname */
161 db = optarg;
162 break;
163 case 'u': /* username */
164 db_user = optarg;
165 break;
166 case 'p': /* authentication information: password */
167 db_pass = optarg;
168 break;
169 case 'P': /* critical time threshold */
170 db_port = atoi (optarg);
171 break;
172 case 'V': /* version */
173 print_revision (progname, REVISION);
174 exit (STATE_OK);
175 case 'h': /* help */
176 print_help ();
177 exit (STATE_OK);
178 case '?': /* help */
179 usage ("Invalid argument\n");
180 }
181 }
183 c = optind;
185 while ( argc > c ) {
187 if (strlen(db_host) == 0)
188 if (is_host (argv[c])) {
189 db_host = argv[c++];
190 }
191 else {
192 usage ("Invalid host name");
193 }
194 else if (strlen(db_user) == 0)
195 db_user = argv[c++];
196 else if (strlen(db_pass) == 0)
197 db_pass = argv[c++];
198 else if (strlen(db) == 0)
199 db = argv[c++];
200 else if (is_intnonneg (argv[c]))
201 db_port = atoi (argv[c++]);
202 else
203 break;
204 }
206 return validate_arguments ();
207 }
213 int
214 validate_arguments (void)
215 {
216 return OK;
217 }
223 void
224 print_help (void)
225 {
226 print_revision (progname, REVISION);
227 printf
228 ("Copyright (c) 2000 Didi Rieder/Karl DeBisschop\n\n"
229 "This plugin is for testing a mysql server.\n");
230 print_usage ();
231 printf
232 ("\nThere are no required arguments. By default, the local database with\n"
233 "a server listening on MySQL standard port %d will be checked\n\n"
234 "Options:\n"
235 " -d, --database=STRING\n"
236 " Check database with indicated name\n"
237 " -H, --hostname=STRING or IPADDRESS\n"
238 " Check server on the indicated host\n"
239 " -P, --port=INTEGER\n"
240 " Make connection on the indicated port\n"
241 " -u, --username=STRING\n"
242 " Connect using the indicated username\n"
243 " -p, --password=STRING\n"
244 " Use the indicated password to authenticate the connection\n"
245 " ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n"
246 " Your clear-text password will be visible as a process table entry\n"
247 " -h, --help\n"
248 " Print detailed help screen\n"
249 " -V, --version\n" " Print version information\n\n", MYSQL_PORT);
250 support ();
251 }
257 void
258 print_usage (void)
259 {
260 printf
261 ("Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"
262 " %s --help\n"
263 " %s --version\n", progname, progname, progname);
264 }