Took a closer `look' at last nights changes..
[collectd.git] / src / users.c
1 /**
2  * collectd - src/users.c
3  * Copyright (C) 2005  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 "common.h"
24 #include "plugin.h"
25 #include "users.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 static char *rrd_file = "users.rrd";
38 static char *ds_def[] =
39 {
40         "DS:users:GAUGE:25:0:65535",
41         NULL
42 };
43 static int ds_num = 1;
44
45 static void users_init (void)
46 {
47         /* we have nothing to do here :-) */
48         return;
49 } /* static void users_init(void) */
50
51 static void users_write (char *host, char *inst, char *val)
52 {
53         rrd_update_file(host, rrd_file, val, ds_def, ds_num);
54         return;
55 } /* static void users_write(char *host, char *inst, char *val) */
56
57 /* I don't like this temporary macro definition - well it's used everywhere
58    else in the collectd-sources, so I will just stick with it...  */
59 #define BUFSIZE 256
60 static void users_submit (unsigned int users)
61 {
62         char buf[BUFSIZE] = "";
63
64         if (snprintf(buf, BUFSIZE, "%u:%u", 
65                 (unsigned int)curtime, users) >= BUFSIZE)
66         {
67                 return;
68         }
69
70         plugin_submit(MODULE_NAME, NULL, buf);
71         return;
72 } /* static void users_submit(unsigned int users) */
73 #undef BUFSIZE
74
75 static void users_read (void)
76 {
77 #if HAVE_GETUTXENT
78         unsigned int users = 0;
79         struct utmpx *entry = NULL;
80
81         /* according to the *utent(3) man page none of the functions sets errno
82            in case of an error, so we cannot do any error-checking here */
83         setutxent();
84
85         while (NULL != (entry = getutxent())) {
86                 if (USER_PROCESS == entry->ut_type) {
87                         ++users;
88                 }
89         }
90         endutxent();
91
92         users_submit (users);
93 /* #endif HAVE_GETUTXENT */
94         
95 #elif HAVE_GETUTENT
96         unsigned int users = 0;
97         struct utmp *entry = NULL;
98
99         /* according to the *utent(3) man page none of the functions sets errno
100            in case of an error, so we cannot do any error-checking here */
101         setutent();
102
103         while (NULL != (entry = getutent())) {
104                 if (USER_PROCESS == entry->ut_type) {
105                         ++users;
106                 }
107         }
108         endutent();
109
110         users_submit(users);
111 #endif /* HAVE_GETUTENT */
112
113         return;
114 } /* static void users_read(void) */
115
116 void module_register (void)
117 {
118         plugin_register (MODULE_NAME, users_init, users_read, users_write);
119         return;
120 } /* void module_register(void) */