linux监控运维脚本,shell脚本——linux主机监控

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

a1136a35e7f9cdc85175dc7a730fb0b4.png

2430e647208d9351cb04a46c8d5fcd9b.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

自建个人原创站运维网咖社(www.net-add.com),新的博文会在网咖社更新,欢迎浏览。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值