执行./Monitor.sh MTS eth0命令即可,会输出监控信息到excel中,下面是监控脚本实现
#!/bin/bash
function getNet()
{
average=1
band_alarm=0
if [ $# -gt 1 ]; then
average=$2
fi
if [ $# -gt 2 ]; then
band_alarm=$3
fi
typeset in in_old dif_in dif_in1
typeset out out_old dif_out dif_out1
in_average=0
out_average=0
win=0
in_old=$(cat /proc/net/dev | grep ${dev} | sed 's=^.*:==' | awk '{ print $1 }' )
out_old=$(cat /proc/net/dev | grep ${dev} | sed 's=^.*:==' | awk '{ print $9 }')
sleep 1
time_str=$(date)
in=$(cat /proc/net/dev | grep ${dev} | sed 's=^.*:==' | awk '{ print $1 }')
out=$(cat /proc/net/dev | grep ${dev} | sed 's=^.*:==' | awk '{ print $9 }')
dif_in=$(((in-in_old)*8))
dif_in1=$((dif_in/1024/1024))
dif_out=$(((out-out_old)*8))
dif_out1=$((dif_out/1024/1024))
in_old=${in}
out_old=${out}
if [ $average -eq 1 ]; then
#echo "[${time_str}] IN: ${dif_in}Bps(${dif_in1}Mbps) OUT: ${dif_out}Bps(${dif_out1}Mbps)"
inNetflow=$dif_in1
outNetflow=$dif_out1
fi
if [ $average -gt 1 ]; then
if [ $win -lt $average ]; then
win=$((win+1))
fi
in_average=$(((in_average*(win-1)+dif_in1)/win))
out_average=$(((out_average*(win-1)+dif_out1)/win))
out_diff=$((out_average*1024*1024))
echo "[${time_str}] IN: ${dif_in}Bps(${in_average}Mbps) OUT: ${out_diff}Bps(${out_average}Mbps)"
fi
if [ $band_alarm -gt 0 ]; then
if [ $((in_average-dif_in1)) -gt $band_alarm ] || [ $((dif_in1-in_average)) -gt $band_alarm ]; then
echo "************************************************************************IN bandwide change exceed ${band_alarm}Mbps"
fi
if [ $((out_average-dif_out1)) -gt 100 ] || [ $((dif_out1-out_average)) -gt 100 ]; then
echo "************************************************************************OUT bandwide change exceed ${band_alarm}Mbps"
fi
fi
}
function init_csv()
{
rm -rf "Monitor.csv"
#获取CPU信息
CPU_MODEL=$(cat /proc/cpuinfo | grep "model name" | uniq | awk -F":" '{print $2}')
CPU_CORES=$(cat /proc/cpuinfo | grep "core id" | uniq | wc -l)
CPU_LOAD=$(uptime | awk '{print $NF}')
echo "服务器CPU型号:$CPU_MODEL,物理核心数:$CPU_CORES,负载:$CPU_LOAD" >> "Monitor.csv"
first_line="localtime,process_name,pid,pid_cpu(%),pid_mem(Mb),thread_cound,fd_count,in_bandwide(Mb),out_bandwide(Mb),CLOSEWAIT,ESTABLISHED,idle_cpu(%),mem_total(Gb),mem_free(Gb),mem_used(%),mem_cached(Kb),mem_buffers(Kb),core_count"
echo $first_line >> "Monitor.csv"
}
function checkAllNetWork()
{
eth_name=($(cat /proc/net/dev | grep -E 'eth|bond' | awk '{print $1}'))
#time_str=$(date)
len=${#eth_name[*]}
if [[ $len -eq 0 ]]; then
echo -e "\e[1;31m错误:没有找到网卡 !\e[0m"
return
fi
in_old=($(cat /proc/net/dev | grep -E 'eth|bond' | awk '{print $2}'))
out_old=($(cat /proc/net/dev | grep -E 'eth|bond' | awk '{print $10}'))
sleep 1
in=($(cat /proc/net/dev | grep -E 'eth|bond' | awk '{print $2}'))
out=($(cat /proc/net/dev | grep -E 'eth|bond' | awk '{print $10}'))
for((index=0;index<$len;index++));
do
dif_in=$(((${in[$index]}-${in_old[$index]})*8))
dif_in1=$((dif_in/1024/1024))
dif_out=$(((${out[$index]}-${out_old[$index]})*8))
dif_out1=$((dif_out/1024/1024))
if [[ $dif_in1 -ge 1 ]] || [[ $dif_out1 -ge 1 ]]; then
echo "网卡${eth_name[$index]}实时流量统计 IN: ${dif_in1}Mbps OUT: ${dif_out1}Mbps"
fi
done
}
function getProcessPidByName()
{
pid=`pidof $process_name`
#echo $pid
}
function getCurrentTime()
{
current_time=`date "+%Y-%m-%d %H:%M:%S"`
#echo $current_time
}
function getCpu()
{
if [ $g_dn_type == 1 ];then
cpu=`top -n 1 -b|grep $pid| grep $process_name|awk '{print $9}'`
else
cpu=`top -n 1 -b|grep $pid| grep Media|awk '{print $9}'`
fi
#echo $cpu
}
function etMemory()
{
if [ $g_dn_type == 1 ];then
memory=`top -n 1 -b|grep $pid| grep $process_name |awk '{print $6}'`
else
memory=`top -n 1 -b|grep $pid| grep Media |awk '{print $6}'`
fi
#echo $memory
}
function getThreads()
{
threads=`ls /proc/$pid/task |wc -l`
#echo $threads
}
function getHandles()
{
handles=`ls -l /proc/$pid/fd |wc -l`
#echo $handles
}
function getCloseWait()
{
CloseWait=`netstat -anp|grep $monitor_port|grep CLOSE_WAIT|wc -l`
#echo $CloseWait
}
function getEST()
{
EST=`netstat -anp|grep $monitor_port|grep EST|wc -l`
#echo $EST
}
function getIdleCpu()
{
idle_cpu=`top -n 2 -b -d 0.1|grep Cpu | tail -1|awk '{print $5}'|awk -F% '{print $1}'`
#echo $allCpu
}
function getSystemMemInfo()
{
MemInfo=($(cat /proc/meminfo | grep Mem | awk '{print $2}'))
MemTotal=$(printf "%.2f" `echo "scale=5;${MemInfo[0]}/1048576"|bc`)
MemFree=$(printf "%.2f" `echo "scale=5;${MemInfo[1]}/1048576"|bc`)
MemUsed=$(printf "%.0f" `echo "scale=5;100-100*${MemInfo[1]}/${MemInfo[0]}"|bc`)
MemCached=$(cat /proc/meminfo | grep ^Cached | awk '{print $2}')
MemBuffers=$(cat /proc/meminfo | grep ^Buffers | awk '{print $2}')
}
function getCoreCount()
{
if [ $g_dn_type == 1 ];then
core_count=$(ls /log/|grep core|grep $process_name|wc -l)
fi
}
function GetProcessInfo()
{
getProcessPidByName
if [ -n "$pid" ];then
getCurrentTime
getCpu
etMemory
getThreads
getHandles
getNet
getIdleCpu
getSystemMemInfo
getCloseWait
getEST
getCoreCount
#echo "$current_time,$process_name,$pid,$cpu,$memory,$threads,$handles,$inNetflow,$outNetflow,$CloseWait,$EST,$idle_cpu,$allMemory"
echo "$current_time,$process_name,$pid,$cpu,$memory,$threads,$handles,$inNetflow,$outNetflow,$CloseWait,$EST,$idle_cpu,$MemTotal,$MemFree,$MemUsed,$MemCached,$MemBuffers,$core_count" >> "Monitor.csv"
fi
}
# if [ $# -lt 1 ] || [ $# -gt 3 ] ; then
# echo "Usage: ./Monitor.sh MTS eth0"
# exit 0
# fi
if [ $# -ne 3 ] ; then
echo "Usage: ./Monitor.sh MTS eth0 9100"
exit 0
fi
process_name=$1
dev=$2
monitor_port=$3
##############################################
pid=""
current_time=""
cpu=""
memory=""
threads=""
handles=""
inNetflow=""
outNetflow=""
CloseWait=""
EST=""
idle_cpu=""
core_count=0
MemTotal=""
MemFree=""
MemUsed=""
MemCached=""
MemBuffers=""
##############################################
g_dn_type=1
loop_time=10
if [ $process_name == "MTS" ] || [ $process_name == "SS" ] ;then
g_dn_type=1
elif [ $process_name == "MediaDataNode64" ] || [ $process_name == "MediaDataNode" ] || [ $process_name == "MediaMTS" ] || [ $process_name == "MediaManager64" ] || [ $process_name == "MediaManager" ]; then
g_dn_type=2
else
exit 0
fi
init_csv
while true
do
starttime=`date +'%Y-%m-%d %H:%M:%S'`
GetProcessInfo
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s)
end_seconds=$(date --date="$endtime" +%s)
current_sleep_time=$((loop_time+start_seconds-end_seconds))
if [ $current_sleep_time -ge 0 ];then
sleep $current_sleep_time
fi
done