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