Exemplo n.º 1
0
// main takes care of OS-specific startup and dispatches to xmain.
void
main(int argc, char **argv)
{
	Buf b;

	setvbuf(stdout, nil, _IOLBF, BUFSIZ);
	setvbuf(stderr, nil, _IOLBF, BUFSIZ);

	binit(&b);

	rfork(RFENVG);

	slash = "/";
	gohostos = "plan9";

	xgetenv(&b, "objtype");
	if(b.len == 0)
		fatal("$objtype is unset");
	gohostarch = btake(&b);

	xgetenv(&b, "GOBIN");
	if(b.len == 0){
		bpathf(&b, "/%s/bin", gohostarch);
		xsetenv("GOBIN", bstr(&b));
	}

	srand(time(0)+getpid());
	init();
	xmain(argc, argv);

	bfree(&b);
	exits(nil);
}
Exemplo n.º 2
0
void delete_old_acks(void)
{
	DIR             *xymonacks;
	struct dirent   *d;
	struct stat     st;
	time_t		now = getcurrenttime(NULL);
	char		fn[PATH_MAX];

	xymonacks = opendir(xgetenv("XYMONACKDIR"));
	if (!xymonacks) {
		errprintf("No XYMONACKDIR! Cannot cd to directory %s\n", xgetenv("XYMONACKDIR"));
		return;
        }

	chdir(xgetenv("XYMONACKDIR"));
	while ((d = readdir(xymonacks))) {
		strcpy(fn, d->d_name);
		if (strncmp(fn, "ack.", 4) == 0) {
			stat(fn, &st);
			if (S_ISREG(st.st_mode) && (st.st_mtime < now)) {
				unlink(fn);
			}
		}
	}
	closedir(xymonacks);
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
	int argi;
	char *envarea = NULL;
	char *critconfig = NULL;
	char *hffile = "critical";

	for (argi = 1; (argi < argc); argi++) {
		if (argnmatch(argv[argi], "--env=")) {
			char *p = strchr(argv[argi], '=');
			loadenv(p+1, envarea);
		}
		else if (argnmatch(argv[argi], "--area=")) {
			char *p = strchr(argv[argi], '=');
			envarea = strdup(p+1);
		}
		else if (strcmp(argv[argi], "--debug") == 0) {
			debug = 1;
		}
		else if (strcmp(argv[argi], "--tooltips") == 0) {
			usetooltips = 1;
		}
		else if (argnmatch(argv[argi], "--acklevel=")) {
			char *p = strchr(argv[argi], '=');
			critacklevel = atoi(p+1);
		}
		else if (argnmatch(argv[argi], "--config=")) {
			char *p = strchr(argv[argi], '=');
			critconfig = strdup(p+1);
		}
		else if (argnmatch(argv[argi], "--hffile=")) {
			char *p = strchr(argv[argi], '=');
			hffile = strdup(p+1);
		}
	}

	redirect_cgilog("criticalview");

	setdocurl(hostsvcurl("%s", xgetenv("INFOCOLUMN"), 1));

	parse_query();
	load_hostnames(xgetenv("HOSTSCFG"), NULL, get_fqdn());
	load_critconfig(critconfig);
	load_all_links();
	fprintf(stdout, "Content-type: %s\n\n", xgetenv("HTMLCONTENTTYPE"));

	if (loadstatus(maxprio, maxage, mincolor, wantacked) == 0) {
		use_recentgifs = 1;
		generate_critpage(stdout, hffile);
	}
	else {
		fprintf(stdout, "Cannot load Xymon status\n");
	}

	return 0;
}
Exemplo n.º 4
0
void load_cookies(void)
{
	char cookiefn[PATH_MAX];

	sprintf(cookiefn, "%s/etc/cookies", xgetenv("XYMONHOME"));
	load_cookies_one(cookiefn);

	sprintf(cookiefn, "%s/etc/cookies.session", xgetenv("XYMONHOME"));
	load_cookies_one(cookiefn);
}
Exemplo n.º 5
0
static void xymon_default_envs(char *envfn)
{
	FILE *fd;
	char buf[1024];
	char *evar;
	char *homedir, *p;

	if (getenv("MACHINEDOTS") == NULL) {
	    if (getenv("HOSTNAME") != NULL) sprintf(buf, "%s", xgetenv("HOSTNAME"));
	    else {
		fd = popen("uname -n", "r");
		if (fd && fgets(buf, sizeof(buf), fd)) {
			p = strchr(buf, '\n'); if (p) *p = '\0';
			pclose(fd);
		}
		else strcpy(buf, "localhost");
	    }
		evar = (char *)malloc(strlen(buf) + 13);
		sprintf(evar, "MACHINEDOTS=%s", buf);
		putenv(evar);
	}

	xgetenv("MACHINE");

	if (getenv("SERVEROSTYPE") == NULL) {
		fd = popen("uname -s", "r");
		if (fd && fgets(buf, sizeof(buf), fd)) {
			p = strchr(buf, '\n'); if (p) *p = '\0';
			pclose(fd);
		}
		else strcpy(buf, "unix");
		for (p=buf; (*p); p++) *p = (char) tolower((int)*p);

		evar = (char *)malloc(strlen(buf) + 14);
		sprintf(evar, "SERVEROSTYPE=%s", buf);
		putenv(evar);
	}

	if (getenv("XYMONCLIENTHOME") == NULL) {
		homedir = strdup(envfn);
		p = strrchr(homedir, '/');
		if (p) {
			*p = '\0';
			if (strlen(homedir) > 4) {
				p = homedir + strlen(homedir) - 4;
				if (strcmp(p, "/etc") == 0) {
					*p = '\0';
					evar = (char *)malloc(20 + strlen(homedir));
					sprintf(evar, "XYMONCLIENTHOME=%s", homedir);
					putenv(evar);
				}
			}
		}
	}
}
Exemplo n.º 6
0
void libxymon_init(char *toolname)
{
	static int firstcall = 1;

	if (firstcall) {
		firstcall = 0;

		programname = basename(strdup(toolname));

		pidfn = (char *)malloc(strlen(xgetenv("XYMONSERVERLOGS")) + strlen(programname) + 6);
		sprintf(pidfn, "%s/%s.pid", xgetenv("XYMONSERVERLOGS"), programname);
	}
}
Exemplo n.º 7
0
sendresult_t sendmessage(char *msg, char *recipient, int timeout, sendreturn_t *response)
{
	static char *xymsrv = NULL;
	int res = 0;

	*errordetails = '\0';

 	if ((xymsrv == NULL) && xgetenv("XYMSRV")) xymsrv = strdup(xgetenv("XYMSRV"));
	if (recipient == NULL) recipient = xymsrv;
	if ((recipient == NULL) && xgetenv("XYMSERVERS")) {
		recipient = "0.0.0.0";
	} else if (recipient == NULL) {
		errprintf("No recipient for message\n");
		return XYMONSEND_EBADIP;
	}

	res = sendtomany(recipient, xgetenv("XYMSERVERS"), msg, timeout, response);

	if (res != XYMONSEND_OK) {
		char *statustext = "";
		char *eoln;

		switch (res) {
		  case XYMONSEND_OK            : statustext = "OK"; break;
		  case XYMONSEND_EBADIP        : statustext = "Bad IP address"; break;
		  case XYMONSEND_EIPUNKNOWN    : statustext = "Cannot resolve hostname"; break;
		  case XYMONSEND_ENOSOCKET     : statustext = "Cannot get a socket"; break;
		  case XYMONSEND_ECANNOTDONONBLOCK   : statustext = "Non-blocking I/O failed"; break;
		  case XYMONSEND_ECONNFAILED   : statustext = "Connection failed"; break;
		  case XYMONSEND_ESELFAILED    : statustext = "select(2) failed"; break;
		  case XYMONSEND_ETIMEOUT      : statustext = "timeout"; break;
		  case XYMONSEND_EWRITEERROR   : statustext = "write error"; break;
		  case XYMONSEND_EREADERROR    : statustext = "read error"; break;
		  case XYMONSEND_EBADURL       : statustext = "Bad URL"; break;
		  default:                statustext = "Unknown error"; break;
		};

		eoln = strchr(msg, '\n'); if (eoln) *eoln = '\0';
		if (strcmp(recipient, "0.0.0.0") == 0) recipient = xgetenv("XYMSERVERS");
		errprintf("Whoops ! Failed to send message (%s)\n", statustext);
		errprintf("->  %s\n", errordetails);
		errprintf("->  Recipient '%s', timeout %d\n", recipient, timeout);
		errprintf("->  1st line: '%s'\n", msg);
		if (eoln) *eoln = '\n';
	}

	/* Give it a break */
	if (sleepbetweenmsgs) usleep(sleepbetweenmsgs);
	xymonmsgcount++;
	return res;
}
Exemplo n.º 8
0
void print_colheaders(FILE *output, RbtHandle rbcolumns)
{
	int colcount;
	RbtIterator colhandle;

	colcount = 1;	/* Remember the hostname column */

	/* Group column headings */
	fprintf(output, "<TR>");
	fprintf(output, "<TD ROWSPAN=2>&nbsp;</TD>\n");	/* For the prio column - in both row headers+dash rows */
	fprintf(output, "<TD ROWSPAN=2>&nbsp;</TD>\n");	/* For the host column - in both row headers+dash rows */
	for (colhandle = rbtBegin(rbcolumns); (colhandle != rbtEnd(rbcolumns)); colhandle = rbtNext(rbcolumns, colhandle)) {
		void *k1, *k2;
		char *colname;

	        rbtKeyValue(rbcolumns, colhandle, &k1, &k2);
		colname = (char *)k1;
		colcount++;

		fprintf(output, " <TD ALIGN=CENTER VALIGN=BOTTOM WIDTH=45>\n");
		fprintf(output, " <A HREF=\"%s\"><FONT %s><B>%s</B></FONT></A> </TD>\n",
			columnlink(colname), xgetenv("XYMONPAGECOLFONT"), colname);
	}
	fprintf(output, "</TR>\n");
	fprintf(output, "<TR><TD COLSPAN=%d><HR WIDTH=\"100%%\"></TD></TR>\n\n", colcount);
}
Exemplo n.º 9
0
static int servicecode(char *testname)
{
    /*
     * The SVCCODES environment is a list of servicecodes:
     * SVCCODES="disk:100,cpu:200,procs:300,msgs:400,conn:500,http:600,dns:800,smtp:725,telnet:721"
     * This routine returns the number associated with the service.
     */
    static char *svccodes = NULL;
    char *tname;
    char *p;

    if (svccodes == NULL) {
        p = xgetenv("SVCCODES");
        if (p == NULL) p = "none";
        svccodes = (char *)malloc(strlen(p)+2);
        sprintf(svccodes, ",%s", p);
    }

    tname = (char *)malloc(strlen(testname)+3);
    sprintf(tname, ",%s:", testname);
    p = strstr(svccodes, tname);
    xfree(tname);

    if (p) {
        p = strchr(p, ':');
        return atoi(p+1);
    }

    return 0;
}
Exemplo n.º 10
0
void save_session_cookies(void)
{
	FILE *fd = NULL;
	char cookiefn[PATH_MAX];
	xtreePos_t h;
	hcookie_t *itm;

	sprintf(cookiefn, "%s/etc/cookies.session", xgetenv("XYMONHOME"));
	fd = fopen(cookiefn, "w");
	if (fd == NULL) return;

	for (h=xtreeFirst(cookietree); (h != xtreeEnd(cookietree)); h = xtreeNext(cookietree, h)) {
		char *urlhost, *ckpath, *cknam;

		itm = (hcookie_t *)xtreeData(cookietree, h);
		urlhost = strtok(itm->key, "\t");
		cknam = strtok(NULL, "\t");
		ckpath = strtok(NULL, "\t");

		fprintf(fd, "%s\tFALSE\t%s\tFALSE\t0\t%s\t%s\n",
			urlhost, ckpath, cknam, itm->val);
	}

	fclose(fd);
}
Exemplo n.º 11
0
int main(int argc, char *argv[])
{
	void *walk;

	load_hostnames(xgetenv("HOSTSCFG"), NULL, get_fqdn());

	for (walk = first_host(); (walk); walk=next_host(walk, 0)) {
		char *nk, *nktime, *tok;

		nk = xmh_item(walk, XMH_NK); if (!nk) continue;
		nktime = xmh_item(walk, XMH_NKTIME);

		nk = strdup(nk);

		tok = strtok(nk, ",");
		while (tok) {
			char *hostname = xmh_item(walk, XMH_HOSTNAME);
			char *startstr = "", *endstr = "", *ttgroup = "", *ttextra = "", *updinfo = "Migrated";
			int priority = 2;

			fprintf(stdout, "%s|%s|%s|%s|%s|%d|%s|%s|%s\n",
				hostname, tok,
				startstr, endstr,
				(nktime ? nktime : ""),
				priority, ttgroup, ttextra, updinfo);

			tok = strtok(NULL, ",");
		}

		xfree(nk);
	}

	return 0;
}
Exemplo n.º 12
0
static void print_disklist(char *hostname)
{
    /*
     * We get the list of monitored disks/filesystems by looking at the
     * set of disk RRD files for this host. That way we do not have to
     * parse the disk status reports that come in many different flavours.
     */

    char dirname[PATH_MAX];
    char fn[PATH_MAX];
    DIR *d;
    struct dirent *de;
    char *p;

    sprintf(dirname, "%s/%s", xgetenv("BBRRDS"), hostname);
    d = opendir(dirname);
    if (!d) return;

    while ((de = readdir(d)) != NULL) {
        if (strncmp(de->d_name, "disk,", 5) == 0) {
            strcpy(fn, de->d_name + 4);
            p = strstr(fn, ".rrd");
            if (!p) continue;
            *p = '\0';
            p = fn;
            while ((p = strchr(p, ',')) != NULL) *p = '/';
            fprintf(stdout, "%s<br>\n", fn);
        }
    }

    closedir(d);
}
Exemplo n.º 13
0
YBORM_DECL const String env_cfg(const String &entry, const String &def_val)
{
    String value = xgetenv(_T("YBORM_") + entry);
    if (str_empty(value))
        return def_val;
    return value;
}
Exemplo n.º 14
0
// The env command prints the default environment.
void
cmdenv(int argc, char **argv)
{
	bool pflag;
	char *sep;
	Buf b, b1;
	char *format;

	binit(&b);
	binit(&b1);

	format = "%s=\"%s\"\n";
	pflag = 0;
	ARGBEGIN{
	case '9':
		format = "%s='%s'\n";
		break;
	case 'p':
		pflag = 1;
		break;
	case 'v':
		vflag++;
		break;
	case 'w':
		format = "set %s=%s\r\n";
		break;
	default:
		usage();
	}ARGEND

	if(argc > 0)
		usage();

	xprintf(format, "CC", defaultcc);
	xprintf(format, "CC_FOR_TARGET", defaultcctarget);
	xprintf(format, "GOROOT", goroot);
	xprintf(format, "GOBIN", gobin);
	xprintf(format, "GOARCH", goarch);
	xprintf(format, "GOOS", goos);
	xprintf(format, "GOHOSTARCH", gohostarch);
	xprintf(format, "GOHOSTOS", gohostos);
	xprintf(format, "GOTOOLDIR", tooldir);
	xprintf(format, "GOCHAR", gochar);
	if(streq(goarch, "arm"))
		xprintf(format, "GOARM", goarm);
	if(streq(goarch, "386"))
		xprintf(format, "GO386", go386);

	if(pflag) {
		sep = ":";
		if(streq(gohostos, "windows"))
			sep = ";";
		xgetenv(&b, "PATH");
		bprintf(&b1, "%s%s%s", gobin, sep, bstr(&b));
		xprintf(format, "PATH", bstr(&b1));
	}

	bfree(&b);
	bfree(&b1);
}
Exemplo n.º 15
0
void errormsg(char *msg)
{
	printf("Content-type: %s\n\n", xgetenv("HTMLCONTENTTYPE"));
	printf("<html><head><title>Invalid request</title></head>\n");
	printf("<body>%s</body></html>\n", msg);
	exit(1);
}
Exemplo n.º 16
0
static int cert_password_cb(char *buf, int size, int rwflag, void *userdata)
{
	FILE *passfd;
	char *p;
	char passfn[PATH_MAX];
	char passphrase[1024];
	tcptest_t *item = (tcptest_t *)userdata;

	memset(passphrase, 0, sizeof(passphrase));

	/*
	 * Private key passphrases are stored in the file named same as the
	 * certificate itself, but with extension ".pass"
	 */
	sprintf(passfn, "%s/certs/%s", xgetenv("XYMONHOME"), item->ssloptions->clientcert);
	p = strrchr(passfn, '.'); if (p == NULL) p = passfn+strlen(passfn);
	strcpy(p, ".pass");

	passfd = fopen(passfn, "r");
	if (passfd) {
		fgets(passphrase, sizeof(passphrase)-1, passfd);
		p = strchr(passphrase, '\n'); if (p) *p = '\0';
		fclose(passfd);
	}

	strncpy(buf, passphrase, size);
	buf[size - 1] = '\0';

	/* Clear this buffer for security! Dont want passphrases in core dumps... */
	memset(passphrase, 0, sizeof(passphrase));

	return strlen(buf);
}
Exemplo n.º 17
0
void setup_opts(struct opts *opts, int argc, char **argv) {
    opts->o_loglevel = getenv("VCSLOG_DEBUG")? LOG_DEBUG : LOG_INFO;
    const char *datadir = getenv("VCSLOG_HOME");
    if (datadir == NULL)
        datadir = xasprintf("%s/.vcslog", xgetenv("HOME"));
    opts->o_datadir = datadir;
    opts->o_logdir = xasprintf("%s/logs", opts->o_datadir);
    opts->o_execname = xstrdup(basename(argv[0]));
    // If a full path was used
    if (strchr(argv[0], '/')) {
        if (!realpath(argv[0], opts->o_realpath)) {
            fprintf(stderr, "vcslog-wrapper: realpath(\"%s\") failed\n",
                    argv[0]);
            exit(1);
        }
    } else {
        char temp_path[PATH_MAX + 1];
        xfind_real_executable(opts, temp_path);
        strcpy(opts->o_realpath, temp_path);
    }
    opts->o_argc = argc - 1;
    opts->o_argv = argv + 1;

    struct passwd *passwd = getpwuid(geteuid());
    opts->o_session_log = xasprintf("%s/vcslog-%s-%s-%d", opts->o_logdir, 
                                    opts->o_execname, passwd->pw_name, getpid());

    opts->o_session_log_file = NULL;
}
Exemplo n.º 18
0
void errormsg(char *msg)
{
	printf("Content-type: %s\n\n", xgetenv("HTMLCONTENTTYPE"));
	printf("<html><head><title>Xymon FindHost Error</title></head>\n");
	printf("<body><BR><BR><BR>%s</body></html>\n", msg);
	exit(1);
}
Exemplo n.º 19
0
int main(int argc, char *argv[])
{
	int argi;
	char *envarea = NULL;

	for (argi = 1; (argi < argc); argi++) {
		if (argnmatch(argv[argi], "--env=")) {
			char *p = strchr(argv[argi], '=');
			loadenv(p+1, envarea);
		}
		else if (argnmatch(argv[argi], "--area=")) {
			char *p = strchr(argv[argi], '=');
			envarea = strdup(p+1);
		}
		else if (strcmp(argv[argi], "--debug") == 0) {
			debug = 1;
		}
	}

	redirect_cgilog("hobbit-notifylog");
	load_hostnames(xgetenv("BBHOSTS"), NULL, get_fqdn());

	fprintf(stdout, "Content-type: %s\n\n", xgetenv("HTMLCONTENTTYPE"));

	cgidata = cgi_request();
	if (cgidata == NULL) {
		/* Present the query form */
		sethostenv("", "", "", colorname(COL_BLUE), NULL);
		showform(stdout, "notify", "notify_form", COL_BLUE, getcurrenttime(NULL), NULL, NULL);
		return 0;
	}

	parse_query();

	/* Now generate the webpage */
	headfoot(stdout, "notify", "", "header", COL_GREEN);
	fprintf(stdout, "<center>\n");
	do_notifylog(stdout, maxcount, maxminutes, fromtime, totime, 
			pageregex, expageregex, 
			hostregex, exhostregex, 
			testregex, extestregex,
			rcptregex, exrcptregex);
	fprintf(stdout, "</center>\n");
	headfoot(stdout, "notify", "", "footer", COL_GREEN);

	return 0;
}
Exemplo n.º 20
0
static void errormsg(char *msg)
{
	snprintf(errortxt, sizeof(errortxt),
		 "Content-type: %s\n\n<html><head><title>Invalid request</title></head>\n<body>%s</body></html>\n", 
		 xgetenv("HTMLCONTENTTYPE"), msg);

	errortxt[sizeof(errortxt)-1] = '\0';
}
Exemplo n.º 21
0
void redirect_cgilog(char *cginame)
{
	char logfn[PATH_MAX];

	if (cginame) errappname = strdup(cginame);
	sprintf(logfn, "%s/cgierror.log", xgetenv("BBSERVERLOGS"));
	freopen(logfn, "a", stderr);
}
Exemplo n.º 22
0
/* Routines for handling combo message transmission */
static void combo_params(void)
{
	static int issetup = 0;

	if (issetup) return;

	issetup = 1;

	if (xgetenv("MAXMSGSPERCOMBO")) maxmsgspercombo = atoi(xgetenv("MAXMSGSPERCOMBO"));
	if (maxmsgspercombo == 0) {
		/* Force it to 100 */
		dbgprintf("MAXMSGSPERCOMBO is 0, setting it to 100\n");
		maxmsgspercombo = 100;
	}

	if (xgetenv("SLEEPBETWEENMSGS")) sleepbetweenmsgs = atoi(xgetenv("SLEEPBETWEENMSGS"));
}
Exemplo n.º 23
0
void do_extensions(FILE *output, char *extenv, char *family)
{
	/*
	 * Extension scripts. These are ad-hoc, and implemented as a
	 * simple pipe. So we do a fork here ...
	 */

	char *exts, *p;
	FILE *inpipe;
	char extfn[PATH_MAX];
	strbuffer_t *inbuf;

	p = xgetenv(extenv);
	if (p == NULL) {
		/* No extension */
		return;
	}

	MEMDEFINE(extfn);

	exts = strdup(p);
	p = strtok(exts, "\t ");
	inbuf = newstrbuffer(0);

	while (p) {
		/* Dont redo the eventlog or acklog things */
		if ((strcmp(p, "eventlog.sh") != 0) &&
		    (strcmp(p, "acklog.sh") != 0)) {
			sprintf(extfn, "%s/ext/%s/%s", xgetenv("XYMONHOME"), family, p);
			inpipe = popen(extfn, "r");
			if (inpipe) {
				initfgets(inpipe);
				while (unlimfgets(inbuf, inpipe)) fputs(STRBUF(inbuf), output);
				pclose(inpipe);
				freestrbuffer(inbuf);
			}
		}
		p = strtok(NULL, "\t ");
	}

	xfree(exts);

	MEMUNDEFINE(extfn);
	MEMUNDEFINE(buf);
}
Exemplo n.º 24
0
void print_header(void)
{
        /* It's ok with these hardcoded values, as they are not used for this page */
        sethostenv("", "", "", colorname(COL_BLUE), NULL);
	printf("Content-Type: %s\n\n", xgetenv("HTMLCONTENTTYPE"));
        headfoot(stdout, "findhost", "", "header", COL_BLUE);
	printf("<br><br><CENTER><TABLE CELLPADDING=5 SUMMARY=\"Hostlist\">\n");
	printf("<tr><th align=left>Hostname (DisplayName)</th><th align=left>Location (Group Name)</th></tr>\n");
}
Exemplo n.º 25
0
static void initial_rss_setup(void)
{
	static int hasrun = 0;

	if (hasrun) return;

	if (xgetenv("BBRSSTITLE")) rsstitle = strdup(xgetenv("BBRSSTITLE"));

	if      (strcmp(rssversion, "0.91") == 0) rssver = RSS091;
	else if (strcmp(rssversion, "0.92") == 0) rssver = RSS092;
	else if (strcmp(rssversion, "1.0") == 0)  rssver = RSS10;
	else if (strcmp(rssversion, "2.0") == 0)  rssver = RSS20;
	else {
		errprintf("Unknown RSS version requested (%s), using 0.91\n", rssversion);
		rssver = RSS091;
	}

	ttlvalue = (xgetenv("BBSLEEP") ? (atoi(xgetenv("BBSLEEP")) / 60) : 5);
}
Exemplo n.º 26
0
void addnetpeer(char *peername)
{
	xymon_peer_t *newpeer;
	struct in_addr addr;
	char *oneip;
	int peerport = 0;
	char *delim;

	dbgprintf("Adding network peer %s\n", peername);

	oneip = strdup(peername);

	delim = strchr(oneip, ':');
	if (delim) {
		*delim = '\0';
		peerport = atoi(delim+1);
	}

	if (inet_aton(oneip, &addr) == 0) {
		/* peer is not an IP - do DNS lookup */

		struct hostent *hent;

		hent = gethostbyname(oneip);
		if (hent) {
			char *realip;

			memcpy(&addr, *(hent->h_addr_list), sizeof(struct in_addr));
			realip = inet_ntoa(addr);
			if (inet_aton(realip, &addr) == 0) {
				errprintf("Invalid IP address for %s (%s)\n", oneip, realip);
				goto done;
			}
		}
		else {
			errprintf("Cannot determine IP address of peer %s\n", oneip);
			goto done;
		}
	}

	if (peerport == 0) peerport = atoi(xgetenv("XYMONDPORT"));

	newpeer = calloc(1, sizeof(xymon_peer_t));
	newpeer->peername = strdup(peername);
	newpeer->peerstatus = P_DOWN;
	newpeer->peertype = P_NET;
	newpeer->peeraddr.sin_family = AF_INET;
	newpeer->peeraddr.sin_addr.s_addr = addr.s_addr;
	newpeer->peeraddr.sin_port = htons(peerport);

	rbtInsert(peers, newpeer->peername, newpeer);

done:
	xfree(oneip);
}
Exemplo n.º 27
0
char *xgetenv(const char *name)
{
	char *result, *newstr;
	int i;

	result = getenv(name);
	if ((result == NULL) && (strcmp(name, "MACHINE") == 0) && xgetenv("MACHINEDOTS")) {
		/* If MACHINE is undefined, but MACHINEDOTS is there, create MACHINE  */
		char *oneenv, *p;
		
		oneenv = (char *)malloc(10 + strlen(xgetenv("MACHINEDOTS")));
		sprintf(oneenv, "%s=%s", name, xgetenv("MACHINEDOTS"));
		p = oneenv; while ((p = strchr(p, '.')) != NULL) *p = ',';
		putenv(oneenv);
		result = getenv(name);
	}

	if (result == NULL) {
		for (i=0; (xymonenv[i].name && (strcmp(xymonenv[i].name, name) != 0)); i++) ;
		if (xymonenv[i].name) result = expand_env(xymonenv[i].val);
		if (result == NULL) {
			errprintf("xgetenv: Cannot find value for variable %s\n", name);
			return NULL;
		}

		/* 
		 * If we got a result, put it into the environment so it will stay there.
		 * Allocate memory for this new environment string - this stays allocated.
		 */
		newstr = malloc(strlen(name) + strlen(result) + 2);
		sprintf(newstr, "%s=%s", name, result);
		putenv(newstr);

		/*
		 * Return pointer to the environment string.
		 */
		result = getenv(name);
	}

	return result;
}
Exemplo n.º 28
0
Arquivo: sig.c Projeto: tjyang/abmon
void setup_signalhandler(char *programname)
{
	struct rlimit lim;
	struct sigaction sa;

	MEMDEFINE(signal_bbcmd);
	MEMDEFINE(signal_bbdisp);
	MEMDEFINE(signal_bbtmp);
	MEMDEFINE(signal_msg);

	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = sigsegv_handler;

	/*
	 * Try to allow ourselves to generate core files
	 */
	getrlimit(RLIMIT_CORE, &lim);
	lim.rlim_cur = RLIM_INFINITY;
	setrlimit(RLIMIT_CORE, &lim);

	if (xgetenv("BB") == NULL) return;
	if (xgetenv("BBDISP") == NULL) return;

	/*
	 * Used inside signal-handler. Must be setup in
	 * advance.
	 */
	strcpy(signal_bbcmd, xgetenv("BB"));
	strcpy(signal_bbdisp, xgetenv("BBDISP"));
	strcpy(signal_bbtmp, xgetenv("BBTMP"));
	sprintf(signal_msg, "status %s.%s red - Program crashed\n\nFatal signal caught!\n", 
		(xgetenv("MACHINE") ? xgetenv("MACHINE") : "BBDISPLAY"), programname);

	sigaction(SIGSEGV, &sa, NULL);
	sigaction(SIGILL, &sa, NULL);
#ifdef SIGBUS
	sigaction(SIGBUS, &sa, NULL);
#endif

	/*
	 * After lengthy debugging and perusing of mail archives:
	 * Need to ignore SIGPIPE since FreeBSD (and others?) can throw this
	 * on a write() instead of simply returning -EPIPE like any sane
	 * OS would.
	 */
	signal(SIGPIPE, SIG_IGN);

	/* Ignore SIGUSR1 unless explicitly set by main program */
	signal (SIGUSR1, SIG_IGN);

	/* SIGUSR2 toggles debugging */
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = sigusr2_handler;
	sigaction(SIGUSR2, &sa, NULL);
}
Exemplo n.º 29
0
void save_state(void)
{
	char *tmpdir;
	char *fn;
	FILE *fd;
	int tidx;

	tmpdir = xgetenv("XYMONTMP"); if (!tmpdir) tmpdir = "/tmp";
	fn = (char *)malloc(strlen(tmpdir) + 100);

	sprintf(fn, "%s/locator.servers.chk", tmpdir);
	fd = fopen(fn, "w");
	if (fd == NULL) {
		errprintf("Cannot save state to %s: %s\n", fn, strerror(errno));
		return;
	}
	for (tidx = 0; (tidx < ST_MAX); tidx++) {
		const char *tname = servicetype_names[tidx];
		xtreePos_t handle;
		serverinfo_t *itm;

		for (handle = xtreeFirst(sitree[tidx]); (handle != xtreeEnd(sitree[tidx])); handle = xtreeNext(sitree[tidx], handle)) {
			itm = xtreeData(sitree[tidx], handle);
			fprintf(fd, "%s|%s|%d|%d|%d|%s\n",
				tname, itm->servername, itm->serverconfweight, itm->serveractualweight,
				((itm->sticky == LOC_STICKY) ? 1 : 0), 
				(itm->serverextras ? itm->serverextras : ""));
		}
	}
	fclose(fd);

	sprintf(fn, "%s/locator.hosts.chk", tmpdir);
	fd = fopen(fn, "w");
	if (fd == NULL) {
		errprintf("Cannot save state to %s: %s\n", fn, strerror(errno));
		return;
	}
	for (tidx = 0; (tidx < ST_MAX); tidx++) {
		const char *tname = servicetype_names[tidx];
		xtreePos_t handle;
		hostinfo_t *itm;

		for (handle = xtreeFirst(hitree[tidx]); (handle != xtreeEnd(hitree[tidx])); handle = xtreeNext(hitree[tidx], handle)) {
			itm = xtreeData(hitree[tidx], handle);
			if (itm->server) {
				fprintf(fd, "%s|%s|%s\n",
					tname, itm->hostname, itm->server->servername);
			}
		}
	}
	fclose(fd);
}
Exemplo n.º 30
0
void loadhostdata(char *hostname, char **ip, char **displayname)
{
	namelist_t *hinfo = NULL;

	load_hostnames(xgetenv("BBHOSTS"), NULL, get_fqdn());

	if ((hinfo = hostinfo(hostname)) == NULL) {
		errormsg("No such host");
		exit(1);
	}

	*ip = bbh_item(hinfo, BBH_IP);
	*displayname = bbh_item(hinfo, BBH_DISPLAYNAME);
	if (!(*displayname)) *displayname = hostname;
}