31c936c84d9ab018cb23756c74358677d2711d73
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 "users.h"
25 #if COLLECT_USERS
26 #define MODULE_NAME "users"
28 #include "plugin.h"
29 #include "common.h"
31 #ifdef HAVE_UTMPX_H
32 #include <utmpx.h>
33 #elif defined(HAVE_UTMP_H)
34 #include <utmp.h>
35 #endif
37 static char *rrd_file = "users.rrd";
39 static char *ds_def[] = {
40 "DS:users:GAUGE:25:0:65535",
41 NULL
42 };
43 static int ds_num = 1;
45 void users_init(void)
46 {
47 /* we have nothing to do here :-) */
48 return;
49 }
51 void users_read(void)
52 {
53 #ifdef HAVE_GETUTXENT
54 unsigned int users = 0;
55 struct utmpx *entry = NULL;
57 /* according to the *utent(3) man page none of the functions sets errno in
58 * case of an error, so we cannot do any error-checking here */
59 setutxent();
61 while (NULL != (entry = getutxent()))
62 if (USER_PROCESS == entry->ut_type)
63 ++users;
64 endutxent();
66 users_submit(users);
67 /* #endif HAVE_GETUTXENT */
69 #elif defined(HAVE_GETUTENT)
70 unsigned int users = 0;
71 struct utmp *entry = NULL;
73 /* according to the *utent(3) man page none of the functions sets errno in
74 * case of an error, so we cannot do any error-checking here */
75 setutent();
77 while (NULL != (entry = getutent()))
78 if (USER_PROCESS == entry->ut_type)
79 ++users;
80 endutent();
82 users_submit(users);
83 #endif
85 return;
86 }
88 /* I don't like this temporary macro definition - well it's used everywhere
89 * else in the collectd-sources, so I will just stick with it... */
90 #define BUFSIZE 256
91 void users_submit(users)
92 unsigned int users;
93 {
94 char buf[BUFSIZE] = "";
96 if (snprintf(buf, BUFSIZE, "%u:%u",
97 (unsigned int)curtime,
98 users) >= BUFSIZE)
99 return;
101 plugin_submit(MODULE_NAME, NULL, buf);
102 return;
103 }
104 #undef BUFSIZE
106 void users_write(host, inst, val)
107 char *host;
108 char *inst;
109 char *val;
110 {
111 rrd_update_file(host, rrd_file, val, ds_def, ds_num);
112 return;
113 }
115 void module_register(void)
116 {
117 plugin_register(MODULE_NAME, users_init, users_read, users_write);
118 return;
119 }
121 #undef MODULE_NAME
122 #endif /* COLLECT_USERS */