home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / convex / errlogd < prev    next >
Encoding:
Text File  |  1990-03-02  |  4.4 KB  |  136 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # beware the nasty modeline --> vi:set ts=6|set sw=3:
  4. #
  5. #
  6. # errlogd: forward spu:/mnt/errlog to do syslogd
  7. # until we merge do syslog and errlog, this'll have to do.
  8. #
  9. # periodically examine spu's errlog file for growth since last time
  10. # looked.  retrieve only new part, discarding date stamps, and do syslog'ing
  11. # with facility class LOCAL0 anything else, priority set to be INFO for
  12. # routine stuff and NOTICE for other stuff.  reserve higher pris for
  13. # internal problems.  note that you only see things that occur while JP
  14. # running, ie, no panic messages, diags, etc.
  15. #
  16. # written by tom christiansen
  17.  
  18. ($program = $0) =~ s%.*/%%;
  19.  
  20. $\ = "\n"; $! = 0;
  21.  
  22. # magic to check for run time switches
  23. eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
  24.  
  25. $priority = "warn" unless $priority; # should really 
  26. $facility = "local0" unless $facility;
  27. $sleep = 3 unless $sleep;
  28. $sleep *= 60;
  29.  
  30. die "$program: not superuser\n" if $>;
  31. die "$program: usage error; see man page\n" if $#ARGV > $[;
  32.  
  33. # wanna get this:
  34. #    + ls -l /mnt/errlog -rw-rw-rw- 1 root       5613 Feb  4 15:51 /mnt/errlog
  35. #    0 1  2  3            4        5 6          7    8    9 10    11
  36. #
  37. again:
  38. $log = `/usr/convex/spucmd ls -l /mnt/errlog 2>&1 `;
  39. @log = split(/\s+/,$log);
  40. #die "$program: initial /usr/convex/spucmd exited ".($?>>8)."\n" if $?;
  41.  
  42. if ($#log != 11) {
  43.    #do syslog("err","initial /usr/convex/spucmd seems misformatted: ".join(@log,' ')."\n");
  44.    sleep 60;
  45.    goto again;
  46. }
  47.  
  48. $size = $log[7];
  49.  
  50. do syslog("debug","startup, spu:/mnt/errlog @ $size bytes");
  51.  
  52. if (open(pidlog, ">/usr/adm/tmp/errlogd.pid")) {
  53.     print pidlog $$;
  54.     close pidlog;
  55. } else {
  56.    do syslog("err","couldn't log pid $$");
  57.  
  58.  
  59. foreach $signal ((
  60.    'QUIT', 'ILL', 'TRAP', 'IOT', 'EMT', 'FPE', 'BUS', 'SEGV', 'SYS', 
  61.    'PIPE', 'ALRM', 'TERM', 'URG', 'IO', 'POLL', 'XCPU', 'XFSZ', 'VTALRM', 
  62.    'PROF', 'LOST', 'USR1', 'USR2' ))
  63. {
  64.     $SIG{$signal} = 'godown';
  65.  
  66. $SIG{'ALRM'} = "IGNORE"; # spurious SIGALRMs by impatient sysadmins
  67. $SIG{'HUP'}  = "reboot"; # why do this?  new script i guess
  68.  
  69. for (;;) {
  70.    sleep $sleep;
  71.    @log = split(/\s+/,`/usr/convex/spucmd ls -l /mnt/errlog 2>&1`);
  72.  
  73.    if ($?) {
  74.     #do syslog("notice", "/usr/convex/spucmd exited ".($?>>8));
  75.     next;                 # maybe should exit
  76.    }
  77.  
  78.    if ($#log != 11) {
  79.     #do syslog("notice", "\`/usr/convex/spucmd ls -l /mnt/errlog\` seems misformatted: \"".join(' ',@log)."\"" );
  80.     next;                 # maybe should exit
  81.    } 
  82.    next if $log[7] == $size;         # no growth
  83.    if ($log[7] < $size) {
  84.     do syslog("debug", "spu:/mnt/errlog just shrank from " .
  85.         $size . " to " . $log[7] . " bytes long");
  86.     $size = 0;     # negative growth; assume new file
  87.    }
  88.    $tail = sprintf("/usr/convex/spucmd tail +%dc /mnt/errlog | ",$size+1);
  89.    $size = $log[7];
  90.    open tail || (do syslog("err","can't open \"$tail\": $!"), next);
  91.    $oldwho = "";                # no previous logger 
  92.    while(<tail>) {
  93.       next if /<(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Ju[nl]|Aug|Sep|Oct|Nov|Dec) [ 123]\d \d+>$/ || /CPU date\/time changed/ ;    # skip ugly datestamps
  94.       ($who,$which,$when,$msg) =     # parse message
  95.           /^\[(SPU|C[CP]U)(\d*)[_ ]*@(\d\d.\d\d.\d\d\])\s*(.*)/;
  96.       $who =~ tr/A-Z/a-z/;        # tolower
  97.       $which += 0 if length($which); # force string to numeric to trim leading 0
  98.       next unless $who;        # race condition?
  99.       $who .= $which if length($which); # catenate unit number of cpu or ccu
  100.       if ( $who ne $oldwho ) {
  101.           close logger if $oldwho;    # logger id (spu,ccuX,..) changed
  102.           $pri = $priority;        # assume defualt level unless boring
  103.           $pri = "notice" if /sniff/ || /\bta\d+:/ || /NFS/; # too boring to biff us for
  104.           $logger = sprintf("| logger -t %s -p %s.%s", $who, $facility, $pri);
  105.           open logger ||(do syslog("err","\"$logger\" wouldn't open: $!"),last);
  106.         select(logger); $| = 1;    # unbuffer PIPE, just in case
  107.           select(stdout);        # just in case
  108.           $oldwho = $who;        # remember who just spoke
  109.       } 
  110.       print logger $msg;        # off to syslog we go
  111.    }
  112.    close logger if $oldwho;        # send EOF to logger PIPE if one started
  113.    close tail;                # until next time
  114. }  # forever loop
  115.  
  116.  
  117. # usage: do syslog("alert","some message string"); 
  118. sub syslog {
  119.    local ($level,$message) = @_;
  120.    system "logger","-t",$program,"-p",$facility.".".$level,$message;
  121. }
  122.  
  123. # generic interrupt handler
  124. sub godown {
  125.    local($signal) = @_;
  126.    do syslog("notice","going down on SIG$signal");
  127.    exit(1);
  128.  
  129. sub reboot {
  130.    do syslog("notice","restarting $mypath");
  131.    exec($mypath);
  132.