Merge branch 'collectd-5.5' into collectd-5.6
[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 collectd.org>
25  *   Oleg King <king2 at kaluga.ru>
26  **/
27
28 #include "collectd.h"
29
30 #include "common.h"
31 #include "plugin.h"
32
33 #if HAVE_STATGRAB_H
34 # include <statgrab.h>
35 #endif /* HAVE_STATGRAB_H */
36
37 #if HAVE_UTMPX_H
38 # include <utmpx.h>
39 /* #endif HAVE_UTMPX_H */
40
41 #elif HAVE_UTMP_H
42 # include <utmp.h>
43 /* #endif HAVE_UTMP_H */
44 #endif
45
46 static void users_submit (gauge_t value)
47 {
48         value_t values[1];
49         value_list_t vl = VALUE_LIST_INIT;
50
51         values[0].gauge = value;
52
53         vl.values = values;
54         vl.values_len = 1;
55         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
56         sstrncpy (vl.plugin, "users", sizeof (vl.plugin));
57         sstrncpy (vl.type, "users", sizeof (vl.plugin));
58
59         plugin_dispatch_values (&vl);
60 } /* void users_submit */
61
62 static int users_read (void)
63 {
64 #if HAVE_GETUTXENT
65         unsigned int users = 0;
66         struct utmpx *entry = NULL;
67
68         /* according to the *utent(3) man page none of the functions sets errno
69            in case of an error, so we cannot do any error-checking here */
70         setutxent();
71
72         while (NULL != (entry = getutxent())) {
73                 if (USER_PROCESS == entry->ut_type) {
74                         ++users;
75                 }
76         }
77         endutxent();
78
79         users_submit (users);
80 /* #endif HAVE_GETUTXENT */
81
82 #elif HAVE_GETUTENT
83         unsigned int users = 0;
84         struct utmp *entry = NULL;
85
86         /* according to the *utent(3) man page none of the functions sets errno
87            in case of an error, so we cannot do any error-checking here */
88         setutent();
89
90         while (NULL != (entry = getutent())) {
91                 if (USER_PROCESS == entry->ut_type) {
92                         ++users;
93                 }
94         }
95         endutent();
96
97         users_submit (users);
98 /* #endif HAVE_GETUTENT */
99
100 #elif HAVE_LIBSTATGRAB
101         sg_user_stats *us;
102
103 # if HAVE_LIBSTATGRAB_0_90
104         size_t num_entries;
105         us = sg_get_user_stats (&num_entries);
106 # else
107         us = sg_get_user_stats ();
108 # endif
109         if (us == NULL)
110                 return (-1);
111
112         users_submit ((gauge_t)
113 # if HAVE_LIBSTATGRAB_0_90
114                       num_entries);
115 # else
116                       us->num_entries);
117 # endif
118 /* #endif HAVE_LIBSTATGRAB */
119
120 #else
121 # error "No applicable input method."
122 #endif
123
124         return (0);
125 } /* int users_read */
126
127 void module_register (void)
128 {
129         plugin_register_read ("users", users_read);
130 } /* void module_register(void) */