206 lines
6.8 KiB
Bash
Executable File
206 lines
6.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# powerwall - CLI commands to control VM guest power
|
|
|
|
source /opt/idssys/defaults/colors.inc
|
|
source /opt/idssys/defaults/default.inc
|
|
source /opt/idssys/dsmon/config.settings.inc
|
|
|
|
action="$1"
|
|
|
|
RUN(){
|
|
if [ "${SERVERMON_ID}" != "" ]; then
|
|
|
|
if [ "${1}" = "hdd" ]; then
|
|
DRIVEINFO=`df -BM | grep -vE '^Filesystem|tmpfs|cdrom|@|ram|loop|udev|veeamimage' | awk '{ print $1 " " $2 " " $4 }'`
|
|
DRIVEINFO=(${DRIVEINFO})
|
|
NUMDRIVES=$((${#DRIVEINFO[@]} / 3))
|
|
|
|
DRIVE_INFO=()
|
|
|
|
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}" = "shm" ] && [ ${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}"
|
|
DRIVE_INFO+=("${ia};${ib};${ic}")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
QRY="USE servermonitor; INSERT INTO sysinfo (\`host\`, \`entry\`, \`value\`) VALUES ('${SERVERMON_ID}','hdd','${DRIVE_INFO[@]}');"
|
|
MYSQL_PWD="sysmoninsert" mysql -h mysqldb.scity.us -P 6033 -u sysmoninsert -e "${QRY}"
|
|
|
|
elif [ "${1}" = "sys" ]; then
|
|
|
|
MEMORY=$(free -m | awk 'NR==2{printf "%.2f\t\t", $3*100/$2 }')
|
|
CPU=$(top -bn1 | grep load | awk '{printf "%.2f\t\t\n", $(NF-2)}')
|
|
QRY="USE servermonitor; INSERT INTO sysinfo (\`host\`, \`entry\`, \`value\`) VALUES ('${SERVERMON_ID}','sys','${CPU// /};${MEMORY}');"
|
|
MYSQL_PWD="sysmoninsert" mysql -h mysqldb.scity.us -P 3306 -u sysmoninsert -e "${QRY}"
|
|
fi
|
|
|
|
SERVERHOSTNAME=(`hostname`)
|
|
SERVERIP=(`hostname -I`)
|
|
SERVERIPS=''
|
|
for nip in "${SERVERIP[@]}"; do
|
|
if [[ "${nip}" == *"."* ]]; then
|
|
if [ "${SERVERIPS}" = "" ]; then
|
|
SERVERIPS=${nip}
|
|
else
|
|
SERVERIPS=${SERVERIPS}";"${nip}
|
|
fi
|
|
fi
|
|
done
|
|
|
|
thresholdlog=()
|
|
for KEY in "${!THRESHOLD[@]}"; do
|
|
thresholdlog+=("${KEY}:${THRESHOLD[$KEY]}")
|
|
done
|
|
|
|
QRY="USE servermonitor; UPDATE hosts SET ip='${SERVERIPS// /}',hostname='${SERVERHOSTNAME}',limits='${thresholdlog[@]}' WHERE id='${SERVERMON_ID}';"
|
|
mysql -h mysqldb.scity.us -P 3306 --user=sysmoninsert --password=sysmoninsert -e "${QRY}"
|
|
|
|
|
|
fi
|
|
}
|
|
|
|
CHECK(){
|
|
|
|
declare -A host_ip
|
|
declare -A host_name
|
|
declare -A host_limits
|
|
declare -A host_ids
|
|
declare -a hostnames_sort
|
|
|
|
while read hostid hostname hostip hostlimits; do
|
|
if [ "$hostid" != "id" ]; then
|
|
hostip=$(echo $hostip | cut -d ";" -f1)
|
|
# echo "$hostid - $hostip - $hostname - $hostlimits"
|
|
host_ip[${hostid}]=$hostip
|
|
host_name[${hostid}]=$hostname
|
|
host_limits[${hostid}]=$hostlimits
|
|
host_ids[$hostname]=$hostid
|
|
hostnames_sort+=("$hostname")
|
|
fi
|
|
done <<< $(MYSQL_PWD="sysmoninsert" mysql -h mysqldb.scity.us -P 3306 -u sysmoninsert -e "SELECT id,host,ip,limits FROM servermonitor.hosts")
|
|
|
|
IFS=$'\n' hostnames_sort=($(sort <<<"${hostnames_sort[*]}"))
|
|
unset IFS
|
|
|
|
#for hostid in "${!host_ip[@]}"; do
|
|
for hostname in "${hostnames_sort[@]}"; do
|
|
hostid=${host_ids[$hostname]}
|
|
echo -e "${idsST[Bold]}"; DIVIDER
|
|
echo -e "${idsCL[LightCyan]} ${host_name[$hostid]} - Drive Space Check${idsCL[Default]}"
|
|
DIVIDER; echo -e "${idsST[Reset]}"
|
|
|
|
checkhost=$(CHECK_HOST ${host_ip[$hostid]})
|
|
if [ "${checkhost}" != "false" ]; then
|
|
DRIVEINFO=$(ssh root@${host_ip[$hostid]} df -BM | grep -vE '^Filesystem|tmpfs|cdrom|@|ram|loop|udev|veeamimage|nvme|localhost|shm|mmcblk|overlay|-volume|Music|media-data|Software' | awk '{ print $1 " " $2 " " $4 }')
|
|
DRIVEINFO=(${DRIVEINFO})
|
|
NUMDRIVES=$((${#DRIVEINFO[@]} / 3))
|
|
|
|
for ((i = 0 ; i <= $((${NUMDRIVES}-1)) ; i++)); do
|
|
ii=$((${i}*3))
|
|
dname=${DRIVEINFO[${ii}]}
|
|
dtot=$(bc <<< "scale=2; ${DRIVEINFO[$((${ii}+1))]}/1024/10")
|
|
dfree=$(bc <<< "scale=2; ${DRIVEINFO[$((${ii}+2))]}/1024/10")
|
|
dfreeper=$(printf "%0.2f" $(jq -n ${dfree}/${dtot}*100))
|
|
dname=${dname//\/dev\/mapper\//}
|
|
dname=${dname//\/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}" = "shm" ] && [ ${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}"
|
|
DRIVE_INFO+=("${ia};${ib};${ic}")
|
|
|
|
fi
|
|
|
|
if (( $(bc <<<"$dfreeper <= 15") )); then
|
|
fs_status='error'
|
|
fs_status_color='Red'
|
|
if [ "${ALERT_EMAIL}" != "" ]; then echo -e "${host_name[$hostid]} - (${host_ip[$hostid]})\n${dname} : ${dfree} GB free\t$(date)" | mail -s "Free Space Critical: '${host_name[$hostid]}'" ${ALERT_EMAIL}; fi
|
|
|
|
elif (( $(bc <<<"$dfreeper <= 25") )); then
|
|
fs_status='warn'
|
|
fs_status_color='Yellow'
|
|
if [ "${ALERT_EMAIL}" != "" ]; then echo -e "${host_name[$hostid]} - (${host_ip[$hostid]})\n${dname} : ${dfree} GB free\t$(date)" | mail -s "Free Space Warning: '${host_name[$hostid]}'" ${ALERT_EMAIL}; fi
|
|
|
|
else
|
|
fs_status=''
|
|
fs_status_color='Green'
|
|
fi
|
|
|
|
c=0; cw=40; spcA=''
|
|
spc1=`expr ${cw} - ${#dname}`
|
|
until [ $c = ${spc1} ]; do spcA="${spcA} "; c=`expr $c + 1`; done
|
|
|
|
c=0; cw=12; spcB=''
|
|
spc2=`expr ${cw} - ${#dfree}`
|
|
until [ $c = ${spc2} ]; do spcB="${spcB} "; c=`expr $c + 1`; done
|
|
|
|
echo -e "${idsCL[$fs_status_color]} ${dname}${spcA}: ${dfree}GB Free${spcB}- ${dfreeper}% -${idsCL[Default]}"
|
|
|
|
done
|
|
|
|
else
|
|
echo -e "${idsCL[Yellow]} ${host_name[$hostid]} is down${idsCL[Default]}"
|
|
fi
|
|
|
|
done
|
|
}
|
|
|
|
SETUPSSH(){
|
|
declare -A host_ip
|
|
declare -A host_name
|
|
|
|
while read hostid hostname hostip hostlimits; do
|
|
if [ "$hostid" != "id" ]; then
|
|
host_ip[${hostid}]=$(echo $hostip | cut -d ";" -f1)
|
|
host_name[${hostid}]=$hostname
|
|
fi
|
|
done <<< $(MYSQL_PWD="sysmoninsert" mysql -h mysqldb.scity.us -P 3306 -u sysmoninsert -e "SELECT id,host,ip,limits FROM servermonitor.hosts")
|
|
|
|
for hostid in "${!host_ip[@]}"; do
|
|
echo -e "${idsST[Bold]}${idsCL[LightCyan]} ${host_name[$hostid]} - SSH KEY COPY${idsCL[Default]}${idsST[Reset]}"
|
|
ssh-copy-id root@${host_ip[$hostid]}
|
|
done
|
|
}
|
|
|
|
|
|
case $action in
|
|
check) CHECK;;
|
|
setupssh) SETUPSSH;;
|
|
run) RUN ${2};;
|
|
update);;
|
|
*) RUN ${action};;
|
|
esac
|
|
|
|
exit 0
|
|
|
|
|