[hobbit] hobbit monitoring

Henrik Stoerner henrik at hswn.dk
Wed Jan 17 13:54:23 CET 2007


On Wed, Jan 17, 2007 at 01:02:21PM +0100, Stef Coene wrote:

> The second problem is the fact that I want to distribute the same client and 
> scripts to all my servers.  So the clienlaunch config file needs a parameter 
> where I can specify on which server I want to run the tests (in the old 
> BigBrother days, there was a config file for this).
> I don't think this is hard to do, but my C knowledge is really that bad that I 
> don't even try to do this :(

Try this modification for hobbitlaunch; it adds an "ONHOST" setting to
hobbitlaunch.cfg, so a task will be disabled unless the local hostname
matches this expression. E.g. if you have a task that should only run on
hosts whose hostname begins with "web", then

   [webchecks]
        ENVFILE /usr/lib/hobbit/server/etc/hobbitserver.cfg
	CMD $BBHOME/ext/webchecks.sh
	INTERVAL 5m
	ONHOST ^web


Regards,
Henrik

-------------- next part --------------
--- common/hobbitlaunch.cfg.5	2006/08/09 19:49:47	1.47
+++ common/hobbitlaunch.cfg.5	2007/01/17 12:46:54
@@ -55,6 +55,12 @@
 tasks back into the file, so unless you have them listed as DISABLED
 then tasks may re-appear unexpectedly after an upgrade.
 
+The \fBONHOST\fR keyword tells hobbitlaunch that this task should
+only run on specific hosts. After the ONHOST keyword, you must
+provide a "regular expression"; if the hostname where hobbitlaunch
+runs matches this expression, then the task will run. If it doesn't
+match, then the task is treated as if it were DISABLED.
+
 The \fBNEEDS\fR instructs hobbitlaunch not to run this task unless
 the task defined by the NEEDS keyword is already running. This
 is used e.g. to delay the start of some application until the
--- common/hobbitlaunch.c	2006/07/20 16:06:41	1.41
+++ common/hobbitlaunch.c	2007/01/17 12:44:12
@@ -26,6 +26,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <ctype.h>
+#include <regex.h>
 
 #include "libbbgen.h"
 
@@ -56,7 +57,7 @@
 	char *cmd;
 	int interval;
 	char *logfile;
-	char *envfile, *envarea;
+	char *envfile, *envarea, *onhostptn;
 	pid_t pid;
 	time_t laststart;
 	int exitcode;
@@ -80,6 +81,7 @@
 	int freeit = 1;
 	int logfilechanged = 0;
 	int envfilechanged = 0;
+	int disablechanged = 0;
 
 	for (twalk = taskhead; (twalk && (strcmp(twalk->key, newtask->key))); twalk = twalk->next);
 
@@ -113,6 +115,11 @@
 		else {
 			envfilechanged = 0;
 		}
+
+		if (twalk->disabled != newtask->disabled)
+			disablechanged = 1;
+		else
+			disablechanged = 0;
 	}
 
 	if (newtask->cmd == NULL) {
@@ -128,16 +135,18 @@
 		tasktail = newtask;
 		freeit = 0;
 	}
-	else if (strcmp(twalk->cmd, newtask->cmd) || logfilechanged || envfilechanged) {
+	else if (strcmp(twalk->cmd, newtask->cmd) || logfilechanged || envfilechanged || disablechanged) {
 		/* Task changed. */
 		xfree(twalk->cmd); 
 		if (twalk->logfile) xfree(twalk->logfile);
 		if (twalk->envfile) xfree(twalk->envfile);
 		if (twalk->envarea) xfree(twalk->envarea);
+		if (twalk->onhostptn) xfree(twalk->onhostptn);
 		twalk->cmd = strdup(newtask->cmd);
 		if (newtask->logfile) twalk->logfile = strdup(newtask->logfile); else twalk->logfile = NULL;
 		if (newtask->envfile) twalk->envfile = strdup(newtask->envfile); else twalk->envfile = NULL;
 		if (newtask->envarea) twalk->envarea = strdup(newtask->envarea); else twalk->envarea = NULL;
+		if (newtask->onhostptn) twalk->onhostptn = strdup(newtask->onhostptn); else twalk->onhostptn = NULL;
 
 		/* Must bounce the task */
 		twalk->cfload = 1;
@@ -161,6 +170,7 @@
 		if (newtask->logfile) xfree(newtask->logfile);
 		if (newtask->envfile) xfree(newtask->envfile);
 		if (newtask->envarea) xfree(newtask->envarea);
+		if (newtask->onhostptn) xfree(newtask->onhostptn);
 		xfree(newtask);
 	}
 }
