--- check_mysql_query.c 2007-12-14 11:41:41.000000000 -0800 +++ check_mysql_query.c 2007-12-14 12:17:56.000000000 -0800 @@ -59,6 +59,8 @@ void print_usage (void); char *sql_query = NULL; +unsigned int sql_column = 0; + int verbose = 0; thresholds *my_thresholds = NULL; @@ -74,6 +76,7 @@ double value; char *error = NULL; int status; + unsigned int numcols; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); @@ -121,6 +124,12 @@ mysql_close(&mysql); die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned")); } + + /* Check number of columns in result */ + numcols = mysql_num_fields(res); + if(sql_column >= numcols) { + die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("Result has too few columns to satisfy --column")); + } /* fetch the first row */ if ( (row = mysql_fetch_row (res)) == NULL) { @@ -136,11 +145,11 @@ /* close the connection */ mysql_close (&mysql); - if (! is_numeric(row[0])) { + if (! is_numeric(row[sql_column])) { die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]); } - value = strtod(row[0], NULL); + value = strtod(row[sql_column], NULL); if (verbose >= 3) printf("mysql result: %f\n", value); @@ -182,6 +191,7 @@ {"query", required_argument, 0, 'q'}, {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, + {"column", required_argument, 0, 'C'}, {0, 0, 0, 0} }; @@ -189,7 +199,7 @@ return ERROR; while (1) { - c = getopt_long (argc, argv, "hvVSP:p:u:d:H:q:w:c:", longopts, &option); + c = getopt_long (argc, argv, "hvVSP:p:u:d:H:q:w:c:C:", longopts, &option); if (c == -1 || c == EOF) break; @@ -233,6 +243,9 @@ case 'q': asprintf(&sql_query, "%s", optarg); break; + case 'C': /* column */ + sql_column = atoi(optarg); + break; case 'w': warning = optarg; break; @@ -292,7 +305,9 @@ printf (_(UT_HELP_VRSN)); printf (" -q, --query=STRING\n"); - printf (" %s\n", _("SQL query to run. Only first column in first row will be read")); + printf (" %s\n", _("SQL query to run. Only first column (unless -c says otherwise) in first row will be read")); + printf (" -C, --column=INTEGER\n"); + printf (" %s\n", _("Column number in result row to use for comparison (Default: 0)")); printf (_(UT_WARN_CRIT_RANGE)); printf (_(UT_HOST_PORT), 'P', myport); printf (" -d, --database=STRING\n"); @@ -317,6 +332,6 @@ { printf (_("Usage:")); printf ("%s -q SQL_query [-w warn] [-c crit]\n",progname); - printf ("[-d database] [-H host] [-P port] [-u user] [-p password]\n"); + printf ("[ -C column ] [-d database] [-H host] [-P port] [-u user] [-p password]\n"); }