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