[collectd] users plugin

Sebastian Harl sh at tokkee.org
Sun Dec 4 18:40:29 CET 2005


Hi there,

I've written a small plugin for collectd which counts the number of users
logged into the system. It is based on revision 258 of the source code. I
could only test it on my Linux box, but it should work on any other Unix
System as well - any feedback is appreciated ;-)

Also, I found that the '-P' command line option was still undocumented in the
manpage, so I added an appropriate section to the POD-file.

Hope you'll like it ;-)

Cheers,
Sebastian


Index: contrib/users
===================================================================
--- contrib/users	(revision 0)
+++ contrib/users	(revision 0)
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+FILE="/var/lib/collectd/users.rrd"
+HOST=""
+
+if [ -n "$SERVER_NAME" ]; then
+    HOST=$SERVER_NAME
+    echo "Content-Type: image/png";
+    echo "";
+else
+    HOST=`hostname -f`
+fi
+
+rrdtool graph - -a PNG -t "$HOST users" -v "Users" \
+    -c "BACK#000000" \
+    -c "SHADEA#000000" \
+    -c "SHADEB#000000" \
+    -c "FONT#dddddd" \
+    -c "CANVAS#202020" \
+    -c "GRID#666666" \
+    -c "MGRID#aaaaaa" \
+    -c "FRAME#202020" \
+    -c "ARROW#ffffff" \
+    "DEF:users_avg=$FILE:users:AVERAGE" \
+    "DEF:users_min=$FILE:users:MIN" \
+    "DEF:users_max=$FILE:users:MAX" \
+    "AREA:users_max#804040" \
+    "AREA:users_min#202020" \
+    "LINE1:users_avg#ff0000:Users" \
+    "GPRINT:users_min:MIN:%4.1lf Min," \
+    "GPRINT:users_avg:AVERAGE:%4.1lf Average," \
+    "GPRINT:users_max:MAX:%4.1lf Max," \
+    "GPRINT:users_avg:LAST:%4.1lf Last\l"
+

Property changes on: contrib/users
___________________________________________________________________
Name: svn:executable
   + *

Index: contrib/museum/collection.cgi
===================================================================
--- contrib/museum/collection.cgi	(revision 258)
+++ contrib/museum/collection.cgi	(working copy)
@@ -521,6 +521,18 @@
                         'GPRINT:cpufreq_max:MAX:%5.1lf%s Max,',
                         'GPRINT:cpufreq_avg:LAST:%5.1lf%s Last\l'
                 ],
+        users => [
+            'DEF:users_avg={file}:users:AVERAGE',
+            'DEF:users_min={file}:users:MIN',
+            'DEF:users_max={file}:users:MAX',
+            "AREA:users_max#$HalfBlue",
+            "AREA:users_min#$Canvas",
+            "LINE1:users_avg#$FullBlue:Users",
+            'GPRINT:users_min:MIN:%4.1lf Min,',
+            'GPRINT:users_avg:AVERAGE:%4.1lf Average,',
+            'GPRINT:users_max:MAX:%4.1lf Max,',
+            'GPRINT:users_avg:LAST:%4.1lf Last\l'
+        ],
 	};
 	$GraphDefs->{'disk'} = $GraphDefs->{'partition'};
 	$GraphDefs->{'meminfo'} = $GraphDefs->{'memory'};
@@ -543,6 +555,7 @@
 	sensors => ['-t', '{host} sensor {inst}', '-v', '°Celsius'],
 	swap => ['-t', '{host} swap usage', '-v', 'Bytes', '-b', '1024', '-l', '0'],
 	traffic => ['-t', '{host} {inst} traffic', '-v', 'Bit/s'],
+    users => ['-t', '{host} users', '-v', 'Users'],
 };
 
 our $GraphMulti =
@@ -556,7 +569,8 @@
 	partition => 1,
 	ping	=> \&output_graph_ping,
 	sensors	=> 1,
-	traffic	=> 1
+	traffic	=> 1,
+    users => 1
 };
 
 our @Info;
Index: debian/control
===================================================================
--- debian/control	(revision 258)
+++ debian/control	(working copy)
@@ -94,3 +94,9 @@
 Depends: collectd (= ${Source-Version})
 Description: collectd module for network traffic.
  collectd module to collect network traffic counters.
