[collectd] client-side collectd without rrd

David Bacher drbacher at gmail.com
Sat Oct 29 02:10:22 CEST 2005


Skipped content of type multipart/alternative-------------- next part --------------
/* $Id: $ */

#include "collectd.h"

#include "multicast.h"
#include "plugin.h"

#include "ping.h"

static int loop = 0;

#ifdef HAVE_LIBKSTAT
kstat_ctl_t *kc;
#endif /* HAVE_LIBKSTAT */

#if COLLECT_PING
char *pinghosts[MAX_PINGHOSTS];
int   num_pinghosts = 0;
#endif

/*
 * exported variables
 */
time_t curtime;
int operating_mode;

extern char* optarg;


void sigIntHandler (int signal)
{
	loop++;
}

int change_basedir (char *dir)
{
	int dirlen = strlen (dir);
	
	while ((dirlen > 0) && (dir[dirlen - 1] == '/'))
		dir[--dirlen] = '\0';

	if (dirlen <= 0)
		return (-1);

	if (chdir (dir) == -1)
	{
		if (errno == ENOENT)
		{
			if (mkdir (dir, 0755) == -1)
			{
				syslog (LOG_ERR, "mkdir: %s", strerror (errno));
				return (-1);
			}
			else if (chdir (dir) == -1)
			{
				syslog (LOG_ERR, "chdir: %s", strerror (errno));
				return (-1);
			}
		}
		else
		{
			syslog (LOG_ERR, "chdir: %s", strerror (errno));
			return (-1);
		}
	}

	return (0);
}

#ifdef HAVE_LIBKSTAT
void update_kstat (void)
{
	if (kc == NULL)
	{
		if ((kc = kstat_open ()) == NULL)
			syslog (LOG_ERR, "Unable to open kstat control structure");
	}
	else
	{
		kid_t kid;
		kid = kstat_chain_update (kc);
		if (kid > 0)
		{
			syslog (LOG_INFO, "kstat chain has been updated");
			plugin_init_all ();
		}
		else if (kid < 0)
			syslog (LOG_ERR, "kstat chain update failed");
		/* else: everything works as expected */
	}

	return;
}
#endif /* HAVE_LIBKSTAT */

void exit_usage (char *name)
{
	printf ("Usage: %s [OPTIONS]\n\n"
			
			"Available options:\n"
			"  General:\n"
			"    -d <dir>        Base directory to use. Default: /var/lib/collectd\n"
			"    -P <dir>        Set the plugin-directory\n"
#if COLLECT_PING
			"  Ping:\n"
			"    -p <host>       Host to ping periodically, may be repeated to ping\n"
			"                    more than one host.\n"
#endif /* COLLECT_PING */
			"\n%s %s, http://verplant.org/collectd/\n"
			"by Florian octo Forster <octo at verplant.org>\n",
			PACKAGE, PACKAGE, VERSION);
	exit (0);
}

int main (int argc, char **argv)
{
	struct sigaction sigIntAction, sigChldAction;
	pid_t pid;

	char *plugindir = NULL;
	char *basedir = "/var/lib/collectd";

	int sleepingtime;

	operating_mode = MODE_CLIENT;
	
	/*
	 * open syslog
	 */
	openlog (PACKAGE, LOG_CONS | LOG_PID, LOG_DAEMON);

	/*
	 * read options
	 */
	while (1)
	{
		int c;

		c = getopt (argc, argv, "d:"
#if COLLECT_PING
				"p:"
#endif /* COLLECT_PING */
				"P:h");

		if (c == -1)
			break;

		switch (c)
		{
			case 'd':
				basedir = optarg;
				break;

#if COLLECT_PING
			case 'p':
				if (num_pinghosts < MAX_PINGHOSTS)
					pinghosts[num_pinghosts++] = optarg;
				else
					fprintf (stderr, "Maximum of %i ping hosts reached.\n", MAX_PINGHOSTS);
				break;
#endif /* COLLECT_PING */
			case 'P':
				plugindir = optarg;
				break;

			case 'h':
			default:
				exit_usage (argv[0]);
		}
				
	}

	/*
	 * Load plugins and change to output directory
	 */
	if (plugin_load_all (plugindir) < 1)
	{
		fprintf (stderr, "Error: No plugins found.\n");
		return (1);
	}

	if (change_basedir (basedir))
	{
		fprintf (stderr, "Unable to change to directory `%s'.\n", basedir);
		return (1);
	}

	/*
	 * install signal handlers
	 */
	sigIntAction.sa_handler = sigIntHandler;
	sigaction (SIGINT, &sigIntAction, NULL);

	sigChldAction.sa_handler = SIG_IGN;
	sigaction (SIGCHLD, &sigChldAction, NULL);

#ifdef HAVE_LIBKSTAT
	kc = NULL;
	update_kstat ();
#endif

#ifdef HAVE_LIBSTATGRAB
	if (sg_init ())
	{
		syslog (LOG_ERR, "sg_init: %s", sg_str_error (sg_get_error ()));
		return (-1);
	}

	if (sg_drop_privileges ())
	{
		syslog (LOG_ERR, "sg_drop_privileges: %s", sg_str_error (sg_get_error ()));
		return (-1);
	}
#endif

	plugin_init_all ();

	while (loop == 0)
	{
		curtime = time (NULL);
#ifdef HAVE_LIBKSTAT
		update_kstat ();
#endif
		plugin_read_all ();

		sleepingtime = 10;
		while (sleepingtime != 0)
		{
			if (loop != 0)
				break;
			sleepingtime = sleep (sleepingtime);
		}
	}

	/*
	 * close syslog
	 */
	closelog ();

	return (0);
}



















-------------- next part --------------
A non-text attachment was scrubbed...
Name: client.patch
Type: application/octet-stream
Size: 4204 bytes
Desc: not available
Url : http://mailman.verplant.org/pipermail/collectd/attachments/20051028/a656084c/client.obj


More information about the Collectd mailing list