users plugin: Converted to the new plugin interface.
[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 static data_source_t dsrc[1] =
44 {
45         {"users",  DS_TYPE_GAUGE, 0.0, 65535.0}
46 };
47
48 static data_set_t ds =
49 {
50         "users", 1, dsrc
51 };
52
53 #if USERS_HAVE_READ
54 static void users_submit (gauge_t value)
55 {
56         value_t values[1];
57         value_list_t vl = VALUE_LIST_INIT;
58
59         DBG ("value = %lf;", value);
60
61         values[0].gauge = value;
62
63         vl.values = values;
64         vl.values_len = 1;
65         vl.time = time (NULL);
66         strcpy (vl.host, hostname_g);
67         strcpy (vl.plugin, "users");
68
69         plugin_dispatch_values ("users", &vl);
70 } /* void users_submit */
71
72 static int users_read (void)
73 {
74 #if HAVE_GETUTXENT
75         unsigned int users = 0;
76         struct utmpx *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         setutxent();
81
82         while (NULL != (entry = getutxent())) {
83                 if (USER_PROCESS == entry->ut_type) {
84                         ++users;
85                 }
86         }
87         endutxent();
88
89         users_submit (users);
90 /* #endif HAVE_GETUTXENT */
91         
92 #elif HAVE_GETUTENT
93         unsigned int users = 0;
94         struct utmp *entry = NULL;
95
96         /* according to the *utent(3) man page none of the functions sets errno
97            in case of an error, so we cannot do any error-checking here */
98         setutent();
99
100         while (NULL != (entry = getutent())) {
101                 if (USER_PROCESS == entry->ut_type) {
102                         ++users;
103                 }
104         }
105         endutent();
106
107         users_submit (users);
108 #endif /* HAVE_GETUTENT */
109
110         return (0);
111 } /* int users_read */
112 #endif /* USERS_HAVE_READ */
113
114 void module_register (void)
115 {
116         plugin_register_data_set (&ds);
117         plugin_register_read ("users", users_read);
118         return;
119 } /* void module_register(void) */