您的位置:首页 > 运维架构 > Shell

常用的主机监控的 Shell 脚本

2013-09-04 11:35 381 查看
作为系统运维人员,就要实时的监控系统的一些重要参数,不如僵尸进程,CPU的利用率,内存的使用情况,磁盘空间的使用情况,系统的均衡负载,根据得到的最新信息,我们就能判断系统运行的状态是否良好。本人接触Linux系统也有一点时间,在网上看看了,经过整理,将一些常用的系统监控的命令shell脚本拿出来晒晒,希望给新手提供学习的机会,同时也希望经验丰富者提够些技术知道,多多探讨交流!

获得某个用户的某个进行的信息:

function GetPID #user #name 

PsUser=$1 
#echo $PsUser 
PsName=$2 
#echo $PsName 
pid=`ps -u $PsUser | grep $PsName | grep -v grep | grep -v vi | grep -v dbx\n | grep -v tail | grep -v start | grep -v stop | sed -n 1p | awk '{print $1}'` 
echo $pid 

PID=`GetPID lgy cat` 
echo $PID 
#The process does not exist. 
if [ "-$PID" == "-" ] 
then 

echo "The process does not exist." 

fi 

这里面涉及一些最基本的进程监控命令,在linux中你只要man ps就会得到很多使用说明,grep按照一定的匹配规则进行字符串分割匹配,还有一些简单的控制语句,这些基本的shell语法应该是我们的基本技能,在晚上会有很多的学习资料。

获得CPU的使用率:

function GetCPU 

CPUValue=`ps -p $1 -o pcpu | grep -v CPU | awk '{print $1}' | awk -F. '{print $1}'` 
echo $CPUValue 

function CheckCPU 

PID=$1 
cpu=`GetCPU $PID` 
if [ $cpu -gt 80 ] 
then 

echo "The usage of cpu is larger than 80%" 

else 

echo "The usage of cpu is normal" 

fi 

CheckCPU $PID 

这个程序和上一个代码片段是一脉相承,本段代码的运行结果一会会有相应的展示!

这段脚本的主要目的是用来进行对某个进程的内存使用情况的检测:

function GetMem 

MemUsage=`ps -o vsz -p $1 | grep -v VSZ` 
((MemUsage /= 1024)) 
echo $MemUsage 

mem=`GetMem $PID` 
if [ $mem -gt 1600 ] 
then 

echo "The usage of memory is larger than 1.6G" 

else 

echo "The usage of memory is normal" 

fi 

这是用来进行获得此进程的句柄使用量:

function GetDes 

DES=`ls /proc/$1/fd | wc -l` 
echo $DES 

des=`GetDes $PID` 
if [ $des -gt 900 ] 
then 

echo "The number of des is larger than 900" 

else 

echo "The number of des is normal" 

fi 

进行对某个端口的绑定的查询:

function Listening 

TCPListeningNum=`netstat -an | grep ":$1 " | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l` 
UDPListenNum=`netstat -an | grep ":$1 " | awk '$1 == "udp" && $NF == "0.0.0.0:*" {print $0}' | wc -l` 
((ListeningNum = TCPListeningNum + UDPListenNum )) 
if [ $ListeningNum == 0 ] 
then 

echo "0" 

else 

echo "1" 

fi 

isListen=`Listening 8080` 
if [ $isListen -eq 1 ] 
then 

echo "The port is listening" 

else 

echo "The port is not listening" 

fi 

系统CPU的使用情况:

function GetSysCPU 

CPUIdle=`vmstat 1 5 | sed -n '3,$p' | awk '{xx = x + $15} END {print x/5}' | awk -F. '{print $1}'` 
CPUNum=`echo "100-$CPUIdle" | bc` 
echo $CPUNum 

cpu=`GetSysCPU` 
echo "The System CPU is $cpu" 
if [ $cpu -gt 90 ] 
then 

echo "The usage of system cpu is larger than 90%" 

else 

echo "The usage of system cpu is normal" 

fi 

获得某制定的磁盘空间:

function GetDiskSpc 

