summaryrefslogtreecommitdiffstats
path: root/web/attachments/258626-check_mysql_query.c.diff
blob: c06efe0fa79d06e0dbe0f538d3cdbf78ffe5c002 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
--- 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");
 }