Implemented `LoadDS' which tells plugins to only register their DataSources.
[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         values[0].gauge = value;
60
61         vl.values = values;
62         vl.values_len = 1;
63         vl.time = time (NULL);
64         strcpy (vl.host, hostname_g);
65         strcpy (vl.plugin, "users");
66
67         plugin_dispatch_values ("users", &vl);
68 } /* void users_submit */
69
70 static int users_read (void)
71 {
72 #if HAVE_GETUTXENT
73         unsigned int users = 0;
74         struct utmpx *entry = NULL;
75
76         /* according to the *utent(3) man page none of the functions sets errno
77            in case of an error, so we cannot do any error-checking here */
78         setutxent();
79
80         while (NULL != (entry = getutxent())) {
81                 if (USER_PROCESS == entry->ut_type) {
82                         ++users;
83                 }
84         }
85         endutxent();
86
87         users_submit (users);
88 /* #endif HAVE_GETUTXENT */
89         
90 #elif HAVE_GETUTENT
91         unsigned int users = 0;
92         struct utmp *entry = NULL;
93
94         /* according to the *utent(3) man page none of the functions sets errno
95            in case of an error, so we cannot do any error-checking here */
96         setutent();
97
98         while (NULL != (entry = getutent())) {
99                 if (USER_PROCESS == entry->ut_type) {
100                         ++users;
101                 }
102         }
103         endutent();
104
105         users_submit (users);
106 #endif /* HAVE_GETUTENT */
107
108         return (0);
109 } /* int users_read */
110 #endif /* USERS_HAVE_READ */
111
112 void module_register (modreg_e load)
113 {
114         if (load & MR_DATASETS)
115                 plugin_register_data_set (&ds);
116
117 #if USERS_HAVE_READ
118         if (load & MR_READ)
119                 plugin_register_read ("users", users_read);
120 #endif
121 } /* void module_register(void) */