@@ -172,6 +182,7 @@
 	FILE *fd;
 	strbuffer_t *inbuf;
 	char *p;
+	char myhostname[256];
 
 	/* First check if there were no modifications at all */
 	if (configfiles) {
@@ -186,6 +197,10 @@
 	}
 
 	errprintf("Loading tasklist configuration from %s\n", conffn);
+	if (gethostname(myhostname, sizeof(myhostname)) != 0) {
+		errprintf("Cannot get the local hostname, using 'localhost' (error: %s)\n", strerror(errno));
+		strcpy(myhostname, "localhost");
+	}
 
 	/* The cfload flag: -1=delete task, 0=old task unchanged, 1=new/changed task */
 	for (twalk = taskhead; (twalk); twalk = twalk->next) {
@@ -290,6 +305,25 @@
 		else if (curtask && (strcasecmp(p, "DISABLED") == 0)) {
 			curtask->disabled = 1;
 		}
+		else if (curtask && (strncasecmp(p, "ONHOST ", 7) == 0)) {
+			regex_t cpattern;
+			int status;
+
+			p += 7;
+			p += strspn(p, " \t");
+
+			curtask->onhostptn = strdup(p);
+
+			/* Match the hostname against the pattern; if it doesnt match then disable the task */
+			status = regcomp(&cpattern, curtask->onhostptn, REG_EXTENDED|REG_ICASE|REG_NOSUB);
+			if (status == 0) {
+				status = regexec(&cpattern, myhostname, 0, NULL, 0);
+				if (status == REG_NOMATCH) curtask->disabled = 1;
+			}
+			else {
+				errprintf("ONHOST pattern '%s' is invalid\n", p);
+			}
+		}
 	}
 	if (curtask) update_task(curtask);
 	stackfclose(fd);
@@ -310,6 +344,7 @@
 			if (twalk->logfile) xfree(twalk->logfile);
 			if (twalk->envfile) xfree(twalk->envfile);
 			if (twalk->envarea) xfree(twalk->envarea);
+			if (twalk->onhostptn) xfree(twalk->onhostptn);
 			break;
 
 		  case 0:
@@ -437,12 +472,14 @@
 			for (twalk = taskhead; (twalk); twalk = twalk->next) {
 				printf("[%s]\n", twalk->key);
 				printf("\tCMD %s\n", twalk->cmd);
+				if (twalk->disabled) printf("\tDISABLED\n");
 				if (twalk->group)    printf("\tGROUP %s\n", twalk->group->groupname);
 				if (twalk->depends)  printf("\tNEEDS %s\n", twalk->depends->key);
 				if (twalk->interval) printf("\tINTERVAL %d\n", twalk->interval);
 				if (twalk->logfile)  printf("\tLOGFILE %s\n", twalk->logfile);
 				if (twalk->envfile)  printf("\tENVFILE %s\n", twalk->envfile);
 				if (twalk->envarea)  printf("\tENVAREA %s\n", twalk->envarea);
+				if (twalk->onhostptn) printf("\tONHOST %s\n", twalk->onhostptn);
 				printf("\n");
 			}
 			return 0;


More information about the Xymon mailing list