202
Chapter 15: Advanced Configuration
15.1.2 Running custom scripts when alerts are triggered
Wheneveranalertgetstriggered,specicscriptsgetcalled.Thesescriptsallresidein /etc/scripts/. Below is a list of the default
scripts that get run for each applicable alert:
• Foraconnectionalert(whenauserconnectsordisconnectsfromaportornetworkhost):
/etc/scripts/portmanager-user-alert (forportconnections)or/etc/scripts/sdt-user-alert(forhostconnections)
• Forasignalalert(whenasignalonaportchangesstate): /etc/scripts/portmanager-signal-alert
• Forapatternmatchalert(whenaspecicregularexpressionisfoundintheserialportscharacterstream):
/etc/scripts/portmanager-pattern-alert
• ForaUPSstatusalert(whentheUPSpowerstatuschangesbetweenonline,onbattery,andlowbattery):
/etc/scripts/ups-status-alert
• Foraenvironmental,powerandalarmsensoralerts(temperature,humidity,powerloadandbatterychargealerts):
/etc/scripts/environmental-alert
• Foraninterfacefailoveralert: /etc/scripts/interface-failover-alert
All of these scripts do a check to see whether you have created a custom script to run instead. The code that does this check
isshownbelow(anextractfromthele/etc/scripts/portmanager-pattern-alert):
# If there's a user-configured script, run it instead
scripts[0]="/etc/config/scripts/pattern-alert.${ALERT_PORTNAME}"
scripts[1]="/etc/config/scripts/portmanager-pattern-alert"
for (( i=0 ; i < ${#scripts[@]} ; i++ )); do
if [ -f "${scripts[$i]}" ]; then
exec /bin/sh "${scripts[$i]}"
fi
done
This code shows that there are two alternative scripts that can be run instead of the default one. This code first checks
whetherale"/etc/config/scripts/pattern-alert.${ALERT_PORTNAME}"exists.Thevariable${ALERT_PORTNAME}mustbe
replacedwith"port01"or"port13"orwhicheverportthealertshouldrunfor.Ifthislecannotbefound,thescriptchecks
whetherthele"/etc/config/scripts/portmanager-pattern-alert"exists.Ifeitheroftheselesexiststhescriptcallstheexec
command on the first file that it finds and runs that custom file/script instead.
Asanexample,youcancopythe /etc/scripts/portmanager-pattern-alert script file to
/etc/config/scripts/portmanager-pattern-alert:
# cd /
# mkdir /etc/config/scripts (ifthedirectorydoesnotalreadyexist)
# cp /etc/scripts/portmanager-pattern-alert /etc/config/scripts/portmanager-pattern-alert
Thenextstepwillbetoeditthenewscriptle.Firstly,openthele/etc/config/scripts/portmanager-pattern-alertusingvi(or
anyothereditor),andremovethelinesthatcheckforacustomscript(thecodefromabove)-thiswillpreventthenewcustom
scriptfromrepeatedlycallingitself.Aftertheselineshavebeenremoved,editthele,oraddanyadditionalscriptingtothe
file.