This file is indexed.

/usr/lib/news/bin/cnfsstat is in inn2 2.6.1-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#! /usr/bin/perl -w
use lib '/usr/share/perl5'; use INN::Config;

#  $Id: cnfsstat.in 10076 2016-10-08 20:37:36Z iulius $
#
#  Copyright Andreas Lamrecht 1998
#  <Andreas.Lamprect@siemens.at>
#
#  Modified by Kjetil T. Homme 1998, 2000
#  <kjetilho@ifi.uio.no>
#
#  Modified by Robert R. Collier 1998
#  <rob@lspace.org>
#
#  bigint support added by Duane Currie (sandman@hub.org) 1998

use strict;
use Getopt::Long;
use Math::BigInt;
use Math::BigFloat;
use English;

my $conffile = "$INN::Config::pathetc/cycbuff.conf";
my $storageconf = "$INN::Config::pathetc/storage.conf";
my $lastconftime = 0;

sub usage {
    print <<"_end_";
Summary tool for CNFS

Usage:
	$0 [-ahpPsv] [-c class] [-i seconds] [-l [seconds]] [-m buffer]

	If called without args, does a one-time status of all CNFS buffers
	-a:           print the age of the oldest article in the cycbuff
	-c class:     print out status of CNFS buffers in that class
	-h:           this information
	-i seconds:   initial sleep of that many seconds at startup
	-l [seconds]: loop like vmstat, default seconds = 600
	-m buffer:    print out information suitable for MRTG
	-p:           print out an MRTG config file
	-P:           write PID into $INN::Config::pathrun/cnfsstat.pid
	-s:           log through syslog
	-v:           do consistency checks and print the result
_end_
    exit(1);
}

my (%class, %buff, %stor, @storsort);
my (%prevclass, %prevbuff, %prevstor, @prevstorsort);

my @buffers;

my ($oclass, $obuffer);
my %opt = (c=>\$oclass, m=>\$obuffer);
Getopt::Long::config('no_ignore_case');
GetOptions(\%opt,
           "-a", "-c=s", "-h", "-i=i", "-l:i", "-m=s",
           "-p", "-P", "-s", "-v");

usage() if $opt{'h'};

my $use_syslog = 0;
if ($opt{'s'}) {
    eval { require Sys::Syslog; import Sys::Syslog; $use_syslog = 1 };
    if ($use_syslog) {
        if ($Sys::Syslog::VERSION lt 0.15) {
            eval "sub Sys::Syslog::_PATH_LOG { '/dev/log' }" if $^O eq 'dec_osf';
            Sys::Syslog::setlogsock('unix') if $^O =~ /linux|dec_osf|freebsd|darwin/;
        }
	openlog('cnfsstat', 'pid', $INN::Config::syslog_facility);
    } else {
	print STDERR "Syslog is not available.  -s option is ignored.\n";
    }
}

if ($opt{'P'}) {
    if (open my $FILE, '>', "$INN::Config::pathrun/cnfsstat.pid") {
	print $FILE "$$\n";
	close $FILE;
    };
}

my $sleeptime = (defined($opt{'l'}) && $opt{'l'} > 0) ? $opt{'l'} : 600;

unless (read_cycbuffconf()) {
    print STDERR "Invalid $conffile file.\n";
    exit (1);
}

unless (read_storageconf()) {
    print STDERR "Invalid $storageconf file.\n";
    exit (1);
}


mrtg($obuffer) if $obuffer;
mrtg_config() if $opt{'p'};

# Initial sleep, before starting the work.
if(defined($opt{'i'}) && $opt{'i'} > 0) {
    sleep($opt{'i'});
    if (!$use_syslog) {
        print STDOUT "$opt{'i'} seconds later:\n";
    }
}


START:

# Check whether the configuration files need reloading.
my $cycbufftime = (stat($conffile))[9] if (-r $conffile);
my $storagetime = (stat($storageconf))[9] if (-r $storageconf);
my $maxtime = ($cycbufftime < $storagetime) ? $storagetime : $cycbufftime;

# Set $lastconftime for the first run of the comparison.
$lastconftime = $maxtime if not $lastconftime;

