Added temperature monitoring script to vhost1 and vhost2.
authorZoltán Felleg <zoltan.felleg@userrendszerhaz.hu>
Mon, 30 Nov 2020 14:34:21 +0000 (15:34 +0100)
committerZoltán Felleg <zoltan.felleg@userrendszerhaz.hu>
Mon, 30 Nov 2020 14:34:21 +0000 (15:34 +0100)
sources/vhost1.usr/root/temperature.cfg [new file with mode: 0644]
sources/vhost1.usr/root/temperature.sh [new file with mode: 0755]
sources/vhost2.usr/root/temperature.cfg [new file with mode: 0644]
sources/vhost2.usr/root/temperature.sh [new file with mode: 0755]

diff --git a/sources/vhost1.usr/root/temperature.cfg b/sources/vhost1.usr/root/temperature.cfg
new file mode 100644 (file)
index 0000000..30ad422
--- /dev/null
@@ -0,0 +1,13 @@
+MODEM_IP="192.168.8.1"
+#        zfelleg        fschnell       rrendek        mszabo
+PHONES=("+36209541513" "+36309502529" "+36309775888" "+36704581234")
+NUMBER_OF_PHONES=4
+WARNING_PHONES=("+36209541513" "+36309502529" "+36309775888" "+36704581234")
+ERROR_PHONES=("+36209541513" "+36309502529" "+36309775888" "+36704581234")
+CRITICAL_PHONES=("+36209541513" "+36309502529" "+36309775888" "+36704581234")
+HEARTBEAT_PHONES=("+36303744065" "+36309502529" "+36309775888" "+36704581234")
+WARNING_THRESHOLD=25
+ERROR_THRESHOLD=30
+CRITICAL_THRESHOLD=35
+HOST_SERIAL=$(hostname | cut -c 6)
+IMM_FQDN="vhost${HOST_SERIAL}imm.usr.user.hu"
diff --git a/sources/vhost1.usr/root/temperature.sh b/sources/vhost1.usr/root/temperature.sh
new file mode 100755 (executable)
index 0000000..9dc4de7
--- /dev/null
@@ -0,0 +1,196 @@
+#!/bin/sh
+
+
+SCRIPT_PATH=$(dirname $(realpath $0))
+SCRIPT_BASE=$(basename $(realpath $0) .sh)
+SCRIPT_CONFIG=$SCRIPT_PATH/${SCRIPT_BASE}.cfg
+SCRIPT_STOP=$SCRIPT_PATH/${SCRIPT_BASE}.stop
+
+
+source $SCRIPT_CONFIG
+
+
+delete_logs() {
+    LOG_COUNT=$(ls /tmp/temperature-????????.log | wc -l)
+    if [ $LOG_COUNT -gt 14 ]
+    then
+        FIRST_LOG=$(ls /tmp/temperature-????????.log | head -n 1)
+        rm -f $FIRST_LOG
+    fi
+}
+
+delete_mail() {
+    echo 'delete (from "Cron Daemon")' | mail -N
+}
+
+delete_received_smses() {
+    curl --silent --request GET "http://$MODEM_IP/api/webserver/SesTokInfo" >ses_tok.xml
+
+    SESSION_ID=$(sed 's|^.*<SesInfo>\(.*\)</SesInfo>.*$|\1|' <ses_tok.xml)
+    TOKEN=$(sed 's|^.*<TokInfo>\(.*\)</TokInfo>.*$|\1|' <ses_tok.xml)
+
+    # BoxType:
+    #   1: received
+    #   2: sent
+    curl --header "Cookie: $SESSION_ID" \
+         --header "__RequestVerificationToken: $TOKEN" \
+         --request POST \
+         --silent \
+         --data "<?xml version='1.0' encoding='UTF-8'?><request><PageIndex>1</PageIndex><ReadCount>20</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>" \
+         http://$MODEM_IP/api/sms/sms-list \
+         | grep '<Index>' \
+         | while read LINE
+    do
+        INDEX=$(echo $LINE | sed 's|^ *<Index>\([0-9]*\)</Index> *$|\1|')
+
+        curl --silent --request GET "http://$MODEM_IP/api/webserver/SesTokInfo" >ses_tok.xml
+
+        SESSION_ID=$(sed 's|^.*<SesInfo>\(.*\)</SesInfo>.*$|\1|' <ses_tok.xml)
+        TOKEN=$(sed 's|^.*<TokInfo>\(.*\)</TokInfo>.*$|\1|' <ses_tok.xml)
+
+        curl --header "Cookie: $SESSION_ID" \
+             --header "__RequestVerificationToken: $TOKEN" \
+             --request POST \
+             --silent \
+             --data "<?xml version='1.0' encoding='UTF-8'?><request><Index>$INDEX</Index></request>" \
+             http://$MODEM_IP/api/sms/delete-sms
+
+       touch $SCRIPT_STOP
+    done
+}
+
+delete_sent_smses() {
+    curl --silent --request GET "http://$MODEM_IP/api/webserver/SesTokInfo" >ses_tok.xml
+
+    SESSION_ID=$(sed 's|^.*<SesInfo>\(.*\)</SesInfo>.*$|\1|' <ses_tok.xml)
+    TOKEN=$(sed 's|^.*<TokInfo>\(.*\)</TokInfo>.*$|\1|' <ses_tok.xml)
+
+    # BoxType:
+    #   1: received
+    #   2: sent
+    curl --header "Cookie: $SESSION_ID" \
+         --header "__RequestVerificationToken: $TOKEN" \
+         --request POST \
+         --silent \
+         --data "<?xml version='1.0' encoding='UTF-8'?><request><PageIndex>1</PageIndex><ReadCount>20</ReadCount><BoxType>2</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>" \
+         http://$MODEM_IP/api/sms/sms-list \
+         | grep '<Index>' \
+         | while read LINE
+    do
+        INDEX=$(echo $LINE | sed 's|^ *<Index>\([0-9]*\)</Index> *$|\1|')
+
+        curl --silent --request GET "http://$MODEM_IP/api/webserver/SesTokInfo" >ses_tok.xml
+
+        SESSION_ID=$(sed 's|^.*<SesInfo>\(.*\)</SesInfo>.*$|\1|' <ses_tok.xml)
+        TOKEN=$(sed 's|^.*<TokInfo>\(.*\)</TokInfo>.*$|\1|' <ses_tok.xml)
+
+        curl --header "Cookie: $SESSION_ID" \
+             --header "__RequestVerificationToken: $TOKEN" \
+             --request POST \
+             --silent \
+             --data "<?xml version='1.0' encoding='UTF-8'?><request><Index>$INDEX</Index></request>" \
+             http://$MODEM_IP/api/sms/delete-sms
+    done
+}
+
+log_temperature() {
+    MESSAGE_TYPE=$1
+    TEMPERATURE=$2
+    PRG_DATE=$(date +%Y%m%d)
+    STR_DATE=$(date)
+
+
+    echo "${STR_DATE}: ${MESSAGE_TYPE}: $TEMPERATURE" | tee -a /tmp/temperature-${PRG_DATE}.log
+}
+
+send_sms() {
+    MESSAGE_TYPE=$1
+    TEMPERATURE=$2
+
+
+    MESSAGE="${MESSAGE_TYPE}: temperature is $TEMPERATURE"
+    SMS_TARGETS=""
+    i=0
+    while [ $i -lt $NUMBER_OF_PHONES ]
+    do
+        SMS_TARGETS="${SMS_TARGETS}<Phone>${PHONES[$i]}</Phone>"
+        i=$(( $i + 1 ))
+    done
+
+    curl --silent --request GET "http://$MODEM_IP/api/webserver/SesTokInfo" >ses_tok.xml
+
+    SESSION_ID=$(sed 's|^.*<SesInfo>\(.*\)</SesInfo>.*$|\1|' <ses_tok.xml)
+    TOKEN=$(sed 's|^.*<TokInfo>\(.*\)</TokInfo>.*$|\1|' <ses_tok.xml)
+
+    curl --header "Cookie: $SESSION_ID" \
+         --header "__RequestVerificationToken: $TOKEN" \
+        --request POST \
+        --silent \
+         --data "<?xml version='1.0' encoding='UTF-8'?><request><Index>-1</Index><Phones>${SMS_TARGETS}</Phones><Sca></Sca><Content>${MESSAGE}</Content><Length>-1</Length><Reserved>1</Reserved><Date>-1</Date></request>" \
+         http://$MODEM_IP/api/sms/send-sms
+}
+
+shutdown_hosts() {
+    echo "shutting down hosts"
+}
+
+
+TEMPERATURE=$(ipmitool -H $IMM_FQDN -I open -P pwd sensor get 'Ambient Temp' \
+              | grep 'Sensor Reading' \
+              | sed 's/^ Sensor Reading        : \([^ ]*\) (+\/- .) degrees C$/\1/')
+
+MESSAGE_TYPE="Info"
+SEVERITY=0
+
+if [ $TEMPERATURE -ge $WARNING_THRESHOLD ]
+then
+    MESSAGE_TYPE="Warning"
+    SEVERITY=1
+fi
+
+if [ $TEMPERATURE -ge $ERROR_THRESHOLD ]
+then
+    MESSAGE_TYPE="Error"
+    SEVERITY=2
+fi
+
+if [ $TEMPERATURE -ge $CRITICAL_THRESHOLD ]
+then
+    MESSAGE_TYPE="Critical"
+    SEVERITY=3
+fi
+
+log_temperature $MESSAGE_TYPE $TEMPERATURE
+
+if [ -f $SCRIPT_STOP ]
+then
+    exit 0
+fi
+
+MINUTE_LASTDIGIT=$(date "+%M" | cut -c 2)
+case "$MINUTE_LASTDIGIT" in
+    "1" | "3" | "5" | "7" | "9")
+        PARITY=1
+        ;;
+    "0" | "2" | "4" | "6" | "8")
+        PARITY=2
+        ;;
+esac
+
+if [ "$PARITY" != "$HOST_SERIAL" ]
+then
+    exit 0
+fi
+
+if [ $SEVERITY -gt 0 ]
+then
+    send_sms $MESSAGE_TYPE $TEMPERATURE
+fi
+if [ $SEVERITY -gt 1 ]
+then
+    shutdown_hosts
+fi
+delete_logs
+delete_mail
+delete_sent_smses
+delete_received_smses
diff --git a/sources/vhost2.usr/root/temperature.cfg b/sources/vhost2.usr/root/temperature.cfg
new file mode 100644 (file)
index 0000000..30ad422
--- /dev/null
@@ -0,0 +1,13 @@
+MODEM_IP="192.168.8.1"
+#        zfelleg        fschnell       rrendek        mszabo
+PHONES=("+36209541513" "+36309502529" "+36309775888" "+36704581234")
+NUMBER_OF_PHONES=4
+WARNING_PHONES=("+36209541513" "+36309502529" "+36309775888" "+36704581234")
+ERROR_PHONES=("+36209541513" "+36309502529" "+36309775888" "+36704581234")
+CRITICAL_PHONES=("+36209541513" "+36309502529" "+36309775888" "+36704581234")
+HEARTBEAT_PHONES=("+36303744065" "+36309502529" "+36309775888" "+36704581234")
+WARNING_THRESHOLD=25
+ERROR_THRESHOLD=30
+CRITICAL_THRESHOLD=35
+HOST_SERIAL=$(hostname | cut -c 6)
+IMM_FQDN="vhost${HOST_SERIAL}imm.usr.user.hu"
diff --git a/sources/vhost2.usr/root/temperature.sh b/sources/vhost2.usr/root/temperature.sh
new file mode 100755 (executable)
index 0000000..9dc4de7
--- /dev/null
@@ -0,0 +1,196 @@
+#!/bin/sh
+
+
+SCRIPT_PATH=$(dirname $(realpath $0))
+SCRIPT_BASE=$(basename $(realpath $0) .sh)
+SCRIPT_CONFIG=$SCRIPT_PATH/${SCRIPT_BASE}.cfg
+SCRIPT_STOP=$SCRIPT_PATH/${SCRIPT_BASE}.stop
+
+
+source $SCRIPT_CONFIG
+
+
+delete_logs() {
+    LOG_COUNT=$(ls /tmp/temperature-????????.log | wc -l)
+    if [ $LOG_COUNT -gt 14 ]
+    then
+        FIRST_LOG=$(ls /tmp/temperature-????????.log | head -n 1)
+        rm -f $FIRST_LOG
+    fi
+}
+
+delete_mail() {
+    echo 'delete (from "Cron Daemon")' | mail -N
+}
+
+delete_received_smses() {
+    curl --silent --request GET "http://$MODEM_IP/api/webserver/SesTokInfo" >ses_tok.xml
+
+    SESSION_ID=$(sed 's|^.*<SesInfo>\(.*\)</SesInfo>.*$|\1|' <ses_tok.xml)
+    TOKEN=$(sed 's|^.*<TokInfo>\(.*\)</TokInfo>.*$|\1|' <ses_tok.xml)
+
+    # BoxType:
+    #   1: received
+    #   2: sent
+    curl --header "Cookie: $SESSION_ID" \
+         --header "__RequestVerificationToken: $TOKEN" \
+         --request POST \
+         --silent \
+         --data "<?xml version='1.0' encoding='UTF-8'?><request><PageIndex>1</PageIndex><ReadCount>20</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>" \
+         http://$MODEM_IP/api/sms/sms-list \
+         | grep '<Index>' \
+         | while read LINE
+    do
+        INDEX=$(echo $LINE | sed 's|^ *<Index>\([0-9]*\)</Index> *$|\1|')
+
+        curl --silent --request GET "http://$MODEM_IP/api/webserver/SesTokInfo" >ses_tok.xml
+
+        SESSION_ID=$(sed 's|^.*<SesInfo>\(.*\)</SesInfo>.*$|\1|' <ses_tok.xml)
+        TOKEN=$(sed 's|^.*<TokInfo>\(.*\)</TokInfo>.*$|\1|' <ses_tok.xml)
+
+        curl --header "Cookie: $SESSION_ID" \
+             --header "__RequestVerificationToken: $TOKEN" \
+             --request POST \
+             --silent \
+             --data "<?xml version='1.0' encoding='UTF-8'?><request><Index>$INDEX</Index></request>" \
+             http://$MODEM_IP/api/sms/delete-sms
+
+       touch $SCRIPT_STOP
+    done
+}
+
+delete_sent_smses() {
+    curl --silent --request GET "http://$MODEM_IP/api/webserver/SesTokInfo" >ses_tok.xml
+
+    SESSION_ID=$(sed 's|^.*<SesInfo>\(.*\)</SesInfo>.*$|\1|' <ses_tok.xml)
+    TOKEN=$(sed 's|^.*<TokInfo>\(.*\)</TokInfo>.*$|\1|' <ses_tok.xml)
+
+    # BoxType:
+    #   1: received
+    #   2: sent
+    curl --header "Cookie: $SESSION_ID" \
+         --header "__RequestVerificationToken: $TOKEN" \
+         --request POST \
+         --silent \
+         --data "<?xml version='1.0' encoding='UTF-8'?><request><PageIndex>1</PageIndex><ReadCount>20</ReadCount><BoxType>2</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>" \
+         http://$MODEM_IP/api/sms/sms-list \
+         | grep '<Index>' \
+         | while read LINE
+    do
+        INDEX=$(echo $LINE | sed 's|^ *<Index>\([0-9]*\)</Index> *$|\1|')
+
+        curl --silent --request GET "http://$MODEM_IP/api/webserver/SesTokInfo" >ses_tok.xml
+
+        SESSION_ID=$(sed 's|^.*<SesInfo>\(.*\)</SesInfo>.*$|\1|' <ses_tok.xml)
+        TOKEN=$(sed 's|^.*<TokInfo>\(.*\)</TokInfo>.*$|\1|' <ses_tok.xml)
+
+        curl --header "Cookie: $SESSION_ID" \
+             --header "__RequestVerificationToken: $TOKEN" \
+             --request POST \
+             --silent \
+             --data "<?xml version='1.0' encoding='UTF-8'?><request><Index>$INDEX</Index></request>" \
+             http://$MODEM_IP/api/sms/delete-sms
+    done
+}
+
+log_temperature() {
+    MESSAGE_TYPE=$1
+    TEMPERATURE=$2
+    PRG_DATE=$(date +%Y%m%d)
+    STR_DATE=$(date)
+
+
+    echo "${STR_DATE}: ${MESSAGE_TYPE}: $TEMPERATURE" | tee -a /tmp/temperature-${PRG_DATE}.log
+}
+
+send_sms() {
+    MESSAGE_TYPE=$1
+    TEMPERATURE=$2
+
+
+    MESSAGE="${MESSAGE_TYPE}: temperature is $TEMPERATURE"
+    SMS_TARGETS=""
+    i=0
+    while [ $i -lt $NUMBER_OF_PHONES ]
+    do
+        SMS_TARGETS="${SMS_TARGETS}<Phone>${PHONES[$i]}</Phone>"
+        i=$(( $i + 1 ))
+    done
+
+    curl --silent --request GET "http://$MODEM_IP/api/webserver/SesTokInfo" >ses_tok.xml
+
+    SESSION_ID=$(sed 's|^.*<SesInfo>\(.*\)</SesInfo>.*$|\1|' <ses_tok.xml)
+    TOKEN=$(sed 's|^.*<TokInfo>\(.*\)</TokInfo>.*$|\1|' <ses_tok.xml)
+
+    curl --header "Cookie: $SESSION_ID" \
+         --header "__RequestVerificationToken: $TOKEN" \
+        --request POST \
+        --silent \
+         --data "<?xml version='1.0' encoding='UTF-8'?><request><Index>-1</Index><Phones>${SMS_TARGETS}</Phones><Sca></Sca><Content>${MESSAGE}</Content><Length>-1</Length><Reserved>1</Reserved><Date>-1</Date></request>" \
+         http://$MODEM_IP/api/sms/send-sms
+}
+
+shutdown_hosts() {
+    echo "shutting down hosts"
+}
+
+
+TEMPERATURE=$(ipmitool -H $IMM_FQDN -I open -P pwd sensor get 'Ambient Temp' \
+              | grep 'Sensor Reading' \
+              | sed 's/^ Sensor Reading        : \([^ ]*\) (+\/- .) degrees C$/\1/')
+
+MESSAGE_TYPE="Info"
+SEVERITY=0
+
+if [ $TEMPERATURE -ge $WARNING_THRESHOLD ]
+then
+    MESSAGE_TYPE="Warning"
+    SEVERITY=1
+fi
+
+if [ $TEMPERATURE -ge $ERROR_THRESHOLD ]
+then
+    MESSAGE_TYPE="Error"
+    SEVERITY=2
+fi
+
+if [ $TEMPERATURE -ge $CRITICAL_THRESHOLD ]
+then
+    MESSAGE_TYPE="Critical"
+    SEVERITY=3
+fi
+
+log_temperature $MESSAGE_TYPE $TEMPERATURE
+
+if [ -f $SCRIPT_STOP ]
+then
+    exit 0
+fi
+
+MINUTE_LASTDIGIT=$(date "+%M" | cut -c 2)
+case "$MINUTE_LASTDIGIT" in
+    "1" | "3" | "5" | "7" | "9")
+        PARITY=1
+        ;;
+    "0" | "2" | "4" | "6" | "8")
+        PARITY=2
+        ;;
+esac
+
+if [ "$PARITY" != "$HOST_SERIAL" ]
+then
+    exit 0
+fi
+
+if [ $SEVERITY -gt 0 ]
+then
+    send_sms $MESSAGE_TYPE $TEMPERATURE
+fi
+if [ $SEVERITY -gt 1 ]
+then
+    shutdown_hosts
+fi
+delete_logs
+delete_mail
+delete_sent_smses
+delete_received_smses