alert.awk
Generated on Tue Dec 05 17:39:05 Eastern Standard Time 2006 from alert.awk
# Program : ALERT.AWK
# Purpose : Generate pager alerts based on thresholds in a .CSV file
# Author : Bob Jonkman <bjonkman@sobac.com>
# Copyright 2008 Bob Jonkman and/or SOBAC Microcomputer Services
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Date : 19 July 2005
# Usage : gawk -f ALERT.AWK [-v THRESHOLD=x -v COMP=(GT|LT|EQ|EXISTS)] [-v ALERTFILE=filename.101] [-v (FIELDNAME=header|FIELDNUM=x)] [-v SUBJECT=subjectline] [-v FROM=emailaddress] -v TO=emailaddress filename.csv > anyfile
# Variables : THRESHOLD - (optional, COMP=EXISTS if omitted) Threshold for comparison
# COMP - (required if THRESHOLD is specified) Comparison: "GT" for Greater Than;
# "LT" for Less Than; "EQ" for Equal To ; "EXISTS" for all entries
# ALERTFILE - (optional, default="ALERT.101") Output file in the form of a
# Mercury "Glue Headers" file for use by the Mercury Mail Server,
# 8 chars for filename, "101" for extension (see
# http://listserver.info/mercury/glue.htm)
# FIELDNAME - (optional) Header of field on which to perform threshold comparison
# FIELDNUM - (optional, default=1) Field number on which to perform threshold comparison
# if FIELDNAME is not specified
# TO - (required) E-mail address of alert recipient
# FROM - (optional, default="gwmon@gwmon.example.com") E-mail address of sender (this application)
# SUBJECT - (optional, default= violation summary) Subject line of alert message
BEGIN { if(!TO)
{ print("Error(1): No TO: address specified") > "/dev/stderr" ;
exit(1) ;
}
if(!FROM)
FROM = "gwmon@gwmon.example.com" ;
if(!THRESHOLD)
COMP = "EXISTS" ;
else
{ if((COMP != "LT") && (COMP != "GT") && (COMP != "EQ") && (COMP != "EXISTS"))
{ print("Error(2): THRESHOLD specified with invalid COMP= " COMP ) > "/dev/stderr" ;
exit(2) ;
}
}
if(!ALERTFILE)
ALERTFILE = "ALERT.101" ;
# print("##### DEBUG ##### tz= " strftime("%z") "#####") ;
if(substr(strftime("%z"),1,21) == "Eastern Daylight Time") # because strftime doesn't work as documented
tz = "-0400" ;
else
tz = "-0500" ;
}
(FNR == 1) { numheaders = parsecsv($0,headers) ;
for(i=1; i<= numheaders; i++)
{
if(FIELDNAME == headers[i])
FIELDNUM = i ;
}
if (!FIELDNUM)
FIELDNUM = 1 ;
}
($0 == "=====") { nextfile; # end-of-file, don't process footers
}
(FNR != 1) { numfields = parsecsv($0,record) ;
# print("##### DEBUG ##### FIELDNUM= " FIELDNUM, " record[FIELDNUM]= " record[FIELDNUM]+0, " THRESHOLD= " THRESHOLD );
# print("##### DEBUG ##### (COMP == GT) " (COMP == "GT"), " rec > TH " (record[FIELDNUM]+0 > THRESHOLD)," = " ((COMP == "GT") && (record[FIELDNUM]+0 > THRESHOLD))) ;
# print("##### DEBUG ##### (COMP == LT) " (COMP == "LT"), " rec < TH " (record[FIELDNUM]+0 < THRESHOLD)," = " ((COMP == "LT") && (record[FIELDNUM]+0 < THRESHOLD))) ;
# print("##### DEBUG ##### (COMP == EQ) " (COMP == "EQ"), " rec == TH " (record[FIELDNUM] == THRESHOLD)," = " ((COMP == "EQ") && (record[FIELDNUM] == THRESHOLD))) ;
if(((COMP == "GT") && (record[FIELDNUM]+0 > THRESHOLD)) || ((COMP == "LT") && (record[FIELDNUM]+0 < THRESHOLD)) || ((COMP == "EQ") && (record[FIELDNUM] == THRESHOLD)) || (COMP == "EXISTS"))
{ vcount++ ;
vheader = headers[FIELDNUM]
for(i=1; i <= numfields; i++)
{ vstring[vstringnum++] = headers[i] " = " record[i]
}
vstring[vstringnum++] = ""
}
}
END {
if(vcount)
{
print("Alert: " vheader " " COMP " " THRESHOLD " (Violations: " vcount ")" )
if(!SUBJECT)
SUBJECT = "GWAlert: " vheader " " COMP " " THRESHOLD " (Violations: " vcount ")" ;
print("$$ " FROM ) > ALERTFILE ;
print("T " TO ) > ALERTFILE ;
print("") > ALERTFILE ;
print("X-Mailer: ALERT.AWK by Bob Jonkman <bjonkman@sobac.com>") > ALERTFILE ;
print("From: " FROM ) > ALERTFILE ;
print("To: " TO ) > ALERTFILE ;
print("Date: " strftime("%a, %d %b %Y %H:%M:%S " tz)) > ALERTFILE ;
print("Subject: " SUBJECT ) > ALERTFILE ;
print("") > ALERTFILE ;
print("Threshold exceeded: " vheader " " COMP " " THRESHOLD ) > ALERTFILE
print("Violations: " vcount ) > ALERTFILE ;
print("") > ALERTFILE ;
for(i=0; i<vstringnum; i++)
{
print(vstring[i]) > ALERTFILE
print(vstring[i])
}
}
}
# EOF: ALERT.AWK
1 files processed.