使用shell编写一个检测系统参数的脚本

前端之家收集整理的这篇文章主要介绍了使用shell编写一个检测系统参数的脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

检测os的脚本

目的:学了shell语言后,根据已学编写一个shell的脚本实现检测部分系统参数实现邮件告警,

(之前刚刚学习写的不怎么完善和实用!)


[root@cml scripts]# cat os.monitor.sh

#!/bin/bash

###########

#name=linux查看系统资源脚本

#auth=cml

#time:2015

###########


DATE=`date "+%F %H:%M:%S"`

HOST=`hostname`


###########


echo "check:$DATE"


check_yum(){

yum install -y sysstat

yum install -y iftop

yum install -y lsof


}


check_mail(){

rpm -qa | grep mailx-12.4-8.el6_6.x86_64

mail=`echo $?`

if [ $mail -eq 0 ]; then

echo "mail is installed"

else

yum install -y mailx

fi

service postfix status

mail=`echo $?`

if [ $mail -eq 0 ]; then

echo "postfix is running!"

else

echo "postfix not running"

service postfix start

echo "postfix successfully runnig"

fi

echo -e "\033[32m=========check_mail is ok========\033[0m"

##只是为了好看分隔

}


check_sys(){

# echo "检查主机名,系统参数!"

echo -n "HOSTNAME:"

echo $HOST

echo -n "系统版本:"

cat /etc/issue

echo -n "系统的参数:"

cat /proc/version

echo -e "\033[32m=========check_sys is ok========\033[0m"

}


check_cpu(){

# echo "检查cpu,LOAD,WA,IDEL情况!"

echo -n "cpu内核:"

cpu=`cat /proc/cpuinfo | grep processor | wc -l`

echo $cpu

echo -n "load:"

load1=`w | awk 'NR==1 {print $10}' | awk -F '.' '{print $1}'`

load2=w | awk 'NR==1 {print $10}'

echo $load2

if [ $load1 -lt $cpu ]; then

echo "send_mail to ********@qq.com"

echo "your cpu is normal! cpu=$cpu load balance=$load2 time:$DATE" | mail -s "check your cpu" ********@qq.com

else

echo "your load<cpu load=$load cpu=$cpu $DATE"


fi

echo -e "\033[32m=========check_cpu is ok========\033[0m"

}


check_mem(){

# echo "检查内存使用情况!"

free=`free -m | awk 'NR==2 {print $4}'`

cache=`free -m | awk 'NR==2 {print $7}'`

echo -n "内存使用情况:"

mem=`echo $[free+cache]`

echo $mem

if [ $mem -lt 1000 ]; then

echo "send_mail to ********@qq.com"

echo "your mem is normal time:$DATE" | mail -s "check your mem " ********@qq.com

else

echo "your mem:$mem>1000 is abnormality time:$DATE"

fi

echo -e "\033[32m=========check_mem is ok========\033[0m"

}


check_network(){

# echo "检查网卡eth0的流量!"

# iftop -t >> /tmp/network

echo -n "接收端流量:"

# cat /tmp/network | grep send | awk 'NR==1 {print $4}'

ifconfig eth0 | awk 'NR==8 {print $3}' | awk -F '(' '{print $2}'

echo -n "发送端流量:"

# cat /tmp/network | grep receive | awk 'NR==1 {print $4}'

ifconfig eth0 | awk 'NR==8 {print $7}' | awk -F '(' '{print $2}'

echo -e "\033[32m=========check_network is ok========\033[0m"

}

check_TCP(){

# echo "检查TCP连接状态!"

echo -n "TCP连接状态:"

netstat -an | awk '/^tcp/{a[$NF]++}END{for (i in a)print i,a[i]}'

listen=`netstat -an | awk '/^tcp/{a[$NF]++}END{for (i in a)print i,a[i]}' | awk 'NR==2 {print $2}'`

if [ $listen -gt 7 ]; then

echo "send_mail to ********@qq.com "

echo "your connection is normal! time:$DATE" | mail -s "check your TCP " ********@qq.com

else

echo "your now-listen>Normal-listen now-listen=$listen $DATE"

fi

echo -e "\033[32m=========check_TCP is ok========\033[0m"

}


check_DISK(){

# echo "检查磁盘的使用情况!"

echo "/分区的使用百分比:"

root=`df -h | awk 'NR==3{print $4}' | awk -F "%" '{print $1}'`

echo $root

echo "INNODE使用百分比:"

df -i | awk 'NR==3 {print $4}'

if [ $root -lt 80 ]; then

echo "send_mail to ********@qq.com"

echo "your disk:/ is normal! time:$DATE " | mail -s "check your DISK" ********@qq.com

else

echo "your disk:/ greater than 80% /:$root time:$DATE"

fi

echo -e "\033[32m=========check_DISK is ok========\033[0m"

}




monitor2(){

check_sys

check_cpu

check_mem

check_network

check_TCP

check_DISK

}


monitor2 >> /tmp/$(hostname).txt

原文链接:https://www.f2er.com/bash/390980.html

猜你在找的Bash相关文章