Merge branch 'ff/avl-tree'
[collectd.git] / src / users.c
1 /**
2  * collectd - src/users.c
3  * Copyright (C) 2005,2006  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; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Sebastian Harl <sh at tokkee.org>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #if HAVE_UTMPX_H
28 # include <utmpx.h>
29 #else /* !HAVE_UTMPX_H */
30 # if HAVE_UTMP_H
31 #  include <utmp.h>
32 # endif /* HAVE_UTMP_H */
33 #endif /* HAVE_UTMPX_H */
34
35 #define MODULE_NAME "users"
36
37 #if HAVE_GETUTXENT || HAVE_GETUTENT
38 # define USERS_HAVE_READ 1
39 #else
40 # define USERS_HAVE_READ 0
41 #endif
42
43 static char *rrd_file = "users.rrd";
44 static char *ds_def[] =
45 {
46         "DS:users:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
47         NULL
48 };
49 static int ds_num = 1;
50
51 static void users_init (void)
52 {
53         /* we have nothing to do here :-) */
54         return;
55 } /* static void users_init(void) */
56
57 static void users_write (char *host, char *inst, char *val)
58 {
59         rrd_update_file(host, rrd_file, val, ds_def, ds_num);
60         return;
61 } /* static void users_write(char *host, char *inst, char *val) */
62
63 #if USERS_HAVE_READ
64 /* I don't like this temporary macro definition - well it's used everywhere
65    else in the collectd-sources, so I will just stick with it...  */
66 #define BUFSIZE 256
67 static void users_submit (unsigned int users)
68 {
69         char buf[BUFSIZE] = "";
70
71         if (snprintf(buf, BUFSIZE, "%u:%u", 
72                 (unsigned int)curtime, users) >= BUFSIZE)
73         {
74                 return;
75         }
76
77         plugin_submit(MODULE_NAME, "-", buf);
78         return;
79 } /* static void users_submit(unsigned int users) */
80 #undef BUFSIZE
81
82 static void users_read (void)
83 {
84 #if HAVE_GETUTXENT
85         unsigned int users = 0;
86         struct utmpx *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         setutxent();
91
92         while (NULL != (entry = getutxent())) {
93                 if (USER_PROCESS == entry->ut_type) {
94                         ++users;
95                 }
96         }
97         endutxent();
98
99         users_submit (users);
100 /* #endif HAVE_GETUTXENT */
101         
102 #elif HAVE_GETUTENT
103         unsigned int users = 0;
104         struct utmp *entry = NULL;
105
106         /* according to the *utent(3) man page none of the functions sets errno
107            in case of an error, so we cannot do any error-checking here */
108         setutent();
109
110         while (NULL != (entry = getutent())) {
111                 if (USER_PROCESS == entry->ut_type) {
112                         ++users;
113                 }
114         }
115         endutent();
116
117         users_submit(users);
118 #endif /* HAVE_GETUTENT */
119
120         return;
121 } /* static void users_read(void) */
122 #else
123 # define users_read NULL
124 #endif /* USERS_HAVE_READ */
125
126 void module_register (void)
127 {
128         plugin_register (MODULE_NAME, users_init, users_read, users_write);
129         return;
130 } /* void module_register(void) */