Linux监控脚本

while :
do
if [ -f "./mon_linux.sh" ]
then
echo "进程开始运行"
else
exit 1
fi
#!/bin/bash
>result.tmp
#set -x
#version: 2.0
export LC_ALL="en_US.UTF-8"
 
server_info(){
echo ================System basic informaion==============================
#echo ======Time======
#date
echo "hostname" is `/bin/hostname`
echo "IP ADDRESS" is `/sbin/ifconfig em1|grep "inet addr:"|awk '{print $2,"/ "$4}' | awk -F ':' '{print $2}' | awk -F '/' '{print $1}' | sed 's/ .*//g'`
HOST_NAME=`hostname`
#dmidecode | grep -A10 "System Information$" |grep "Product Name:"|awk '{print $3,$4,$5}'
##dmidecode | grep -A10 "System Information$" |grep "Serial Number:"|awk '{print "SN:",$3}'
echo "CPU INFORMATION" is `cat /proc/cpuinfo|grep "name"|cut -d: -f2 |awk '{print "*"$1,$2,$3,$4}'|uniq -c`
#echo "Physical memory number" is  `dmidecode | grep -A 16 "Memory Device$" |grep Size:|grep -v "No Module Installed"|awk '{print "*" $2,$3}'|uniq -c`
echo "System version" is `cat /etc/issue | head -1`
}
 
OS_info(){
echo "system running time" is `/usr/bin/uptime |awk '{print $3,$4}'`
}
 
performance_info(){
echo "================System cpu&&memory information=========================================="
cpu_usage=`top -b -n 1 |grep C[Pp][Uu] |grep id|awk '{print $5}'|awk -F "%" '{print $1}'`
echo "CPU FREE PERCENT is $cpu_usage"
mem_total=$(free -m |grep Mem|awk '{print $2}')
mem_used=$(free -m |grep Mem|awk '{print $3}')
echo "mem_rate" is `expr $mem_used/$mem_total*100|bc -l`

Swap_total=$(free -m |grep Swap|awk '{print $2}')
Swap_used=$(free -m |grep Swap|awk '{print $3}')
echo "Swap_rate" is `expr $Swap_used/$Swap_total*100|bc -l`
echo "================System process information=========================================="
i=1
ps aux | sort -nr -k3 | awk '{print $3,$NF}' |  head -10 | while read A B
do
echo "process $B cpu  usage is $A"
 i=$(($i + 1))
done

i=1
ps aux | sort -nr -k4 |awk '{print $4,$NF}' | head -10 | while read A B 
do
echo "process  $B memory use is  $A"

i=$(($i + 1))

done
}
sec_info(){
echo "==============User logon  information====================="
w | grep -v ^USER | grep -v ^" " | while read A
do
echo "user logon information" is  $A
done

echo "==============disk  information====================="
df -h | grep -i -v mount|  awk '{print $1,$(NF-1),$NF}' | while read  A B C
  do
if [ "$A" = "$C" ]
then
continue
else
  echo "$C" usage is  "$B"
fi
  done
echo "==============dmesg  information====================="
dmesg |grep fail | sort -u | while read A
do
  echo "dmesg fail" is $A
done
dmesg |grep error | sort -u |  while read A
do
 echo "dmesg error" is $A
done
}

system_hardware_config(){
echo ===========================disk====================================
df -H |awk "{OFS=\"\t\"}{ print \$1,\$2,\$3,\$4,\$5,\$6}"
echo ===========================free====================================
free |head -1 |awk "{OFS=\"\t\"} {print \$1,\$2,\$6}"
free -m |awk "BEGIN{OFS=\"\t\"}{if (NR==2 ||NR==4 )print \$2,\$3,\$7}"
}
 
server_info>>result.tmp
OS_info>>result.tmp
performance_info>>result.tmp
sec_info>>result.tmp
echo "run Ok"
sleep 30
done

你可能感兴趣的:(Linux监控脚本)