Select Git revision

David Beniamine authored
Monit.sh 2.16 KiB
#!/bin/bash
echo "RUN $(basename "$0") ==="
DIR=$(realpath $(dirname $0)/..)
if [ ! -e $DIR/main.env ]
then
echo "Please copy in racine of NoCloud Auto Installer main.env.sample to main.env and edit it"
exit 1
else
echo "Environment file loaded"
fi
. $DIR/main.env
# Install monit if not installed and general config
if [ -z "$(dpkg -l | grep ' monit ')" ]; then
## Install
apt-get update && apt-get install -y monit
## General config
sed -i 's/#set httpd/set httpd/' /etc/monit/monitrc
sed -i 's/^# allow localhost/ allow localhost/' /etc/monit/monitrc
echo -e "set mailserver 127.0.0.1\nset mail-format { from: Monit <monit@$DOMAIN>}\nset alert root@localhost" >> /etc/monit/monitrc
sed -i 's/^\(set alert [^ ]*\).*$/\1 with reminder on 720 cycles/' /etc/monit/monitrc
fi
# Disk config
## Monit all partition without swap and boot
list_of_part=(`grep -v "#\|swap\|noauto" /etc/fstab | awk '{print $2}'`)
echo "" > ./$$-parts
for part in "${list_of_part[@]}"
do
name=$(echo $part | sed -e 's@^/$@root@' -e 's@^/@@')
echo -e "check device $name with path $part\n if SPACE usage > echo ${MEM_OVERLOAD_PERCENT:-75}% then alert\n\n" >> ./$$-parts
done
mv ./$$-parts /etc/monit/conf-available/disc
ln -s /etc/monit/conf-available/disc /etc/monit/conf-enabled/
# CPU config
contentCPU='check system $HOST-cpu
start program = "/etc/init.d/memcached start"
stop program = "/etc/init.d/memcached stop"
if cpu > 75% for '"`echo ${CPU_OVERLOAD_TIME:-30}`"' cycles then alert'
## write in file
echo "$contentCPU" > /etc/monit/conf-available/cpu
ln -s /etc/monit/conf-available/cpu /etc/monit/conf-enabled
# Ram config
if [ -z "$(dpkg -l | grep libmemcached-tools)" ]; then
apt-get update && apt-get install -y install libmemcached-tools
fi
totalmem=`free --mega | grep Mem | awk '{print $2}'`
usemax=$(($totalmem * `echo ${MEM_OVERLOAD_PERCENT:-75}` / 100))
contentMEM='check system $HOST-mem
if memory > '"$usemax"' MB for '"`echo ${MEM_OVERLOAD_TIME:-30}`"' cycles then alert'
## write in file
echo "$contentMEM" > /etc/monit/conf-available/memory
ln -s /etc/monit/conf-available/memory /etc/monit/conf-enabled
systemctl restart monit
monit status