From ede6cd6f5d78f8f857b0235fd6d433f9e5ee90e3 Mon Sep 17 00:00:00 2001 From: mt190502 Date: Wed, 24 Apr 2024 13:10:37 +0300 Subject: [PATCH 1/4] added check function --- monocloud/monocloud-health.sh | 75 ++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/monocloud/monocloud-health.sh b/monocloud/monocloud-health.sh index 3c60158..aa8b9da 100644 --- a/monocloud/monocloud-health.sh +++ b/monocloud/monocloud-health.sh @@ -17,7 +17,7 @@ if [[ "$CRON_MODE" == "1" ]]; then #~ redirect all outputs to file exec &>>/tmp/monocloud-health.log -else +else color_red=$(tput setaf 1) color_green=$(tput setaf 2) color_yellow=$(tput setaf 3) @@ -90,32 +90,41 @@ check_partitions() { #~ check status check_status() { printf "\n" - echo "Disk durumları kontrol ediliyor..." - diskout="$(check_partitions)" - if [[ -n "$diskout" && $(echo $diskout | jq -r ".[] | select(.percentage | tonumber > $PART_USE_LIMIT)") ]]; then - echo -e "${c_red}[ WARN ] Disk kullanımı limitini aşan bölüm bulundu. Alarm gönderilecek...${c_reset}" - else - echo -e "${c_green}[ OK ] Disk kullanımı limitini aşan bölüm yok.${c_reset}" - fi + echo "Monocloud Health Check - $script_version - $(date)" printf "\n" - echo "Sistem yükü ve RAM kullanımı kontrol ediliyor..." - systemout="$(check_system_load_and_ram)" + log_header "Disk Usages" + info="$(check_partitions | jq -r '.[] | [.percentage, .usage, .total, .partition, .mountpoint, .note] | @tsv')" + oldIFS=$IFS + IFS=$'\n' + for i in ${info[@]}; do + IFS=$oldIFS a=($i) + if [[ ${a[0]} -gt $PART_USE_LIMIT ]]; then + print_colour "Disk Usage is ${a[3]}" "greater than $PART_USE_LIMIT (${a[0]}%)" "error" + else + print_colour "Disk Usage is ${a[3]}" "less than $PART_USE_LIMIT (${a[0]}%)" + fi + done + + printf "\n" - if [[ -n "$systemout" && $(echo $systemout | jq -r ". | select(.load | tonumber > $LOAD_LIMIT)") ]]; then - echo -e "${c_red}[ WARN ] Sistem yükü limiti aşıldı. Alarm gönderiliyor...${c_reset}" + log_header "System Load and RAM" + systemstatus="$(check_system_load_and_ram)" + if [[ -n $(echo $systemstatus | jq -r ". | select(.load | tonumber > $LOAD_LIMIT)") ]]; then + print_colour "System Load" "greater than $LOAD_LIMIT ($(echo $systemstatus | jq -r '.load'))" "error" else - echo -e "${c_green}[ OK ] Sistem yükü limiti aşılmadı." + print_colour "System Load" "less than $LOAD_LIMIT ($(echo $systemstatus | jq -r '.load'))" fi - if [[ -n "$systemout" && $(echo $systemout | jq -r ". | select(.ram | tonumber > $RAM_LIMIT)") ]]; then - echo -e "${c_red}[ WARN ] RAM kullanımı limiti aşıldı. Alarm gönderiliyor...${c_reset}" + if [[ -n $(echo $systemstatus | jq -r ". | select(.ram | tonumber > $RAM_LIMIT)") ]]; then + print_colour "RAM Usage" "greater than $RAM_LIMIT ($(echo $systemstatus | jq -r '.ram'))" "error" else - echo -e "${c_green}[ OK ] RAM kullanımı limiti aşılmadı.${c_reset}" + print_colour "RAM Usage" "less than $RAM_LIMIT ($(echo $systemstatus | jq -r '.ram'))" fi + printf "\n" - report_status + report_status &>/dev/null } #~ check system load and ram @@ -163,6 +172,20 @@ convertToProper() { echo $result } +log_header() { + echo "$1" + echo "--------------------------------------------------" +} + +print_colour() { + #~ $1: service name $2: status $3: type + if [ "$3" != 'error' ]; then + printf " %-40s %s\n" "${color_blue}$1${color_reset}" "is ${color_green}$2${color_reset}" + else + printf " %-40s %s\n" "${color_blue}$1${color_reset}" "is ${color_red}$2${color_reset}" + fi +} + report_status() { local diskstatus="$(check_partitions)" local systemstatus="$(check_system_load_and_ram)" @@ -227,7 +250,7 @@ report_status() { overthreshold_disk=1 fi table+="\n$(printf '%-5s | %-10s | %-10s | %-50s | %-35s' $percentage% $usage $total $partition ${mountpoint//sys_root/})" - done + done message+="$table\n\`\`\`\"}" IFS=$oldifs #[[ "$overthreshold_disk" == "1" ]] && echo $message || { echo "Overthreshold için bugün gönderilecek alarm yok..."; } @@ -286,9 +309,8 @@ report_status() { #~ usage usage() { - echo -e "Usage: $0 [-c ] [-C] [-h] [-l] [-V] [-v]" + echo -e "Usage: $0 [-c ] [-h] [-l] [-V] [-v]" echo -e "\t-c | --config : Use custom config file. (default: $CONFIG_PATH)" - echo -e "\t-C | --check : Check system load, ram and disk." echo -e "\t-l | --list : List partition status." echo -e "\t-V | --validate : Validate temporary directory and config." echo -e "\t-v | --version : Print script version." @@ -312,18 +334,17 @@ validate() { #~ main main() { mkdir -p /tmp/monocloud-health - opt=($(getopt -l "config:,check,debug,list,validate,version,help" -o "c:,C,d,l,V,v,h" -n "$0" -- "$@")) + opt=($(getopt -l "config:,debug,list,validate,version,help" -o "c:,d,l,V,v,h" -n "$0" -- "$@")) eval set -- "${opt[@]}" CONFIG_PATH="/etc/monocloud-health.conf" [[ "$1" == '-c' ]] || [[ "$1" == '--config' ]] && { [[ -n $2 ]] && CONFIG_PATH=$2; } [[ "$1" == '-d' ]] || [[ "$1" == '--debug' ]] && { set +x; } check_config_file "$CONFIG_PATH" && . "$CONFIG_PATH" - # [[ "${#opt[@]}" == "1" ]] && { check_partitions; exit 1; } - while true; do + + [[ "${#opt[@]}" == "1" ]] && { check_status; exit 1; } + + while true; do case $1 in - -C | --check) - check_status - ;; -l | --list) check_partitions | jq ;; @@ -348,4 +369,4 @@ main() { done } -main "$@" +main "$@" \ No newline at end of file From 3a64613b007de36388627e533cda341706f669d3 Mon Sep 17 00:00:00 2001 From: mt190502 Date: Wed, 24 Apr 2024 13:27:33 +0300 Subject: [PATCH 2/4] changed messages --- monocloud/monocloud-health.sh | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/monocloud/monocloud-health.sh b/monocloud/monocloud-health.sh index aa8b9da..c8280ed 100644 --- a/monocloud/monocloud-health.sh +++ b/monocloud/monocloud-health.sh @@ -192,7 +192,7 @@ report_status() { [[ -n "$SERVER_NICK" ]] && alarm_hostname=$SERVER_NICK || alarm_hostname="$(hostname)" local underthreshold_disk=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [✅] Bölüm kullanım seviyesi aşağıdaki bölümler için %$PART_USE_LIMIT seviyesinin altına indi;\n\`\`\`\n" + message="{\"text\": \"[Monocloud - $alarm_hostname] [✅] Partition usage levels went below ${PART_USE_LIMIT}% for the following partitions;\n\`\`\`\n" table="$(printf '%-5s | %-10s | %-10s | %-50s | %s' '%' 'Used' 'Total' 'Partition' 'Mount Point')" table+='\n' for z in $(seq 1 110); do table+="$(printf '-')"; done @@ -216,13 +216,13 @@ report_status() { done message+="$table\n\`\`\`\"}" IFS=$oldifs - #[[ "$underthreshold_disk" == "1" ]] && echo $message || { echo "Underthreshold için bugün gönderilecek alarm yok..."; } - [[ "$underthreshold_disk" == "1" ]] && curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" || { echo "Underthreshold (DISK) için bugün gönderilecek alarm yok..."; } + #[[ "$underthreshold_disk" == "1" ]] && echo $message || { echo "There's no alarm for Underthreshold today..."; } + [[ "$underthreshold_disk" == "1" ]] && curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" || { echo "There's no alarm for Underthreshold (DISK) today."; } fi local overthreshold_disk=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] Bölüm kullanım seviyesi aşağıdaki bölümler için %$PART_USE_LIMIT seviyesinin üstüne çıktı;\n\`\`\`\n" + message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] Partition usage level has exceeded ${PART_USE_LIMIT}% for the following partitions;\n\`\`\`\n" table="$(printf '%-5s | %-10s | %-10s | %-50s | %s' '%' 'Used' 'Total' 'Partition' 'Mount Point')\n" for z in $(seq 1 110); do table+="$(printf '-')"; done if [[ -n "$(echo $diskstatus | jq -r ".[] | select(.percentage | tonumber > $PART_USE_LIMIT)")" ]]; then @@ -253,27 +253,27 @@ report_status() { done message+="$table\n\`\`\`\"}" IFS=$oldifs - #[[ "$overthreshold_disk" == "1" ]] && echo $message || { echo "Overthreshold için bugün gönderilecek alarm yok..."; } - [[ "$overthreshold_disk" == "1" ]] && curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" || { echo "Overthreshold (DISK) için bugün gönderilecek alarm yok..."; } + #[[ "$overthreshold_disk" == "1" ]] && echo $message || { echo "There's no alarm for Overthreshold today."; } + [[ "$overthreshold_disk" == "1" ]] && curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" || { echo "There's no alarm for Overthreshold (DISK) today..."; } fi local underthreshold_system=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [✅] Sistem yükü limiti $LOAD_LIMIT seviyesinin altına indi...\"}" + message="{\"text\": \"[Monocloud - $alarm_hostname] [✅] System load limit went below $LOAD_LIMIT\"}" if [[ -n $(echo $systemstatus | jq -r ". | select(.load | tonumber < $LOAD_LIMIT)") ]]; then if [[ -f "/tmp/monocloud-health/system_load" ]]; then underthreshold_system=1 rm -f /tmp/monocloud-health/system_load curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" else - echo "Underthreshold (SYS) için bugün gönderilecek alarm yok..." + echo "There's no alarm for Underthreshold (SYS) today..." fi fi local overthreshold_system=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] Sistem yükü limiti $LOAD_LIMIT seviyesinin üstüne çıktı...\"}" + message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] The system load limit has exceeded $LOAD_LIMIT...\"}" if [[ -n $(echo $systemstatus | jq -r ". | select(.load | tonumber > $LOAD_LIMIT)") ]]; then if [[ -f "/tmp/monocloud-health/system_load" && "$(cat /tmp/monocloud-health/system_load)" == "$(date +%Y-%m-%d)" ]]; then - echo "Overthreshold (SYS) için bugün gönderilecek alarm yok..." + echo "There's no alarm for Overthreshold (SYS) today..." else overthreshold_system=1 date +%Y-%m-%d >/tmp/monocloud-health/system_load @@ -282,22 +282,22 @@ report_status() { fi local underthreshold_ram=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [✅] RAM kullanımı limiti $RAM_LIMIT seviyesinin altına indi...\"}" + message="{\"text\": \"[Monocloud - $alarm_hostname] [✅] RAM usage limit went below $RAM_LIMIT\"}" if [[ -n $(echo $systemstatus | jq -r ". | select(.ram | tonumber < $RAM_LIMIT)") ]]; then if [[ -f "/tmp/monocloud-health/ram_usage" ]]; then underthreshold_ram=1 rm -f /tmp/monocloud-health/ram_usage curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" else - echo "Underthreshold (RAM) için bugün gönderilecek alarm yok..." + echo "There's no alarm for Underthreshold (RAM) today..." fi fi local overthreshold_ram=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] RAM kullanımı limiti $RAM_LIMIT seviyesinin üstüne çıktı...\"}" + message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] RAM usage limit has exceeded $RAM_LIMIT...\"}" if [[ -n $(echo $systemstatus | jq -r ". | select(.ram | tonumber > $RAM_LIMIT)") ]]; then if [[ -f "/tmp/monocloud-health/ram_usage" && "$(cat /tmp/monocloud-health/ram_usage)" == "$(date +%Y-%m-%d)" ]]; then - echo "Overthreshold (RAM) için bugün gönderilecek alarm yok..." + echo "There's no alarm for Overthreshold (RAM) today..." else overthreshold_ram=1 date +%Y-%m-%d >/tmp/monocloud-health/ram_usage From e7ba9edc46e39b6b8277e275b208a1527afc8ec8 Mon Sep 17 00:00:00 2001 From: mt190502 Date: Wed, 24 Apr 2024 13:40:58 +0300 Subject: [PATCH 3/4] show current ram and load usage --- monocloud/monocloud-health.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/monocloud/monocloud-health.sh b/monocloud/monocloud-health.sh index c8280ed..d922ec2 100644 --- a/monocloud/monocloud-health.sh +++ b/monocloud/monocloud-health.sh @@ -258,11 +258,12 @@ report_status() { fi local underthreshold_system=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [✅] System load limit went below $LOAD_LIMIT\"}" + message="{\"text\": \"[Monocloud - $alarm_hostname] [✅] System load limit went below $LOAD_LIMIT " if [[ -n $(echo $systemstatus | jq -r ". | select(.load | tonumber < $LOAD_LIMIT)") ]]; then if [[ -f "/tmp/monocloud-health/system_load" ]]; then underthreshold_system=1 rm -f /tmp/monocloud-health/system_load + message+="(Current: $(echo $systemstatus | jq -r '.load'))...\"}" curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" else echo "There's no alarm for Underthreshold (SYS) today..." @@ -270,23 +271,25 @@ report_status() { fi local overthreshold_system=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] The system load limit has exceeded $LOAD_LIMIT...\"}" + message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] The system load limit has exceeded $LOAD_LIMIT " if [[ -n $(echo $systemstatus | jq -r ". | select(.load | tonumber > $LOAD_LIMIT)") ]]; then if [[ -f "/tmp/monocloud-health/system_load" && "$(cat /tmp/monocloud-health/system_load)" == "$(date +%Y-%m-%d)" ]]; then echo "There's no alarm for Overthreshold (SYS) today..." else overthreshold_system=1 date +%Y-%m-%d >/tmp/monocloud-health/system_load + message+="(Current: $(echo $systemstatus | jq -r '.load'))...\"}" curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" fi fi local underthreshold_ram=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [✅] RAM usage limit went below $RAM_LIMIT\"}" + message="{\"text\": \"[Monocloud - $alarm_hostname] [✅] RAM usage limit went below $RAM_LIMIT " if [[ -n $(echo $systemstatus | jq -r ". | select(.ram | tonumber < $RAM_LIMIT)") ]]; then if [[ -f "/tmp/monocloud-health/ram_usage" ]]; then underthreshold_ram=1 rm -f /tmp/monocloud-health/ram_usage + message+="(Current: $(echo $systemstatus | jq -r '.ram'))...\"}" curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" else echo "There's no alarm for Underthreshold (RAM) today..." @@ -294,13 +297,14 @@ report_status() { fi local overthreshold_ram=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] RAM usage limit has exceeded $RAM_LIMIT...\"}" + message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] RAM usage limit has exceeded $RAM_LIMIT }" if [[ -n $(echo $systemstatus | jq -r ". | select(.ram | tonumber > $RAM_LIMIT)") ]]; then if [[ -f "/tmp/monocloud-health/ram_usage" && "$(cat /tmp/monocloud-health/ram_usage)" == "$(date +%Y-%m-%d)" ]]; then echo "There's no alarm for Overthreshold (RAM) today..." else overthreshold_ram=1 date +%Y-%m-%d >/tmp/monocloud-health/ram_usage + message+="(Current: $(echo $systemstatus | jq -r '.ram'))...\"}" curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" fi fi From c094dfa71e9e0b2267cbe8d6082b960b59fb838f Mon Sep 17 00:00:00 2001 From: mt190502 Date: Wed, 24 Apr 2024 14:00:45 +0300 Subject: [PATCH 4/4] fix typo --- monocloud/monocloud-health.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/monocloud/monocloud-health.sh b/monocloud/monocloud-health.sh index d922ec2..60fed54 100644 --- a/monocloud/monocloud-health.sh +++ b/monocloud/monocloud-health.sh @@ -263,7 +263,7 @@ report_status() { if [[ -f "/tmp/monocloud-health/system_load" ]]; then underthreshold_system=1 rm -f /tmp/monocloud-health/system_load - message+="(Current: $(echo $systemstatus | jq -r '.load'))...\"}" + message+="(Current: $(echo $systemstatus | jq -r '.load')%)...\"}" curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" else echo "There's no alarm for Underthreshold (SYS) today..." @@ -278,7 +278,7 @@ report_status() { else overthreshold_system=1 date +%Y-%m-%d >/tmp/monocloud-health/system_load - message+="(Current: $(echo $systemstatus | jq -r '.load'))...\"}" + message+="(Current: $(echo $systemstatus | jq -r '.load')%)...\"}" curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" fi fi @@ -289,7 +289,7 @@ report_status() { if [[ -f "/tmp/monocloud-health/ram_usage" ]]; then underthreshold_ram=1 rm -f /tmp/monocloud-health/ram_usage - message+="(Current: $(echo $systemstatus | jq -r '.ram'))...\"}" + message+="(Current: $(echo $systemstatus | jq -r '.ram')%)...\"}" curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" else echo "There's no alarm for Underthreshold (RAM) today..." @@ -297,14 +297,14 @@ report_status() { fi local overthreshold_ram=0 - message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] RAM usage limit has exceeded $RAM_LIMIT }" + message="{\"text\": \"[Monocloud - $alarm_hostname] [🔴] RAM usage limit has exceeded $RAM_LIMIT " if [[ -n $(echo $systemstatus | jq -r ". | select(.ram | tonumber > $RAM_LIMIT)") ]]; then if [[ -f "/tmp/monocloud-health/ram_usage" && "$(cat /tmp/monocloud-health/ram_usage)" == "$(date +%Y-%m-%d)" ]]; then echo "There's no alarm for Overthreshold (RAM) today..." else overthreshold_ram=1 date +%Y-%m-%d >/tmp/monocloud-health/ram_usage - message+="(Current: $(echo $systemstatus | jq -r '.ram'))...\"}" + message+="(Current: $(echo $systemstatus | jq -r '.ram')%)...\"}" curl -fsSL -X POST -H "Content-Type: application/json" -d "$message" "$WEBHOOK_URL" fi fi