~ubuntu-branches/ubuntu/precise/nagios-plugins/precise-proposed

« back to all changes in this revision

Viewing changes to plugins/check_mysql.c

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter
  • Date: 2004-06-15 15:37:48 UTC
  • Revision ID: james.westby@ubuntu.com-20040615153748-pq7702qdzghqfcns
Tags: upstream-1.3.1.0
ImportĀ upstreamĀ versionĀ 1.3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: check_mysql.c,v 1.7 2003/02/11 00:47:47 tonvoon Exp $
 
11
*
 
12
* Description:
 
13
*
 
14
* This plugin is for testing a mysql server.
 
15
******************************************************************************/
 
16
 
 
17
const char *progname = "check_mysql";
 
18
#define REVISION "$Revision: 1.7 $"
 
19
#define COPYRIGHT "1999-2002"
 
20
 
 
21
#include "common.h"
 
22
#include "utils.h"
 
23
 
 
24
#include <mysql/mysql.h>
 
25
#include <mysql/errmsg.h>
 
26
 
 
27
char *db_user = "";
 
28
char *db_host = "";
 
29
char *db_pass = "";
 
30
char *db = "";
 
31
unsigned int db_port = MYSQL_PORT;
 
32
 
 
33
int process_arguments (int, char **);
 
34
int validate_arguments (void);
 
35
void print_help (void);
 
36
void print_usage (void);
 
37
 
 
38
int
 
39
main (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 */
 
124
int
 
125
process_arguments (int argc, char **argv)
 
126
{
 
127
        int c;
 
128
 
 
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
 
143
 
 
144
        if (argc < 1)
 
145
                return ERROR;
 
146
 
 
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
 
154
 
 
155
                if (c == -1 || c == EOF)
 
156
                        break;
 
157
 
 
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
        }
 
189
 
 
190
        c = optind;
 
191
 
 
192
        while ( argc > c ) {
 
193
 
 
194
                if (strlen(db_host) == 0)
 
195
                        if (is_host (argv[c])) {
 
196
                                db_host = argv[c++];
 
197
                        }
 
198
                        else {
 
199
                                usage ("Invalid host name");
 
200
                        }
 
201
                else if (strlen(db_user) == 0)
 
202
                        db_user = argv[c++];
 
203
                else if (strlen(db_pass) == 0)
 
204
                        db_pass = argv[c++];
 
205
                else if (strlen(db) == 0)
 
206
                        db = argv[c++];
 
207
                else if (is_intnonneg (argv[c]))
 
208
                        db_port = atoi (argv[c++]);
 
209
                else
 
210
                        break;
 
211
        }
 
212
 
 
213
        return validate_arguments ();
 
214
}
 
215
 
 
216
 
 
217
 
 
218
 
 
219
 
 
220
int
 
221
validate_arguments (void)
 
222
{
 
223
        return OK;
 
224
}
 
225
 
 
226
 
 
227
 
 
228
 
 
229
 
 
230
void
 
231
print_help (void)
 
232
{
 
233
        print_revision (progname, REVISION);
 
234
        printf
 
235
                ("Copyright (c) 2000 Didi Rieder/Karl DeBisschop\n\n"
 
236
                 "This plugin is for testing a mysql server.\n");
 
237
        print_usage ();
 
238
        printf
 
239
                ("\nThere are no required arguments. By default, the local database with\n"
 
240
                 "a server listening on MySQL standard port %d will be checked\n\n"
 
241
                 "Options:\n"
 
242
                 " -d, --database=STRING\n"
 
243
                 "   Check database with indicated name\n"
 
244
                 " -H, --hostname=STRING or IPADDRESS\n"
 
245
                 "   Check server on the indicated host\n"
 
246
                 " -P, --port=INTEGER\n"
 
247
                 "   Make connection on the indicated port\n"
 
248
                 " -u, --username=STRING\n"
 
249
                 "   Connect using the indicated username\n"
 
250
                 " -p, --password=STRING\n"
 
251
                 "   Use the indicated password to authenticate the connection\n"
 
252
                 "   ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n"
 
253
                 "   Your clear-text password will be visible as a process table entry\n"
 
254
                 " -h, --help\n"
 
255
                 "    Print detailed help screen\n"
 
256
                 " -V, --version\n" "    Print version information\n\n", MYSQL_PORT);
 
257
        support ();
 
258
}
 
259
 
 
260
 
 
261
 
 
262
 
 
263
 
 
264
void
 
265
print_usage (void)
 
266
{
 
267
        printf
 
268
                ("Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"
 
269
                 "       %s --help\n"
 
270
                 "       %s --version\n", progname, progname, progname);
 
271
}