summaryrefslogtreecommitdiffstats
path: root/plugins/check_mysql.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_mysql.c')
-rw-r--r--plugins/check_mysql.c297
1 files changed, 297 insertions, 0 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
new file mode 100644
index 00000000..50836f91
--- /dev/null
+++ b/plugins/check_mysql.c
@@ -0,0 +1,297 @@
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******************************************************************************/
16
17#define PROGNAME "check_mysql"
18
19#include "common.h"
20#include "utils.h"
21
22#include <mysql/mysql.h>
23#include <mysql/errmsg.h>
24
25char *db_user = NULL;
26char *db_host = NULL;
27char *db_pass = NULL;
28char *db = NULL;
29unsigned int db_port = MYSQL_PORT;
30
31int process_arguments (int, char **);
32int call_getopt (int, char **);
33int validate_arguments (void);
34int check_disk (int usp, int free_disk);
35void print_help (void);
36void print_usage (void);
37
38int
39main (int argc, char **argv)
40{
41
42 MYSQL mysql;
43 char result[1024];
44
45 if (process_arguments (argc, argv) != OK)
46 usage ("Invalid command arguments supplied\n");
47
48 /* initialize mysql */
49 mysql_init (&mysql);
50
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)) {
54
55 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) {
56 printf ("%s\n", mysql_error (&mysql));
57 return STATE_WARNING;
58
59 }
60 else if (mysql_errno (&mysql) == CR_VERSION_ERROR) {
61 printf ("%s\n", mysql_error (&mysql));
62 return STATE_WARNING;
63
64 }
65 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) {
66 printf ("%s\n", mysql_error (&mysql));
67 return STATE_WARNING;
68
69 }
70 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) {
71 printf ("%s\n", mysql_error (&mysql));
72 return STATE_WARNING;
73
74 }
75 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) {
76 printf ("%s\n", mysql_error (&mysql));
77 return STATE_WARNING;
78
79 }
80 else {
81 printf ("%s\n", mysql_error (&mysql));
82 return STATE_CRITICAL;
83 }
84
85 }
86
87 /* get the server stats */
88 sprintf (result, mysql_stat (&mysql));
89
90 /* error checking once more */
91 if (mysql_error (&mysql)) {
92
93 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) {
94 printf ("%s\n", mysql_error (&mysql));
95 return STATE_CRITICAL;
96
97 }
98 else if (mysql_errno (&mysql) == CR_SERVER_LOST) {
99 printf ("%s\n", mysql_error (&mysql));
100 return STATE_CRITICAL;
101
102 }
103 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) {
104 printf ("%s\n", mysql_error (&mysql));
105 return STATE_UNKNOWN;
106 }
107
108 }
109
110 /* close the connection */
111 mysql_close (&mysql);
112
113 /* print out the result of stats */
114 printf ("%s\n", result);
115
116 return STATE_OK;
117}
118
119
120
121
122
123/* process command-line arguments */
124int
125process_arguments (int argc, char **argv)
126{
127 int c;
128
129 if (argc < 1)
130 return ERROR;
131
132 c = 0;
133 while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
134
135 if (is_option (argv[c]))
136 continue;
137
138 if (db_host == NULL)
139 if (is_host (argv[c])) {
140 db_host = argv[c];
141 }
142 else {
143 usage ("Invalid host name");
144 }
145 else if (db_user == NULL)
146 db_user = argv[c];
147 else if (db_pass == NULL)
148 db_pass = argv[c];
149 else if (db == NULL)
150 db = argv[c];
151 else if (is_intnonneg (argv[c]))
152 db_port = atoi (argv[c]);
153 }
154
155 if (db_host == NULL)
156 db_host = strscpy (db_host, "127.0.0.1");
157
158 return validate_arguments ();
159}
160
161
162
163
164
165
166int
167call_getopt (int argc, char **argv)
168{
169 int c, i = 0;
170
171#ifdef HAVE_GETOPT_H
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 };
184#endif
185
186 while (1) {
187#ifdef HAVE_GETOPT_H
188 c =
189 getopt_long (argc, argv, "+hVP:p:u:d:H:", long_options, &option_index);
190#else
191 c = getopt (argc, argv, "+?hVP:p:u:d:H:");
192#endif
193
194 i++;
195
196 if (c == -1 || c == EOF || c == 1)
197 break;
198
199 switch (c) {
200 case 'P':
201 case 'p':
202 case 'u':
203 case 'd':
204 case 'H':
205 i++;
206 }
207
208 switch (c) {
209 case 'H': /* hostname */
210 if (is_host (optarg)) {
211 db_host = optarg;
212 }
213 else {
214 usage ("Invalid host name\n");
215 }
216 break;
217 case 'd': /* hostname */
218 db = optarg;
219 break;
220 case 'u': /* username */
221 db_user = optarg;
222 break;
223 case 'p': /* authentication information: password */
224 db_pass = optarg;
225 break;
226 case 'P': /* critical time threshold */
227 db_port = atoi (optarg);
228 break;
229 case 'V': /* version */
230 print_revision (my_basename (argv[0]), "$Revision$");
231 exit (STATE_OK);
232 case 'h': /* help */
233 print_help ();
234 exit (STATE_OK);
235 case '?': /* help */
236 usage ("Invalid argument\n");
237 }
238 }
239 return i;
240}
241
242
243
244
245
246int
247validate_arguments (void)
248{
249 return OK;
250}
251
252
253
254
255
256void
257print_help (void)
258{
259 print_revision (PROGNAME, "$Revision$");
260 printf
261 ("Copyright (c) 2000 Didi Rieder/Karl DeBisschop\n\n"
262 "This plugin is for testing a mysql server.\n");
263 print_usage ();
264 printf
265 ("\nThere are no required arguments. By default, the local database with\n"
266 "a server listening on MySQL standard port %d will be checked\n\n"
267 "Options:\n"
268 " -d, --database=STRING\n"
269 " Check database with indicated name\n"
270 " -H, --hostname=STRING or IPADDRESS\n"
271 " Check server on the indicated host\n"
272 " -P, --port=INTEGER\n"
273 " Make connection on the indicated port\n"
274 " -u, --username=STRING\n"
275 " Connect using the indicated username\n"
276 " -p, --password=STRING\n"
277 " Use the indicated password to authenticate the connection\n"
278 " ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n"
279 " Your clear-text password will be visible as a process table entry\n"
280 " -h, --help\n"
281 " Print detailed help screen\n"
282 " -V, --version\n" " Print version information\n\n", MYSQL_PORT);
283 support ();
284}
285
286
287
288
289
290void
291print_usage (void)
292{
293 printf
294 ("Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"
295 " %s --help\n"
296 " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
297}