#!/usr/bin/perl

# Gregory D. Troxel <gtroxel@bbn.com>

do "sub-rsadj.pl";

select STDERR; $| = 1;
select STDOUT; $| = 1;

foreach $host (@HOSTS) {
    select STDOUT;
    print "PREP $host:\n";
    &cdhost($host);

    system "rm -f xplot.* LOG PARAM* detailed.$host stray.$host hourly.$host";

    open(DETAILED, ">detailed.$host") || die "Can't open detailed.$host" ;
    open(HOURLY, ">hourly.$host") || die "Can't open hourly.$host" ;
    open(STRAY, ">stray.$host") || die "Can't open stray.$host" ;

#   select DETAILED; $| = 1;

    select STDOUT;

    @files = split(' ', `ls D.??????.*z 2>/dev/null`);
    foreach $file (@files)
    {
	select STDOUT;
	print "READING $host $file\n";
	if ( $file =~ /.gz$/ ) {
	    open(INF, "gzip -d < $file |") || die "Can't unzip $file" ;
	} else {
	    open(INF, $file) || die "Can't open $file" ;
	}
	while ( <INF> ) {
	    if ( ! /ntpd/ ) { next; }

	    if ( /unix:.*out of disk/ ) { next; }

	    select STDOUT;

	    # ultrix format
	    if ( /.*: ([0-9]*) x*ntpd[-\.0-9a-z]*: / ) {
	        # remove header, ntpd[10], ntpd.new, etc.:
		s/.*: ([0-9]*) x*ntpd[-\.0-9a-z]*: /\1 /; # 
		($pid) = split(' '); # extract pid
	        s/[0-9]* //;	# remove pid
            } elsif ( /.*ntpd[-\.0-9a-z]*\[([0-9]*)\]: / ) {
	 	s/.*ntpd[-\.0-9a-z]*\[([0-9]*)\]: /\1 /; # remove date
		($pid) = split(' '); # extract pid
		s/[0-9]* //;	# remove up to time field
            } else {
		warn "Invalid syslog format (not 4.3 or ultrix): $_";
            }

	    # check for reset, hourly, or detailed
	    if ( /xntpd version/ )
	    { select DETAILED; print "RESTART\n"; next; }
	    if ( /STEP dropped/ )
	    {  select STRAY; print; next; }

	    # xntp3.3l
	    if ( /.*adjust: time / ) {
		s/.*adjust: //;
		@inf = split(' ');
		$dtype = ($inf[2] eq "SLEW" ? 1 : 2);
		select DETAILED;
		print "ADJUST $pid $inf[1] $dtype $inf[3] $inf[5] $inf[7] $inf[9] $inf[11]\n";
		next;
	    }

	    # xntp3.4e or later
	    if ( /.*local_clock: phase .* allan/ ) {
		s/.*local_clock: //;
		@inf = split(' ');
		$dtype = ($inf[16] eq "SLEW" ? 1 : 2);
		if ( $inf[11] < 0 ) {
		    $inf[11] += 4*1024*1024*1024;
		}
		select DETAILED;
		#              pid time       type   clock  aoffset drift   tau     rsadj
		print "ADJUST $pid $inf[11] $dtype $inf[15] $inf[1] $inf[3] $inf[9] $inf[13]\n";
		next;
	    }

	    # xntp3.3zf
	    if ( /.*local_clock: phase/ ) {
		s/.*local_clock: //;
		@inf = split(' ');
		$dtype = ($inf[14] eq "SLEW" ? 1 : 2);
		select DETAILED;
		#              pid time       type   clock  aoffset drift   tau     rsadj
		print "ADJUST $pid $inf[9] $dtype $inf[13] $inf[1] $inf[3] $inf[7] $inf[11]\n";
		next;
	    }

	    # xntp3.3l, xntp3.3zf, xntp3.4e
	    if ( /.*observation: time/ ) {
	        s/.*observation: //;
		@inf = split(' ');
		select DETAILED;
		# observation pid time peer phase/off delay error rsadj
		print "OBSERVATION $pid $inf[1] $inf[2] $inf[4] $inf[6] $inf[8] $inf[10]\n";
		next;
	    }

	    if ( /^offset .*freq/ )
	    { select HOURLY; print; next; }

	    if ( /peer .* event .* status/ )
	    { next; }
	    if ( /RSADJDEFICIT/ )
	    { next; }
	    select STRAY;
	    print;
	}
	close INF;
	select STDOUT;
    }	
    close DETAILED; close HOURLY; close STRAY;
}

&cdtop;

system "crunch-data @HOSTS" ;
