Shell监控脚本

#!/bin/bash
ip=""
user=""
password=""
num=0
time=$(date '+%y%m%d%H%M')
echo "$time: Monitor is running..."
echo "<Servers> ">>/home/ETHAN/SMR/Logs/$time.xml
for mystr in $(cat /home/ETHAN/SMR/hosts2)
do
  let num=num+1
  if [ $num -eq 1 ];then ip=$(echo $mystr)
  elif [ $num -eq 2 ];then user=$mystr
  elif [ $num -eq 3 ];then password=$mystr
  elif [ $num -eq 4 ];then describe=$mystr
  fi
  if [ $num -eq 4 ];then let num=0
 
  echo "---------$ip----------"
  echo "<ServerInfos> ">>/home/ETHAN/SMR/Logs/$time.xml
  useCpu=$(/usr/local/bin/sshpass -p$password ssh -o StrictHostKeyChecking=no $user@$ip -p10087 ps aux|awk '{print $3}'|awk 'BEGIN{total=0}{total+=$1}END{print total}')
  totalMemory=$(/usr/local/bin/sshpass -p$password ssh -o StrictHostKeyChecking=no $user@$ip -p10087 free |head -n2|tail -n1|awk '{print $2}') 
  useMemory=$(/usr/local/bin/sshpass -p$password ssh -o StrictHostKeyChecking=no $user@$ip -p10087 free |head -n2|tail -n1|awk '{print $3}') 
  MemoryPct=$(expr "scale=3;$useMemory/$totalMemory*100"|bc)
  DiskPct=$(expr "scale=3;$usedDisk/$totalDisk*100"|bc)
  rootDisk=$(/usr/local/bin/sshpass -p$password ssh -o StrictHostKeyChecking=no $user@$ip -p10087  df -H|head -n2|tail -n1|awk '{print $4}')
  rootDiskPct=$(/usr/local/bin/sshpass -p$password ssh -o StrictHostKeyChecking=no $user@$ip -p10087  df -H|head -n2|tail -n1|awk '{print $5}')
  loadFivteen=$(/usr/local/bin/sshpass -p$password ssh -o StrictHostKeyChecking=no $user@$ip -p10087  uptime|awk '{print $10}')
  runTime=$(/usr/local/bin/sshpass -p$password ssh -o StrictHostKeyChecking=no $user@$ip -p10087  uptime|awk '{print $3}'|cut -d, -f1)
  diskMaxUsage=$(/usr/local/bin/sshpass -p$password ssh -o StrictHostKeyChecking=no $user@$ip -p10087 df -H|awk '{print $5,$6,$4}'|sed '1d' |awk 'BEGIN {max=0}{if(max<=$1) max=$1","$2","$3 }END{print max}')
  #load=$(/usr/local/bin/sshpass -p$password ssh -o StrictHostKeyChecking=no $user@$ip -p10087  uptime|awk '{print $10}'|cut -d, -f1') 
  echo "Cpu:$useCpu"
  echo "Memory:$MemoryPct"
  echo "RootFreeSpace:$rootDisk"
  echo "RootFreeSpace:$rootDiskPct"
  echo "RunTime:$runTime"
  echo "LoadIn15Min:$loadFivteen"
  echo "diskMaxUsage:$diskMaxUsage"
  echo "<IP>$ip</IP>">>/home/ETHAN/SMR/Logs/$time.xml
  echo "<Cpu>$useCpu</Cpu>">>/home/ETHAN/SMR/Logs/$time.xml
  echo "<Memory>$MemoryPct</Memory>">>/home/ETHAN/SMR/Logs/$time.xml
  echo "<RootDisk>$rootDisk</RootDisk>">>/home/ETHAN/SMR/Logs/$time.xml
  echo "<RootDiskPct>$rootDiskPct</RootDiskPct>">>/home/ETHAN/SMR/Logs/$time.xml
  echo "<RunTime>$runTime</RunTime>">>/home/ETHAN/SMR/Logs/$time.xml
  echo "<LoadIn15Min>$loadFivteen</LoadIn15Min>">>/home/ETHAN/SMR/Logs/$time.xml
  echo "<Describe>$describe</Describe>">>/home/ETHAN/SMR/Logs/$time.xml
  echo "<diskMaxUsage>$diskMaxUsage></diskMaxUsage>>">>/home/ETHAN/SMR/Logs/$time.xml
  echo "</ServerInfos> ">>/home/ETHAN/SMR/Logs/$time.xml
  fi
done
echo "</Servers>">>/home/ETHAN/SMR/Logs/$time.xml
rm -rf /home/ETHAN/SMR/UseLog/*
cp /home/ETHAN/SMR/Logs/$time.xml /home/ETHAN/SMR/UseLog/InfosFmLinux.xml
echo "Monitor is over."
posted @ 2019-04-04 17:16  Ethan.Sun  阅读(283)  评论(0编辑  收藏  举报