home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume5 / peerssa < prev    next >
Internet Message Format  |  1989-02-03  |  5KB

  1. Path: xanth!nic.MR.NET!hal!ncoast!allbery
  2. From: allbery@ncoast.UUCP (Brandon S. Allbery)
  3. Newsgroups: comp.sources.misc
  4. Subject: v05i091: peerssa -- a checker script in perl
  5. Message-ID: <13273@ncoast.UUCP>
  6. Date: 23 Dec 88 01:26:17 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: allbery@ncoast.UUCP (Brandon S. Allbery)
  9. Organization: Cleveland Public Access UN*X, Cleveland, Oh
  10. Lines: 151
  11. Approved: allbery@ncoast.UUCP
  12.  
  13. Posting-number: Volume 5, Issue 91
  14. Submitted-by: "Brandon S. Allbery" <allbery@ncoast.UUCP>
  15. Archive-name: peerssa
  16.  
  17. This is a perl script I whipped up to check the status of ncoast when I log
  18. in.  It may not be in the best form it could be; it suffers from having been
  19. written when perl wasn't working right on ncoast (courtesy of a botched C
  20. compiler).  However, it's useful as it is.
  21.  
  22. It can be invoked with the name of a checker script, or will default to the
  23. file ~/.checklist if a script is not specified.  An example checker script is
  24. below:
  25.  
  26. #
  27. # Checklist for Peerssa:  The Checker
  28. #
  29.  
  30. "df|" /([^     ]+).*[     ][     ]*(([0-9]?[0-9])?[0-9]) blocks/ + "WARNING: $2 BLOCKS FREE ON $1"
  31.   |   /([^     ]+).*[     ][     ]*(([0-9]?[0-9])?[0-9]) inodes/ + "WARNING: $2 INODES FREE ON $1"
  32. "ps -e|" /expire/ "Expire is running."
  33. "/bin/who|" /^rhg / "The Boss is on the system."
  34.     |       /^hal / "We are receiving news and mail."
  35. /etc/inittab /^2:13:c:/ "The system is in Night/Weekend mode."
  36.  
  37. The format of the file is:
  38.  
  39.         source pattern [opcode] message
  40.  
  41. The source may be quoted, the message must be quoted.  Escaped quotes are
  42. handled in all quoted strings.  At present, the pattern must be enclosed in
  43. slashes, although I intend to change this.  A source followed by a pipe
  44. character is considered to be a program or pipeline to be run to produce
  45. input, just as in perl itself.  A source consisting of a single pipe
  46. character signifies an additional pattern for use with the preceding source.
  47. When the message is printed, variables $0-$9 are substituted from the pattern
  48. by the same rules as for perl's $0-$9 variables.
  49.  
  50. The opcodes are:
  51.  
  52.     = (default)    print message for first matching line
  53.     +        print message for all matching lines
  54.     -        print message if NO matching lines
  55.  
  56. In the latter case, $0-$9 are not substituted.
  57.  
  58. #! /bin/sh
  59. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  60. # Contents:  peerssa
  61. echo extracting 'peerssa'
  62. if test -f 'peerssa' -a -z "$1"; then echo Not overwriting 'peerssa'; else
  63. sed 's/^X//' << \EOF > 'peerssa'
  64. Xeval "exec perl -S $0 $*"
  65. X    if $execed_by_shell;
  66. X
  67. X#
  68. X# Peerssa:  The Checker
  69. X# A perl script by Brandon S. Allbery
  70. X# I hereby place this program in the public domain
  71. X#
  72. X# Scan a list of files or processes for specified patterns, and print messages
  73. X# based on those patterns.  This is in some ways similar to "file" under
  74. X# System V, but using patterns instead of fixed offsets and values.  I use it
  75. X# to monitor certain critical system values, and certain things important to
  76. X# me.
  77. X#
  78. X# If you don't understand the name, read Larry Niven's short story "Rammer".
  79. X#
  80. X
  81. X# The file containing things to check for
  82. X$checklist = $ENV{"HOME"} . "/.checklist";
  83. X
  84. X$npat = 0;
  85. X$l = 0;
  86. X$program = $0;
  87. Xopen checklist;
  88. Xwhile (<checklist>) {
  89. X    $l++;
  90. X    chop;
  91. X# skip blank lines or comments
  92. X    next if /^#/ || /^[ \t]*$/;
  93. X    $line = $_;
  94. X    do check() if $npat != 0 && $_ !~ /[     ]*\|[     ]/;
  95. X# file-or-process /pattern/ [op] "message"
  96. X# "file or process" /pattern/ [op] "message"
  97. X# | /pattern/ [op] "message"    -- continue command
  98. X# op is:  "=" match once, "+" match multiple, "!" print on no match
  99. X    if ($line !~ m#^[ \t]*"(([^"]*\\")*[^"]*)"[ \t]+/(([^/]*\\/)*[^/]*)/[ \t]*([=!+]?)[ \t]*"(([^"]*\\")*[^"]*)"[ \t]*$#) {
  100. X        if ($line !~ m#^[ \t]*([^ \t]+)[ \t]+/(([^/]*\\/)*[^/]*)/[ \t]*([=!+]?)[ \t]*"(([^"]*\\")*[^"]*)"[ \t]*$#) {
  101. X            print "$program: syntax error on line $l of \"$checklist\"\n";
  102. X            exit;
  103. X        }
  104. X        else {
  105. X            $cf = $1;
  106. X            $pat = $2;
  107. X            $opr = $4;
  108. X            $msg = $5;
  109. X            die "$program: no previous pattern on line $l of \"$checklist\"\n" if $cf eq '|' & $npat == 0;
  110. X            $checkfile = $cf if $cf ne '|';
  111. X        }
  112. X    }
  113. X    else {
  114. X        $checkfile = $1;
  115. X# remove escaped quotes on filename
  116. X        $checkfile =~ s/\\"/"/g;
  117. X        $pat = $3;
  118. X        $opr = $5;
  119. X        $msg = $6;
  120. X    }
  121. X# default op is "="
  122. X    $opr = "=" if $opr eq "";
  123. X# remove escaped quotes on pattern and message
  124. X    $pat =~ s#\\/#/#g;
  125. X    $msg =~ s/\\"/"/g;
  126. X# assign into array of [op, pat, message]
  127. X    $pattern[$npat] = $pat;
  128. X    $op[$npat] = $opr;
  129. X    $message[$npat] = $msg;
  130. X    $npat++;
  131. X}
  132. Xexit if $npat == 0;
  133. Xdo check();
  134. Xexit(0);
  135. X
  136. Xsub check {
  137. X    local ($matched, $cnt);
  138. X
  139. X    open checkfile;
  140. X    $matched = 0;
  141. X    while (<checkfile>) {
  142. X        for ($cnt = 0; $cnt < $npat; $cnt++) {
  143. X            if ($_ =~ $pattern[$cnt]) {
  144. X                $matched = 1;
  145. X# the "eval" lets us print $1-$9 from the pattern, for customized messages
  146. X                eval 'print "' . $message[$cnt] . "\n" . '"' if $op[$cnt] ne '!';
  147. X                last if $op[$cnt] ne '+';
  148. X            }
  149. X        }
  150. X    }
  151. X    close checkfile;
  152. X    print $message[0] . "\n" if !$matched & $op[0] eq '!';
  153. X}
  154. EOF
  155. chars=`wc -c < 'peerssa'`
  156. if test $chars !=    2544; then echo 'peerssa' is $chars characters, should be    2544 characters!; fi
  157. fi
  158. exit 0
  159. -- 
  160. Brandon S. Allbery, comp.sources.misc moderator and one admin of ncoast PA UN*X
  161. uunet!hal.cwru.edu!ncoast!allbery            ncoast!allbery@hal.cwru.edu
  162. comp.sources.misc is moving off ncoast -- please do NOT send submissions direct
  163.       Send comp.sources.misc submissions to comp-sources-misc@<backbone>.
  164.