if ($lastconftime < $maxtime) {
    my $reloadok = 1;

    $lastconftime = $maxtime;

    # Save the previous configuration, in case reloading it fails.
    # Direct copies of the arrays and hashes works fine here.
    %prevclass = %class; undef %class;
    %prevbuff = %buff; undef %buff;
    %prevstor = %stor; undef %stor;
    @prevstorsort = @storsort; undef @storsort;

    unless (read_cycbuffconf()) {
        print STDERR "Invalid $conffile file.\n";
        $reloadok = 0;
    }

    unless (read_storageconf()) {
        print STDERR "Invalid $storageconf file.\n";
        $reloadok = 0;
    }

    # In case reloading configuration files fails, restore the
    # previous known configuration for this run of cnfsstat.
    if (!$reloadok) {
        %class = %prevclass;
        %buff = %prevbuff;
        %stor = %prevstor;
        @storsort = @prevstorsort;
    }
}

my $logline;
my $header_printed = 0;
my ($gr, $cl, $min, $max);
if ($oclass) {
    if ($class{$oclass}) {
	if (!$header_printed) {
            if ($stor{$oclass}) {
                ($gr, $cl, $min, $max) = split(/:/, $stor{$oclass});
            } else {
                ($gr, $cl, $min, $max) = ('', $oclass, 0, 0);
            }
            # Remove leading and trailing double quotes, if present.
            $gr =~ s/"?([^"]*)"?/$1/g;
	    if ($use_syslog) {
		if ($min || $max) {
		    $logline = sprintf("Class %s for groups matching \"%s\" article size min/max: %d/%d", $oclass, $gr, $min, $max);
		} else {
		    $logline = sprintf("Class %s for groups matching \"%s\"", $oclass, $gr);
		}
	    } else {
		print STDOUT "Class $oclass";
		print STDOUT " for groups matching \"$gr\"";
		if ($min || $max) {
		    print STDOUT ", article size min/max: $min/$max";
		}
		print STDOUT "\n";
	    }
	    $header_printed = 1;
	}
	
	@buffers = split(/,/, $class{$oclass});
	if (! @buffers) {
	    print STDERR "No buffers in Class $oclass ...\n";
	    next;
	}
	
	foreach my $b (@buffers) {
	    if (! $buff{$b} ) {
		print STDERR "No buffer definition for buffer $b ...\n";
		next;
	    }
	    print_cycbuff_head($buff{$b});
	}
    } else {
	print STDERR "Class $oclass not found ...\n";
    }
} else { # Print all Classes

    foreach my $c (@storsort) {
	($gr, $cl, $min, $max) = split(/:/, $stor{$c});
        # Remove leading and trailing double quotes, if present.
        $gr =~ s/"?([^"]*)"?/$1/g;
	if ($use_syslog) {
	    if ($min || $max) {
		$logline = sprintf("Class %s for groups matching \"%s\" article size min/max: %d/%d", $c, $gr, $min, $max);
	    } else {
		$logline = sprintf("Class %s for groups matching \"%s\"", $c, $gr);
	    }
	} else {
	    print STDOUT "Class $c";
	    print STDOUT " for groups matching \"$gr\"";
	    if($min || $max) {
		print STDOUT ", article size min/max: $min/$max";
	    }
	    print STDOUT "\n";
	}
	@buffers = split(/,/, $class{$c});
	if(! @buffers) {
	    print STDERR "No buffers in Class $c ...\n";
	    next;
	}
	
	foreach my $b (@buffers) {
	    if(! $buff{$b} ) {
		print STDERR "No buffer definition for buffer $b ...\n";
		next;
	    }
	    print_cycbuff_head($buff{$b});
	}
	if (!$use_syslog) {
	    print STDOUT "\n";
	}
    }
}

if(defined($opt{'l'})) {
    sleep($sleeptime);
    if (!$use_syslog) {
	print STDOUT "$sleeptime seconds later:\n";
    }
    goto START;
}

