Project

General

Profile

Actions

UserOnline

Module mod_useronline

Description

Module that tracks users online/active by unique IPs (no cookies), includes counting hits, and linking into (mod_status Server Statistics).

Background

Previously, I made a module in PHP that performed a similar function of tracking online users.
At a certain point I realized it to be a bit wasteful with resources, and decided to make the module into Lighttpd.

I started basing the code off mod_rewrite, using PCRE to match patterns to decide whether to track a given page or not.
I emailed my code to Jan for review and in hopes of getting it included into one of the branches so everyone can make use of it.
Jan replied shortly after with a few suggestions/issues that improved the module considerably (as I am sort of new to Lighttpd internal workings).

My name is Wojciech (nick: Wojtek) and I go by the alias Wojjie online. I run/own http://www.game-monitor.com, which is the site that is currently running this module and the site I made reference to above.

If you wish to contact me directly via email, I can be reached at: wojtekATwojjie.com (please note: I do get a lot of spam, so put [mod_useronline] or [lighttpd] in the subject so your email stands out, and just hope my spam filter does not pick it up). Another method to contact me is to get on IRC, and idle in Lighttpd's IRC channel (irc://irc.freenode.net/lighttpd) where I will most likely be idling.

Limitations

(code review by @gstrauss)

  • As written, code operates only on IPv4
  • Bug: out-of-bounds read occurs after useronline.maxips is reached
  • Pruning does not occur when not enabled path is not hit (so mod_status Server Statistics, if used, would not be accurate)
  • RFE: module could be written in Lua and run by mod_magnet
  • RFE: module could store data in more efficiently searchable table/tree (like internal array *) and updated in lighttpd handle_request_done hook, though should then also keep refcount of in-flight active requests, and increment/decrement active count when refcount changes from/to 0.

Installation

  1. Download mod_useronline.c (http://redmine.lighttpd.net/attachments/download/197/mod_useronline.c)
  2. Copy mod_useronline.c into lighttpd src directory.
  3. Edit your src/Makefile.am and add this after the last module:

    lib_LTLIBRARIES += mod_useronline.la
    mod_useronline_la_SOURCES = mod_useronline.c
    mod_useronline_la_LDFLAGS = -module -export-dynamic -avoid-version -no-undefined
    mod_useronline_la_LIBADD = $(common_libadd)

  1. Go back to lighttpd root directory and first do: aclocal && automake && autoconf, after that do: make clean; ./configure --enable-maintainer-mode; make; make install
  1. Make sure /usr/local/lib is in your ld.so.conf file and rebuld the ld database (using: ldconfig).

Options

  • useronline.enable

enable or disable tracking for given area

Default: 0 [disabled]

Example: useronline.enable = 1

  • useronline.online-age

number of seconds until user/IP is no longer considered online

Default: 300

Example: useronline.online-age = 600

  • useronline.active-age

number of seconds till user/ip is no longer considered active (note: setting this higher than online_age will give you the same active count as online)

Default: useronline.online-age / 3 = 100

Example: useronline.active-age = 60

  • useronline.max-ips

max number of ips to track (note: if this limit is hit, the server will not track new users/ips)

Memory Usage: 16 * max-ips (on a 64bit system)

Default: 1024

Example: useronline.max-ips = 4096

  • useronline.status-name

name to associate to the variables shown in server-statistics (if not set, it will not report)
!! Note: linked code below does not do this; maybe an earlier version did !!

Default: unset

Example: useronline.status-name = "myWonderfulSite"

Output/Result

When configured properly, you will notice a few new environment variables accessible to you. In PHP you can output the number of online users like so:

<?php echo $_ENV['USERS_ONLINE'] ?>

Here is a current list of environment variables generated by this module:

  • users_online

number of online users (IPs that last hit >= online_age seconds ago)

  • users_active

number of active users (IPs that last hit >= min(online_age, active_age) seconds ago)

  • user_hits

number of hits seen by the current ip while being online/active

  • users_online_age

number of seconds that a user is still considered online (just reports the current setting)

  • users_active_age

number of seconds that a user is still considered active (just reports the current setting)

  • users_online_max_ips

max number of ips tracked (just reports the current setting)

  • users_online_id

site/conditional id, where the tracking data is located (may be used in the future to link)

Server Statistics

If useronline.status_name is set, the module will report to status counters that will be displayed in mod_status Server Statistics
!! Note: linked code below does not do this; maybe an earlier version did !!

Example: (using: useronline.status_name = "myWonderfulSite")

useronline.myWonderfulSite.active-age: 100
useronline.myWonderfulSite.id: 9
useronline.myWonderfulSite.max-ips: 4096
useronline.myWonderfulSite.online-age: 300
useronline.myWonderfulSite.users-active: 110
useronline.myWonderfulSite.users-online: 296

The top 4 lines report settings, while the last 2 report the current counters.

You will notice an id number listed, this is the id where the data for that site/conditional is stored. It may be used in the future to link multiple sites/conditionals together to use the same counters, to make it easier to count online users across multiple domains.

Download

For 1.4.x: 09/08/2006 (MM/DD/YYYY)

View: https://redmine.lighttpd.net/attachments/197/mod_useronline.c

Download: http://redmine.lighttpd.net/attachments/download/197/mod_useronline.c

Download: (updated link) see attachment to #840

Updated by gstrauss over 1 year ago · 35 revisions