- crontab을 사용하여 1분마다 해당 스크립트 실행
#!/bin/bash
#: Title : server alert service
#: Description : Checks port, CPU, and memory every minute and sends an alert
# Send message to Slack channel #test if WAS is overloaded.
# local dir of the script
homeDir="$(dirname "$0")"
hostname=$(hostname)
system_number=""
datetime=$(date "+%m-%d %H:%M:%S")
# resource indexing
online_pid=$(pgrep -f 'java.*online')
cnt_8103=$(netstat -natu | grep 'ESTABLISHED.*8103' | wc -l)
cnt_3306=$(netstat -natu | grep 'ESTABLISHED.*3306' | wc -l)
resource_percentage=$(ps -p $online_pid -o %cpu,%mem --no-headers)
cpu_usage=$(echo $resource_percentage | awk '{print $1}')
mem_usage=$(echo $resource_percentage | awk '{print $2}')
metric_string="[$hostname $datetime]\nWEB-WAS session: $cnt_8103/200\nDB Conn: $cnt_3306/100\n%CPU=$cpu_usage, %MEM=$mem_usage"
# Initialize alarm_desc
alarm_desc=""
# report limits
# session
if [[ $cnt_8103 -ge 250 ]]; then
alarm_desc+="Too many sessions (>=250)\n"
fi
# db pool
if [[ $cnt_3306 -ge 100 ]]; then
alarm_desc+="Too many DB connections (>=100)\n"
fi
# CPU
if [[ $(echo "$cpu_usage" | cut -d '.' -f 1) -ge 80 ]]; then
alarm_desc+="CPU alert\n"
fi
# MEM
if [[ $(echo "$mem_usage" | cut -d '.' -f 1) -ge 70 ]]; then
alarm_desc+="Memory alert\n"
fi
# Slack report
hook="https://hooks.slack.com/services/***/***/***"
time_ph=$(date)
# slack json format
request=$(cat <<EOF
{
"channel": "***",
"username": "***",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "$hostname Alert"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "$metric_string\n$alarm_desc"
}
]
}
]
}
EOF
)
# Send alert if there is any
if [ -n "$alarm_desc" ]; then
curl -X POST -H "Content-type: application/json" -d "$request" $hook
fi