1e337540e5b4f5b258d00234472385c8a1187454
[collectd.git] / src / users.c
1 /**
2  * collectd - src/users.c
3  * Copyright (C) 2005-2007  Sebastian Harl
4  * Copyright (C) 2005       Niki W. Waibel
5  * Copyright (C) 2005-2007  Florian octo Forster
6  * Copyright (C) 2008       Oleg King 
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; only version 2 of the license is applicable.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *   Sebastian Harl <sh at tokkee.org>
23  *   Niki W. Waibel <niki.waibel at newlogic.com>
24  *   Florian octo Forster <octo at verplant.org>
25  *   Oleg King <king2 at kaluga.ru>
26  **/
27
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31
32 #if HAVE_STATGRAB_H
33 # include <statgrab.h>
34 #endif /* HAVE_STATGRAB_H */
35
36 #if HAVE_UTMPX_H
37 # include <utmpx.h>
38 /* #endif HAVE_UTMPX_H */
39
40 #elif HAVE_UTMP_H
41 # include <utmp.h>
42 /* #endif HAVE_UTMP_H */
43
44 #else
45 # error "No applicable input method."
46 #endif
47
48 static void users_submit (gauge_t value)
49 {
50         value_t values[1];
51         value_list_t vl = VALUE_LIST_INIT;
52
53         values[0].gauge = value;
54
55         vl.values = values;
56         vl.values_len = 1;
57         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
58         sstrncpy (vl.plugin, "users", sizeof (vl.plugin));
59         sstrncpy (vl.type, "users", sizeof (vl.plugin));
60
61         plugin_dispatch_values (&vl);
62 } /* void users_submit */
63
64 static int users_read (void)
65 {
66 #if HAVE_GETUTXENT
67         unsigned int users = 0;
68         struct utmpx *entry = NULL;
69
70         /* according to the *utent(3) man page none of the functions sets errno
71            in case of an error, so we cannot do any error-checking here */
72         setutxent();
73
74         while (NULL != (entry = getutxent())) {
75                 if (USER_PROCESS == entry->ut_type) {
76                         ++users;
77                 }
78         }
79         endutxent();
80
81         users_submit (users);
82 /* #endif HAVE_GETUTXENT */
83         
84 #elif HAVE_GETUTENT
85         unsigned int users = 0;
86         struct utmp *entry = NULL;
87
88         /* according to the *utent(3) man page none of the functions sets errno
89            in case of an error, so we cannot do any error-checking here */
90         setutent();
91
92         while (NULL != (entry = getutent())) {
93                 if (USER_PROCESS == entry->ut_type) {
94                         ++users;
95                 }
96         }
97         endutent();
98
99         users_submit (users);
100 /* #endif HAVE_GETUTENT */
101
102 #elif HAVE_LIBSTATGRAB
103         sg_user_stats *us;
104
105         us = sg_get_user_stats ();
106         if (us == NULL)
107                 return (-1);   
108
109         users_submit ((gauge_t) us->num_entries);
110 /* #endif HAVE_LIBSTATGRAB */
111
112 #else
113 # error "No applicable input method."
114 #endif
115
116         return (0);
117 } /* int users_read */
118
119 void module_register (void)
120 {
121         plugin_register_read ("users", users_read);
122 } /* void module_register(void) */