Mq's MRTG Jabberd server statistics scripts script

Download, Add a comment, Back to main page
[2003-11-02 12:57 CST] Jesper Krogh (email) The test for counting the users that have been online the last 2 months don't work here since I have no timestamp inside the rosters.. i exchanged the lines with:

@info = stat("$spooldir/$file");

$age = $now - $info[10];


[2003-11-04 08:25 CST] Mq Be aware the spool file's date may be inaccurate. For example, if an otherwise inactive account receives an offline message, the filedate gets updated as the message gets stored in the spool file.
[2004-01-09 09:53 CST] Mq Counting active accounts works only with jabberd 1.4.x server software.
[2005-01-01 03:50 CST] Jeremy Lunn (email) I'm curious as to why the count for s2s is initialised at -1?

It shouldn't be too hard to count active accounts on a jabberd 2.0 server. Though personally I'd only want to run mrtg for this about once a day. Pity that you can't ahve per section interval settings.

Also there's no point counting SSL connections with j2 because you don't know if a client is using STARTTLS.


[mrtg.cfg]

# MRTG Jabberd configuration file
# MRTG is a tool to generate HTML pages with usage graphs
# for a certain resource.
# http://people.ee.ethz.ch/~oetiker/webtools/mrtg/
# This is a MRTG configuration file for common Jabberd server
# statistics (number of c2s/s2s connections and total/active
# accounts).
# http://scriptrepo.jabberstudio.org/mq_mrtg-stats/
# Run with "mrtg mrtg.cfg".

# HTML pages will be generated here
WorkDir: /var/www/jabber/stat/
# Run as daemon, gathering stats every five minutes
RunAsDaemon: Yes
Interval: 5
# The HTML page's language
# Language: english

# c2s statistics
Target[c2s]: `/usr/bin/perl j-mrtg.pl c2s`
Options[c2s]: growright,nopercent,integer,noinfo,gauge
# Background[c2s]: #FFFFFF
Title[c2s]: Jabber server c2s statistics
PageTop[c2s]: <h1>Jabber server c2s statistics</h1>
 [ <a href="/">Main page</a> | <a href="c2s.html">c2s</a> |
 <a href="s2s.html">s2s</a> | <a href="acc.html">Accounts</a> ]
# Don't show monthly and yearly graphs
Suppress[c2s]: ym
# Sanity check, more than 10000 connections are not possible
MaxBytes[c2s]: 10000
YLegend[c2s]: Connections
LegendI[c2s]: all&nbsp;
LegendO[c2s]: SSL&nbsp;
Legend1[c2s]: Count of connections to Jabber clients
Legend2[c2s]: Count of SSL connections to Jabber clients
ShortLegend[c2s]: conn.&nbsp;&nbsp;

# s2s statistics
Target[s2s]: `/usr/bin/perl j-mrtg.pl s2s`
Options[s2s]: growright,nopercent,integer,noinfo,gauge
# Background[s2s]: #FFFFFF
Title[s2s]: Jabber server s2s statistics
PageTop[s2s]: <h1>Jabber server s2s statistics</h1>
 [ <a href="/">Main page</a> | <a href="c2s.html">c2s</a> |
 <a href="s2s.html">s2s</a> | <a href="acc.html">Accounts</a> ]
# Don't show monthly and yearly graphs
Suppress[s2s]: ym
# Sanity check, more than 10000 connections are not possible
MaxBytes[s2s]: 10000
YLegend[s2s]: Connections
LegendI[s2s]: s2s&nbsp;
LegendO[s2s]:
Legend1[s2s]: Count of connections to other Jabber servers
Legend2[s2s]:
ShortLegend[s2s]: conn.&nbsp;&nbsp;

# Account statistics
Target[acc]: `/usr/bin/perl j-mrtg.pl acc`
Options[acc]: growright,nopercent,integer,noinfo,gauge
# Background[acc]: #FFFFFF
Title[acc]: Jabber server account statistics
PageTop[acc]: <h1>Jabber server account statistics</h1>
 [ <a href="/">Main page</a> | <a href="c2s.html">c2s</a> |
 <a href="s2s.html">s2s</a> | <a href="acc.html">Accounts</a> ]
# Don't show daily and weekly graphs
Suppress[acc]: dw
# Sanity check, more than 100000 accounts are not possible
MaxBytes[acc]: 100000
YLegend[acc]: accounts
LegendI[acc]: all&nbsp;
LegendO[acc]: active&nbsp;
Legend1[acc]: Count of Jabber accounts on this server
Legend2[acc]: Count of Jabber accounts (used in the last two months) on this server
ShortLegend[acc]: accounts&nbsp;&nbsp;



[j-mrtg.pl]
#!/usr/bin/perl

# MRTG Jabberd script
# When called with parameter "c2s", return count of
# standard c2s connections and count of SSL connections.
# When called with parameter "s2s", return count of
# s2s connections.
# When called with parameter "acc", return count of
# total accounts and number of accounts used in the last
# two months.
# "c2s" and "s2s" use netstat which may be inaccurate.
# "acc" is slow so keep an eye on the machine's load.

# spooldir must point to the server's account XDB spool dir
$spooldir = "/var/jabberd/spool/jabber.myserver.net/";

if($ARGV[0] eq "c2s") {
  print `netstat -tn | grep ":522" | wc -l`;
  print `netstat -tn | grep ":5223" | wc -l`;
  print "UPTIME\nMACHINE\n";
}

if($ARGV[0] eq "s2s") {
  # Count s2s connections, ignore same IP
  $conncount = -1;
  $s2sconn = `netstat -tn | grep ":5269"`;
  @s2slines = split /^/m, $s2sconn;
  foreach $line (@s2slines) {
    $line =~ /([\d\.]+\:5269)/;
    $machine = $+;
    if(!$servers{$machine}) {
      $conncount++;
    }
    $servers{$machine} = 1;
  }
  print "$conncount\n0\nUPTIME\nMACHINE\n";
}

if($ARGV[0] eq "acc") {
  my @files = ();
  my $totalacc = 0;
  my $activeacc = 0;
  my $now = time();

  opendir DIR, $spooldir || die;
  @files = grep { /\.xml$/} readdir DIR;
  closedir DIR;

  foreach my $file (@files) {
    if (! -d "$spooldir/$file" ) {
      $list = '';

      sysopen (LIST, "$spooldir/$file", O_RDONLY, 0700);
      while(<LIST>) {
        $list .= $_;
      }
      close LIST;

      $list =~ /jabber:iq:last.*last=\'(\d*)\'/;
      $age = $now - $1;

      # accounts used in the last 61 days are active
      if(($age/60/60/24)<61) {
        $activeacc++;
      }
      $totalacc++;
    }
  }

  print "$totalacc\n$activeacc\nUPTIME\nMACHINE\n";
}