码迷,mamicode.com
首页 > 系统相关 > 详细

shell脚本——linux主机监控

时间:2016-03-04 14:37:42      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:linux 监控 shell

写着玩儿的,在init 5级别,运行后会主动生成两个窗口,对系统以及系统上的主要服务进行监控,并及时刷新,抛砖引玉,分享一下。


技术分享


技术分享


一共有三个脚本: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

shell脚本——linux主机监控

标签:linux 监控 shell

原文地址:http://benpaozhe.blog.51cto.com/10239098/1747537

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!