sub read_cycbuffconf {
    my @line;
    return 0 unless open my $CONFFILE, '<', $conffile;

    while(<$CONFFILE>) {
	$_ =~ s/^\s*(.*?)\s*$/$1/;
	# Here we handle continuation lines
	while (m/\\$/) {
	    my $contline = <$CONFFILE>;
	    $contline =~ s/^\s*(.*?)\s*$/$1/;
	    chop;
	    $_ .= $contline;
	}
	# \x23 below is #.  Emacs perl-mode gets confused by the "comment"
	next if ($_ =~ /^\s*$/ || $_ =~ /^\x23/);
	next if ($_ =~ /^cycbuffupdate:/ || $_ =~ /^refreshinterval:/);
	
	if($_ =~ /^metacycbuff:/) {
	    @line = split(/:/, $_);
	    if ($class{$line[1]}) {
		print STDERR "Class $line[1] more than one time in CycBuff Conffile $conffile ...\n";
		return 0;
	    }

	    $class{$line[1]} = $line[2];
	    next;
	}

	if ($_ =~ /^cycbuff/) {
	    @line = split(/:/, $_);
	    if ($buff{$line[1]}) {
		print STDERR "Buff $line[1] more than one time in CycBuff Conffile $conffile ...\n";
		return 0;
	    }
	    $buff{$line[1]} = $line[2];
	    next;
	}

	print STDERR "Unknown config line \"$_\" in CycBuff Conffile $conffile ...\n";
        return 0;
    }
    close $CONFFILE;
    return 1;
}

sub read_storageconf {
    my $line = 0;
    return 0 unless open my $STOR, '<', $storageconf;

    while (<$STOR>) {
	++$line;
	next if /^\s*#/;

	# defaults
	my %key = ("NEWSGROUPS" => "*",
		    "SIZE" => "0,0");
		
	if (/method\s+cnfs\s+\{/) {
	    while (<$STOR>) {
		++$line;
		next if /^\s*#/;
		last if /\}/;
		if (/(\w+):\s+(\S+)/i) {
		    $key{uc($1)} = $2;
		}
	    }
	    unless (defined $key{'CLASS'} && defined $key{'OPTIONS'}) {
		print STDERR "storage.conf:$line: ".
			"Missing 'class' or 'options'\n";
		return 0;
	    }

	    $key{'SIZE'} .= ",0" unless $key{'SIZE'} =~ /,/;
	    $key{'SIZE'} =~ s/,/:/;

	    if (!defined $stor{$key{'OPTIONS'}}) {
		$stor{$key{'OPTIONS'}} = "$key{'NEWSGROUPS'}:$key{'CLASS'}:" .
			"$key{'SIZE'}:$key{'OPTIONS'}";
		push(@storsort, $key{'OPTIONS'});
	    }
	}
    }
    return 1;
}

sub print_cycbuff_head {
    my ($buffpath) = @_;
    my ($name, $len, $free, $update, $cyclenum, $oldart) =
	    get_cycbuff_info($buffpath);

    if ($use_syslog) {
	($name) = split(/\s/, $name);
	$name =~ s/\0//g;
	# Log only if the buffer is initialized (cyclenum is not -1).
	syslog ('notice', '%s Buffer %s, len: %.2f  Mbytes, used: %.2f Mbytes (%4.1f%%) %3d cycles',
		$logline, $name, Math::BigFloat->new($len) / (1024 * 1024),
		Math::BigFloat->new($free) / (1024 * 1024),
		100 * Math::BigFloat->new($free) / Math::BigFloat->new($len),
		$cyclenum) if $cyclenum >= 0;
	return 0;
    }

    $name =~ s/\0//g;
    print " Buffer $name, size: ", human_readable($len, 4);
    print ", position: ", human_readable($free, 4);
    printf "  %.2f cycles\n", $cyclenum + Math::BigFloat->new($free) / Math::BigFloat->new($len);

    # The CNFS buffer may not have been initialized yet or received an article.
    # Take it into account because $oldart may be undefined.
    my ($when, $ago) = make_time($update);
    if (defined $oldart or not $opt{'a'}) {
        print "  Newest: $when, $ago ago\n";
    } else {
        print "  Created: $when, $ago ago\n";
    }

    if ($opt{'a'}) {
        if (defined $oldart) {
            my ($when, $ago) = make_time($oldart);
            print "  Oldest: $when, $ago ago\n";
        } else {
            print "  No oldest article\n";
        }
    }
    return;
}

