allbery@ncoast.UUCP (Brandon S. Allbery) (12/23/88)
Posting-number: Volume 5, Issue 91 Submitted-by: "Brandon S. Allbery" <allbery@ncoast.UUCP> Archive-name: peerssa This is a perl script I whipped up to check the status of ncoast when I log in. It may not be in the best form it could be; it suffers from having been written when perl wasn't working right on ncoast (courtesy of a botched C compiler). However, it's useful as it is. It can be invoked with the name of a checker script, or will default to the file ~/.checklist if a script is not specified. An example checker script is below: # # Checklist for Peerssa: The Checker # "df|" /([^ ]+).*[ ][ ]*(([0-9]?[0-9])?[0-9]) blocks/ + "WARNING: $2 BLOCKS FREE ON $1" | /([^ ]+).*[ ][ ]*(([0-9]?[0-9])?[0-9]) inodes/ + "WARNING: $2 INODES FREE ON $1" "ps -e|" /expire/ "Expire is running." "/bin/who|" /^rhg / "The Boss is on the system." | /^hal / "We are receiving news and mail." /etc/inittab /^2:13:c:/ "The system is in Night/Weekend mode." The format of the file is: source pattern [opcode] message The source may be quoted, the message must be quoted. Escaped quotes are handled in all quoted strings. At present, the pattern must be enclosed in slashes, although I intend to change this. A source followed by a pipe character is considered to be a program or pipeline to be run to produce input, just as in perl itself. A source consisting of a single pipe character signifies an additional pattern for use with the preceding source. When the message is printed, variables $0-$9 are substituted from the pattern by the same rules as for perl's $0-$9 variables. The opcodes are: = (default) print message for first matching line + print message for all matching lines - print message if NO matching lines In the latter case, $0-$9 are not substituted. #! /bin/sh # This file was wrapped with "dummyshar". "sh" this file to extract. # Contents: peerssa echo extracting 'peerssa' if test -f 'peerssa' -a -z "$1"; then echo Not overwriting 'peerssa'; else sed 's/^X//' << \EOF > 'peerssa' Xeval "exec perl -S $0 $*" X if $execed_by_shell; X X# X# Peerssa: The Checker X# A perl script by Brandon S. Allbery X# I hereby place this program in the public domain X# X# Scan a list of files or processes for specified patterns, and print messages X# based on those patterns. This is in some ways similar to "file" under X# System V, but using patterns instead of fixed offsets and values. I use it X# to monitor certain critical system values, and certain things important to X# me. X# X# If you don't understand the name, read Larry Niven's short story "Rammer". X# X X# The file containing things to check for X$checklist = $ENV{"HOME"} . "/.checklist"; X X$npat = 0; X$l = 0; X$program = $0; Xopen checklist; Xwhile (<checklist>) { X $l++; X chop; X# skip blank lines or comments X next if /^#/ || /^[ \t]*$/; X $line = $_; X do check() if $npat != 0 && $_ !~ /[ ]*\|[ ]/; X# file-or-process /pattern/ [op] "message" X# "file or process" /pattern/ [op] "message" X# | /pattern/ [op] "message" -- continue command X# op is: "=" match once, "+" match multiple, "!" print on no match X if ($line !~ m#^[ \t]*"(([^"]*\\")*[^"]*)"[ \t]+/(([^/]*\\/)*[^/]*)/[ \t]*([=!+]?)[ \t]*"(([^"]*\\")*[^"]*)"[ \t]*$#) { X if ($line !~ m#^[ \t]*([^ \t]+)[ \t]+/(([^/]*\\/)*[^/]*)/[ \t]*([=!+]?)[ \t]*"(([^"]*\\")*[^"]*)"[ \t]*$#) { X print "$program: syntax error on line $l of \"$checklist\"\n"; X exit; X } X else { X $cf = $1; X $pat = $2; X $opr = $4; X $msg = $5; X die "$program: no previous pattern on line $l of \"$checklist\"\n" if $cf eq '|' & $npat == 0; X $checkfile = $cf if $cf ne '|'; X } X } X else { X $checkfile = $1; X# remove escaped quotes on filename X $checkfile =~ s/\\"/"/g; X $pat = $3; X $opr = $5; X $msg = $6; X } X# default op is "=" X $opr = "=" if $opr eq ""; X# remove escaped quotes on pattern and message X $pat =~ s#\\/#/#g; X $msg =~ s/\\"/"/g; X# assign into array of [op, pat, message] X $pattern[$npat] = $pat; X $op[$npat] = $opr; X $message[$npat] = $msg; X $npat++; X} Xexit if $npat == 0; Xdo check(); Xexit(0); X Xsub check { X local ($matched, $cnt); X X open checkfile; X $matched = 0; X while (<checkfile>) { X for ($cnt = 0; $cnt < $npat; $cnt++) { X if ($_ =~ $pattern[$cnt]) { X $matched = 1; X# the "eval" lets us print $1-$9 from the pattern, for customized messages X eval 'print "' . $message[$cnt] . "\n" . '"' if $op[$cnt] ne '!'; X last if $op[$cnt] ne '+'; X } X } X } X close checkfile; X print $message[0] . "\n" if !$matched & $op[0] eq '!'; X} EOF chars=`wc -c < 'peerssa'` if test $chars != 2544; then echo 'peerssa' is $chars characters, should be 2544 characters!; fi fi exit 0 -- Brandon S. Allbery, comp.sources.misc moderator and one admin of ncoast PA UN*X uunet!hal.cwru.edu!ncoast!allbery ncoast!allbery@hal.cwru.edu comp.sources.misc is moving off ncoast -- please do NOT send submissions direct Send comp.sources.misc submissions to comp-sources-misc@<backbone>.