Merge branch 'collectd-4.0'
[collectd.git] / src / users.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #if HAVE_UTMPX_H
27 # include <utmpx.h>
28 /* #endif HAVE_UTMPX_H */
29
30 #elif HAVE_UTMP_H
31 # include <utmp.h>
32 /* #endif HAVE_UTMP_H */
33
34 #else
35 # error "No applicable input method."
36 #endif
37
38 static void users_submit (gauge_t value)
39 {
40         value_t values[1];
41         value_list_t vl = VALUE_LIST_INIT;
42
43         values[0].gauge = value;
44
45         vl.values = values;
46         vl.values_len = 1;
47         vl.time = time (NULL);
48         strcpy (vl.host, hostname_g);
49         strcpy (vl.plugin, "users");
50
51         plugin_dispatch_values ("users", &vl);
52 } /* void users_submit */
53
54 static int users_read (void)
55 {
56 #if HAVE_GETUTXENT
57         unsigned int users = 0;
58         struct utmpx *entry = NULL;
59
60         /* according to the *utent(3) man page none of the functions sets errno
61            in case of an error, so we cannot do any error-checking here */
62         setutxent();
63
64         while (NULL != (entry = getutxent())) {
65                 if (USER_PROCESS == entry->ut_type) {
66                         ++users;
67                 }
68         }
69         endutxent();
70
71         users_submit (users);
72 /* #endif HAVE_GETUTXENT */
73         
74 #elif HAVE_GETUTENT
75         unsigned int users = 0;
76         struct utmp *entry = NULL;
77
78         /* according to the *utent(3) man page none of the functions sets errno
79            in case of an error, so we cannot do any error-checking here */
80         setutent();
81
82         while (NULL != (entry = getutent())) {
83                 if (USER_PROCESS == entry->ut_type) {
84                         ++users;
85                 }
86         }
87         endutent();
88
89         users_submit (users);
90 /* #endif HAVE_GETUTENT */
91
92 #else
93 # error "No applicable input method."
94 #endif
95
96         return (0);
97 } /* int users_read */
98
99 void module_register (void)
100 {
101         plugin_register_read ("users", users_read);
102 } /* void module_register(void) */