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