Shell基础02

自动部署及监控


  1. 编写脚本自动部署反向代理、web、nfs
    • 部署nginx反向代理三个web服务,调度算法使用加权轮询
    • 所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性
      *** 主服务器 ***
#!/bin/bash
rpm -qa | grep epel
if [ $? != '0' ]
then
    yum -y install epel-release
fi
rpm -qa | grep nginx
if [ $? != '0' ]
then
    yum -y install nginx
fi
rpm -qa | grep nfs-utils
if [ $? != '0' ]
then
    yum -y install nfs-utils
fi
rpm -qa | grep rpcbind
if [ $? != '0' ]
then
    yum -y install rpcbind
fi
#变量
function inputipport(){
        ip0=a
        port0=a
        ipportstr=a
        read -p "Plz input ip:" ip0
        read -p "Plz input port:" port0
        ipportstr=${ip0}:${port0}
        echo $ipportstr
}
#输入三台服务器IP端口
echo "请输入第三台服务器信息"
ipport0=`inputipport`
ipport1=`inputipport`
ipport2=`inputipport`
#修改nginx文件
sed -ri '/^[\ ]*#/d' /etc/nginx/nginx.conf
sed -ri "/http \{/a
\ \ \ \ upstream webCluster\{\n\ \ \ \ \ \ \ \ server $ipport0 weight=2;\n\ \ \ \ \ \ \ \ server $ipport1 weight=1;\n\ \ \ \ \ \ \ \ server $ipport2 weight=1;\n\ \ \ \ }" /etc/nginx/nginx.conf
sed -ri '/\ \ \ \ \ \ \ \ location\ \/\ \{/a\ \ \ \ \ \ \ \ \ \ \ \ proxy_pass http://webCluster/;' /etc/nginx/nginx.conf
#配置nfs
echo "/share 192.168.16.0/24(rw,sync,no_root_squash)" > /etc/exports
mkdir /share && chmod 777 /share 
touch /share/index.html
echo "surprise motherfuck!" > /share/index.html
exportfs -a
#启动服务器
systemctl enable nfs
systemctl enable rpcbind
systemctl enable nginx
systemctl start  nfs
systemctl start rpcbind
systemctl start nginx
systemctl stop firewalld

*** web服务器 ***

#!/bin/bash
rpm -qa | grep epel
if [ $? != '0' ]
then
    yum -y install epel-release
fi
rpm -qa | grep nginx
if [ $? != '0' ]
then
    yum -y install nginx
fi
rpm -qa | grep nfs-utils
if [ $? != '0' ]
then
    yum -y install nfs-utils
fi
rpm -qa | grep rpcbind
if [ $? != '0' ]
then
    yum -y install rpcbind
fi
nfssrv=''
read -p "input nfs server ip add:" nfssrv
#修改web服务器配置
sed -ri '/^[\ ]*#/d' /etc/nginx/nginx.conf
sed -ri '/\ \ \ \ \ \ \ \ location\ \/\ \{/a\ \ \ \ \ \ \ \ \ \ \ \ root /webRoot/;\n\ \ \ \ \ \ \ \ \ \ \ \ index\ index.html;' /etc/nginx/nginx.conf
#NFS挂载
mkdir /webRoot/
mount -t nfs $nfssrv:/share /webRoot
echo "$nfsrv:/share	/webRoot 	nfs 	defaults	0 0" >> /etc/fstab
#启动nginx服务器
systemctl enable nginx
systemctl start nginx
systemctl stop firewalld
  1. 编写监控脚本,监控集群内所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件
#!/bin/bash
#提示信息
#内存
buffercached=`free | sed -n '2p' | awk '{print $6}'`
used=`free | sed -n '2p' | awk '{print $3}'`
usedmem=$(($buffercached+$used))
totalmem=`free | sed -n '2p' | awk '{print $2}'`
memratio=`echo "scale=2;$usedmem/$totalmem" | bc -l | sed -r 's/.//'`
#磁盘
rootused=`df -h | sort | egrep '[\/]$' | awk '{print $5}' | sed -r 's/[%]//'` #根目录使用量
#nginx&&nfs
netstat -tlnp | sort | grep 'nginx' > /dev/null
ngstat=$?
netstat -tlnp | sort | grep 'rpc.mountd' > /dev/null
nfsstat=$?
sor=0
msg="
        TIME:$(date +%F_%T)
        HOSTNAME:$(hostname)
        IPADDR:$(ifconfig |awk 'NR==2{print $2}')
        "
if [[ $ngstat != 0 ]] || [[ $nfsstat != 0 ]]
        then
        msg+="
        WARNING:[nginx] or [nfs] has been down!!!check ur little poor Server!!!!
                        "
        sor=1
fi
if [[ $rootused>=80 ]]
        then
                msg+="
        WARNING:upgrad ur disk plz,cuz it only have less than 20% free space now!!!
                        "
        sor=1
fi
if [[ $memratio > 70 ]]
        then
                msg+="
        WARNING:memory usage is higher than 70%,[Server will BOOM after 10s]!!!
        "
        sor=1
fi
if [[ $sor == 1 ]]
        then
                echo $msg
fi
  1. 编写计划任务,定时运行监控脚本,完成监控操作
    cd ~ && chmod +x warning.sh 添加执行权限
    crontab -e编辑计划任务表
    * * * * * /root/warning每分钟执行一次监测程序
posted @ 2017-03-26 15:52  我是你爸爸的爸爸  阅读(145)  评论(0编辑  收藏  举报