[nagiosplug] check_mysql: add perfromance metrics for all ...

Nagios Plugin Development nagios-plugins at users.sourceforge.net
Sun Aug 18 18:50:16 CEST 2013


    Module: nagiosplug
    Branch: master
    Commit: c8d8b584475692d0e325e3f6658a0d3e5d464d4a
    Author: Tim Laszlo <tim.laszlo at gmail.com>
 Committer: Holger Weiss <holger at zedat.fu-berlin.de>
      Date: Mon Aug  6 11:15:35 2012 -0500
       URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=c8d8b58

check_mysql: add perfromance metrics for all checks

---

 plugins/check_mysql.c |   71 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index eaad709..212910d 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -62,6 +62,29 @@ int verbose = 0;
 static double warning_time = 0;
 static double critical_time = 0;
 
+#define LENGTH_METRIC_UNIT 7
+static const char *metric_unit[LENGTH_METRIC_UNIT] = {
+	"Connections",
+	"Open_files",
+	"Open_tables",
+	"Qcache_free_memory",
+	"Qcache_queries_in_cache",
+	"Threads_connected",
+	"Threads_running"
+};
+
+#define LENGTH_METRIC_COUNTER 8
+static const char *metric_counter[LENGTH_METRIC_COUNTER] = {
+	"Qcache_hits",
+	"Qcache_inserts",
+	"Qcache_lowmem_prunes",
+	"Qcache_not_cached",
+	"Queries",
+	"Questions",
+	"Table_locks_waited",
+	"Uptime"
+};
+
 thresholds *my_threshold = NULL;
 
 int process_arguments (int, char **);
@@ -82,7 +105,9 @@ main (int argc, char **argv)
 	char *result = NULL;
 	char *error = NULL;
 	char slaveresult[SLAVERESULTSIZE];
-	char* slaveperfdata = NULL;
+	char* perf;
+
+        perf = strdup ("");
 
 	setlocale (LC_ALL, "");
 	bindtextdomain (PACKAGE, LOCALEDIR);
@@ -130,6 +155,34 @@ main (int argc, char **argv)
 			die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
 	}
 
+	/* try to fetch some perf data */
+	if (mysql_query (&mysql, "show global status") == 0) {
+		if ( (res = mysql_store_result (&mysql)) == NULL) {
+			error = strdup(mysql_error(&mysql));
+			mysql_close (&mysql);
+			die (STATE_CRITICAL, _("status store_result error: %s\n"), error);
+		}
+
+		while ( (row = mysql_fetch_row (res)) != NULL) {
+			int i;
+
+			for(i = 0; i < LENGTH_METRIC_UNIT - 1; i++) {
+				if (strcmp(row[0], metric_unit[i]) == 0) {
+					xasprintf(&perf, "%s %s", perf, perfdata(metric_unit[i],
+						atol(row[1]), "", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0));
+					continue;
+				}
+			}
+			for(i = 0; i < LENGTH_METRIC_COUNTER - 1; i++) {
+				if (strcmp(row[0], metric_counter[i]) == 0) {
+					xasprintf(&perf, "%s %s", perf, perfdata(metric_counter[i],
+						atol(row[1]), "c", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0));
+					continue;
+				}
+			}
+		}
+	}
+
 	if(check_slave) {
 		/* check the slave status */
 		if (mysql_query (&mysql, "show slave status") != 0) {
@@ -222,17 +275,17 @@ main (int argc, char **argv)
 
 				status = get_status(value, my_threshold);
 
-				slaveperfdata = fperfdata ("seconds behind master", value, "s",
+				xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s",
         	                        TRUE, (double) warning_time,
                 	                TRUE, (double) critical_time,
                         	        FALSE, 0,
-                                	FALSE, 0);
+                                	FALSE, 0));
 
 				if (status == STATE_WARNING) {
-					printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, slaveperfdata);
+					printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf);
 					exit(STATE_WARNING);
 				} else if (status == STATE_CRITICAL) {
-					printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, slaveperfdata);
+					printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf);
 					exit(STATE_CRITICAL);
 				}
 			}
@@ -246,12 +299,10 @@ main (int argc, char **argv)
 	mysql_close (&mysql);
 
 	/* print out the result of stats */
-	if (check_slave && slaveperfdata) {
-		printf ("%s %s|%s\n", result, slaveresult, slaveperfdata);
-	} else if (check_slave) {
-		printf ("%s %s\n", result, slaveresult);
+	if (check_slave) {
+		printf ("%s %s|%s\n", result, slaveresult, perf);
 	} else {
-		printf ("%s\n", result);
+		printf ("%s|%s\n", result, perf);
 	}
 
 	return STATE_OK;





More information about the Commits mailing list