+
+Package: collectd-users
+Architecture: any
+Depends: collectd (= ${Source-Version})
+Description: collectd module for user count.
+ collectd module to collect the number of users logged into the system.
Index: debian/collectd-users.files
===================================================================
--- debian/collectd-users.files	(revision 0)
+++ debian/collectd-users.files	(revision 0)
@@ -0,0 +1 @@
+usr/lib/collectd/users.so*
Index: debian/rules
===================================================================
--- debian/rules	(revision 258)
+++ debian/rules	(working copy)
@@ -18,7 +18,7 @@
 
 CFLAGS = -Wall -g
 
-PLUGINS = cpu cpufreq disk hddtemp load memory nfs ping processes sensors serial swap traffic
+PLUGINS = cpu cpufreq disk hddtemp load memory nfs ping processes sensors serial swap traffic users
 
 ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
 	CFLAGS += -O0
Index: debian/collectd.examples
===================================================================
--- debian/collectd.examples	(revision 258)
+++ debian/collectd.examples	(working copy)
@@ -8,4 +8,5 @@
 contrib/swap
 contrib/temperature
 contrib/traffic
+contrib/users
 contrib/museum
Index: AUTHORS
===================================================================
--- AUTHORS	(revision 258)
+++ AUTHORS	(working copy)
@@ -19,6 +19,9 @@
 tape module by:
   Scott Garrett <sgarrett at technomancer.com>
 
+users module by:
+  Sebastian Harl <sh at tokkee.org>
+
 don't-fork-patch by:
   Alvaro Barcellos <alvaro.barcellos at gmail.com>
 
Index: configure.in
===================================================================
--- configure.in	(revision 258)
+++ configure.in	(working copy)
@@ -384,6 +384,22 @@
 fi
 AM_CONDITIONAL(BUILD_MODULE_TRAFFIC, test "x$enable_traffic" = "xyes")
 
+AC_ARG_ENABLE(users, AC_HELP_STRING([--disable-users], [Disable user count statistics]),, [enable_users="yes"])
+if test "x$enable_users" != "xno"
+then
+       if test "x$ac_system" = "xLinux"
+       then
+               enable_users="yes"
+       else
+               enable_users="no"
+       fi
+fi
+if test "x$enable_users" = "xno"
+then
+       AC_DEFINE(COLLECT_USERS, 0, [Wether or not to collect user count statistics])
+fi
+AM_CONDITIONAL(BUILD_MODULE_USERS, test "x$enable_users" = "xyes")
+
 AC_OUTPUT(Makefile src/libping/Makefile src/Makefile)
 
 cat <<EOF;
@@ -410,5 +426,6 @@
     swap  . . . . . . . $enable_swap
     tape  . . . . . . . $enable_tape
     traffic . . . . . . $enable_traffic
+    users . . . . . . . $enable_users
 
 EOF
Index: src/collectd.pod
===================================================================
--- src/collectd.pod	(revision 258)
+++ src/collectd.pod	(working copy)
@@ -66,6 +66,10 @@
 
 Network traffic (I<traffic>)
 
+=item
+
+Number of users logged into the system (I<users>)
+
 =back
 
 =head1 OPTIONS
@@ -82,6 +86,11 @@
 Sets the directory collectd should work in. All F<.rrd>-files are created in
 this directory. Per default this is F</var/lib/collectd/>.
 
+=item B<-P> I<E<lt>directoryE<gt>>
+
+Sets the directory collectd should look for plugins in. Per default this is
+F</usr/lib/collectd>.
+
 =item B<-f>
 
 Don't fork to the background. I<collectd> will also B<not> close standard file
@@ -272,6 +281,10 @@
   DS:incoming:COUNTER:25:0:U
   DS:outgoing:COUNTER:25:0:U
 
+=item Users
+
+  DS:users:GAUGE:25:0:65535
+
 =back
 
 =head1 MODES
