Skip to content

Commit

Permalink
Update disk check (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
fewensa authored Oct 22, 2024
1 parent d5a1931 commit 045d096
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
5 changes: 2 additions & 3 deletions ansible/roles/monitor/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ monitor:
notify_slack_channel: 'darwinia-alert-notification'
server_name: ''
check_disks:
- /dev/sda
- /dev/sdb
alert_thread_cpu_p2: 90
alert_thread_cpu_p1: 98
alert_thread_ram_p2: 90
alert_thread_ram_p1: 98
alert_thread_disk_p2: 98
alert_thread_disk_p1: 99
alert_thread_disk_p2: 6
alert_thread_disk_p1: 3

22 changes: 11 additions & 11 deletions ansible/roles/monitor/templates/crawl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ memory_usage() {
free | grep Mem | awk '{print $3/$2 * 100.0}'
}

disk_usage() {
disk_remain() {
for disk in "${DISKS_TO_MONITOR[@]}"; do
usage=$(df -h | grep "^$disk" | awk '{print $5}' | sed 's/%//')
echo "$disk $usage"
remain=$(df -h | grep "^$disk" | awk '{print $4}' | sed 's/G//')
echo "$disk $remain"
done
}

Expand Down Expand Up @@ -79,19 +79,19 @@ generate_disk_alert_message() {

while IFS= read -r line; do
local disk=$(echo $line | awk '{print $1}')
local usage=$(echo $line | awk '{print $2}')
if [[ -z "$usage" ]]; then
local remain=$(echo $line | awk '{print $2}')
if [[ -z "$remain" ]]; then
continue
fi

if (( $(echo "$usage > $ALERT_THREAD_DISK_P1" | bc -l) )); then
if (( $(echo "$remain < $ALERT_THREAD_DISK_P1" | bc -l) )); then
priority='P1'
fi
if (( $(echo "$usage > $ALERT_THREAD_DISK_P2" | bc -l) )); then
disk_alert=$(jq -n --arg disk "*DISK* ($disk)" --arg usage "${usage}%" '[{"type":"mrkdwn","text":$disk},{"type":"plain_text","text":$usage}]')
if (( $(echo "$remain < $ALERT_THREAD_DISK_P2" | bc -l) )); then
disk_alert=$(jq -n --arg disk "*DISK* ($disk)" --arg remain "${remain}G" '[{"type":"mrkdwn","text":$disk},{"type":"plain_text","text":$remain}]')
alert_message=$(echo "$alert_message" | jq --argjson disk_alert "$disk_alert" '. += $disk_alert')
fi
done < <(disk_usage)
done < <(disk_remain)

if [[ "P1" == "$priority" ]]; then
priority_alert=$(jq -n --arg priority "${priority}" '[{"type":"mrkdwn","text":"*Priority*"},{"type":"plain_text","text":$priority}]')
Expand Down Expand Up @@ -158,9 +158,9 @@ send_alert() {
main() {
local cpu=$(cpu_usage)
local ram=$(memory_usage)
local disk=$(disk_usage)
local disk=$(disk_remain)
local requests=$(request_count)
echo "$(timestamp) CPU: ${cpu}% RAM: ${ram}% Disk: ${disk}% Requests: ${requests}"
echo "$(timestamp) CPU: ${cpu}% RAM: ${ram}% Remain Disk: ${disk}G Requests: ${requests}"

check_and_send_alert
}
Expand Down

0 comments on commit 045d096

Please sign in to comment.