Various plugins: Update copyright information.
[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         vl.time = time (NULL);
58         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
59         sstrncpy (vl.plugin, "users", sizeof (vl.plugin));
60         sstrncpy (vl.type, "users", sizeof (vl.plugin));
61
62         plugin_dispatch_values (&vl);
63 } /* void users_submit */
64
65 static int users_read (void)
66 {
67 #if HAVE_GETUTXENT
68         unsigned int users = 0;
69         struct utmpx *entry = NULL;
70
71         /* according to the *utent(3) man page none of the functions sets errno
72            in case of an error, so we cannot do any error-checking here */
73         setutxent();
74
75         while (NULL != (entry = getutxent())) {
76                 if (USER_PROCESS == entry->ut_type) {
77                         ++users;
78                 }
79         }
80         endutxent();
81
82         users_submit (users);
83 /* #endif HAVE_GETUTXENT */
84         
85 #elif HAVE_GETUTENT
86         unsigned int users = 0;
87         struct utmp *entry = NULL;
88
89         /* according to the *utent(3) man page none of the functions sets errno
90            in case of an error, so we cannot do any error-checking here */
91         setutent();
92
93         while (NULL != (entry = getutent())) {
94                 if (USER_PROCESS == entry->ut_type) {
95                         ++users;
96                 }
97         }
98         endutent();
99
100         users_submit (users);
101 /* #endif HAVE_GETUTENT */
102
103 #elif HAVE_LIBSTATGRAB
104         sg_user_stats *us;
105
106         us = sg_get_user_stats ();
107         if (us == NULL)
108                 return (-1);   
109
110         users_submit ((gauge_t) us->num_entries);
111 /* #endif HAVE_LIBSTATGRAB */
112
113 #else
114 # error "No applicable input method."
115 #endif
116
117         return (0);
118 } /* int users_read */
119
120 void module_register (void)
121 {
122         plugin_register_read ("users", users_read);
123 } /* void module_register(void) */