Linux下脚本实战之系统监控
Posted jks212454
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下脚本实战之系统监控相关的知识,希望对你有一定的参考价值。
一、脚本要求
1.监控系统的CPU、内存、硬盘使用率
二、脚本内容
#!/bin/bash
##########################################################
#File Name:monitor.sh
#Version:V1.0
#Aurhor:
#Emali:
#Created Time:2021-05-03 12:45:32
#Description:
##########################################################
#! /bin/bash
DATETIME=$(date +%F" "%H%M)
IP=$(ifconfig bond0 |awk '/netmask/ {print $2}')
which vmstat &> /dev/null
if [ ! $? = 0 ];then
echo "vmstat command no found,please install the package!"
exit 10
elif [ $? = 0 ] ; then
## mem the state
TOTAL=$(free -m |awk '/Mem/ {print $2}')
USED=$(free -m |awk '/Mem/ {print $3}')
FREE=$(free -m |awk '/Mem/ {print $4+$6}')
if [ $FREE -lt 1024 ];then
echo "
Date: $DATETIME
Hosts: $IP
Problem: Total=$TOTAL,USE=$USED,Free=$FREE"
fi
## the cpu state
US=$(vmstat |awk 'NR==3 {print $13}')
SY=$(vmstat |awk 'NR==3 {print $14}')
IDLE=$(vmstat |awk 'NR==3 {print $15}')
WAIT=$(vmstat |awk 'NR==3 {print $15}')
USE=$(($US+$SY))
if [ $USE -ge 50 ];then
echo "
Date: $DATETIME
Host: $IP
Problem: CPU utilization $USE"
fi
#disk the state
PART_USE=$(df -h |awk -F'[% ]+' 'BEGIN{OFS="="} /^\\/dev/ {print $1,$2,$5,$6}')
for i in $PART_USE; do
PART=$(echo $i |cut -d"=" -f1)
TOTAL=$(echo $i |cut -d "=" -f2)
USE=$(echo $i |cut -d"=" -f3)
MOUNT=$(echo $i |cut -d"=" -f4)
if [ $USE -gt 80 ]; then
echo "
Date: $DATETIME
Host: $IP
Total: $TOTAL
Problem: $PART=$USE($MOUNT)"
fi
done
else
echo " the system is OK! "
fi
三、运行脚本
[root@control scripts]# ./monitor.sh
the system is OK!
以上是关于Linux下脚本实战之系统监控的主要内容,如果未能解决你的问题,请参考以下文章
Zabbix实战之部署篇Zabbix使用SNMP监控Linux系统