if [ $# -ne 1 ] 
then 
return 1 
fi 
Folder="$1$" 
DiskSpace=`df -k | grep $Folder | awk '{print $5}' | awk -F% '{print $1}'` 
echo $DiskSpace 

Folder="/dev" 
DiskSpace=`GetDiskSpc $Folder` 
echo "The system $Folder disk space is $DiskSpace%" 
if [ $DiskSpace -gt 90 ] 
then 

echo "The usage of system disk($Folder) is larger than 90%" 

else 

echo "The usage of system disk($Folder) is normal" 

fi 

在本机上代码检测的结果如下:

lgy@lgy-HP:~$ ./monitorCPU.sh 
2388 
The usage of cpu is normal 
The usage of memory is normal 
The number of des is normal 
The port is not listening 
The System CPU is 1 
The usage of system cpu is normal 
The system /dev disk space is 1% 
The usage of system disk(/dev) is normal

1、查看主机网卡流量#!/bin/bash 
#!/bin/bash 
#network 
#Mike.Xu 
while : ; do 
time='date +%m"-"%d" "%k":"%M' 
day='date +%m"-"%d' 
rx_before='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-' 
tx_before='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-' 
sleep 2 
rx_after='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-' 
tx_after='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-' 
rx_result=$[(rx_after-rx_before)/256] 
tx_result=$[(tx_after-tx_before)/256] 
echo "$time Now_In_Speed: "$rx_result"kbps Now_OUt_Speed: "$tx_result"kbps" 
sleep 2 
done 
done 
2、系统状况监控#!/bin/sh 
#systemstat.sh 
#Mike.Xu 
IP=192.168.1.227 
top -n 2| grep "Cpu" >>./temp/cpu.txt 
free -m | grep "Mem" >> ./temp/mem.txt 
df -k | grep "sda1" >> ./temp/drive_sda1.txt 
#df -k | grep sda2 >> ./temp/drive_sda2.txt 
df -k | grep "/mnt/storage_0" >> ./temp/mnt_storage_0.txt 
df -k | grep "/mnt/storage_pic" >> ./temp/mnt_storage_pic.txt 
time=`date +%m"."%d" "%k":"%M` 
connect=`netstat -na | grep "219.238.148.30:80" | wc -l` 
echo "$time  $connect" >> ./temp/connect_count.txt 
3、监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告#!/bin/bash 
#monitor available disk space 
SPACE='df | sed -n '/ \ / $ / p' | gawk '{print $5}' | sed  's/%//' 
if [ $SPACE -ge 90 ] 
then 
fty89@163.com 
fi 
4、 监控CPU和内存的使用情况#!/bin/bash 
#script  to capture system statistics 
OUTFILE=/home/xu/capstats.csv 
DATE='date +%m/%d/%Y' 
TIME='date +%k:%m:%s' 
TIMEOUT='uptime' 
VMOUT='vmstat 1 2' 
USERS='echo $TIMEOUT | gawk '{print $4}' ' 
LOAD='echo $TIMEOUT | gawk '{print $9}' | sed "s/,//' ' 
FREE='echo $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $4} ' ' 
IDLE='echo  $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' |gawk '{print $15}' ' 
echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $OUTFILE 
5、全方位监控主机#!/bin/bash 
# check_xu.sh 
# 0 * * * * /home/check_xu.sh 
DAT="`date +%Y%m%d`" 
HOUR="`date +%H`" 
DIR="/home/oslog/host_${DAT}/${HOUR}" 
DELAY=60 
COUNT=60 
# whether the responsible directory exist 
if ! test -d ${DIR} 
then 
/bin/mkdir -p ${DIR} 
fi 
# general check 
export TERM=linux 
/usr/bin/top -b -d ${DELAY} -n ${COUNT} > ${DIR}/top_${DAT}.log 2>&1 & 
# cpu check 
/usr/bin/sar -u ${DELAY} ${COUNT} > ${DIR}/cpu_${DAT}.log 2>&1 & 
#/usr/bin/mpstat -P 0 ${DELAY} ${COUNT} > ${DIR}/cpu_0_${DAT}.log 2>&1 & 
#/usr/bin/mpstat -P 1 ${DELAY} ${COUNT} > ${DIR}/cpu_1_${DAT}.log 2>&1 & 
# memory check 
/usr/bin/vmstat ${DELAY} ${COUNT} > ${DIR}/vmstat_${DAT}.log 2>&1 & 
# I/O check 
/usr/bin/iostat ${DELAY} ${COUNT} > ${DIR}/iostat_${DAT}.log 2>&1 & 
# network check 
/usr/bin/sar -n DEV ${DELAY} ${COUNT} > ${DIR}/net_${DAT}.log 2>&1 & 
#/usr/bin/sar -n EDEV ${DELAY} ${COUNT} > ${DIR}/net_edev_${DAT}.log 2>&1 & 
放在crontab里每小时自动执行:0 * * * * /home/check_xu.sh 
这样会在/home/oslog/host_yyyymmdd/hh目录下生成各小时cpu、内存、网络,IO的统计数据。
如果某个时间段产生问题了,就可以去看对应的日志信息,看看当时的主机性能如何。
原文链接:http://www.dbasky.net/archives/2009/10/shell.html
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell