Code

cleanup on users module
[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  **/
23 #include "common.h"
24 #include "plugin.h"
25 #include "users.h"
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 */
37 #define MODULE_NAME "users"
39 static char *rrd_file = "users.rrd";
40 static char *ds_def[] = {
41         "DS:users:GAUGE:25:0:65535",
42         NULL
43 };
44 static int ds_num = 1;
48 static void users_submit(unsigned int users);
49 static void users_init(void);
50 static void users_read(void);
51 static void users_write(char *host, char *inst, char *val);
55 /* I don't like this temporary macro definition - well it's used everywhere
56    else in the collectd-sources, so I will just stick with it...  */
57 #define BUFSIZE 256
58 static void
59 users_submit(unsigned int users)
60 {
61         char buf[BUFSIZE] = "";
63         if (snprintf(buf, BUFSIZE, "%u:%u", 
64                 (unsigned int)curtime, users) >= BUFSIZE)
65         {
66                 return;
67         }
69         plugin_submit(MODULE_NAME, NULL, buf);
70         return;
71 } /* static void users_submit(unsigned int users) */
72 #undef BUFSIZE
76 static void
77 users_init(void)
78 {
79         /* we have nothing to do here :-) */
80         return;
81 } /* static void users_init(void) */
83 static void
84 users_read(void)
85 {
86 #if HAVE_GETUTXENT
87         unsigned int users = 0;
88         struct utmpx *entry = NULL;
90         /* according to the *utent(3) man page none of the functions sets errno
91            in case of an error, so we cannot do any error-checking here */
92         setutxent();
94         while (NULL != (entry = getutxent())) {
95                 if (USER_PROCESS == entry->ut_type) {
96                         ++users;
97                 }
98         }
99         endutxent();
101         users_submit(users);
102 #else /* !HAVE_GETUTXENT */
103 # if HAVE_GETUTENT
104         unsigned int users = 0;
105         struct utmp *entry = NULL;
107         /* according to the *utent(3) man page none of the functions sets errno
108            in case of an error, so we cannot do any error-checking here */
109         setutent();
111         while (NULL != (entry = getutent())) {
112                 if (USER_PROCESS == entry->ut_type) {
113                         ++users;
114                 }
115         }
116         endutent();
118         users_submit(users);
119 # endif /* HAVE_GETUTENT */
120 #endif /* HAVE_GETUTXENT */
122         return;
123 } /* static void users_read(void) */
125 static void
126 users_write(char *host, char *inst, char *val)
128         rrd_update_file(host, rrd_file, val, ds_def, ds_num);
129         return;
130 } /* static void users_write(char *host, char *inst, char *val) */
134 void
135 module_register(void)
137         plugin_register(MODULE_NAME, users_init, users_read, users_write);
138         return;
139 } /* void module_register(void) */