Added "type" to the value_list_t struct.
[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         strcpy (vl.type, "users");
51
52         plugin_dispatch_values (&vl);
53 } /* void users_submit */
54
55 static int users_read (void)
56 {
57 #if HAVE_GETUTXENT
58         unsigned int users = 0;
59         struct utmpx *entry = NULL;
60
61         /* according to the *utent(3) man page none of the functions sets errno
62            in case of an error, so we cannot do any error-checking here */
63         setutxent();
64
65         while (NULL != (entry = getutxent())) {
66                 if (USER_PROCESS == entry->ut_type) {
67                         ++users;
68                 }
69         }
70         endutxent();
71
72         users_submit (users);
73 /* #endif HAVE_GETUTXENT */
74         
75 #elif HAVE_GETUTENT
76         unsigned int users = 0;
77         struct utmp *entry = NULL;
78
79         /* according to the *utent(3) man page none of the functions sets errno
80            in case of an error, so we cannot do any error-checking here */
81         setutent();
82
83         while (NULL != (entry = getutent())) {
84                 if (USER_PROCESS == entry->ut_type) {
85                         ++users;
86                 }
87         }
88         endutent();
89
90         users_submit (users);
91 /* #endif HAVE_GETUTENT */
92
93 #else
94 # error "No applicable input method."
95 #endif
96
97         return (0);
98 } /* int users_read */
99
100 void module_register (void)
101 {
102         plugin_register_read ("users", users_read);
103 } /* void module_register(void) */