diff options
Diffstat (limited to 'plugins/check_mysql.c')
-rw-r--r-- | plugins/check_mysql.c | 196 |
1 files changed, 94 insertions, 102 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index 16a1f133..a7afa52f 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c | |||
@@ -25,65 +25,23 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; | |||
25 | #include <mysql/mysql.h> | 25 | #include <mysql/mysql.h> |
26 | #include <mysql/errmsg.h> | 26 | #include <mysql/errmsg.h> |
27 | 27 | ||
28 | void | 28 | char *db_user = NULL; |
29 | print_usage (void) | 29 | char *db_host = NULL; |
30 | { | 30 | char *db_pass = NULL; |
31 | printf (_("\ | 31 | char *db = NULL; |
32 | Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"), | ||
33 | progname); | ||
34 | printf (_(UT_HLP_VRS), progname, progname); | ||
35 | } | ||
36 | |||
37 | void | ||
38 | print_help (void) | ||
39 | { | ||
40 | char *myport; | ||
41 | asprintf (&myport, "%d", MYSQL_PORT); | ||
42 | |||
43 | print_revision (progname, revision); | ||
44 | |||
45 | printf (_(COPYRIGHT), copyright, email); | ||
46 | |||
47 | printf (_("This program tests connections to a mysql server\n")); | ||
48 | |||
49 | print_usage (); | ||
50 | |||
51 | printf (_(UT_HELP_VRSN)); | ||
52 | |||
53 | printf (_(UT_HOST_PORT), 'P', myport); | ||
54 | |||
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")); | ||
64 | |||
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); | ||
68 | |||
69 | printf (_(UT_SUPPORT)); | ||
70 | } | ||
71 | |||
72 | char *db_user = ""; | ||
73 | char *db_host = ""; | ||
74 | char *db_pass = ""; | ||
75 | char *db = ""; | ||
76 | unsigned int db_port = MYSQL_PORT; | 32 | unsigned int db_port = MYSQL_PORT; |
77 | 33 | ||
78 | int process_arguments (int, char **); | 34 | int process_arguments (int, char **); |
79 | int validate_arguments (void); | 35 | int validate_arguments (void); |
36 | void print_help (void); | ||
37 | void print_usage (void); | ||
80 | 38 | ||
81 | int | 39 | int |
82 | main (int argc, char **argv) | 40 | main (int argc, char **argv) |
83 | { | 41 | { |
84 | 42 | ||
85 | MYSQL mysql; | 43 | MYSQL mysql; |
86 | char result[1024]; | 44 | char *result = NULL; |
87 | 45 | ||
88 | if (process_arguments (argc, argv) != OK) | 46 | if (process_arguments (argc, argv) != OK) |
89 | usage (_("Invalid command arguments supplied\n")); | 47 | usage (_("Invalid command arguments supplied\n")); |
@@ -92,62 +50,32 @@ main (int argc, char **argv) | |||
92 | mysql_init (&mysql); | 50 | mysql_init (&mysql); |
93 | 51 | ||
94 | /* establish a connection to the server and error checking */ | 52 | /* establish a connection to the server and error checking */ |
95 | if (!mysql_real_connect | 53 | if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) { |
96 | (&mysql, db_host, db_user, db_pass, db, db_port, NULL, 0)) { | 54 | if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) |
97 | 55 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | |
98 | if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) { | 56 | else if (mysql_errno (&mysql) == CR_VERSION_ERROR) |
99 | printf ("%s\n", mysql_error (&mysql)); | 57 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); |
100 | return STATE_WARNING; | 58 | else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) |
101 | 59 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | |
102 | } | 60 | else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) |
103 | else if (mysql_errno (&mysql) == CR_VERSION_ERROR) { | 61 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); |
104 | printf ("%s\n", mysql_error (&mysql)); | 62 | else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) |
105 | return STATE_WARNING; | 63 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); |
106 | 64 | else | |
107 | } | 65 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); |
108 | else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) { | ||
109 | printf ("%s\n", mysql_error (&mysql)); | ||
110 | return STATE_WARNING; | ||
111 | |||
112 | } | ||
113 | else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) { | ||
114 | printf ("%s\n", mysql_error (&mysql)); | ||
115 | return STATE_WARNING; | ||
116 | |||
117 | } | ||
118 | else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) { | ||
119 | printf ("%s\n", mysql_error (&mysql)); | ||
120 | return STATE_WARNING; | ||
121 | |||
122 | } | ||
123 | else { | ||
124 | printf ("%s\n", mysql_error (&mysql)); | ||
125 | return STATE_CRITICAL; | ||
126 | } | ||
127 | |||
128 | } | 66 | } |
129 | 67 | ||
130 | /* get the server stats */ | 68 | /* get the server stats */ |
131 | sprintf (result, mysql_stat (&mysql)); | 69 | result = strdup (mysql_stat (&mysql)); |
132 | 70 | ||
133 | /* error checking once more */ | 71 | /* error checking once more */ |
134 | if (mysql_error (&mysql)) { | 72 | if (mysql_error (&mysql)) { |
135 | 73 | if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) | |
136 | if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) { | 74 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); |
137 | printf ("%s\n", mysql_error (&mysql)); | 75 | else if (mysql_errno (&mysql) == CR_SERVER_LOST) |
138 | return STATE_CRITICAL; | 76 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); |
139 | 77 | else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) | |
140 | } | 78 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); |
141 | else if (mysql_errno (&mysql) == CR_SERVER_LOST) { | ||
142 | printf ("%s\n", mysql_error (&mysql)); | ||
143 | return STATE_CRITICAL; | ||
144 | |||
145 | } | ||
146 | else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) { | ||
147 | printf ("%s\n", mysql_error (&mysql)); | ||
148 | return STATE_UNKNOWN; | ||
149 | } | ||
150 | |||
151 | } | 79 | } |
152 | 80 | ||
153 | /* close the connection */ | 81 | /* close the connection */ |
@@ -169,8 +97,8 @@ process_arguments (int argc, char **argv) | |||
169 | { | 97 | { |
170 | int c; | 98 | int c; |
171 | 99 | ||
172 | int option_index = 0; | 100 | int option = 0; |
173 | static struct option long_options[] = { | 101 | static struct option longopts[] = { |
174 | {"hostname", required_argument, 0, 'H'}, | 102 | {"hostname", required_argument, 0, 'H'}, |
175 | {"database", required_argument, 0, 'd'}, | 103 | {"database", required_argument, 0, 'd'}, |
176 | {"username", required_argument, 0, 'u'}, | 104 | {"username", required_argument, 0, 'u'}, |
@@ -186,7 +114,7 @@ process_arguments (int argc, char **argv) | |||
186 | return ERROR; | 114 | return ERROR; |
187 | 115 | ||
188 | while (1) { | 116 | while (1) { |
189 | c = getopt_long (argc, argv, "hVP:p:u:d:H:", long_options, &option_index); | 117 | c = getopt_long (argc, argv, "hVP:p:u:d:H:", longopts, &option); |
190 | 118 | ||
191 | if (c == -1 || c == EOF) | 119 | if (c == -1 || c == EOF) |
192 | break; | 120 | break; |
@@ -256,5 +184,69 @@ process_arguments (int argc, char **argv) | |||
256 | int | 184 | int |
257 | validate_arguments (void) | 185 | validate_arguments (void) |
258 | { | 186 | { |
187 | if (db_user == NULL) | ||
188 | db_user = strdup(""); | ||
189 | |||
190 | if (db_host == NULL) | ||
191 | db_host = strdup(""); | ||
192 | |||
193 | if (db_pass == NULL) | ||
194 | db_pass == strdup(""); | ||
195 | |||
196 | if (db == NULL) | ||
197 | db = strdup(""); | ||
198 | |||
259 | return OK; | 199 | return OK; |
260 | } | 200 | } |
201 | |||
202 | |||
203 | |||
204 | |||
205 | |||
206 | |||
207 | void | ||
208 | print_help (void) | ||
209 | { | ||
210 | char *myport; | ||
211 | asprintf (&myport, "%d", MYSQL_PORT); | ||
212 | |||
213 | print_revision (progname, revision); | ||
214 | |||
215 | printf (_(COPYRIGHT), copyright, email); | ||
216 | |||
217 | printf (_("This program tests connections to a mysql server\n")); | ||
218 | |||
219 | print_usage (); | ||
220 | |||
221 | printf (_(UT_HELP_VRSN)); | ||
222 | |||
223 | printf (_(UT_HOST_PORT), 'P', myport); | ||
224 | |||
225 | printf (_("\ | ||
226 | -d, --database=STRING\n\ | ||
227 | Check database with indicated name\n\ | ||
228 | -u, --username=STRING\n\ | ||
229 | Connect using the indicated username\n\ | ||
230 | -p, --password=STRING\n\ | ||
231 | Use the indicated password to authenticate the connection\n\ | ||
232 | ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\ | ||
233 | Your clear-text password will be visible as a process table entry\n")); | ||
234 | |||
235 | printf (_("\n\ | ||
236 | There are no required arguments. By default, the local database with\n\ | ||
237 | a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT); | ||
238 | |||
239 | printf (_(UT_SUPPORT)); | ||
240 | } | ||
241 | |||
242 | |||
243 | |||
244 | |||
245 | void | ||
246 | print_usage (void) | ||
247 | { | ||
248 | printf (_("\ | ||
249 | Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"), | ||
250 | progname); | ||
251 | printf (_(UT_HLP_VRS), progname, progname); | ||
252 | } | ||