Index: src/users.c
===================================================================
--- src/users.c	(revision 0)
+++ src/users.c	(revision 0)
@@ -0,0 +1,105 @@
+/* 
+ * users.c
+ *
+ * users plugin for collectd
+ *
+ * This plugin collects the number of users currently logged into the system.
+ *
+ * Written by Sebastian Harl <sh at tokkee.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "users.h"
+
+#if COLLECT_USERS
+#define MODULE_NAME "users"
+
+#include "plugin.h"
+#include "common.h"
+
+#include <utmp.h>
+
+static char *rrd_file = "users.rrd";
+
+static char *ds_def[] = {
+    "DS:users:GAUGE:25:0:65535",
+    NULL
+};
+static int ds_num = 1;
+
+extern time_t curtime;
+
+void users_init(void)
+{
+    /* we have nothing to do here :-) */
+    return;
+}
+
+void users_read(void)
+{
+    unsigned int users = 0;
+    unsigned int root  = 0;
+    struct utmp *entry = NULL;
+
+    /* according to the *utent(3) man page none of the functions sets errno in
+     * case of an error, so we cannot do any error-checking here */
+    setutent();
+
+    while (NULL != (entry = getutent()))
+        if (USER_PROCESS == entry->ut_type)
+            ++users;
+    endutent();
+
+    users_submit(users);
+    return;
+}
+
+/* I don't like this temporary macro definition - well it's used everywhere
+ * else in the collectd-sources, so I will just stick with it...  */
+#define BUFSIZE 256
+void users_submit(users)
+    unsigned int users;
+{
+    char buf[BUFSIZE] = "";
+
+    if (snprintf(buf, BUFSIZE, "%u:%u", 
+                (unsigned int)curtime, 
+                users) >= BUFSIZE)
+        return;
+
+    plugin_submit(MODULE_NAME, NULL, buf);
+    return;
+}
+#undef BUFSIZE
+
+void users_write(host, inst, val)
+    char *host;
+    char *inst;
+    char *val;
+{
+    rrd_update_file(host, rrd_file, val, ds_def, ds_num);
+    return;
+}
+
+void module_register(void)
+{
+    plugin_register(MODULE_NAME, users_init, users_read, users_write);
+    return;
+}
+
+#undef MODULE_NAME
+#endif /* COLLECT_USERS */
+
Index: src/users.h
===================================================================
--- src/users.h	(revision 0)
+++ src/users.h	(revision 0)
@@ -0,0 +1,42 @@
+/* 
+ * users.h
+ *
+ * users plugin for collectd
+ *
+ * This plugin collects the number of users currently logged into the system.
+ *
+ * Written by Sebastian Harl <sh at tokkee.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef USERS_H
+#define USERS_H 1
+
+#include "config.h"
+
+#ifndef COLLECT_USERS
+#define COLLECT_USERS 1
+#endif /* ! defined(COLLECT_USERS) */
+
+void users_init(void);
+void users_read(void);
+void users_submit(unsigned int);
+void users_write(char *, char *, char *);
+
+void module_register(void);
+
+#endif /* ! defined(USERS_H) */
+
Index: src/config.h.in
===================================================================
--- src/config.h.in	(revision 258)
+++ src/config.h.in	(working copy)
@@ -42,6 +42,9 @@
 /* Wether or not to collect network traffic statistics */
 #undef COLLECT_TRAFFIC
 
+/* Wether or not to collect user count statistics */
+#undef COLLECT_USERS
+
 /* Define to 1 if you have the <arpa/inet.h> header file. */
 #undef HAVE_ARPA_INET_H
 
Index: src/Makefile.am
===================================================================
--- src/Makefile.am	(revision 258)
+++ src/Makefile.am	(working copy)
@@ -103,6 +103,12 @@
 traffic_la_LDFLAGS = -module
 endif
 
+if BUILD_MODULE_USERS
+pkglib_LTLIBRARIES += users.la
+users_la_SOURCES = users.c users.h
+users_la_LDFLAGS = -module
+endif
+
 man_MANS = collectd.1
 #collectd_1_SOURCES = collectd.pod
 
-- 
Sebastian "tokkee" Harl
GnuPG-ID: 0x8501C7FC
http://tokkee.org/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mailman.verplant.org/pipermail/collectd/attachments/20051204/5fd340af/attachment.pgp


More information about the Collectd mailing list