A simple script to notify Zabbix alert on Matrix
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

134 lines
4.4 KiB

#!/bin/bash
## author : Dryusdan
## date : 27/12/2019
## description : A Matrix Zabbix alerting
## usage : ./matrix-zabbix-alerting.sh SENDTO SUBJECT MESSAGE
## Bash strict mode ####################################
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
## Bash color ##########################################
# Set colors
RED='\033[0;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'
LIGHTGRAY='\033[00;37m'
LRED='\033[01;31m'
LGREEN='\033[01;32m'
LYELLOW='\033[01;33m'
LBLUE='\033[01;34m'
LPURPLE='\033[01;35m'
LCYAN='\033[01;36m'
WHITE='\033[01;37m'
NC='\033[0m' # No Color
## Logs ################################################
readonly SCRIPTNAME="$(basename "$0")"
info() { echo -e "${LBLUE}[INFO] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
warning() { echo -e "${YELLOW}[WARNING] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
error() { echo -e "${LRED}[ERROR] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
fatal() { echo -e "${RED}[FATAL] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; exit 1 ; }
########################################################
info "Setting config file path"
CONFIG_FILE="/etc/matrix-zabbix-alerting.ini"
## Message color HTML ##################################
info "Setting color vars from ${CONFIG_FILE}"
COLOR[0]=$(awk -F "=" '/not_classified/ {print $2}' ${CONFIG_FILE} | head -n 1 )
COLOR[1]=$(awk -F "=" '/information/ {print $2}' ${CONFIG_FILE} | head -n 1 )
COLOR[2]=$(awk -F "=" '/warning/ {print $2}' ${CONFIG_FILE} | head -n 1 )
COLOR[3]=$(awk -F "=" '/average/ {print $2}' ${CONFIG_FILE} | head -n 1 )
COLOR[4]=$(awk -F "=" '/high/ {print $2}' ${CONFIG_FILE} | head -n 1 )
COLOR[5]=$(awk -F "=" '/disaster/ {print $2}' ${CONFIG_FILE} | head -n 1 )
COLOR[6]=$(awk -F "=" '/resolved/ {print $2}' ${CONFIG_FILE} | head -n 1 )
########################################################
## Message Emoji #######################################
info "Setting emoji vars from ${CONFIG_FILE}"
EMOJI[0]=$(awk -F "=" '/not_classified/ {print $2}' ${CONFIG_FILE} | tail -1 )
EMOJI[1]=$(awk -F "=" '/information/ {print $2}' ${CONFIG_FILE} | tail -1 )
EMOJI[2]=$(awk -F "=" '/warning/ {print $2}' ${CONFIG_FILE} | tail -1 )
EMOJI[3]=$(awk -F "=" '/average/ {print $2}' ${CONFIG_FILE} | tail -1 )
EMOJI[4]=$(awk -F "=" '/high/ {print $2}' ${CONFIG_FILE} | tail -1 )
EMOJI[5]=$(awk -F "=" '/disaster/ {print $2}' ${CONFIG_FILE} | tail -1 )
EMOJI[6]=$(awk -F "=" '/resolved/ {print $2}' ${CONFIG_FILE} | tail -1 )
########################################################
## Variables ###########################################
info "Setting Matrix vars from ${CONFIG_FILE}"
ROOM=$(awk -F "=" '/room/ {print $2}' ${CONFIG_FILE})
MATRIX_SERVER=$(awk -F "=" '/domain/ {print $2}' ${CONFIG_FILE})
MATRIX_PORT=$(awk -F "=" '/port/ {print $2}' ${CONFIG_FILE})
TOKEN=$(awk -F "=" '/token/ {print $2}' ${CONFIG_FILE})
info "Setting data from Zabbix parameters"
SENDTO=${1}
SUBJECT=${2}
MESSAGE=${3}
########################################################
## Functions ###########################################
generate_post_data()
{
cat <<EOF
{
"msgtype": "m.text",
"body": "${SUBJECT}",
"format": "org.matrix.custom.html",
"formatted_body": "<p style='color=${COLOR[${ALERT_LEVEL}]}'>${EMOJI[${ALERT_LEVEL}]} <font color='${COLOR[${ALERT_LEVEL}]}'>${SUBJECT}</font></p>"
}
EOF
}
########################################################
info "Define severity level"
if echo "${MESSAGE}" | grep -q "Not classified"
then
ALERT_LEVEL=0
else
if echo ${MESSAGE} | grep -q "Information"
then
ALERT_LEVEL=1
else
if echo ${MESSAGE} | grep -q "Warning"
then
ALERT_LEVEL=2
else
if echo ${MESSAGE} | grep -q "Average"
then
ALERT_LEVEL=3
else
if echo ${MESSAGE} | grep -q "High"
then
ALERT_LEVEL=4
else
if echo ${MESSAGE} | grep -q "Disaster"
then
ALERT_LEVEL=5
else
ALERT_LEVEL=0
fi
fi
fi
fi
fi
fi
if echo ${SUBJECT} | grep -q "Resolved"
then
ALERT_LEVEL=6
fi
info "Post message"
curl -H "Authorization: Bearer ${TOKEN}" -X POST -d "$(generate_post_data)" "https://"${MATRIX_SERVER}":"${MATRIX_PORT}"/_matrix/client/r0/rooms/%21"${ROOM}":"${MATRIX_SERVER}"/send/m.room.message"
########################################################
info "exiting ${SCRIPTNAME}"
exit 0