1 /*****************************************************************************
3 * Monitoring check_mysql_query plugin
6 * Copyright (c) 2006-2024 Monitoring Plugins Development Team
7 * Original code from check_mysql, copyright 1999 Didi Rieder
11 * This file contains the check_mysql_query plugin
13 * This plugin is for running arbitrary SQL and checking the results
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *****************************************************************************/
32 const char *progname
= "check_mysql_query";
33 const char *copyright
= "1999-2024";
34 const char *email
= "devel@monitoring-plugins.org";
38 #include "utils_base.h"
44 static char *db_user
= NULL
;
45 static char *db_host
= NULL
;
46 static char *db_socket
= NULL
;
47 static char *db_pass
= NULL
;
48 static char *db
= NULL
;
49 static char *opt_file
= NULL
;
50 static char *opt_group
= NULL
;
51 static unsigned int db_port
= MYSQL_PORT
;
53 static int process_arguments(int /*argc*/, char ** /*argv*/);
54 static int validate_arguments(void);
55 static void print_help(void);
56 void print_usage(void);
58 static char *sql_query
= NULL
;
59 static int verbose
= 0;
60 static thresholds
*my_thresholds
= NULL
;
62 int main(int argc
, char **argv
) {
63 setlocale(LC_ALL
, "");
64 bindtextdomain(PACKAGE
, LOCALEDIR
);
67 /* Parse extra opts if any */
68 argv
= np_extra_opts(&argc
, argv
, progname
);
70 if (process_arguments(argc
, argv
) == ERROR
)
71 usage4(_("Could not parse arguments"));
74 /* initialize mysql */
78 mysql_options(&mysql
, MYSQL_READ_DEFAULT_FILE
, opt_file
);
80 if (opt_group
!= NULL
)
81 mysql_options(&mysql
, MYSQL_READ_DEFAULT_GROUP
, opt_group
);
83 mysql_options(&mysql
, MYSQL_READ_DEFAULT_GROUP
, "client");
85 /* establish a connection to the server and error checking */
86 if (!mysql_real_connect(&mysql
, db_host
, db_user
, db_pass
, db
, db_port
, db_socket
, 0)) {
87 if (mysql_errno(&mysql
) == CR_UNKNOWN_HOST
)
88 die(STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql
));
89 else if (mysql_errno(&mysql
) == CR_VERSION_ERROR
)
90 die(STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql
));
91 else if (mysql_errno(&mysql
) == CR_OUT_OF_MEMORY
)
92 die(STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql
));
93 else if (mysql_errno(&mysql
) == CR_IPSOCK_ERROR
)
94 die(STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql
));
95 else if (mysql_errno(&mysql
) == CR_SOCKET_CREATE_ERROR
)
96 die(STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql
));
98 die(STATE_CRITICAL
, "QUERY %s: %s\n", _("CRITICAL"), mysql_error(&mysql
));
102 if (mysql_query(&mysql
, sql_query
) != 0) {
103 error
= strdup(mysql_error(&mysql
));
105 die(STATE_CRITICAL
, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error
);
109 /* store the result */
110 if ((res
= mysql_store_result(&mysql
)) == NULL
) {
111 error
= strdup(mysql_error(&mysql
));
113 die(STATE_CRITICAL
, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error
);
116 /* Check there is some data */
117 if (mysql_num_rows(res
) == 0) {
119 die(STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
123 /* fetch the first row */
124 if ((row
= mysql_fetch_row(res
)) == NULL
) {
125 error
= strdup(mysql_error(&mysql
));
126 mysql_free_result(res
);
128 die(STATE_CRITICAL
, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error
);
131 if (!is_numeric(row
[0])) {
132 die(STATE_CRITICAL
, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row
[0]);
135 double value
= strtod(row
[0], NULL
);
137 /* free the result */
138 mysql_free_result(res
);
140 /* close the connection */
144 printf("mysql result: %f\n", value
);
146 int status
= get_status(value
, my_thresholds
);
148 if (status
== STATE_OK
) {
149 printf("QUERY %s: ", _("OK"));
150 } else if (status
== STATE_WARNING
) {
151 printf("QUERY %s: ", _("WARNING"));
152 } else if (status
== STATE_CRITICAL
) {
153 printf("QUERY %s: ", _("CRITICAL"));
155 printf(_("'%s' returned %f | %s"), sql_query
, value
,
156 fperfdata("result", value
, "", my_thresholds
->warning
? true : false, my_thresholds
->warning
? my_thresholds
->warning
->end
: 0,
157 my_thresholds
->critical
? true : false, my_thresholds
->critical
? my_thresholds
->critical
->end
: 0, false, 0, false,
164 /* process command-line arguments */
165 int process_arguments(int argc
, char **argv
) {
166 static struct option longopts
[] = {
167 {"hostname", required_argument
, 0, 'H'}, {"socket", required_argument
, 0, 's'}, {"database", required_argument
, 0, 'd'},
168 {"username", required_argument
, 0, 'u'}, {"password", required_argument
, 0, 'p'}, {"file", required_argument
, 0, 'f'},
169 {"group", required_argument
, 0, 'g'}, {"port", required_argument
, 0, 'P'}, {"verbose", no_argument
, 0, 'v'},
170 {"version", no_argument
, 0, 'V'}, {"help", no_argument
, 0, 'h'}, {"query", required_argument
, 0, 'q'},
171 {"warning", required_argument
, 0, 'w'}, {"critical", required_argument
, 0, 'c'}, {0, 0, 0, 0}};
176 char *warning
= NULL
;
177 char *critical
= NULL
;
181 int option_char
= getopt_long(argc
, argv
, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts
, &option
);
183 if (option_char
== -1 || option_char
== EOF
)
186 switch (option_char
) {
187 case 'H': /* hostname */
188 if (is_host(optarg
)) {
191 usage2(_("Invalid hostname/address"), optarg
);
194 case 's': /* socket */
197 case 'd': /* database */
200 case 'u': /* username */
203 case 'p': /* authentication information: password */
204 db_pass
= strdup(optarg
);
206 /* Delete the password from process list */
207 while (*optarg
!= '\0') {
212 case 'f': /* client options file */
215 case 'g': /* client options group */
218 case 'P': /* critical time threshold */
219 db_port
= atoi(optarg
);
224 case 'V': /* version */
225 print_revision(progname
, NP_VERSION
);
231 xasprintf(&sql_query
, "%s", optarg
);
244 set_thresholds(&my_thresholds
, warning
, critical
);
246 return validate_arguments();
249 int validate_arguments(void) {
250 if (sql_query
== NULL
)
251 usage("Must specify a SQL query to run");
254 db_user
= strdup("");
257 db_host
= strdup("");
265 void print_help(void) {
267 xasprintf(&myport
, "%d", MYSQL_PORT
);
269 print_revision(progname
, NP_VERSION
);
271 printf(_(COPYRIGHT
), copyright
, email
);
273 printf("%s\n", _("This program checks a query result against threshold levels"));
279 printf(UT_HELP_VRSN
);
280 printf(UT_EXTRA_OPTS
);
281 printf(" -q, --query=STRING\n");
282 printf(" %s\n", _("SQL query to run. Only first column in first row will be read"));
283 printf(UT_WARN_CRIT_RANGE
);
284 printf(UT_HOST_PORT
, 'P', myport
);
285 printf(" %s\n", "-s, --socket=STRING");
286 printf(" %s\n", _("Use the specified socket (has no effect if -H is used)"));
287 printf(" -d, --database=STRING\n");
288 printf(" %s\n", _("Database to check"));
289 printf(" %s\n", "-f, --file=STRING");
290 printf(" %s\n", _("Read from the specified client options file"));
291 printf(" %s\n", "-g, --group=STRING");
292 printf(" %s\n", _("Use a client options group"));
293 printf(" -u, --username=STRING\n");
294 printf(" %s\n", _("Username to login with"));
295 printf(" -p, --password=STRING\n");
296 printf(" %s\n", _("Password to login with"));
297 printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
298 printf(" %s\n", _("Your clear-text password could be visible as a process table entry"));
301 printf(" %s\n", _("A query is required. The result from the query should be numeric."));
302 printf(" %s\n", _("For extra security, create a user with minimal access."));
305 printf("%s\n", _("Notes:"));
306 printf(" %s\n", _("You must specify -p with an empty string to force an empty password,"));
307 printf(" %s\n", _("overriding any my.cnf settings."));
312 void print_usage(void) {
313 printf("%s\n", _("Usage:"));
314 printf(" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n", progname
);
315 printf(" [-d database] [-u user] [-p password] [-f optfile] [-g group]\n");