Skip to content

Instantly share code, notes, and snippets.

@binsky08
Last active June 25, 2020 17:13
Show Gist options
  • Save binsky08/fa297394041afba043edc52bc0c46610 to your computer and use it in GitHub Desktop.
Save binsky08/fa297394041afba043edc52bc0c46610 to your computer and use it in GitHub Desktop.
#!/bin/bash
############### configuration
minFreeSpaceGB=3
betterFreeSpaceGB=5
pathToCheckForFreeSpace="$PWD"
enableLog=1
v3MysqlIni="/etc/openitcockpit/mysql.cnf"
#irgnore root rights and mysql configuration file
testing=0
############## initialization
declare -a oks
declare -a warnings=()
declare -a errors=()
declare -a hints=()
okCount=0
hintCount=0
warningCount=0
errorCount=0
okSign=$'\U2713'
warningSign=$'\U26A0'
errorSign=$'\U2718'
hintSign='>'
Red=$'\e[1;31m'
Green=$'\e[1;32m'
Yellow=$'\e[1;33m'
Blue=$'\e[1;34m'
isAptWorking=0
satellitesCount=0
satellitesEntries=""
############# helping methods
if [[ "$(id -u)" != "0" && $testing == 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if [[ ! -f "$v3MysqlIni" && $testing == 0 ]]; then
echo "Error: Could not find v3 database configuration $v3MysqlIni"
exit 1;
fi
debug_log(){
if [ $enableLog == 1 ]; then
echo "${Blue}${1}" | tr '___' ' '
fi
}
print_results(){
echo -e "\n\n${Green}Results:\n"
for msg in ${oks[@]}; do
echo "${Green}${okSign} ${msg}" | tr '___' ' '
done
for msg in ${hints[@]}; do
echo "${Blue}${hintSign} ${msg}" | tr '___' ' '
done
for msg in ${warnings[@]}; do
echo "${Yellow}${warningSign} ${msg}" | tr '___' ' '
done
for msg in ${errors[@]}; do
echo "${Red}${errorSign} ${msg}" | tr '___' ' '
done
echo -e "\n${Green}Oks: $okCount | ${Blue}Hints: $hintCount | ${Yellow}Warnings: $warningCount | ${Red}Errors: $errorCount"
}
get_configured_satellites(){
satellitesCount=$(mysql "--defaults-extra-file=$v3MysqlIni" -e "SELECT COUNT(*) FROM satellites;" -B -s 2>/dev/null)
satellitesEntries=$(mysql "--defaults-extra-file=$v3MysqlIni" -e "SELECT name, '----', address, ';;' FROM satellites;" -B -s 2>/dev/null)
}
########### check definitions
check_free_disk_space(){
debug_log $(echo "Checking free disk space ..." | tr ' ' '___')
FREE=`df -k --output=avail "$pathToCheckForFreeSpace" | tail -n1`
if [[ $FREE -lt $(($minFreeSpaceGB*1024*1024)) ]]; then
# less than (minimum) required space is free!
errors+=($(echo "Not enough free space: less than ${minFreeSpaceGB}GBs free!" | tr ' ' '___'))
((errorCount++))
elif [[ $FREE -lt $(($betterFreeSpaceGB*1024*1024)) ]]; then
# less than (better) required space is free!
warnings+=($(echo "Free space of $(($FREE/1024/1024))GBs could be enough, but $(($betterFreeSpaceGB-($FREE/1024/1024)))GB more would be better!" | tr ' ' '___'))
((warningCount++))
else
oks+=($(echo "More than ${betterFreeSpaceGB}GBs free space" | tr ' ' '___'))
((okCount++))
fi
}
check_aptget_working(){
debug_log $(echo "Checking apt-get ..." | tr ' ' '___')
aptResult=$( (apt-get update >/dev/null) 2>&1)
if [ $? != 0 ]; then
errors+=($(echo "'apt-get update' may not work properly:" | tr ' ' '___'))
errors+=($(echo "$aptResult" | tr ' ' '___'))
((errorCount++))
else
oks+=($(echo "'apt-get update' is working fine" | tr ' ' '___'))
isAptWorking=1
((okCount++))
fi
}
check_package_installed_discovery(){
if dpkg -s "openitcockpit-module-discovery" >/dev/null 2>&1; then
warnings+=($(echo "The DiscoveryModule (openitcockpit-module-discovery) is no longer available in openITCOCKPIT v4 and will be removed!" | tr ' ' '___'))
((warningCount++))
fi
}
check_package_installed_idoit(){
if dpkg -s "openitcockpit-module-idoit" >/dev/null 2>&1; then
warnings+=($(echo "The IdoitModule (openitcockpit-module-idoit) is no longer available in openITCOCKPIT v4 and will be removed!" | tr ' ' '___'))
warnings+=($(echo "Make sure you don't have i-doit hosts configured!" | tr ' ' '___'))
((warningCount++))
fi
}
check_package_installed_mk(){
if dpkg -s "openitcockpit-module-mk" >/dev/null 2>&1; then
hints+=($(echo "Checkmk will be upgraded to version 1.6.0. Please consider updating your client agents. (not required)" | tr ' ' '___'))
((hintCount++))
fi
}
check_package_installed_distribute(){
if dpkg -s "openitcockpit-module-distribute" >/dev/null 2>&1; then
get_configured_satellites
if [ $satellitesCount > 0 ]; then
warnings+=($(echo "You need to upgrade your installed satellites to v4!" | tr ' ' '___'))
((warningCount++))
if [ "$satellitesEntries" != "" ]; then
satellitesEntries=$(echo ${satellitesEntries/"\n"/""} | xargs)
while IFS=';;' read -ra Satellites; do
for Satellite in "${Satellites[@]}"; do
if [ "$Satellite" != "" ]; then
name=$(echo "${Satellite%%" ---- "*}" | xargs)
addr=$(echo ${Satellite/"${name} ----"/""} | xargs)
warnings+=($(echo " - '${name}' at ${addr}" | tr ' ' '___'))
fi
done
done <<< "$satellitesEntries"
fi
hints+=($(echo "With openITCOCKPIT v4 we provide a new satellite gui. Consider installing the satellite extension." | tr ' ' '___'))
((hintCount++))
fi
fi
}
################## run checks
check_free_disk_space
check_aptget_working
if [ $isAptWorking == 1 ]; then
debug_log $(echo "Checking installed openITCOCKPIT modules ..." | tr ' ' '___')
check_package_installed_discovery
check_package_installed_idoit
check_package_installed_mk
check_package_installed_distribute
fi
print_results
tput sgr0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment