Files
Misc-Scripts/install-dsmon.sh

171 lines
5.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Wrapper script for install, for easier execution via URL.
source /dev/stdin <<< "$(curl -sL http://scity.xyz/colorsinc)"
source /dev/stdin <<< "$(curl -sL http://scity.xyz/defaultinc)"
echo ""
echo -e "${idsCL[LightGreen]}DiskSpace-Monitor Installation Script${idsCL[Default]}"
echo ""
if [ -d "/opt/idssys/dsmon" ]; then
echo -en "${idsCL[Red]}DiskSpace-Monitor is already installed, would you like to re-install (y/n)?${idsCL[Default]}"
read -n 1 reinstall
case $reinstall in
[Yy]) uninstall=$(`bash <(curl -sL https://git.schroedercity.com/voltron/Misc-Scripts/raw/branch/master/uninstall-dsmon.sh)`);;
*) EXIT1;;
esac
fi
do_with_root $APTFUNC install wget curl git mailutils mariadb-client bc
do_with_root set -eu
do_with_root git clone https://git.schroedercity.com/voltron/dsmon.git /opt/idssys/dsmon
do_with_root ln -s /opt/idssys/dsmon/run.sh /usr/local/bin/dsmon
if [ ! -d "/opt/idssys/defaults" ]; then
do_with_root set -eu
do_with_root git clone https://git.schroedercity.com/voltron/iDS-Defaults.git /opt/idssys/defaults
fi
#touch /opt/idssys/dsmon/config.settings.inc
echo -e "declare -A THRESHOLD" > /opt/idssys/dsmon/config.settings.inc
echo ""
DRIVEINFO=`df -BM | grep -vE '^Filesystem|tmpfs|cdrom|@|ram|loop|udev|veeamimage' | awk '{ print $1 " " $2 " " $4 }'`
DRIVEINFO=(${DRIVEINFO})
NUMDRIVES=$((${#DRIVEINFO[@]} / 3))
for ((i = 0 ; i <= $((${NUMDRIVES}-1)) ; i++)); do
ii=$((${i}*3))
ia=${DRIVEINFO[${ii}]}
ib=$(bc <<< "scale=2; ${DRIVEINFO[$((${ii}+1))]//M/}/1024")
ic=$(bc <<< "scale=2; ${DRIVEINFO[$((${ii}+2))]//M/}/1024")
ia=${ia//\/dev\/mapper\//}
ia=${ia//\/dev\//}
if [ "${ia}" = "sda1" ] && [ ${DRIVEINFO[$((${ii}+1))]//M/} -lt 1024 ]; then
systempartition=true
elif [ "${ia}" = "sda2" ] && [ ${DRIVEINFO[$((${ii}+1))]//M/} -lt 1024 ]; then
systempartition=true
elif [ "${ia}" = "nvme0n1p1" ] && [ ${DRIVEINFO[$((${ii}+1))]//M/} -lt 1024 ]; then
systempartition=true
elif [ "${ia}" = "mmcblk0p1" ] && [ ${DRIVEINFO[$((${ii}+1))]//M/} -lt 1024 ]; then
systempartition=true
elif [ "${ia}" != "overlay" ]; then
#echo "${ia};${ib};${ic}"
THRESHOLDSPACE=$(bc <<< "scale=2; ${ib}*.8")
echo -e "${idsCL[LightCyan]}(${ia}) total size: ${idsCL[LightGreen]}${ib}GB${idsCL[Default]}"
echo -e "${idsCL[LightCyan]}Using the default threshold of 80% would alert at: ${idsCL[LightGreen]}${THRESHOLDSPACE}GB${idsCL[Default]}"
echo -e -n "${idsCL[LightCyan]}What threshold should we set for this drive? Default=80: ${idsCL[Default]}"
read SET_THRESHOLD
echo ""
if [ "${SET_THRESHOLD}" = "" ]; then
SET_THRESHOLD=80
fi
echo -e "THRESHOLD['${ia}']=${SET_THRESHOLD}" >> /opt/idssys/dsmon/config.settings.inc
echo ""
fi
done
echo -e -n "${idsCL[LightCyan]}Email address to receive alerts: ${idsCL[Default]}"
read SET_ALERTEMAIL
echo ""
if [ "${SET_ALERTEMAIL}" = "txt" ]; then
SET_ALERTEMAIL='9208839613@txt.att.net'
elif [ "${SET_ALERTEMAIL}" = "mms" ]; then
SET_ALERTEMAIL='9208839613@mms.att.net'
fi
echo -e -n "${idsCL[LightCyan]}Server Monitor ID (optional, or type 'new'): ${idsCL[Default]}"
read SET_SERVERMONID
echo ""
echo -e "${idsCL[Yellow]}Sending test Alert... ${idsCL[Default]}"
echo "Test Alert" | mail -r DSMon_$(hostname -s)@scity.us -s "Test" $SET_ALERTEMAIL
# echo -e "${idsCL[Yellow]}Adding task to crontab... ${idsCL[Default]}"
# (crontab -l ; echo "$(($RANDOM % 15)) */1 * * * /usr/local/bin/dsmon hdd >/dev/null 2>&1")| crontab -
# (crontab -l ; echo "*/5 * * * * /usr/local/bin/dsmon sys >/dev/null 2>&1")| crontab -
if [ "${SET_SERVERMONID}" = "new" ]; then
hostname=`hostname`
echo -e -n "${idsCL[LightCyan]}Server Name (Default = \"${hostname}\"): ${idsCL[Default]}"
read ADD_SERVERNAME
echo ""
if [ "${ADD_SERVERNAME}" = "" ]; then
ADD_SERVERNAME=${hostname}
fi
source /opt/idssys/dsmon/config.settings.inc
ADD_SERVERHOSTNAME=(`hostname`)
ADD_SERVERIP=(`hostname -I`)
ADD_SERVERIPS=''
for nip in "${ADD_SERVERIP[@]}"; do
if [[ "${nip}" == *"."* ]]; then
if [ "${ADD_SERVERIPS}" = "" ]; then
SERVERIPS=${nip}
else
SERVERIPS=${ADD_SERVERIPS}";"${nip}
fi
fi
done
thresholdlog=()
for KEY in "${!THRESHOLD[@]}"; do
thresholdlog+=("${KEY}:${THRESHOLD[$KEY]}")
done
QRY="USE servermonitor; INSERT INTO hosts (\`host\`,\`hostname\`,\`ip\`,\`limits\`) VALUES ('${ADD_SERVERNAME}','${ADD_SERVERHOSTNAME}','${SERVERIPS// /}','${thresholdlog[@]}');"
mysql -h mysqldb.scity.us -P 6033 --user=sysmoninsert --password=sysmoninsert -e "${QRY}"
QRY="USE servermonitor; SELECT id FROM hosts WHERE host='${ADD_SERVERNAME}';"
SET_SERVERMONID=`mysql -h mysqldb.scity.us -P 6033 --user=sysmoninsert --password=sysmoninsert -e "${QRY}"`
echo -en "${idsCL[LightGreen]}Server successfully added, it was given ID# "
echo -e ${SET_SERVERMONID//id
}${idsCL[Default]}
echo ""
fi
echo -e "
ALERT_EMAIL=${SET_ALERTEMAIL}
SERVERMON_ID=${SET_SERVERMONID//id
/}
" >> /opt/idssys/dsmon/config.settings.inc
echo -e "
mail -r DSMon-\$(hostname -s)@scity.us -s 'Disk Space Alert' \$ALERT_EMAIL << EOF
Your root partition remaining free space is running low. Used: \$CURRENT%
EOF
echo \"** Low Disk Space Alert Sent **\" >> /opt/idssys/dsmon/diskspace.log
#Add any additionl system specific actions that should occur when a low disk space alert has been triggered
" > /opt/idssys/dsmon/config.actions.inc
/usr/local/bin/dsmon hdd
/usr/local/bin/dsmon sys
echo ""
echo -e "${idsCL[Yellow]}DiskSpace-Monitor has been Installed. Please make sure this system can send emails using the mail command.${idsCL[Default]}"
echo ""
exit 0