home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / tutorial / eg / convex / errlogd < prev    next >
Encoding:
Text File  |  1990-07-18  |  4.8 KB  |  148 lines

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