chiark / gitweb /
WIP before any changes resulting from reading SM API stuff
[inn-innduct.git] / contrib / backlogstat.in
1 #!/usr/bin/perl
2 # fixscript will replace this line with require innshellvars.pl
3
4 # backlogstat - display backlog to sites
5 # based on bklog by bill davidsen <davidsen@tmr.com>
6
7 # breaks if backlog-directory in innfeed.conf is not "innfeed"
8 my $dir = "$inn::pathspool/innfeed";
9 my $Revision = '1.8';
10
11 use strict;
12 use warnings;
13
14 use Getopt::Std;
15 use vars qw($opt_H $opt_h $opt_n $opt_t $opt_k $opt_S $opt_d);
16 $| = 1;
17
18 # option processing
19 &getopts('HhntkS:d:') || &Usage;
20 &Usage if $opt_h;
21
22 # open the directory;
23 $dir = $opt_d if $opt_d;
24 print "$opt_d\n";
25 chdir($dir) or die "Can't cd to $dir";
26 opendir(DIR, ".") or die "Can't open dir";
27
28 my %nodes;
29 while (my $name = readdir(DIR)) {
30         # must be a file, correct name, non-zero size
31         my $size;
32         next unless -f $name;
33         next unless ($size = -s $name);
34         next unless $name =~ m/.*\.(in|out)put/;
35         my $io = $1;
36         (my $nodename = $name) =~ s/\..*//;
37
38         # check for only some sites wanted
39         next if ($opt_S && $nodename !~ /^${opt_S}.*/);
40         # here we do the counts if asked
41         if ($opt_n) {
42                 # open the file and count lines
43                 if (open(IN, "<$name")) {
44                         if ($name =~ m/.*\.input/) {
45                                 my $offset = <IN> + 0;
46                                 seek(IN, $offset, 0);
47                         }
48                         $size = 0;
49                         for ($size = 0; <IN> ; ++$size) {};
50                         close IN;
51                 }
52         } else {
53                 # get the offset on .input files
54                 if ($name =~ m/.*\.input/ && open(IN, "<$name")) {
55                         my $offset = <IN> + 0;
56                         $size -= $offset;
57                         close IN;
58                 }
59         }                       
60         $nodes{$nodename} = () unless defined $nodes{$nodename};
61         $nodes{$nodename}->{$io} = ( $opt_k ? $size / 1024 : $size );
62 }
63 closedir DIR;
64
65 # output the data for each node
66 if (my $numnodes = keys %nodes) {
67         if ($opt_H) {
68                 if ($opt_n) {
69                         print "  <---------- posts ----------->\n";
70                 } else {
71                         print "  <---------- bytes ----------->\n";
72                 }
73         }
74         my $ofmt;
75         if ($opt_k) {
76                 print "  input(k)  output(k)   total(k) Feed Name\n" if $opt_H;
77                 $ofmt = ( $opt_n ? "%10.2f" : "%10.1f" );
78         } else {
79                 print "     input     output      total Feed Name\n" if $opt_H;
80                 $ofmt = "%10d";
81         }
82         for my $node (sort keys %nodes) {
83                 my $hash = $nodes{$node};
84                 my $size_in = $hash->{in} || 0;
85                 my $size_out = $hash->{out} || 0;
86                 my $size_tot = $size_in + $size_out;
87                 printf "${ofmt} ${ofmt} ${ofmt} %s\n",
88                         $size_in, $size_out, $size_tot, $node;
89         }
90 } else {
91         print "NO backlog!\n";
92 }
93
94 exit 0;
95
96 sub Usage
97 {
98         print "\n"
99         . "bklog - print innfeed backlog info - v$Revision\n"
100         . "\n"
101         . "Format:\n"
102         . "  bklog [ options ]\n"
103         . "\n"
104         . "Options:\n"
105         . "  -H     output a header at the top of the output\n"
106         . "  -k     scale all numbers in k (1024) units\n"
107         . "  -n     count number of arts, not bytes of backlog filesize\n"
108         . "         Note: this may be SLOW for large files!\n"
109         . "  -Sxx   Display only site names starting with xx\n"
110         . "  -d dir Use \"dir\" instead of \$pathspool/innfeed\n"
111         . "\n"
112         . "  -h     HELP - this is all, you got it!\n"
113         . "\n";
114
115         exit 1;
116 }
117
118