X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fusers.c;h=7df06c2baa41fe382ab536a25b7e2468dabf8b12;hb=a095d4f8e3917cca8a989bf1d8f0a6979a3d9652;hp=36cea0fb4b4618826cdb7e13af49f4f1b9f6daca;hpb=45ae66d1ad43708da953835b5dd30c1127d59019;p=collectd.git diff --git a/src/users.c b/src/users.c index 36cea0fb..7df06c2b 100644 --- a/src/users.c +++ b/src/users.c @@ -1,104 +1,106 @@ -/* - * users.c +/** + * collectd - src/users.c + * Copyright (C) 2005-2007 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; only version 2 of the license is applicable. * - * 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" -#include - -static char *rrd_file = "users.rrd"; +#if HAVE_UTMPX_H +# include +#else /* !HAVE_UTMPX_H */ +# if HAVE_UTMP_H +# include +# endif /* HAVE_UTMP_H */ +#endif /* HAVE_UTMPX_H */ -static char *ds_def[] = { - "DS:users:GAUGE:25:0:65535", - NULL -}; -static int ds_num = 1; +#define MODULE_NAME "users" -extern time_t curtime; +#if HAVE_GETUTXENT || HAVE_GETUTENT +# define USERS_HAVE_READ 1 +#else +# define USERS_HAVE_READ 0 +#endif -void users_init(void) +#if USERS_HAVE_READ +static void users_submit (gauge_t value) { - /* we have nothing to do here :-) */ - return; -} + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; -void users_read(void) -{ - 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); - return; -} - -/* 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... */ -#define BUFSIZE 256 -void users_submit(users) - unsigned int users; -{ - char buf[BUFSIZE] = ""; - - if (snprintf(buf, BUFSIZE, "%u:%u", - (unsigned int)curtime, - users) >= BUFSIZE) - return; - - plugin_submit(MODULE_NAME, NULL, buf); - return; -} -#undef BUFSIZE - -void users_write(host, inst, val) - char *host; - char *inst; - char *val; -{ - rrd_update_file(host, rrd_file, val, ds_def, ds_num); - return; -} + values[0].gauge = value; -void module_register(void) -{ - plugin_register(MODULE_NAME, users_init, users_read, users_write); - return; -} + vl.values = values; + vl.values_len = 1; + vl.time = time (NULL); + strcpy (vl.host, hostname_g); + strcpy (vl.plugin, "users"); -#undef MODULE_NAME -#endif /* COLLECT_USERS */ + plugin_dispatch_values ("users", &vl); +} /* void users_submit */ +static int users_read (void) +{ +#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; + + /* 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 /* HAVE_GETUTENT */ + + return (0); +} /* int users_read */ +#endif /* USERS_HAVE_READ */ + +void module_register (void) +{ +#if USERS_HAVE_READ + plugin_register_read ("users", users_read); +#endif +} /* void module_register(void) */