sub make_time {
    my ($t) = @_;
    my (@ret);

    my ($sec,$min,$hour,$mday,$mon,$year) =
	    (localtime($t))[0..5];
    push (@ret, sprintf("%04d-%02d-%02d %2d:%02d:%02d",
			$year + 1900, $mon + 1, $mday, $hour, $min, $sec));
    $t = time - $t;

    $mday = int($t/86400); $t = $t % 86400;
    $hour = int($t/3600);  $t = $t % 3600;
    $min  = int($t/60);    $t = $t % 60;

    push (@ret, sprintf("%4d days, %2d:%02d:%02d",
			$mday, $hour, $min, $t));
    return @ret;
}

sub human_readable {
    my ($val, $digits) = @_;
    $val =~ s/\+//;

    my @name = ("kBytes", "MBytes", "GBytes", "TBytes");
    my $base = 1024;
    my $factor = 1024;

    my $unit = -1;
    my $oldscaled = Math::BigFloat->new($val) / $base;
    my $scaled = $oldscaled;
    while ( ( int($scaled) > 0 ) && ( $unit < $#name ) ) {
	$oldscaled = $scaled;
	$scaled /= $factor;
	$unit++;
    }
    $scaled = $oldscaled;
    my $predigits = length (int($scaled));
    my $postdigits = $digits - $predigits - 1;
    $postdigits = 0 if $postdigits < 0;
    ++$digits;

    return sprintf ("%${digits}.${postdigits}f %s", $scaled, $name[$unit]);
}

sub mrtg {
	my $buffer = shift;
	# print "Buffer = $buff{$buffer}\n";
	my @info = get_cycbuff_info($buff{$buffer});
	print "$info[1]\n";
	print "$info[2]\n";
	print "$info[4]\n";
	print "$info[0]\n";
	exit(0);
}

sub mrtg_config {
	print "Sub MRTG-CONFIG\n";
	foreach my $class (sort(keys(%class))) {
		print "##\n## Class  : $class\n## Wildmat: $stor{$class}\n##\n\n";
		foreach my $buffer (split /\,/, $class{$class}) {
			mrtg_buffer($class, $buffer);
		}
	}
	exit(0);
}

sub mrtg_buffer {
	my ($class,$buffer) = @_;
	#my ($name, $num, $buff, $size) = @_;
        my $tag = 'cnfs-' . $buffer;

        print 'Target[', $tag, ']: `', "$INN::Config::pathbin/cnfsstat -m ", $buffer, '`', "\n";
        print 'MaxBytes[', $tag, ']: ', (get_cycbuff_info($buff{$buffer}))[1], "\n";
        print 'Title[', $tag, ']: ', "${buffer} Usage\n";
        print 'Options[', $tag, ']: growright gauge', "\n";
        print 'YLegend[', $tag, ']: ', "${buffer}\n";
        print 'ShortLegend[', $tag, ']: MB', "\n";
        print 'PageTop[', $tag, ']: ', "<H1>Usage of ${buffer}</H1>\n";
	print "<BR><TT>$stor{$class}</TT>\n";
        print "\n";
        return 1;
}

sub bigsysseek {
    my ($handle, $offset) = @_;

    # $offset may be a bigint; and have a value that doesn't fit in a signed long.
    # Even with largefiles enabled, perl will still truncate the argument to lseek64
    # to 32 bits.  So we seek multiple times, <2G at a time.

    if ($offset > 2147483647) {
	# Since perl truncates the return value of lseek64 to 32 bits, it might
	# see a successful return value as negative, and return FALSE (undef).
	# So we must ignore the return value of sysseek and assume that it worked.

	seek($handle, 0, 0);
	while ($offset > 2000000000) {
	    sysseek($handle, 2000000000, 1) || return 0;
	    $offset -= 2000000000;
	}
	sysseek($handle, $offset, 1) || return 0;
	return 1;
    } else {
	return sysseek($handle, $offset, 0);
    }
}

sub check_read_return {
  my $result = shift;
  die "read: $!\n" unless defined($result);
  die "read reached eof\n" unless $result;
  return $result;
}

sub get_cycbuff_info {
    my ($buffpath) = @_;
    my $oldart;

    my $CNFSMASIZ = 8;
    my $CNFSNASIZ = 16;
    my $CNFSPASIZ = 64;
    my $CNFSLASIZ = 16;
    my $headerlength = 2 * $CNFSMASIZ + 2 * $CNFSNASIZ + $CNFSPASIZ + (6 * $CNFSLASIZ);

    my ($BUFF, $buff);

    if ( !open $BUFF, '<', $buffpath ) {
	print STDERR "Cannot open Cycbuff $buffpath ...\n";
	exit(1);
    }

    $buff = "";
    if ( !read $BUFF, $buff, $headerlength ) {
	print STDERR "Cannot read $headerlength bytes from file $buffpath...\n";
	exit(1);
    }

    my ($magic, $name, $path, $lena, $freea, $updatea, $cyclenuma, $metaname,
     $orderinmeta, $currentbuff, $blksza) =
           unpack("a8 a16 a64 a16 a16 a16 a16 a16 a16 a8 a16", $buff);

    if (!$magic) {
	print STDERR "Error while unpacking header ...\n";
	exit(1);
    }

    my $len = bhex($lena);
    my $free = bhex($freea);
    my $update = hex($updatea);
    my $cyclenum = hex($cyclenuma) - 1;
    my $blksz = ($magic =~ m/^CBuf4/) ? hex($blksza) : 512;

    if ($opt{'a'}) {

	my $pagesize = 16384;
	my $minartoffset = int($len / ($blksz * 8)) + 512;
	# Align upwards:
	$minartoffset = ($minartoffset + $pagesize) & ~($pagesize - 1);
	
	if ($cyclenum == 0 && $free == $minartoffset) {
	    # The cycbuff has no articles yet.
	    goto done;
	}

	# Don't loop endlessly, set rough upper bound
	my $sentinel = $cyclenum == 0 ? $free : $len;
	my $offset = $cyclenum == 0 ? $minartoffset : $free + $pagesize;

	bigsysseek($BUFF, $offset) || die "sysseek: $!\n";
	check_read_return (sysread ($BUFF, $buff, $pagesize));
	do {
            my $chunk;
	    check_read_return (sysread ($BUFF, $chunk, $pagesize));

	    $buff .= $chunk;
	    while ($buff =~ /^message-id:\s+(<.*?>)/mi) {
		$buff = $POSTMATCH;
		$oldart = lookup_age($1);
		next unless $oldart;
		
		# Is the article newer than the last update of the cycbuff?
		if ($oldart >= $update) {
		    $update = $oldart;
		} elsif ($oldart < $update - 60) {
		    goto done;
		}
	    }
	    # Just in case we chopped Message-ID in two, use the end
	    # at the front in next iteration.
	    $buff = substr ($buff, -$blksz);

	} while ($sentinel -= $pagesize > 0);
    }

done:
    close $BUFF;
    return($name,$len,$free,$update,$cyclenum,$oldart);
}

sub lookup_age {
    my ($msgid) = @_;

    my $history = safe_run("$INN::Config::newsbin/grephistory", "-l", $msgid);
    if ($history =~ /\t(\d+)~/) {
	return $1;
    }

    if ($opt{'v'}) {
        print "   (Missing $msgid)\n";
    }

    return 0;
}

sub safe_run {
    my $output = "";

    my $pid = open my $KID_TO_READ, "-|";
    die "fork: $!\n" unless defined $pid;
    if ($pid) {
	while (<$KID_TO_READ>) {
	    $output .= $_;
	}
	close $KID_TO_READ;
    } else {
	exec(@_) || die "can't exec $_[0]: $!";
	# NOTREACHED
    }
    return $output;
}

# Hex to bigint conversion routine.
# bhex(HEXSTRING) returns BIGINT (with leading + chopped off).
#
# In most languages, unlimited size integers are done using string math
# libraries usually called bigint.  (Java, Perl, etc.)
#
# Bigint's are really just strings.

sub bhex {
    my $hexValue = shift;
    $hexValue =~ s/^0x//;

    my $integerValue = Math::BigInt->new('0');
    for (my $i = 0; $i < length($hexValue); $i += 2) {
        # Could be more efficient going at larger increments, but byte
        # by byte is safer for the case of 9 byte values, 11 bytes, etc.

        my $byte = substr($hexValue, $i, 2);
        my $byteIntValue = hex($byte);

        # bmuladd() is only in Perl >= 5.10.0.
        $integerValue->bmul('256');
        $integerValue->badd("$byteIntValue");
    }

    my $result = $integerValue->bstr();
    $result =~ s/^\+//;
    return $result;
}