X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fusers.c;h=08fc4fd74d349853be9510ebcc75a689d5623e74;hb=e864bcaff1a5205ab6fcdd34281544fe6a56c0fe;hp=96cd6214622b94019aacb8e42f0cdd9f6344743a;hpb=82d65d8766709d99edac10c46078ab2766341f62;p=collectd.git diff --git a/src/users.c b/src/users.c index 96cd6214..08fc4fd7 100644 --- a/src/users.c +++ b/src/users.c @@ -1,127 +1,130 @@ -/* - * users.c +/** + * collectd - src/users.c + * Copyright (C) 2005,2006 Sebastian Harl * - * users plugin for collectd + * 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 + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. * - * This plugin collects the number of users currently logged into the system. + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. * - * Written by Sebastian Harl + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * - * 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 Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "users.h" - -#if COLLECT_USERS -#define MODULE_NAME "users" + * Authors: + * Sebastian Harl + **/ -#include "plugin.h" +#include "collectd.h" #include "common.h" +#include "plugin.h" + +#if HAVE_UTMPX_H +# include +#else /* !HAVE_UTMPX_H */ +# if HAVE_UTMP_H +# include +# endif /* HAVE_UTMP_H */ +#endif /* HAVE_UTMPX_H */ -#ifdef HAVE_UTMPX_H -#include -#elif defined(HAVE_UTMP_H) -#include +#define MODULE_NAME "users" + +#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) */