--- /dev/null
+#!/usr/bin/perl
+use strict; use warnings;
+use Getopt::Long;
+use XML::LibXML;
+use Unix::Syslog qw(:macros :subs);
+
+my ($config_file, $timeout, $run_once, $help, $services) =
+ ('/openils/conf/opensrf.xml', 15, 0, 0, '');
+
+my $FACILITY = LOG_LOCAL5;
+my $LEVEL = LOG_INFO;
+my $LOG_TAG = 'eg-stats';
+
+GetOptions(
+ 'config=s' => \$config_file,
+ '1' => \$run_once,
+ 'service=s' => \$services,
+ 'delay=i' => \$timeout,
+ 'help' => \$help
+);
+
+help() && exit if ($help);
+
+openlog($LOG_TAG, 0, $FACILITY);
+
+$services = $services ? [split(',', $services)] : [];
+
+my @apps;
+my $parser = XML::LibXML->new();
+
+# Return an XML::LibXML::Document object
+my $config = $parser->parse_file($config_file);
+
+@apps = $config->findnodes('/opensrf/default/apps/*');
+
+if (@$services) {
+ my @tmp_apps;
+ for my $s (@$services) {
+ for my $a (@apps) {
+ if ($a->nodeName eq $s) {
+ push @tmp_apps, $a;
+ last;
+ }
+ }
+ }
+ @apps = @tmp_apps;
+
+} else {
+ $services = [ map { $_->nodeName } @apps ];
+}
+
+do {
+
+ my @data = split /\n/s, `ps ax|grep OpenSRF`;
+ my %service_data;
+ for (@data) {
+ if (/OpenSRF (\w+) \[([^\]]+)\]/) {
+ my ($s,$t) = ($2,lc($1));
+ next unless (grep { $s eq $_ } @$services);
+ if (!exists($service_data{$s}{$t})) {
+ $service_data{$s}{$t} = 1;
+ } else {
+ $service_data{$s}{$t}++
+ }
+ }
+ }
+
+ for my $s ( @$services ) {
+ my ($node) = grep { $_->nodeName eq $s } @apps;
+ next unless ($node);
+
+ my $max_kids = $node->findvalue('unix_config/max_children');
+ my $imp_lang = $node->findvalue('language');
+
+ my $lcount = $service_data{$s}{listener} || 0;
+ my $dcount = $service_data{$s}{drone} || 0;
+
+ my $res = sprintf(
+ "[eg-stats] SERVICE ($s) : ".
+ "listener count: $lcount, drone count: $dcount/$max_kids");
+
+ syslog($FACILITY | $LEVEL, $res);
+ }
+
+ $run_once--;
+} while ($run_once != 0 && sleep($timeout));
+
+sub help {
+ print <<HELP;
+
+Evergreen Server Health Monitor
+
+ --config=<config_file>
+ OpenSRF configuration file for Evergreen.
+ Default: /openils/conf/opensrf.xml
+
+ --1
+ Run once and stop
+
+ --service=<service name>
+ Comma separated list of services to report on. If not supplied, all
+ services are reported.
+
+ --delay=<seconds>
+ Delay time for collecting CPU stats.
+ Default: 5
+
+ --help
+ Print this help message
+
+HELP
+}
+