shell腳本——linux主機監控

寫着玩兒的,在init 5級別,運行後會主動生成兩個窗口,對系統以及系統上的主要服務進行監控,並及時刷新,拋磚引玉,分享一下。


650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/7C/D7/wKiom1bZExnikBOrAAA8FYXAEb8968.png" style="float:none;" title="QQ截圖20160304124006.png" alt="wKiom1bZExnikBOrAAA8FYXAEb8968.png" />


650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/7C/D6/wKioL1bZE5WSdOptAABPto4C8sI427.png" style="float:none;" title="QQ截圖20160304124211.png" alt="wKioL1bZE5WSdOptAABPto4C8sI427.png" />


一共有三個腳本:1個主腳本,2個分屬監控腳本


主腳本啓動:

#!/bin/sh
#writer:gaolixu
path=`pwd`
gnome-terminal --geometry=63x16 -e $path/jk1_xn.sh
gnome-terminal --geometry=63x16+0+350 -e $path/jk2_fw.sh


系統監控腳本:

#!/bin/sh
#writer:gaolixu
cpu(){
   cpuld_15=`uptime |awk -F"[,|:]" '{print $NF}'`
   cpuld_5=`uptime |awk -F"[,|:]" '{n=NF-1;print $n}'`
   cpuld_1=`uptime |awk -F"[,|:]" '{n=NF-2;print $n}'`
   cpu_used=`ps aux |awk 'BEGIN{sum=0}{sum=$3+sum}END{print sum}'`
   echo -e `tput bold`"Cpu load\t1min\t5min\t15min\tCpu Used(%)"
   echo -e `tput sgr0;tput el`"cpu(s):$cpus\t$cpuld_1\t$cpuld_5\t$cpuld_15\t$cpu_used" %
   tput sgr0
}
mem(){
   m_total=`free -m|awk '/^Mem:/{print $2}'`
   m_free=`free -m|awk '/^Mem:/{print $4}'`
   m_used=`free -m|awk '/^Mem:/{print $3}'`
   m_usedd=`echo "$m_used*100/$m_total"|bc`
   echo -e `tput bold`"Title(Mem)\tTotal\tUsed(%)\t\tFree "
   echo -e `tput sgr0`"Memery\t\t$m_total"M"\t$m_used"M"($m_usedd%)\t$m_free"M
}
swap(){
   swap_total=`free -m |awk '/Swap:/{print $2}'`
   swap_free=`free -m |awk '/Swap:/{print $4}'`
   swap_used=`free -m |awk '/Swap:/{print $3}'`
   swap_usedd=`echo "$swap_used*100/$swap_total"|bc`
   echo -e "Swap\t\t$swap_total"M"\t$swap_used"M"($swap_usedd%)\t\t$swap_free"M
}
disk(){
   echo -e `tput bold`"Disk Name\tTotal\tUsed(%)\t\tFree\tType "
   tput sgr0
   df -Th|awk '$NF=="/"{print $NF"\t\t"$3"\t"$4"("$6")""\t"$5"\t"$2}'   
   df -Th|awk '$NF=="/boot"{print $NF"\t\t"$3"\t"$4"("$6")""\t"$5"\t"$2}'
}
tput clear
tput sgr0
tput civis
cpus=`lscpu |awk '/^CPU\(s\):/{print $2}'`
echo
while true
do
n_users=`uptime | awk -F, '{print $2}'`
tput cup 1 0
echo -e `tput setaf 1;tput bold`"服務器性能監控,登錄用戶:$n_users\t"`date "+%m月%d日  %T"`
echo `tput sgr0`"========================================================="
cpu
echo "========================================================="
mem
swap
echo "========================================================="
disk
echo "========================================================="
sleep 1
done

服務監控腳本:

#!/bin/sh
#writer:gaolixu
apache_d(){
echo -n -e `tput sgr0`"apache\t\t"
if service httpd status &>/dev/null ;then
  echo `tput el`"Apache 正常運行中..."
  tput sgr0
else
  echo `tput el;tput setaf 1;tput bold` "嚴重警告:apache服務已停止..."
  tput sgr0
fi
}
mysql_d(){
echo -n -e `tput sgr0`"mysql\t\t"
if service mysqld status &>/dev/null ;then
  echo `tput el`"數據庫mysql 正常運行中..."
  tput sgr0
else
  echo `tput el;tput setaf 1;tput bold` "嚴重警告:mysql服務已停止..."
  tput sgr0
fi
}
vsftp_d(){
echo -n -e `tput sgr0`"vsftp\t\t"
if service vsftpd status &>/dev/null ;then
  echo `tput el`"vsftp 服務正常運行中..."
  tput sgr0
else
  echo `tput el;tput setaf 1;tput bold` "嚴重警告:vsftp服務已停止..."
  tput sgr0
fi
}
rsyslog_d(){
echo -n -e `tput sgr0`"rsyslog\t\t"
if service rsyslog status &>/dev/null ;then
  echo `tput el`"系統日誌rsyslog正常運行中..."
  tput sgr0
else
  echo `tput el;tput setaf 1;tput bold` "嚴重警告:rsyslog日誌服務已停止..."
  tput sgr0
fi
}
iptables_d(){
echo -n -e `tput sgr0`"iptables\t"
if service iptables status &>/dev/null ;then
  echo `tput el`"防火牆iptables服務正常運行中..."
  tput sgr0
else
  echo `tput el;tput setaf 1;tput bold` "嚴重警告:iptables防火牆服務已停止..."
  tput sgr0
fi
}
selinux_d(){
echo -n -e `tput sgr0`"selinux\t\t"
local s=`getenforce`
if [ "$s" = "Enforcing" ] ;then
  echo `tput el`"Selinux正常工作中..."
  tput sgr0
else
  echo `tput el;tput setaf 1;tput bold` "警告:selinux服務已停止..."
  tput sgr0
fi
}
tput clear
tput sgr0
tput civis
while true
do
tput cup 1 0
echo -e `tput setaf 1;tput bold`"重要服務監控\t\t\t" `date "+%m月%d日  %T"`
echo `tput sgr0`"========================================================"
apache_d
echo "--------------------------------------------------------"
mysql_d
echo "--------------------------------------------------------"
vsftp_d
echo "--------------------------------------------------------"
iptables_d
echo "--------------------------------------------------------"
rsyslog_d
echo "--------------------------------------------------------"
selinux_d
echo "========================================================"
sleep 1
done







本文出自 “奔跑的linux” 博客,請務必保留此出處http://benpaozhe.blog.51cto.com/10239098/1747537

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章