1. 程式人生 > >使用shell編寫一個檢測系統參數的腳本

使用shell編寫一個檢測系統參數的腳本

shell

檢測os的腳本

目的:學了shell語言後,根據已學編寫一個shell的腳本實現檢測部分系統參數實現郵件告警,

(之前剛剛學習寫的不怎麽完善和實用!)


[[email protected] 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


本文出自 “成為運維的路上” 博客,請務必保留此出處http://legehappy.blog.51cto.com/13251607/1962803

使用shell編寫一個檢測系統參數的腳本