Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Using shell to monitor cpu, disk and memory utilization

Shulou Source: shulou.com Published: 2022-06-01 03:29:21 10月05日 Update

Use shell to monitor cpu, disk and memory utilization, and email to notify when the alarm threshold is reached

And cooperate with the task plan, the alarm information can be obtained in time.

#! / bin/bash###Author: Liuzhengwei-1135960569@qq.com#QQ:1135960569#Last modified: 2017-04-19 21:50#Filename: jiankong.sh#Description: # # # obtain cpu utilization cpuUsage= `top-n 1 | awk-F'[%] +''NR==3 {print $2}' `# obtain disk utilization data_name= "/ dev/vda1" diskUsage= `df-h | grep $data_name | awk-F'[%] +'{print $5} '`logFile=/tmp/jiankong.log# obtain memory mem_total= `free-m | awk-F' [:] +'' NR==2 {print $2} '`mem_used= `free-m | awk-F' [:] + 'NR==3 {print $3}' `# Statistics memory usage mem_used_persent= `awk 'BEGIN {printf ".0f\ n" ('$mem_used'/'$mem_total') * 100}'`# get the alarm time now_time= `date'+% F% T'` function send_mail () {mail-s "monitoring alarm" 1135960569@qq.com

< /tmp/jiankong.log}function check(){ if [[ "$cpuUsage" >

80]] | | [["$diskUsage" > 80]] | | [["$mem_used_persent" > 80]] Then echo "alarm time: ${now_time}" > $logFile echo "CPU utilization: ${cpuUsage}%-- > disk utilization: ${diskUsage}%-- > memory utilization: ${mem_used_persent}%" > $logFile send_mail fi} function main () {check} main

Tags: Usage memory alarm disk monitoring time task information situation monitoring alarm alarm mail threshold statistics Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Docker Apple Microsoft macOS