uptimeatomic/uptimeatomic

327 lines
9.3 KiB
Bash
Executable File

#!/bin/ksh
#
# Uptime Atomic v20231021
# https://code.high5.nl/High5/uptimeatomic
#
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
CONFIGFILE="uptimeatomic.conf"
WORKDIR=$(dirname $0)
usage() {
echo "usage: ${0##*/} [-c checksfile] [-i incidentsfile] [-p pastincidentsfile] [-o htmlfile]" 1>&2
exit
}
date_rss() {
if [ -n "${1}" ]; then
date -ju '+%a, %d %b %Y %H:%M:%S %z' ${1}
else
date -ju '+%a, %d %b %Y %H:%M:%S %z'
fi
}
date_incident() {
if [ -n "${1}" ]; then
date -ju '+%F %H:%M %Z' ${1}
else
date -ju '+%F %H:%M %Z'
fi
}
get_element() {
echo "${2}" | awk -v col="${1}" -F',' '{gsub(/^[ \t]+|[ \t]+$/, "", $col); print $col}'
}
incidents() {
_date=${1}
_type=${2}
_description=${3}
_file=${4}
[[ ${_type} == "Incident" ]] && color="failed"
[[ ${_type} == "Maintenance" ]] && color="maint"
if [ ${_file} == "${PASTINCIDENTSFILE}" ]; then
echo "<p>$(date_incident ${_date}) - <b class='${color}'>${_type}</b><br />${_description}</p>" >> ${_PASTHTMLFILE}
else
echo "<p>$(date_incident ${_date}) - <b class='${color}'>${_type}</b><br />${_description}</p>" >> ${_HTMLFILE}
fi
if [ -n "${_RSSFILE}" ]; then
cat << EOF >> ${_RSSFILE}
<item>
<title>${_type}</title>
<link>${RSS_URL}/${RSSFILE}</link>
<guid>${RSS_URL}/${RSSFILE}?$(date -ju +%s $(echo "${_date}"))</guid>
<pubDate>$(date_rss "${_date}")</pubDate>
<description><![CDATA[ ${_description} ]]></description>
</item>
EOF
fi
}
notify() {
_name="${1}"
_status="${2}"
_priority="${3}"
if [ ${_priority} == "ko" ]; then
echo "${_status}." | mail -r "${TITLE} <${SENDER}>" -s "${_name} DOWN" ${RECIPIENT}
${PUSHOVER} -c ${PUSHOVER_CONF} -t "${TITLE}" -m "${_name} DOWN ${_status}" -p 1 >/dev/null 2>&1
touch "${PUSHOVER_STATUS}/${_name}"
fi
if [ ${_priority} == "ok" ]; then
_seconds=$(expr $(date -ju +%s) - $(stat -r "${PUSHOVER_STATUS}/${_name}" | awk '{print $11}'))
_downtime=$(date -jur ${_seconds} +%H:%M:%S)
echo "${_status} - down for ${_downtime}" | mail -r "${TITLE} <${SENDER}>" -s "${_name} OK" ${RECIPIENT}
${PUSHOVER} -c ${PUSHOVER_CONF} -t "${TITLE}" -m "${_name} OK ${_status} - down for ${_downtime}" >/dev/null 2>&1
rm -rf "${PUSHOVER_STATUS}/${_name}"
fi
}
check() {
_ctype="${1}"
_expectedcode="${2}"
_name="${3}"
_host="${4}"
_timeout="${5}"
IPv="${_ctype#(http|ping|port)}"
[[ -n "${_timeout}" ]] && TIMEOUT=${_timeout}
case "${_ctype}" in
http*)
statuscode="$(curl -${IPv}sSkLo /dev/null -H "${USERAGENT}" -m "${TIMEOUT}" -w "%{http_code}" "${_host}" 2> "${_TMP}/ko/${_name}.error")"
if [ "${statuscode}" -ne "${_expectedcode}" ]; then
if [ -s "${_TMP}/ko/${_name}.error" ]; then
sed -e 's,curl: ([0-9]*) ,,' "${_TMP}/ko/${_name}.error" > "${_TMP}/ko/${_name}.status"
else
echo "Status code: ${statuscode}" > "${_TMP}/ko/${_name}.status"
fi
else
echo "Status code: ${statuscode}" > "${_TMP}/ok/${_name}.status"
fi
;;
ping*)
ping -${IPv}w "${TIMEOUT}" -c 3 "${_host}" >/dev/null 2>&1
statuscode=$?
if [ "${statuscode}" -ne "${_expectedcode}" ]; then
echo "Host unreachable" > "${_TMP}/ko/${_name}.status"
else
echo "Host reachable" > "${_TMP}/ok/${_name}.status"
fi
;;
port*)
error="$(nc -${IPv}w "${TIMEOUT}" -zv ${_host} 2>&1)"
statuscode=$?
if [ "${statuscode}" -ne "${_expectedcode}" ]; then
echo "Connection refused" > "${_TMP}/ko/${_name}.status"
else
echo "Connection succeeded" > "${_TMP}/ok/${_name}.status"
fi
;;
maint*)
echo "Maintenance" > "${_TMP}/maint/${_name}.status"
;;
esac
}
process_status() {
_status_files=${1}
ls ${_TMP}/${_status_files}/*.status 2>/dev/null | sort -V | while read file; do
[ -e "${file}" ] || continue
_name="$(basename "${file%.status}")"
_status="$(cat "${file}")"
if [ ${_status_files} == "ko" ]; then
echo "<li>${_name} <span class='small failed'>(${_status})</span><span class='status failed'>Disrupted</span></li>" >> ${_HTMLFILE}
if [ ! -e "${PUSHOVER_STATUS}/${_name}" ]; then
notify "${_name}" "${_status}" "${_status_files}"
fi
fi
if [ ${_status_files} == "maint" ]; then
echo "<li>${_name} <span class='status maint'>Maintenance</span></li>" >> ${_HTMLFILE}
fi
if [ ${_status_files} == "ok" ]; then
echo "<li>${_name} <span class='status success'>Operational</span></li>" >> ${_HTMLFILE}
if [ -e "${PUSHOVER_STATUS}/${_name}" ]; then
notify "${_name}" "${_status}" "${_status_files}"
fi
fi
done
}
parse_file() {
_file=${1}
while IFS="$(printf '\n')" read -r line; do
_col1="$(get_element 1 "${line}")"
_col2="$(get_element 2 "${line}")"
_col3="$(get_element 3 "${line}")"
_col4="$(get_element 4 "${line}")"
_col5="$(get_element 5 "${line}")"
if [[ ${_file} == *".csv" ]]; then
check "${_col1}" "${_col2}" "${_col3}" "${_col4}" "${_col5}" &
else
incidents "${_col1}" "${_col2}" "${_col3}" "${_file}"
fi
done < "${_file}"
wait
}
cd ${WORKDIR}
if [ -s "${CONFIGFILE}" ]; then
. ${WORKDIR}/${CONFIGFILE}
else
echo "Config ${WORKDIR}/${CONFIGFILE} doesn't exist."
exit
fi
while getopts c:i:o:r:h arg; do
case ${arg} in
c) CHECKFILE=${OPTARG};;
i) INCIDENTSFILE=${OPTARG};;
p) PASTINCIDENTSFILE=${OPTARG};;
o) HTMLFILE=${OPTARG};;
r) RSSFILE=${OPTARG};;
h) usage;;
*) usage;;
esac
done
if [ ! -e "${CHECKFILE}" ]; then
echo "Checkfile ${WORKDIR}/${CHECKFILE} doesn't exist."
exit
fi
_TMP="$(mktemp -d)"
mkdir -p "${_TMP}/ok" "${_TMP}/ko" "${_TMP}/maint" || exit 1
_HTMLFILE="${_TMP}/${HTMLFILE}"
_PASTHTMLFILE="${_TMP}/${PASTHTMLFILE}"
[[ -n "${RSSFILE}" ]] && _RSSFILE="${_TMP}/${RSSFILE}"
parse_file "${CHECKFILE}"
#
# HTML
#
cat << EOF | tee ${_HTMLFILE} | tee ${_PASTHTMLFILE} >/dev/null
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="${REFRESH}">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>${TITLE}</title>
<style>
body { font-family: segoe ui,Roboto,Oxygen-Sans,Ubuntu,Cantarell,helvetica neue,Verdana,sans-serif; }
h1 { margin-top: 30px; }
ul { padding: 0px; }
li { list-style: none; margin-bottom: 2px; padding: 5px; border-bottom: 1px solid #ddd; }
.container { max-width: 600px; width: 100%; margin: 15px auto; }
.panel { text-align: center; padding: 10px; border: 0px; border-radius: 5px; }
.failed-bg { color: white; background-color: #E25D6A; }
.success-bg { color: white; background-color: #52B86A; }
.maint-bg { color: white; background-color: #5DADE2; }
.failed { color: #E25D6A; }
.success { color: #52B86A; }
.maint { color: #5DADE2; }
.small { font-size: 80%; }
.status { float: right; }
@media (prefers-color-scheme: dark) {
a, a:link, a:visited { color: #ffe489; }
body, html { background-color: #111111; color: #888888; }
pre { background-color: #111111; color: #aaaaaa; }
li { border-bottom: 1px solid #333333; }
.clean { background-color: #111111; color: #aaaaaa; }
}
</style>
</head>
<body>
<div class='container'>
<h1>${HEADER}</h1>
EOF
#
# RSS
#
if [ -n "${_RSSFILE}" ]; then
cat << EOF > ${_RSSFILE}
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="${RSS_URL}/${RSSFILE}" rel="self" type="application/rss+xml" />
<title>${TITLE}</title>
<description>${RSS_DESCRIPTION}</description>
<link>${RSS_URL}</link>
<lastBuildDate>$(date_rss)</lastBuildDate>
EOF
fi
_outage_count="$(find "${_TMP}/ko" -mindepth 1 | grep -c 'status$')"
_maint_count="$(find "${_TMP}/maint" -mindepth 1 | grep -c 'status$')"
if [ "${_outage_count}" -ne 0 ]; then
echo "<ul><li class='panel failed-bg'>${_outage_count} Outage(s)</li></ul>" >> ${_HTMLFILE}
fi
if [ "${_maint_count}" -ne 0 ]; then
echo "<ul><li class='panel maint-bg'>${_maint_count} Maintenance</li></ul>" >> ${_HTMLFILE}
fi
if [[ "${_outage_count}" -eq 0 && "${_maint_count}" -eq 0 ]]; then
echo "<ul><li class='panel success-bg'>All Systems Operational</li></ul>" >> ${_HTMLFILE}
fi
if [ -s "${INCIDENTSFILE}" ]; then
echo '<h2>Incidents / Maintenance</h2>' >> ${_HTMLFILE}
parse_file "${INCIDENTSFILE}"
fi
echo "<h1>Services</h1>" >> ${_HTMLFILE}
echo "<ul>" >> ${_HTMLFILE}
process_status "ko"
process_status "maint"
process_status "ok"
echo "</ul>" >> ${_HTMLFILE}
echo "<p class=small>Last check: $(date -ju '+%FT%T %Z')</p>" >> ${_HTMLFILE}
if [ -s "${PASTINCIDENTSFILE}" ]; then
echo '<h2>Past Incidents / Maintenance</h2>' >> ${_PASTHTMLFILE}
parse_file "${PASTINCIDENTSFILE}"
fi
#
# END HTML
#
cat << EOF | tee -a ${_HTMLFILE} | tee -a ${_PASTHTMLFILE} >/dev/null
<p class=small>
<a href="https://git.high5.nl/uptimeatomic/">Uptime Atomic</a> loosely based on <a href="https://github.com/bderenzo/tinystatus">Tinystatus</a>
EOF
[[ -n "${_PASTHTMLFILE}" ]] && echo " - <a href='${PASTHTMLFILE}'>Past Incidents</a>" >> ${_HTMLFILE}
[[ -n "${_RSSFILE}" ]] && echo " - <a href='${RSS_URL}/${RSSFILE}'>RSS</a>" >> ${_HTMLFILE}
cat << EOF | tee -a ${_HTMLFILE} | tee -a ${_PASTHTMLFILE} >/dev/null
</p>
</div>
</body>
</html>
EOF
#
# END RSS
#
if [ -n "${_RSSFILE}" ]; then
cat << EOF >> ${_RSSFILE}
</channel>
</rss>
EOF
fi
if [[ -f "${HTMLDIR}/${RSSFILE}" ]]; then
_diff=$(diff "${_RSSFILE}" "${HTMLDIR}/${RSSFILE}" | wc -l)
if [ "${_diff}" -ne "4" ]; then
cp ${_RSSFILE} ${HTMLDIR}/${RSSFILE}
fi
elif [ -n "${RSSFILE}" ]; then
cp ${_RSSFILE} ${HTMLDIR}/${RSSFILE}
fi
cp ${_HTMLFILE} ${HTMLDIR}/${HTMLFILE}
cp ${_PASTHTMLFILE} ${HTMLDIR}/${PASTHTMLFILE}
rm -r "${_TMP}" 2>/dev/null