1 /**
2 * collectd - src/users.c
3 * Copyright (C) 2005-2007 Sebastian Harl
4 * Copyright (C) 2005 Niki W. Waibel
5 * Copyright (C) 2005-2007 Florian octo Forster
6 * Copyright (C) 2008 Oleg King
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the license is applicable.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * Authors:
22 * Sebastian Harl <sh at tokkee.org>
23 * Niki W. Waibel <niki.waibel at newlogic.com>
24 * Florian octo Forster <octo at verplant.org>
25 * Oleg King <king2 at kaluga.ru>
26 **/
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #if HAVE_STATGRAB_H
33 # include <statgrab.h>
34 #endif /* HAVE_STATGRAB_H */
36 #if HAVE_UTMPX_H
37 # include <utmpx.h>
38 /* #endif HAVE_UTMPX_H */
40 #elif HAVE_UTMP_H
41 # include <utmp.h>
42 /* #endif HAVE_UTMP_H */
44 #else
45 # error "No applicable input method."
46 #endif
48 static void users_submit (gauge_t value)
49 {
50 value_t values[1];
51 value_list_t vl = VALUE_LIST_INIT;
53 values[0].gauge = value;
55 vl.values = values;
56 vl.values_len = 1;
57 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
58 sstrncpy (vl.plugin, "users", sizeof (vl.plugin));
59 sstrncpy (vl.type, "users", sizeof (vl.plugin));
61 plugin_dispatch_values (&vl);
62 } /* void users_submit */
64 static int users_read (void)
65 {
66 #if HAVE_GETUTXENT
67 unsigned int users = 0;
68 struct utmpx *entry = NULL;
70 /* according to the *utent(3) man page none of the functions sets errno
71 in case of an error, so we cannot do any error-checking here */
72 setutxent();
74 while (NULL != (entry = getutxent())) {
75 if (USER_PROCESS == entry->ut_type) {
76 ++users;
77 }
78 }
79 endutxent();
81 users_submit (users);
82 /* #endif HAVE_GETUTXENT */
84 #elif HAVE_GETUTENT
85 unsigned int users = 0;
86 struct utmp *entry = NULL;
88 /* according to the *utent(3) man page none of the functions sets errno
89 in case of an error, so we cannot do any error-checking here */
90 setutent();
92 while (NULL != (entry = getutent())) {
93 if (USER_PROCESS == entry->ut_type) {
94 ++users;
95 }
96 }
97 endutent();
99 users_submit (users);
100 /* #endif HAVE_GETUTENT */
102 #elif HAVE_LIBSTATGRAB
103 sg_user_stats *us;
105 us = sg_get_user_stats ();
106 if (us == NULL)
107 return (-1);
109 users_submit ((gauge_t) us->num_entries);
110 /* #endif HAVE_LIBSTATGRAB */
112 #else
113 # error "No applicable input method."
114 #endif
116 return (0);
117 } /* int users_read */
119 void module_register (void)
120 {
121 plugin_register_read ("users", users_read);
122 } /* void module_register(void) */