去年写得一个监控脚本,现在贴给大家,希望对大家有用,本脚本主要监控 流量、负载、inode、进程文件打开数等。
适用于:RedHat、Centos、Fedora (Unix下需要做小小的调试)
使用方法:
1、mkdir -p /var/log/system
2、然后写入计划任务
-->
crontab -e
*/5 * * * * /var/log/system/system_monitor.sh
3、vim /var/log/system/system_monitor.sh
|
- #!/bin/sh
-
# By Zongzhuangkai
- # 2010/12/3
-
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
-
export PATH
-
-
#This is the network Traffic monitoring
-
if [ -n "$1" ]; then
-
eth_name=$1
-
else
-
eth_name="eth0"
-
fi
-
send_first=`cat /tmp/send_first`
-
recv_first=`cat /tmp/recv_first`
-
send_o=`/sbin/ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
-
recv_o=`/sbin/ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
-
send_r=`expr $send_o - $send_first`
-
recv_r=`expr $recv_o - $recv_first`
-
total_sr=`expr \( $send_r + $recv_r \) / 1024`
-
/sbin/ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}' > /tmp/send_first
-
/sbin/ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}' > /tmp/recv_first
-
-
#system_monitor
-
day=$(date +%m%d%Y)
-
file="/var/log/system/sysinfo$day"
-
memtotal=`awk '$1 ~ /MemTotal/{print $2}' /proc/meminfo`
-
if [ -f $file ]; then
-
echo "ok">/dev/null
-
else
-
echo "date cpu/avg mem/avg load average inode http java oracle app_procnumber Packet_flow">>$file
-
echo "">>$file
-
fi
-
time=$(date '+%Y%m%d_%H:%M:%S')
-
load=$(uptime | sed 's/\,//g' | awk -F"average:" '{print $2}')
-
inode=$(df -i | awk 'NR==3{print $4}')
-
cpu=$(vmstat 1 2| awk '{NR==$NR}END{printf "%.1f%\n", 100-$15}')
-
mem=$(vmstat | awk 'BEGIN{i=0}{NR==$NR}{i='$memtotal'-$4}END{printf "%.2f%\n", i/'$memtotal'*100}')
-
httpd_number=$(ps -ef | grep "httpd"| grep -v grep|wc -l)
-
app_number=$(ps -ef |grep "java"|grep -v grep | wc -l)
-
oracle_number=$(ps -ef|grep "LOCAL=NO"|grep -v grep|wc -l)
-
java_pid=$(ps -ef | grep java|grep -v grep |grep -v jdk|awk '{print $2}' | awk 'NR==1{print $1}')
-
java_pid2=$(ps -ef | grep java|grep -v grep |grep -v jdk|awk '{print $2}' | awk 'NR==2{print $1}')
-
#if [[ $java_pid"zzk" != "zzk" ]]
-
#then
-
# app_number=$(lsof -p $java_pid)
-
# app_number2=$(lsof -p $java_pid2)
-
# app_procnumber=echo ${app_number}+${app_number2} | bc
-
#else
-
# app_procnumber=none
-
#fi
-
echo "${time} ${cpu} ${mem} ${load} ${inode} ${httpd_number} ${app_number} ${oracle_number} ${app_procnumber} ${total_sr}KB" >>$file
阅读(398) | 评论(0) | 转发(0) |