[hobbit] MRTG-based Graphs in Trends Section

Henrik Stoerner henrik at hswn.dk
Sun May 15 09:53:50 CEST 2005


On Thu, May 12, 2005 at 12:28:31PM -0400, Oktay, Alan F. *HS wrote:

> Any way to have Hobbit read the Title Tag in the MRTG Configuration File
> and thus label the graphs accordingly?

Not directly, but the attached patch lets you have an external
script decide what the title is.

I've tried it on my own MRTG setup and it appears to work fine.
I used this script to generate the title from mrtg.cfg (note that
you may have to change the permissions on mrtg.cfg to let the 
CGI user read it):

#!/bin/sh

HOSTNAME="$1"; shift
GTYPE="$1"; shift
LEGEND="$1"; shift

RRD="$1"; shift
#echo "RRD=$RRD, rest is $*" >>/tmp/title.log

MRTGSET="`echo $RRD | sed -e's/\.rrd$//'`"
#echo "MRTGSET=$MRTGSET" >>/tmp/title.log

MRTGTITLE=`grep -i "Title\[$MRTGSET\]:" /etc/mrtg.cfg | cut -d: -f2-`
#echo "TITLE=$MRTGTITLE" >>/tmp/title.log

echo "$MRTGTITLE $LEGEND"
exit 0


Regards,
Henrik

-------------- next part --------------
--- hobbitd/hobbitgraph.c	2005/05/07 09:24:20	1.28
+++ hobbitd/hobbitgraph.c	2005/05/15 07:40:44
@@ -11,7 +11,7 @@
 /*                                                                            */
 /*----------------------------------------------------------------------------*/
 
-static char rcsid[] = "$Id: hobbitgraph.c,v 1.28 2005/05/07 09:24:20 henrik Exp $";
+static char rcsid[] = "$Id: hobbitgraph.c,v 1.30 2005/05/15 07:40:32 henrik Exp $";
 
 #include <limits.h>
 #include <stdio.h>
@@ -734,7 +734,35 @@
 	qsort(&rrddbs[0], rrddbcount, sizeof(rrddb_t), rrd_name_compare);
 
 	/* Setup the title */
-	sprintf(graphtitle, "%s %s %s", displayname, gdef->title, glegend);
+	if (!gdef->title) gdef->title = strdup("");
+	if (strncmp(gdef->title, "exec:", 5) == 0) {
+		char *pcmd;
+		int i, pcmdlen = 0;
+		FILE *pfd;
+		char *p;
+
+		pcmdlen = strlen(gdef->title+5) + strlen(displayname) + strlen(service) + strlen(glegend) + 5;
+		for (i=0; (i<rrddbcount); i++) pcmdlen += (strlen(rrddbs[i].rrdfn) + 3);
+
+		p = pcmd = (char *)malloc(pcmdlen+1);
+		p += sprintf(p, "%s %s %s \"%s\"", gdef->title+5, displayname, service, glegend);
+		for (i=0; (i<rrddbcount); i++) {
+			if ((firstidx == -1) || ((i >= firstidx) && (i <= lastidx))) {
+				p += sprintf(p, " \"%s\"", rrddbs[i].rrdfn);
+			}
+		}
+		pfd = popen(pcmd, "r");
+		if (pfd) {
+			fgets(graphtitle, sizeof(graphtitle), pfd);
+			pclose(pfd);
+		}
+
+		/* Drop any newline at end of the title */
+		p = strchr(graphtitle, '\n'); if (p) *p = '\0';
+	}
+	else {
+		sprintf(graphtitle, "%s %s %s", displayname, gdef->title, glegend);
+	}
 
 	/*
 	 * Setup the arguments for calling rrd_graph. 


More information about the Xymon mailing list