写着玩儿的,在init 5级别,运行后会主动生成两个窗口,对系统以及系统上的主要服务进行监控,并及时刷新,抛砖引玉,分享一下。
一共有三个脚本:1个主脚本,2个分属监控脚本
主脚本启动:
#!/bin/sh#writer:gaolixupath=`pwd`gnome-terminal --geometry=63x16 -e $path/jk1_xn.shgnome-terminal --geometry=63x16+0+350 -e $path/jk2_fw.sh
系统监控脚本:
#!/bin/sh#writer:gaolixucpu(){ 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 cleartput sgr0tput civiscpus=`lscpu |awk '/^CPU\(s\):/{print $2}'`echowhile truedon_users=`uptime | awk -F, '{print $2}'`tput cup 1 0echo -e `tput setaf 1;tput bold`"服务器性能监控,登录用户:$n_users\t"`date "+%m月%d日 %T"`echo `tput sgr0`"========================================================="cpuecho "========================================================="memswapecho "========================================================="diskecho "========================================================="sleep 1done
服务监控脚本:
#!/bin/sh#writer:gaolixuapache_d(){echo -n -e `tput sgr0`"apache\t\t"if service httpd status &>/dev/null ;then echo `tput el`"Apache 正常运行中..." tput sgr0else echo `tput el;tput setaf 1;tput bold` "严重警告:apache服务已停止..." tput sgr0fi}mysql_d(){echo -n -e `tput sgr0`"mysql\t\t"if service mysqld status &>/dev/null ;then echo `tput el`"数据库mysql 正常运行中..." tput sgr0else echo `tput el;tput setaf 1;tput bold` "严重警告:mysql服务已停止..." tput sgr0fi}vsftp_d(){echo -n -e `tput sgr0`"vsftp\t\t"if service vsftpd status &>/dev/null ;then echo `tput el`"vsftp 服务正常运行中..." tput sgr0else echo `tput el;tput setaf 1;tput bold` "严重警告:vsftp服务已停止..." tput sgr0fi}rsyslog_d(){echo -n -e `tput sgr0`"rsyslog\t\t"if service rsyslog status &>/dev/null ;then echo `tput el`"系统日志rsyslog正常运行中..." tput sgr0else echo `tput el;tput setaf 1;tput bold` "严重警告:rsyslog日志服务已停止..." tput sgr0fi}iptables_d(){echo -n -e `tput sgr0`"iptables\t"if service iptables status &>/dev/null ;then echo `tput el`"防火墙iptables服务正常运行中..." tput sgr0else echo `tput el;tput setaf 1;tput bold` "严重警告:iptables防火墙服务已停止..." tput sgr0fi}selinux_d(){echo -n -e `tput sgr0`"selinux\t\t"local s=`getenforce`if [ "$s" = "Enforcing" ] ;then echo `tput el`"Selinux正常工作中..." tput sgr0else echo `tput el;tput setaf 1;tput bold` "警告:selinux服务已停止..." tput sgr0fi}tput cleartput sgr0tput civiswhile truedotput cup 1 0echo -e `tput setaf 1;tput bold`"重要服务监控\t\t\t" `date "+%m月%d日 %T"`echo `tput sgr0`"========================================================"apache_decho "--------------------------------------------------------"mysql_decho "--------------------------------------------------------"vsftp_decho "--------------------------------------------------------"iptables_decho "--------------------------------------------------------"rsyslog_decho "--------------------------------------------------------"selinux_decho "========================================================"sleep 1done
自建个人原创站,新的博文会在网咖社更新,欢迎浏览。