#!/usr/bin/perl

###################################################################
#
# Copyright 1997 Spinne, Inc.
#
# Primary author: Jeff Garzik <jgarzik@spinne.com>
#
#



use strict;



my $StatsUpdatePeriod = 300;
my $CacheStatusFile = "/var/tmp/inn-status-cache";

my (@html, @statbuf);

@statbuf = stat($CacheStatusFile);
if ((@statbuf) && ($statbuf[9] > (time - $StatsUpdatePeriod))) {
  print &read_text_file($CacheStatusFile);
  exit(0);
}

push (@html, <<"EOM");
Content-type: text/html
Pragma: no-cache

<html>
<head><title>INN server info</title></head>
<body>

<h1>INN server info</h1>
EOM

my @uptime = `uptime`;
chomp @uptime;

push (@html, "Status updated on ", `date`, "<br><strong>", @uptime, "</strong>");

push @html, "<h2>INN mode</h2><pre>";

push @html, `/usr/news/bin/ctlinnd -t 120 mode`;

my @infeed = sort grep(!/:proc:/,
	       grep (!/:file/,
	         grep (!/conn:/,
		   grep (!/control:/,
		     `/usr/news/bin/ctlinnd -t 120 name ''`))));
my $incnt = $#infeed + 1;

push @html, "</pre><h2>Incoming feed connections ($incnt)</h2><p><table border=5>",
      "<tr><th>Host</th><th>FD</th><th>Seconds</th><th>Accepted</th><th>Refused</th><th>Rejected<br></th></tr>\n";

my (@feedstats, $feedn1, $align);

foreach (@infeed) {
  @feedstats = split(/:/);
  next unless ($#feedstats > 1);

  push @html, "<tr>";

  foreach $feedn1 (@feedstats) {
    $feedn1 =~ s/seconds//;
    $feedn1 =~ s/accepted//;
    $feedn1 =~ s/rejected//;
    $feedn1 =~ s/refused//;
    if ($feedn1 ne $feedstats[0]) {
      $align = "align=right";
    } else {
      $align = "";
    }
    push @html, "<td $align>$feedn1";
    if ($feedn1 eq $feedstats[$#feedstats]) {
      push @html, "<br>";
    }
    push @html, "</td>";
  }

  push @html, "</tr>\n";
}

my @outfeed = `/usr/news/bin/ctlinnd -t 120 feedinfo ''`;
my $outcnt = $#outfeed + 1;
my ($left, $right, $ftype, $rest);

push @html, "</table><h2>Outgoing feeds ($outcnt)</h2><p><table border=5>",
	    "<tr><th>Name</th><th>Type</th><th>Attributes</th></tr>\n";

foreach (sort @outfeed) {
  chomp;
  if (($left, $right) = /^([^:]+):\s+(.*)$/) {
    if ($right =~ /funnel/) {
      $right =~ s/: .*$//;
    }

    if (($ftype, $rest) = ($right =~ /^\s*(\S+)\s*(.*)$/)) { 
      push @html, "<tr><td>$left</td><td>$ftype</td><td>$rest</td></tr>\n";
    } else {
      push @html, "<tr><td>$left</td><td>?</td><td>$right</td></tr>\n";
    }
  } else {
    push @html, "<tr><td></td><td>$_</td></tr>\n";
  }
}

my @readers = `ps -wax`;
@readers = sort grep (/-\d+\.\d+\.\d+\.\d+ /, @readers);
my $readercnt = $#readers + 1;

push @html, "</table><h2>Connected readers ($readercnt)</h2><p><pre>";

if ($readercnt > 0) {
  my ($rest, @processed);
  foreach (@readers) {
    if (($rest) = (/(\d+\.\d+\.\d+\.\d+ .*)$/)) {
      chomp $rest;
      push(@processed, $rest);
    }
  }

  foreach (sort @processed) {
    push @html, "$_\n";
  }
  
}

push @html, "</pre></body></html>\n";

print @html;

if (open(F, ">$CacheStatusFile")) {
  print F @html;
  close(F);
}

exit(0);



sub read_text_file {
  my ($filename) = @_;
  my (@tmp);

  open(TEXT, $filename) || return ();
  @tmp = <TEXT>;
  close(TEXT);

  return @tmp;
}



