diff options
Diffstat (limited to 'plugins/check_mysql_query.c')
| -rw-r--r-- | plugins/check_mysql_query.c | 547 |
1 files changed, 289 insertions, 258 deletions
diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c index 842b7a2f..ae6cc15d 100644 --- a/plugins/check_mysql_query.c +++ b/plugins/check_mysql_query.c | |||
| @@ -1,234 +1,252 @@ | |||
| 1 | /***************************************************************************** | 1 | /***************************************************************************** |
| 2 | * | 2 | * |
| 3 | * Monitoring check_mysql_query plugin | 3 | * Monitoring check_mysql_query plugin |
| 4 | * | 4 | * |
| 5 | * License: GPL | 5 | * License: GPL |
| 6 | * Copyright (c) 2006-2009 Monitoring Plugins Development Team | 6 | * Copyright (c) 2006-2024 Monitoring Plugins Development Team |
| 7 | * Original code from check_mysql, copyright 1999 Didi Rieder | 7 | * Original code from check_mysql, copyright 1999 Didi Rieder |
| 8 | * | 8 | * |
| 9 | * Description: | 9 | * Description: |
| 10 | * | 10 | * |
| 11 | * This file contains the check_mysql_query plugin | 11 | * This file contains the check_mysql_query plugin |
| 12 | * | 12 | * |
| 13 | * This plugin is for running arbitrary SQL and checking the results | 13 | * This plugin is for running arbitrary SQL and checking the results |
| 14 | * | 14 | * |
| 15 | * | 15 | * |
| 16 | * This program is free software: you can redistribute it and/or modify | 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 | 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 | 18 | * the Free Software Foundation, either version 3 of the License, or |
| 19 | * (at your option) any later version. | 19 | * (at your option) any later version. |
| 20 | * | 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, | 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. | 24 | * GNU General Public License for more details. |
| 25 | * | 25 | * |
| 26 | * You should have received a copy of the GNU General Public License | 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/>. | 27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | * | 28 | * |
| 29 | * | 29 | * |
| 30 | *****************************************************************************/ | 30 | *****************************************************************************/ |
| 31 | |||
| 32 | const char *progname = "check_mysql_query"; | ||
| 33 | const char *copyright = "1999-2007"; | ||
| 34 | const char *email = "devel@monitoring-plugins.org"; | ||
| 35 | 31 | ||
| 36 | #include "common.h" | 32 | #include "common.h" |
| 33 | #include "output.h" | ||
| 34 | #include "perfdata.h" | ||
| 35 | #include "states.h" | ||
| 36 | #include "thresholds.h" | ||
| 37 | #include "utils.h" | 37 | #include "utils.h" |
| 38 | #include "utils_base.h" | 38 | #include "utils_base.h" |
| 39 | #include "netutils.h" | 39 | #include "netutils.h" |
| 40 | #include "check_mysql_query.d/config.h" | ||
| 40 | 41 | ||
| 41 | #include <mysql.h> | 42 | #include <mysql.h> |
| 42 | #include <errmsg.h> | 43 | #include <errmsg.h> |
| 43 | 44 | ||
| 44 | char *db_user = NULL; | 45 | const char *progname = "check_mysql_query"; |
| 45 | char *db_host = NULL; | 46 | const char *copyright = "1999-2024"; |
| 46 | char *db_socket = NULL; | 47 | const char *email = "devel@monitoring-plugins.org"; |
| 47 | char *db_pass = NULL; | ||
| 48 | char *db = NULL; | ||
| 49 | char *opt_file = NULL; | ||
| 50 | char *opt_group = NULL; | ||
| 51 | unsigned int db_port = MYSQL_PORT; | ||
| 52 | |||
| 53 | int process_arguments (int, char **); | ||
| 54 | int validate_arguments (void); | ||
| 55 | void print_help (void); | ||
| 56 | void print_usage (void); | ||
| 57 | 48 | ||
| 58 | char *sql_query = NULL; | 49 | typedef struct { |
| 59 | int verbose = 0; | 50 | int errorcode; |
| 60 | thresholds *my_thresholds = NULL; | 51 | check_mysql_query_config config; |
| 52 | } check_mysql_query_config_wrapper; | ||
| 53 | static check_mysql_query_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 54 | static check_mysql_query_config_wrapper | ||
| 55 | validate_arguments(check_mysql_query_config_wrapper /*config_wrapper*/); | ||
| 56 | static void print_help(void); | ||
| 57 | void print_usage(void); | ||
| 61 | 58 | ||
| 59 | static int verbose = 0; | ||
| 62 | 60 | ||
| 63 | int | 61 | int main(int argc, char **argv) { |
| 64 | main (int argc, char **argv) | 62 | setlocale(LC_ALL, ""); |
| 65 | { | 63 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 64 | textdomain(PACKAGE); | ||
| 66 | 65 | ||
| 67 | MYSQL mysql; | 66 | /* Parse extra opts if any */ |
| 68 | MYSQL_RES *res; | 67 | argv = np_extra_opts(&argc, argv, progname); |
| 69 | MYSQL_ROW row; | ||
| 70 | |||
| 71 | double value; | ||
| 72 | char *error = NULL; | ||
| 73 | int status; | ||
| 74 | 68 | ||
| 75 | setlocale (LC_ALL, ""); | 69 | check_mysql_query_config_wrapper tmp_config = process_arguments(argc, argv); |
| 76 | bindtextdomain (PACKAGE, LOCALEDIR); | 70 | if (tmp_config.errorcode == ERROR) { |
| 77 | textdomain (PACKAGE); | 71 | usage4(_("Could not parse arguments")); |
| 72 | } | ||
| 78 | 73 | ||
| 79 | /* Parse extra opts if any */ | 74 | const check_mysql_query_config config = tmp_config.config; |
| 80 | argv=np_extra_opts (&argc, argv, progname); | ||
| 81 | 75 | ||
| 82 | if (process_arguments (argc, argv) == ERROR) | 76 | if (config.output_format_is_set) { |
| 83 | usage4 (_("Could not parse arguments")); | 77 | mp_set_format(config.output_format); |
| 78 | } | ||
| 84 | 79 | ||
| 80 | MYSQL mysql; | ||
| 85 | /* initialize mysql */ | 81 | /* initialize mysql */ |
| 86 | mysql_init (&mysql); | 82 | mysql_init(&mysql); |
| 87 | 83 | ||
| 88 | if (opt_file != NULL) | 84 | if (config.opt_file != NULL) { |
| 89 | mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file); | 85 | mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, config.opt_file); |
| 86 | } | ||
| 90 | 87 | ||
| 91 | if (opt_group != NULL) | 88 | if (config.opt_group != NULL) { |
| 92 | mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group); | 89 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, config.opt_group); |
| 93 | else | 90 | } else { |
| 94 | mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); | 91 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client"); |
| 92 | } | ||
| 93 | |||
| 94 | mp_check overall = mp_check_init(); | ||
| 95 | mp_subcheck sc_connect = mp_subcheck_init(); | ||
| 95 | 96 | ||
| 96 | /* establish a connection to the server and error checking */ | 97 | /* establish a connection to the server and error checking */ |
| 97 | if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) { | 98 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, |
| 98 | if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) | 99 | config.db_port, config.db_socket, 0)) { |
| 99 | die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); | 100 | xasprintf(&sc_connect.output, "query failed: %s", mysql_error(&mysql)); |
| 100 | else if (mysql_errno (&mysql) == CR_VERSION_ERROR) | 101 | |
| 101 | die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); | 102 | if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { |
| 102 | else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) | 103 | sc_connect = mp_set_subcheck_state(sc_connect, STATE_WARNING); |
| 103 | die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); | 104 | } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { |
| 104 | else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) | 105 | sc_connect = mp_set_subcheck_state(sc_connect, STATE_WARNING); |
| 105 | die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); | 106 | } else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY) { |
| 106 | else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) | 107 | sc_connect = mp_set_subcheck_state(sc_connect, STATE_WARNING); |
| 107 | die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); | 108 | } else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR) { |
| 108 | else | 109 | sc_connect = mp_set_subcheck_state(sc_connect, STATE_WARNING); |
| 109 | die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql)); | 110 | } else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR) { |
| 111 | sc_connect = mp_set_subcheck_state(sc_connect, STATE_WARNING); | ||
| 112 | } else { | ||
| 113 | sc_connect = mp_set_subcheck_state(sc_connect, STATE_CRITICAL); | ||
| 114 | } | ||
| 115 | |||
| 116 | mp_add_subcheck_to_check(&overall, sc_connect); | ||
| 117 | mp_exit(overall); | ||
| 110 | } | 118 | } |
| 111 | 119 | ||
| 112 | if (mysql_query (&mysql, sql_query) != 0) { | 120 | sc_connect = mp_set_subcheck_state(sc_connect, STATE_OK); |
| 113 | error = strdup(mysql_error(&mysql)); | 121 | xasprintf(&sc_connect.output, "query succeeded"); |
| 114 | mysql_close (&mysql); | 122 | mp_add_subcheck_to_check(&overall, sc_connect); |
| 115 | die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); | 123 | |
| 124 | if (mysql_query(&mysql, config.sql_query) != 0) { | ||
| 125 | char *error = strdup(mysql_error(&mysql)); | ||
| 126 | mysql_close(&mysql); | ||
| 127 | die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); | ||
| 116 | } | 128 | } |
| 117 | 129 | ||
| 130 | MYSQL_RES *res; | ||
| 118 | /* store the result */ | 131 | /* store the result */ |
| 119 | if ( (res = mysql_store_result (&mysql)) == NULL) { | 132 | if ((res = mysql_store_result(&mysql)) == NULL) { |
| 120 | error = strdup(mysql_error(&mysql)); | 133 | char *error = strdup(mysql_error(&mysql)); |
| 121 | mysql_close (&mysql); | 134 | mysql_close(&mysql); |
| 122 | die (STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error); | 135 | die(STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error); |
| 123 | } | 136 | } |
| 124 | 137 | ||
| 125 | /* Check there is some data */ | 138 | /* Check there is some data */ |
| 126 | if (mysql_num_rows(res) == 0) { | 139 | if (mysql_num_rows(res) == 0) { |
| 127 | mysql_close(&mysql); | 140 | mysql_close(&mysql); |
| 128 | die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned")); | 141 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned")); |
| 129 | } | 142 | } |
| 130 | 143 | ||
| 144 | mp_subcheck sc_value = mp_subcheck_init(); | ||
| 145 | MYSQL_ROW row; | ||
| 131 | /* fetch the first row */ | 146 | /* fetch the first row */ |
| 132 | if ( (row = mysql_fetch_row (res)) == NULL) { | 147 | if ((row = mysql_fetch_row(res)) == NULL) { |
| 133 | error = strdup(mysql_error(&mysql)); | 148 | xasprintf(&sc_value.output, "fetch row error - %s", mysql_error(&mysql)); |
| 134 | mysql_free_result (res); | 149 | mysql_free_result(res); |
| 135 | mysql_close (&mysql); | 150 | mysql_close(&mysql); |
| 136 | die (STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error); | 151 | |
| 152 | sc_value = mp_set_subcheck_state(sc_value, STATE_CRITICAL); | ||
| 153 | mp_add_subcheck_to_check(&overall, sc_value); | ||
| 154 | mp_exit(overall); | ||
| 137 | } | 155 | } |
| 138 | 156 | ||
| 139 | if (! is_numeric(row[0])) { | 157 | if (!is_numeric(row[0])) { |
| 140 | die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]); | 158 | xasprintf(&sc_value.output, "query result is not numeric"); |
| 159 | sc_value = mp_set_subcheck_state(sc_value, STATE_CRITICAL); | ||
| 160 | mp_add_subcheck_to_check(&overall, sc_value); | ||
| 161 | mp_exit(overall); | ||
| 141 | } | 162 | } |
| 142 | 163 | ||
| 143 | value = strtod(row[0], NULL); | 164 | double value = strtod(row[0], NULL); |
| 144 | 165 | ||
| 145 | /* free the result */ | 166 | /* free the result */ |
| 146 | mysql_free_result (res); | 167 | mysql_free_result(res); |
| 147 | 168 | ||
| 148 | /* close the connection */ | 169 | /* close the connection */ |
| 149 | mysql_close (&mysql); | 170 | mysql_close(&mysql); |
| 150 | 171 | ||
| 151 | if (verbose >= 3) | 172 | if (verbose >= 3) { |
| 152 | printf("mysql result: %f\n", value); | 173 | printf("mysql result: %f\n", value); |
| 174 | } | ||
| 153 | 175 | ||
| 154 | status = get_status(value, my_thresholds); | 176 | mp_perfdata pd_query_result = perfdata_init(); |
| 177 | pd_query_result = mp_set_pd_value(pd_query_result, value); | ||
| 178 | pd_query_result = mp_pd_set_thresholds(pd_query_result, config.thresholds); | ||
| 179 | pd_query_result.label = "result"; | ||
| 180 | mp_add_perfdata_to_subcheck(&sc_value, pd_query_result); | ||
| 155 | 181 | ||
| 156 | if (status == STATE_OK) { | 182 | sc_value = mp_set_subcheck_state(sc_value, mp_get_pd_status(pd_query_result)); |
| 157 | printf("QUERY %s: ", _("OK")); | 183 | xasprintf(&sc_value.output, "'%s' returned '%f'", config.sql_query, value); |
| 158 | } else if (status == STATE_WARNING) { | ||
| 159 | printf("QUERY %s: ", _("WARNING")); | ||
| 160 | } else if (status == STATE_CRITICAL) { | ||
| 161 | printf("QUERY %s: ", _("CRITICAL")); | ||
| 162 | } | ||
| 163 | printf(_("'%s' returned %f | %s"), sql_query, value, | ||
| 164 | fperfdata("result", value, "", | ||
| 165 | my_thresholds->warning?true:false, my_thresholds->warning?my_thresholds->warning->end:0, | ||
| 166 | my_thresholds->critical?true:false, my_thresholds->critical?my_thresholds->critical->end:0, | ||
| 167 | false, 0, | ||
| 168 | false, 0) | ||
| 169 | ); | ||
| 170 | printf("\n"); | ||
| 171 | 184 | ||
| 172 | return status; | 185 | mp_add_subcheck_to_check(&overall, sc_value); |
| 173 | } | ||
| 174 | 186 | ||
| 187 | mp_exit(overall); | ||
| 188 | } | ||
| 175 | 189 | ||
| 176 | /* process command-line arguments */ | 190 | /* process command-line arguments */ |
| 177 | int | 191 | check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { |
| 178 | process_arguments (int argc, char **argv) | 192 | enum { |
| 179 | { | 193 | output_format_index = CHAR_MAX + 1, |
| 180 | int c; | ||
| 181 | char *warning = NULL; | ||
| 182 | char *critical = NULL; | ||
| 183 | |||
| 184 | int option = 0; | ||
| 185 | static struct option longopts[] = { | ||
| 186 | {"hostname", required_argument, 0, 'H'}, | ||
| 187 | {"socket", required_argument, 0, 's'}, | ||
| 188 | {"database", required_argument, 0, 'd'}, | ||
| 189 | {"username", required_argument, 0, 'u'}, | ||
| 190 | {"password", required_argument, 0, 'p'}, | ||
| 191 | {"file", required_argument, 0, 'f'}, | ||
| 192 | {"group", required_argument, 0, 'g'}, | ||
| 193 | {"port", required_argument, 0, 'P'}, | ||
| 194 | {"verbose", no_argument, 0, 'v'}, | ||
| 195 | {"version", no_argument, 0, 'V'}, | ||
| 196 | {"help", no_argument, 0, 'h'}, | ||
| 197 | {"query", required_argument, 0, 'q'}, | ||
| 198 | {"warning", required_argument, 0, 'w'}, | ||
| 199 | {"critical", required_argument, 0, 'c'}, | ||
| 200 | {0, 0, 0, 0} | ||
| 201 | }; | 194 | }; |
| 202 | 195 | ||
| 203 | if (argc < 1) | 196 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 204 | return ERROR; | 197 | {"socket", required_argument, 0, 's'}, |
| 198 | {"database", required_argument, 0, 'd'}, | ||
| 199 | {"username", required_argument, 0, 'u'}, | ||
| 200 | {"password", required_argument, 0, 'p'}, | ||
| 201 | {"file", required_argument, 0, 'f'}, | ||
| 202 | {"group", required_argument, 0, 'g'}, | ||
| 203 | {"port", required_argument, 0, 'P'}, | ||
| 204 | {"verbose", no_argument, 0, 'v'}, | ||
| 205 | {"version", no_argument, 0, 'V'}, | ||
| 206 | {"help", no_argument, 0, 'h'}, | ||
| 207 | {"query", required_argument, 0, 'q'}, | ||
| 208 | {"warning", required_argument, 0, 'w'}, | ||
| 209 | {"critical", required_argument, 0, 'c'}, | ||
| 210 | {"output-format", required_argument, 0, output_format_index}, | ||
| 211 | {0, 0, 0, 0}}; | ||
| 212 | |||
| 213 | check_mysql_query_config_wrapper result = { | ||
| 214 | .errorcode = OK, | ||
| 215 | .config = check_mysql_query_config_init(), | ||
| 216 | }; | ||
| 217 | |||
| 218 | if (argc < 1) { | ||
| 219 | result.errorcode = ERROR; | ||
| 220 | return result; | ||
| 221 | } | ||
| 205 | 222 | ||
| 206 | while (1) { | 223 | while (true) { |
| 207 | c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option); | 224 | int option = 0; |
| 225 | int option_char = getopt_long(argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option); | ||
| 208 | 226 | ||
| 209 | if (c == -1 || c == EOF) | 227 | if (option_char == -1 || option_char == EOF) { |
| 210 | break; | 228 | break; |
| 229 | } | ||
| 211 | 230 | ||
| 212 | switch (c) { | 231 | switch (option_char) { |
| 213 | case 'H': /* hostname */ | 232 | case 'H': /* hostname */ |
| 214 | if (is_host (optarg)) { | 233 | if (is_host(optarg)) { |
| 215 | db_host = optarg; | 234 | result.config.db_host = optarg; |
| 216 | } | 235 | } else { |
| 217 | else { | 236 | usage2(_("Invalid hostname/address"), optarg); |
| 218 | usage2 (_("Invalid hostname/address"), optarg); | ||
| 219 | } | 237 | } |
| 220 | break; | 238 | break; |
| 221 | case 's': /* socket */ | 239 | case 's': /* socket */ |
| 222 | db_socket = optarg; | 240 | result.config.db_socket = optarg; |
| 223 | break; | 241 | break; |
| 224 | case 'd': /* database */ | 242 | case 'd': /* database */ |
| 225 | db = optarg; | 243 | result.config.db = optarg; |
| 226 | break; | 244 | break; |
| 227 | case 'u': /* username */ | 245 | case 'u': /* username */ |
| 228 | db_user = optarg; | 246 | result.config.db_user = optarg; |
| 229 | break; | 247 | break; |
| 230 | case 'p': /* authentication information: password */ | 248 | case 'p': /* authentication information: password */ |
| 231 | db_pass = strdup(optarg); | 249 | result.config.db_pass = strdup(optarg); |
| 232 | 250 | ||
| 233 | /* Delete the password from process list */ | 251 | /* Delete the password from process list */ |
| 234 | while (*optarg != '\0') { | 252 | while (*optarg != '\0') { |
| @@ -236,119 +254,132 @@ process_arguments (int argc, char **argv) | |||
| 236 | optarg++; | 254 | optarg++; |
| 237 | } | 255 | } |
| 238 | break; | 256 | break; |
| 239 | case 'f': /* client options file */ | 257 | case 'f': /* client options file */ |
| 240 | opt_file = optarg; | 258 | result.config.opt_file = optarg; |
| 241 | break; | 259 | break; |
| 242 | case 'g': /* client options group */ | 260 | case 'g': /* client options group */ |
| 243 | opt_group = optarg; | 261 | result.config.opt_group = optarg; |
| 244 | break; | 262 | break; |
| 245 | case 'P': /* critical time threshold */ | 263 | case 'P': /* critical time threshold */ |
| 246 | db_port = atoi (optarg); | 264 | result.config.db_port = atoi(optarg); |
| 247 | break; | 265 | break; |
| 248 | case 'v': | 266 | case 'v': |
| 249 | verbose++; | 267 | verbose++; |
| 250 | break; | 268 | break; |
| 251 | case 'V': /* version */ | 269 | case 'V': /* version */ |
| 252 | print_revision (progname, NP_VERSION); | 270 | print_revision(progname, NP_VERSION); |
| 253 | exit (STATE_UNKNOWN); | 271 | exit(STATE_UNKNOWN); |
| 254 | case 'h': /* help */ | 272 | case 'h': /* help */ |
| 255 | print_help (); | 273 | print_help(); |
| 256 | exit (STATE_UNKNOWN); | 274 | exit(STATE_UNKNOWN); |
| 257 | case 'q': | 275 | case 'q': |
| 258 | xasprintf(&sql_query, "%s", optarg); | 276 | xasprintf(&result.config.sql_query, "%s", optarg); |
| 259 | break; | ||
| 260 | case 'w': | ||
| 261 | warning = optarg; | ||
| 262 | break; | 277 | break; |
| 263 | case 'c': | 278 | case 'w': { |
| 264 | critical = optarg; | 279 | mp_range_parsed tmp = mp_parse_range_string(optarg); |
| 280 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 281 | die(STATE_UNKNOWN, "failed to parse warning threshold"); | ||
| 282 | } | ||
| 283 | result.config.thresholds = mp_thresholds_set_warn(result.config.thresholds, tmp.range); | ||
| 284 | } break; | ||
| 285 | case 'c': { | ||
| 286 | mp_range_parsed tmp = mp_parse_range_string(optarg); | ||
| 287 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 288 | die(STATE_UNKNOWN, "failed to parse critical threshold"); | ||
| 289 | } | ||
| 290 | result.config.thresholds = mp_thresholds_set_crit(result.config.thresholds, tmp.range); | ||
| 291 | } break; | ||
| 292 | case '?': /* help */ | ||
| 293 | usage5(); | ||
| 294 | case output_format_index: { | ||
| 295 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
| 296 | if (!parser.parsing_success) { | ||
| 297 | printf("Invalid output format: %s\n", optarg); | ||
| 298 | exit(STATE_UNKNOWN); | ||
| 299 | } | ||
| 300 | |||
| 301 | result.config.output_format_is_set = true; | ||
| 302 | result.config.output_format = parser.output_format; | ||
| 265 | break; | 303 | break; |
| 266 | case '?': /* help */ | ||
| 267 | usage5 (); | ||
| 268 | } | 304 | } |
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | return validate_arguments(result); | ||
| 309 | } | ||
| 310 | |||
| 311 | check_mysql_query_config_wrapper | ||
| 312 | validate_arguments(check_mysql_query_config_wrapper config_wrapper) { | ||
| 313 | if (config_wrapper.config.sql_query == NULL) { | ||
| 314 | usage("Must specify a SQL query to run"); | ||
| 315 | } | ||
| 316 | |||
| 317 | if (config_wrapper.config.db_user == NULL) { | ||
| 318 | config_wrapper.config.db_user = strdup(""); | ||
| 269 | } | 319 | } |
| 270 | 320 | ||
| 271 | c = optind; | 321 | if (config_wrapper.config.db_host == NULL) { |
| 322 | config_wrapper.config.db_host = strdup(""); | ||
| 323 | } | ||
| 272 | 324 | ||
| 273 | set_thresholds(&my_thresholds, warning, critical); | 325 | if (config_wrapper.config.db == NULL) { |
| 326 | config_wrapper.config.db = strdup(""); | ||
| 327 | } | ||
| 274 | 328 | ||
| 275 | return validate_arguments (); | 329 | return config_wrapper; |
| 276 | } | 330 | } |
| 277 | 331 | ||
| 332 | void print_help(void) { | ||
| 333 | char *myport; | ||
| 334 | xasprintf(&myport, "%d", MYSQL_PORT); | ||
| 278 | 335 | ||
| 279 | int | 336 | print_revision(progname, NP_VERSION); |
| 280 | validate_arguments (void) | ||
| 281 | { | ||
| 282 | if (sql_query == NULL) | ||
| 283 | usage("Must specify a SQL query to run"); | ||
| 284 | 337 | ||
| 285 | if (db_user == NULL) | 338 | printf(_(COPYRIGHT), copyright, email); |
| 286 | db_user = strdup(""); | ||
| 287 | 339 | ||
| 288 | if (db_host == NULL) | 340 | printf("%s\n", _("This program checks a query result against threshold levels")); |
| 289 | db_host = strdup(""); | ||
| 290 | 341 | ||
| 291 | if (db == NULL) | 342 | printf("\n\n"); |
| 292 | db = strdup(""); | ||
| 293 | 343 | ||
| 294 | return OK; | 344 | print_usage(); |
| 295 | } | ||
| 296 | 345 | ||
| 346 | printf(UT_HELP_VRSN); | ||
| 347 | printf(UT_EXTRA_OPTS); | ||
| 348 | printf(" -q, --query=STRING\n"); | ||
| 349 | printf(" %s\n", _("SQL query to run. Only first column in first row will be read")); | ||
| 350 | printf(UT_WARN_CRIT_RANGE); | ||
| 351 | printf(UT_HOST_PORT, 'P', myport); | ||
| 352 | printf(" %s\n", "-s, --socket=STRING"); | ||
| 353 | printf(" %s\n", _("Use the specified socket (has no effect if -H is used)")); | ||
| 354 | printf(" -d, --database=STRING\n"); | ||
| 355 | printf(" %s\n", _("Database to check")); | ||
| 356 | printf(" %s\n", "-f, --file=STRING"); | ||
| 357 | printf(" %s\n", _("Read from the specified client options file")); | ||
| 358 | printf(" %s\n", "-g, --group=STRING"); | ||
| 359 | printf(" %s\n", _("Use a client options group")); | ||
| 360 | printf(" -u, --username=STRING\n"); | ||
| 361 | printf(" %s\n", _("Username to login with")); | ||
| 362 | printf(" -p, --password=STRING\n"); | ||
| 363 | printf(" %s\n", _("Password to login with")); | ||
| 364 | printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); | ||
| 365 | printf(" %s\n", _("Your clear-text password could be visible as a process table entry")); | ||
| 297 | 366 | ||
| 298 | void | 367 | printf(UT_OUTPUT_FORMAT); |
| 299 | print_help (void) | 368 | |
| 300 | { | 369 | printf("\n"); |
| 301 | char *myport; | 370 | printf(" %s\n", _("A query is required. The result from the query should be numeric.")); |
| 302 | xasprintf (&myport, "%d", MYSQL_PORT); | 371 | printf(" %s\n", _("For extra security, create a user with minimal access.")); |
| 303 | |||
| 304 | print_revision (progname, NP_VERSION); | ||
| 305 | |||
| 306 | printf (_(COPYRIGHT), copyright, email); | ||
| 307 | |||
| 308 | printf ("%s\n", _("This program checks a query result against threshold levels")); | ||
| 309 | |||
| 310 | printf ("\n\n"); | ||
| 311 | |||
| 312 | print_usage (); | ||
| 313 | |||
| 314 | printf (UT_HELP_VRSN); | ||
| 315 | printf (UT_EXTRA_OPTS); | ||
| 316 | printf (" -q, --query=STRING\n"); | ||
| 317 | printf (" %s\n", _("SQL query to run. Only first column in first row will be read")); | ||
| 318 | printf (UT_WARN_CRIT_RANGE); | ||
| 319 | printf (UT_HOST_PORT, 'P', myport); | ||
| 320 | printf (" %s\n", "-s, --socket=STRING"); | ||
| 321 | printf (" %s\n", _("Use the specified socket (has no effect if -H is used)")); | ||
| 322 | printf (" -d, --database=STRING\n"); | ||
| 323 | printf (" %s\n", _("Database to check")); | ||
| 324 | printf (" %s\n", "-f, --file=STRING"); | ||
| 325 | printf (" %s\n", _("Read from the specified client options file")); | ||
| 326 | printf (" %s\n", "-g, --group=STRING"); | ||
| 327 | printf (" %s\n", _("Use a client options group")); | ||
| 328 | printf (" -u, --username=STRING\n"); | ||
| 329 | printf (" %s\n", _("Username to login with")); | ||
| 330 | printf (" -p, --password=STRING\n"); | ||
| 331 | printf (" %s\n", _("Password to login with")); | ||
| 332 | printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); | ||
| 333 | printf (" %s\n", _("Your clear-text password could be visible as a process table entry")); | ||
| 334 | |||
| 335 | printf ("\n"); | ||
| 336 | printf (" %s\n", _("A query is required. The result from the query should be numeric.")); | ||
| 337 | printf (" %s\n", _("For extra security, create a user with minimal access.")); | ||
| 338 | |||
| 339 | printf ("\n"); | ||
| 340 | printf ("%s\n", _("Notes:")); | ||
| 341 | printf (" %s\n", _("You must specify -p with an empty string to force an empty password,")); | ||
| 342 | printf (" %s\n", _("overriding any my.cnf settings.")); | ||
| 343 | |||
| 344 | printf (UT_SUPPORT); | ||
| 345 | } | ||
| 346 | 372 | ||
| 373 | printf("\n"); | ||
| 374 | printf("%s\n", _("Notes:")); | ||
| 375 | printf(" %s\n", _("You must specify -p with an empty string to force an empty password,")); | ||
| 376 | printf(" %s\n", _("overriding any my.cnf settings.")); | ||
| 377 | |||
| 378 | printf(UT_SUPPORT); | ||
| 379 | } | ||
| 347 | 380 | ||
| 348 | void | 381 | void print_usage(void) { |
| 349 | print_usage (void) | 382 | printf("%s\n", _("Usage:")); |
| 350 | { | 383 | printf(" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n", progname); |
| 351 | printf ("%s\n", _("Usage:")); | 384 | printf(" [-d database] [-u user] [-p password] [-f optfile] [-g group]\n"); |
| 352 | printf (" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n",progname); | ||
| 353 | printf (" [-d database] [-u user] [-p password] [-f optfile] [-g group]\n"); | ||
| 354 | } | 385 | } |
