X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fusers.c;h=08fc4fd74d349853be9510ebcc75a689d5623e74;hb=fd909f8965b2b6714dc1da328c77f3681c04af5e;hp=9e8dc34d5420693e47f0ce827efed4ec1415ceba;hpb=7fa270a1fb517c7fbed55d9f5f70bb28516b6229;p=collectd.git diff --git a/src/users.c b/src/users.c index 9e8dc34d..08fc4fd7 100644 --- a/src/users.c +++ b/src/users.c @@ -1,6 +1,6 @@ /** * collectd - src/users.c - * Copyright (C) 2005 Sebastian Harl + * Copyright (C) 2005,2006 Sebastian Harl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -20,106 +20,111 @@ * Sebastian Harl **/ -#include "users.h" +#include "collectd.h" +#include "common.h" +#include "plugin.h" -#if COLLECT_USERS -#define MODULE_NAME "users" +#if HAVE_UTMPX_H +# include +#else /* !HAVE_UTMPX_H */ +# if HAVE_UTMP_H +# include +# endif /* HAVE_UTMP_H */ +#endif /* HAVE_UTMPX_H */ -#include "plugin.h" -#include "common.h" +#define MODULE_NAME "users" -#ifdef HAVE_UTMPX_H -#include -#elif defined(HAVE_UTMP_H) -#include +#if HAVE_GETUTXENT || HAVE_GETUTENT +# define USERS_HAVE_READ 1 +#else +# define USERS_HAVE_READ 0 #endif static char *rrd_file = "users.rrd"; - -static char *ds_def[] = { - "DS:users:GAUGE:25:0:65535", - NULL +static char *ds_def[] = +{ + "DS:users:GAUGE:"COLLECTD_HEARTBEAT":0:65535", + NULL }; static int ds_num = 1; -extern time_t curtime; - -void users_init(void) +static void users_init (void) { - /* we have nothing to do here :-) */ - return; -} + /* we have nothing to do here :-) */ + return; +} /* static void users_init(void) */ -void users_read(void) +static void users_write (char *host, char *inst, char *val) { -#ifdef HAVE_GETUTXENT - unsigned int users = 0; - struct utmpx *entry = NULL; - - /* according to the *utent(3) man page none of the functions sets errno in - * case of an error, so we cannot do any error-checking here */ - setutxent(); - - while (NULL != (entry = getutxent())) - if (USER_PROCESS == entry->ut_type) - ++users; - endutxent(); - - users_submit(users); -/* #endif HAVE_GETUTXENT */ - -#elif defined(HAVE_GETUTENT) - unsigned int users = 0; - struct utmp *entry = NULL; - - /* according to the *utent(3) man page none of the functions sets errno in - * case of an error, so we cannot do any error-checking here */ - setutent(); - - while (NULL != (entry = getutent())) - if (USER_PROCESS == entry->ut_type) - ++users; - endutent(); - - users_submit(users); -#endif - + rrd_update_file(host, rrd_file, val, ds_def, ds_num); return; -} +} /* static void users_write(char *host, char *inst, char *val) */ +#if USERS_HAVE_READ /* I don't like this temporary macro definition - well it's used everywhere - * else in the collectd-sources, so I will just stick with it... */ + else in the collectd-sources, so I will just stick with it... */ #define BUFSIZE 256 -void users_submit(users) - unsigned int users; +static void users_submit (unsigned int users) { - char buf[BUFSIZE] = ""; + char buf[BUFSIZE] = ""; - if (snprintf(buf, BUFSIZE, "%u:%u", - (unsigned int)curtime, - users) >= BUFSIZE) - return; + if (snprintf(buf, BUFSIZE, "%u:%u", + (unsigned int)curtime, users) >= BUFSIZE) + { + return; + } - plugin_submit(MODULE_NAME, NULL, buf); - return; -} + plugin_submit(MODULE_NAME, NULL, buf); + return; +} /* static void users_submit(unsigned int users) */ #undef BUFSIZE -void users_write(host, inst, val) - char *host; - char *inst; - char *val; +static void users_read (void) { - rrd_update_file(host, rrd_file, val, ds_def, ds_num); - return; -} +#if HAVE_GETUTXENT + unsigned int users = 0; + struct utmpx *entry = NULL; + + /* according to the *utent(3) man page none of the functions sets errno + in case of an error, so we cannot do any error-checking here */ + setutxent(); + + while (NULL != (entry = getutxent())) { + if (USER_PROCESS == entry->ut_type) { + ++users; + } + } + endutxent(); + + users_submit (users); +/* #endif HAVE_GETUTXENT */ + +#elif HAVE_GETUTENT + unsigned int users = 0; + struct utmp *entry = NULL; -void module_register(void) -{ - plugin_register(MODULE_NAME, users_init, users_read, users_write); - return; -} + /* according to the *utent(3) man page none of the functions sets errno + in case of an error, so we cannot do any error-checking here */ + setutent(); -#undef MODULE_NAME -#endif /* COLLECT_USERS */ + while (NULL != (entry = getutent())) { + if (USER_PROCESS == entry->ut_type) { + ++users; + } + } + endutent(); + users_submit(users); +#endif /* HAVE_GETUTENT */ + + return; +} /* static void users_read(void) */ +#else +# define users_read NULL +#endif /* USERS_HAVE_READ */ + +void module_register (void) +{ + plugin_register (MODULE_NAME, users_init, users_read, users_write); + return; +} /* void module_register(void) */