home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume05 / peerssa < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  5.5 KB

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