9e8dc34d5420693e47f0ce827efed4ec1415ceba
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 extern time_t curtime;
47 void users_init(void)
48 {
49 /* we have nothing to do here :-) */
50 return;
51 }
53 void users_read(void)
54 {
55 #ifdef HAVE_GETUTXENT
56 unsigned int users = 0;
57 struct utmpx *entry = NULL;
59 /* according to the *utent(3) man page none of the functions sets errno in
60 * case of an error, so we cannot do any error-checking here */
61 setutxent();
63 while (NULL != (entry = getutxent()))
64 if (USER_PROCESS == entry->ut_type)
65 ++users;
66 endutxent();
68 users_submit(users);
69 /* #endif HAVE_GETUTXENT */
71 #elif defined(HAVE_GETUTENT)
72 unsigned int users = 0;
73 struct utmp *entry = NULL;
75 /* according to the *utent(3) man page none of the functions sets errno in
76 * case of an error, so we cannot do any error-checking here */
77 setutent();
79 while (NULL != (entry = getutent()))
80 if (USER_PROCESS == entry->ut_type)
81 ++users;
82 endutent();
84 users_submit(users);
85 #endif
87 return;
88 }
90 /* I don't like this temporary macro definition - well it's used everywhere
91 * else in the collectd-sources, so I will just stick with it... */
92 #define BUFSIZE 256
93 void users_submit(users)
94 unsigned int users;
95 {
96 char buf[BUFSIZE] = "";
98 if (snprintf(buf, BUFSIZE, "%u:%u",
99 (unsigned int)curtime,
100 users) >= BUFSIZE)
101 return;
103 plugin_submit(MODULE_NAME, NULL, buf);
104 return;
105 }
106 #undef BUFSIZE
108 void users_write(host, inst, val)
109 char *host;
110 char *inst;
111 char *val;
112 {
113 rrd_update_file(host, rrd_file, val, ds_def, ds_num);
114 return;
115 }
117 void module_register(void)
118 {
119 plugin_register(MODULE_NAME, users_init, users_read, users_write);
120 return;
121 }
123 #undef MODULE_NAME
124 #endif /* COLLECT_USERS */