Code

markup for translation
[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 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 void
29 print_usage (void)
30 {
31         printf (_("\
32 Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"),
33                 progname);
34         printf (_(UT_HLP_VRS), progname, progname);
35 }
37 void
38 print_help (void)
39 {
40         char *myport;
41         asprintf (&myport, "%d", MYSQL_PORT);
43         print_revision (progname, revision);
45         printf (_(COPYRIGHT), copyright, email);
47         printf (_("This program tests connections to a mysql server\n"));
49         print_usage ();
51         printf (_(UT_HELP_VRSN));
53         printf (_(UT_HOST_PORT), 'P', myport);
55         printf (_("\
56  -d, --database=STRING\n\
57    Check database with indicated name\n\
58  -u, --username=STRING\n\
59    Connect using the indicated username\n\
60  -p, --password=STRING\n\
61    Use the indicated password to authenticate the connection\n\
62    ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
63    Your clear-text password will be visible as a process table entry\n"));
65         printf (_("\n\
66 There are no required arguments. By default, the local database with\n\
67 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
69         printf (_(UT_SUPPORT));
70 }
71 \f
72 char *db_user = "";
73 char *db_host = "";
74 char *db_pass = "";
75 char *db = "";
76 unsigned int db_port = MYSQL_PORT;
78 int process_arguments (int, char **);
79 int validate_arguments (void);
81 int
82 main (int argc, char **argv)
83 {
85         MYSQL mysql;
86         char result[1024];
88         if (process_arguments (argc, argv) != OK)
89                 usage (_("Invalid command arguments supplied\n"));
91         /* initialize mysql  */
92         mysql_init (&mysql);
94         /* establish a connection to the server and error checking */
95         if (!mysql_real_connect
96                         (&mysql, db_host, db_user, db_pass, db, db_port, NULL, 0)) {
98                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) {
99                         printf ("%s\n", mysql_error (&mysql));
100                         return STATE_WARNING;
102                 }
103                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR) {
104                         printf ("%s\n", mysql_error (&mysql));
105                         return STATE_WARNING;
107                 }
108                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) {
109                         printf ("%s\n", mysql_error (&mysql));
110                         return STATE_WARNING;
112                 }
113                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) {
114                         printf ("%s\n", mysql_error (&mysql));
115                         return STATE_WARNING;
117                 }
118                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) {
119                         printf ("%s\n", mysql_error (&mysql));
120                         return STATE_WARNING;
122                 }
123                 else {
124                         printf ("%s\n", mysql_error (&mysql));
125                         return STATE_CRITICAL;
126                 }
128         }
130         /* get the server stats */
131         sprintf (result, mysql_stat (&mysql));
133         /* error checking once more */
134         if (mysql_error (&mysql)) {
136                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) {
137                         printf ("%s\n", mysql_error (&mysql));
138                         return STATE_CRITICAL;
140                 }
141                 else if (mysql_errno (&mysql) == CR_SERVER_LOST) {
142                         printf ("%s\n", mysql_error (&mysql));
143                         return STATE_CRITICAL;
145                 }
146                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) {
147                         printf ("%s\n", mysql_error (&mysql));
148                         return STATE_UNKNOWN;
149                 }
151         }
153         /* close the connection */
154         mysql_close (&mysql);
156         /* print out the result of stats */
157         printf ("%s\n", result);
159         return STATE_OK;
166 /* process command-line arguments */
167 int
168 process_arguments (int argc, char **argv)
170         int c;
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         };
185         if (argc < 1)
186                 return ERROR;
188         while (1) {
189                 c = getopt_long (argc, argv, "hVP:p:u:d:H:", long_options, &option_index);
191                 if (c == -1 || c == EOF)
192                         break;
194                 switch (c) {
195                 case 'H':                                                                       /* hostname */
196                         if (is_host (optarg)) {
197                                 db_host = optarg;
198                         }
199                         else {
200                                 usage (_("Invalid host name\n"));
201                         }
202                         break;
203                 case 'd':                                                                       /* hostname */
204                         db = optarg;
205                         break;
206                 case 'u':                                                                       /* username */
207                         db_user = optarg;
208                         break;
209                 case 'p':                                                                       /* authentication information: password */
210                         db_pass = optarg;
211                         break;
212                 case 'P':                                                                       /* critical time threshold */
213                         db_port = atoi (optarg);
214                         break;
215                 case 'V':                                                                       /* version */
216                         print_revision (progname, revision);
217                         exit (STATE_OK);
218                 case 'h':                                                                       /* help */
219                         print_help ();
220                         exit (STATE_OK);
221                 case '?':                                                                       /* help */
222                         usage (_("Invalid argument\n"));
223                 }
224         }
226         c = optind;
228         while ( argc > c ) {
230                 if (strlen(db_host) == 0)
231                         if (is_host (argv[c])) {
232                                 db_host = argv[c++];
233                         }
234                         else {
235                                 usage ("Invalid host name");
236                         }
237                 else if (strlen(db_user) == 0)
238                         db_user = argv[c++];
239                 else if (strlen(db_pass) == 0)
240                         db_pass = argv[c++];
241                 else if (strlen(db) == 0)
242                         db = argv[c++];
243                 else if (is_intnonneg (argv[c]))
244                         db_port = atoi (argv[c++]);
245                 else
246                         break;
247         }
249         return validate_arguments ();
256 int
257 validate_arguments (void)
259         return OK;