Merge branch 'ff/types' into collectd-4
[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; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Sebastian Harl <sh at tokkee.org>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #if HAVE_UTMPX_H
28 # include <utmpx.h>
29 #else /* !HAVE_UTMPX_H */
30 # if HAVE_UTMP_H
31 #  include <utmp.h>
32 # endif /* HAVE_UTMP_H */
33 #endif /* HAVE_UTMPX_H */
34
35 #define MODULE_NAME "users"
36
37 #if HAVE_GETUTXENT || HAVE_GETUTENT
38 # define USERS_HAVE_READ 1
39 #else
40 # define USERS_HAVE_READ 0
41 #endif
42
43 #if USERS_HAVE_READ
44 static void users_submit (gauge_t value)
45 {
46         value_t values[1];
47         value_list_t vl = VALUE_LIST_INIT;
48
49         values[0].gauge = value;
50
51         vl.values = values;
52         vl.values_len = 1;
53         vl.time = time (NULL);
54         strcpy (vl.host, hostname_g);
55         strcpy (vl.plugin, "users");
56
57         plugin_dispatch_values ("users", &vl);
58 } /* void users_submit */
59
60 static int users_read (void)
61 {
62 #if HAVE_GETUTXENT
63         unsigned int users = 0;
64         struct utmpx *entry = NULL;
65
66         /* according to the *utent(3) man page none of the functions sets errno
67            in case of an error, so we cannot do any error-checking here */
68         setutxent();
69
70         while (NULL != (entry = getutxent())) {
71                 if (USER_PROCESS == entry->ut_type) {
72                         ++users;
73                 }
74         }
75         endutxent();
76
77         users_submit (users);
78 /* #endif HAVE_GETUTXENT */
79         
80 #elif HAVE_GETUTENT
81         unsigned int users = 0;
82         struct utmp *entry = NULL;
83
84         /* according to the *utent(3) man page none of the functions sets errno
85            in case of an error, so we cannot do any error-checking here */
86         setutent();
87
88         while (NULL != (entry = getutent())) {
89                 if (USER_PROCESS == entry->ut_type) {
90                         ++users;
91                 }
92         }
93         endutent();
94
95         users_submit (users);
96 #endif /* HAVE_GETUTENT */
97
98         return (0);
99 } /* int users_read */
100 #endif /* USERS_HAVE_READ */
101
102 void module_register (void)
103 {
104 #if USERS_HAVE_READ
105         plugin_register_read ("users", users_read);
106 #endif
107 } /* void module_register(void) */