diff --git a/EPICS2SMS.sh b/EPICS2SMS.sh new file mode 100755 index 0000000..830dfa3 --- /dev/null +++ b/EPICS2SMS.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +# --- configuration --------------------------------------------------------- + +# PV_NAME="X12SA-FE-VMMG-0010:PLC_RELAY-D" # a PV with states "ON"/"OFF" +PV_NAME="AGEBD-PARAMS:INJECTION-RATE" # a PV with numerical value + units +PHONE_NUMBER="0041793083005" # the phone number(s), space separated +EMAIL="andreas.menzel@psi.ch" # the email address(es), space separated +POLL_INTERVAL=5 # seconds between polls while Running +ERROR_INTERVAL=60 # seconds between polls while NOT Running + +send_sms() { + local numbers="$1 $2" # to combine PHONE_NUMBER and EMAIL + local message="$3" + local email_hdr="" + + # build To: header(s) + for tok in $numbers; do + if [[ "$tok" == *"@"* ]]; then + email_hdr+="To:$tok"$'\n' + else + email_hdr+="To:${tok}@sms.switch.ch"$'\n' + fi + done + + # append a timestampe + local message_with_date + message_with_date="$message"$'\n'"$(date)" + + # mimic: (echo '$email_hdr'; echo '$message_with_date') | /usr/sbin/sendmail -t + { + printf "%s" "$email_hdr" + printf "\n" + printf "%s\n" "$message_with_date" +# } | cat # for testing + } | /usr/sbin/sendmail -t # for production +} + +# --- main loop ------------------------------------------------------------ + +alert_sent=0 # 0 = no alert sent for current outage, 1 = already sent + +echo "[$(date)] Starting EPICS monitor for PV '$PV_NAME' ..." + +while true; do + # Get PV value as a plain string; adjust flags if needed (-S for string) +# caget "$PV_NAME" # only for debugging + value=$(caget -noname -nounit "$PV_NAME" 2>/dev/null) + status=$? + + # If caget fails, treat that as "not Running" (e.g. IOC down) + if [[ $status -ne 0 ]]; then + echo "[$(date)] WARNING: caget failed for '$PV_NAME' (exit $status)" + value="UNAVAILABLE" + fi + +# if [[ "$value" == "\"ON\"" ]]; then # for a PV with states "ON"/"OFF" + if awk "BEGIN { exit !($value > 0) }"; then # to check whether value is smaller than zero + # System is OK again + if [[ $alert_sent -eq 1 ]]; then + echo "[$(date)] PV '$PV_NAME' back to Running." + fi + alert_sent=0 + sleep "$POLL_INTERVAL" + else + # System not OK + if [[ $alert_sent -eq 0 ]]; then + msg="Alert: PV $PV_NAME is '$value'." + send_sms "$PHONE_NUMBER" "$EMAIL" "$msg" + alert_sent=1 + fi + sleep "$ERROR_INTERVAL" + fi +done