题:

开发脚本自动部署及监控
1.编写脚本自动部署反向代理、web、nfs;
要求:
I、部署nginx反向代理三个web服务,调度算法使用加权轮询;
II、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;
2.编写监控脚本,监控集群内所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件
3.编写计划任务,定时运行监控脚本,完成监控操作

------------------------------------------------------

1、安装nginx及nfs服务端

#!/bin/bash

#insatll nginx proxy
  
IP=`ifconfig |awk -F" " 'NR==2{print $2}'
`  
#insyall nginx proxy
function install_nginx() {
yum remove nginx -y
yum install nginx -y
touch /etc/nginx/conf.d/proxy.conf
cat > /etc/nginx/conf.d/proxy.conf <<EOF
    upstream web {
        ip_hash; 
        server 192.168.17.126;
        server 192.168.17.127;
    }
    
    server {
        ip_hash;  
        listen 80;
        server_name $IP;
        location / {
            proxy_pass http://web;
        }
    }
EOF
systemctl start nginx
}
 
#install nfs server
function install_nfs_server() {
yum install rpcbind nfs-utils -y
[ -d /share ] || mkdir  /share && chmod o+w /share
echo "/share 192.168.17.128/24(rw,sync,fsid=0)" > /etc/exports
systemctl start rpcbind.service
systemctl start nfs-server.service
}
while :
do
    read -p "please choice your install server{nginx|nfs}: " server
    if [ $server = "nginx" ]
    then
        install_nginx
    elif [ $server = "nfs" ]
    then
        install_nfs_server
    else
        exit
    fi
done
2、安装客户端nginx-nfs
[root@web01 mnt]# cat install_nginx.sh
#!/bin/bash
#insatll nginx 
  
IP=`ifconfig |awk -F" " 'NR==2{print $2}'`
  
#insyall nginx proxy
function install_nginx() {
yum install nginx -y
echo "welcome to web01" >/usr/share/nginx/html/index.html
systemctl start nginx
}
 
#install nfs server
function install_nfs_server() {
yum install rpcbind nfs-utils -y
systemctl start rpcbind.service
systemctl start nfs-server.service
mount -t nfs 192.168.17.128:/share /usr/share/nginx/html/
}
while :
do
    read -p "please choice your install server{nginx|nfs}: " server
    if [ $server = "nginx" ]
    then
        install_nginx
    elif [ $server = "nfs" ]
    then
        install_nfs_server
    else
        exit
    fi
done
3、编写监控脚本,监控集群内所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件
#!/usr/bin/python

# -*- coding: UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text
 
server = 'smtp.163.com'
port = '25'
 
def sendmail(server,port,user,pwd,msg):
    smtp = smtplib.SMTP()
    smtp.connect(server,port)
    smtp.login(user, pwd)
    smtp.sendmail(msg['from'], msg['to'], msg.as_string())
    smtp.quit()
    print('邮件发送成功email has send out !')
 
 
if __name__ == '__main__':
    msg = email.mime.multipart.MIMEMultipart()
    msg['Subject'] = '你是风儿我是沙,缠缠绵绵回我家'
    msg['From'] = 'python4_mail@163.com'
    msg['To'] = 'python4_recvmail@163.com'
    user = 'python4_mail'
    pwd = 'sbalex3714'
    content='%s\n%s' %('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:])) #格式处理,专门针对我们的邮件格式
 
    txt = email.mime.text.MIMEText(content, _charset='utf-8')
    msg.attach(txt)
 
    sendmail(server,port,user,pwd,msg)
2)将监控脚本重命名为mail,拷贝到/usr/bin/mail.赋x权限
3)定时监控脚本
#!/bin/sh

  
function ngxMonitor(){  #监控nginx服务
ps aux | grep nginx| grep -v grep &>/dev/null
if [ $? -ne 0 ];then
    msg="TIME:$(date +%F_%T)
         HOSTNAME:$(hostname)
         IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
         MSG:Nginx program is crash, Waiting to restart"
    echo $msg
    /usr/bin/my_mail $msg
    systemctl restart nginx
fi
}
  
function nfsMonitor(){ #监控nfs服务
ps aux | grep nfs| grep -v grep &>/dev/null
if [ $? -ne 0 ];then
    msg="TIME:$(date +%F_%T)
         HOSTNAME:$(hostname)
         IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
         MSG:NFS program is crash, Waiting to restart"
    echo $msg
    /usr/bin/my_mail $msg
    systemctl restart nginx
fi
}
  
function memMonitor(){  #监控内存
mem_use=`free | awk 'NR==2{print $3}'`
mem_total=`free | awk 'NR==2{print $2}'`
mem_per=`echo "scale=2;$mem_use/$mem_total"|bc -l |cut -d . -f2`
  
if [ ! -e /usr/bin/bc ];then
    yum install bc -y -q
    echo "bc install successful"
fi
if (( $mem_per > 10 )); then
    msg="TIME:$(date +%F_%T)
         HOSTNAME:$(hostname)
         IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
         MSG:Memory usage exceeds the limit,current value is ${mem_per}%"
         echo $msg
         /usr/bin/my_mail $msg
fi
}
  
function diskMonitor(){  #监控磁盘
space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1`
  
if [ $space_use -gt 80 ];then
    msg="TIME:$(date +%F_%T)
         HOSTNAME:$(hostname)
         IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
         MSG:Disk space usage exceeds the limit,current value is ${space_use}%"
    echo $msg
    /usr/bin/my_mail $msg
fi
}
  
  
ngxMonitor  &>>/tmp/monitor.log
nfsMonitor  &>>/tmp/monitor.log
memMonitor  &>>/tmp/monitor.log
diskMonitor &>>/tmp/monitor.log
 
4、计划任务,每天早上9点执行
00 09 * * * /shell/sysCheck.sh
分 时 日 月 周
 

自动化部署nginx负载均衡及监控短信报警的更多相关文章

  1. Nginx 负载均衡 后端 监控检测 nginx_upstream_check_module 模块的使用

    在使用nginx 的负载均衡 中,我们通常会使用到 Nginx 自带的 ngx_http_proxy_module 健康检测模块. ngx_http_proxy_module 自带的 健康检测模块参数 ...

  2. 利用 NGINX 最大化 Python 性能,第二部分:负载均衡和监控

    [编者按]本文主要介绍 NGINX 的主要功能以及如何通过 Nginx 优化 Python 应用性能.本文系国内 ITOM 管理平台 OneAPM 编译呈现. 本文上一篇系: 利用 NGINX 最大化 ...

  3. Tomcat集群 Nginx负载均衡 shell脚本实时监控Nginx

    第一步,安装Tomcat 系统环境:Centos7 第1步:下载tomcat安装包 tomcat官网:https://tomcat.apache.org/ 第2步:安装包上传至linux中 第3步:下 ...

  4. linux下nginx负载均衡部署

    nginx负载均衡部署 Nginx("engine x") 是一个高性能的 HTTP 和 反向代理 server,也是一个 IMAP/POP3/SMTP 代理server. Ngi ...

  5. nginx负载均衡和tomcat热部署简单了解

    简单说下几个名词 nginx     它是一个反向代理,实际上就是一台负责转发的代理服务器,貌似充当了真正服务器的功能,但实际上并不是,代理服务器只是充当了转发的作用,并且从真正的服务器那里取得返回的 ...

  6. Nginx负载均衡+监控状态检测

    Nginx负载均衡+监控状态检测 想用Nginx或者Tengine替代LVS,即能做七层的负载均衡,又能做监控状态检测,一旦发现后面的realserver挂了就自动剔除,恢复后自动加入服务池里,可以用 ...

  7. Linux 集群概念 , wsgi , Nginx负载均衡实验 , 部署CRM(Django+uwsgi+nginx), 部署学城项目(vue+uwsgi+nginx)

    Linux 集群概念 , wsgi , Nginx负载均衡实验 , 部署CRM(Django+uwsgi+nginx), 部署学城项目(vue+uwsgi+nginx) 一丶集群和Nginx反向代理 ...

  8. tomcat单机多应用部署配合Nginx负载均衡

    一.Windows 配置tomcat . 安装两个tomcat服务器以上 . 配置环境变量 CATALINA_BASE:D:\servers\Tomcat8 CATALINA_HOME:D:\serv ...

  9. nginx负载均衡高可用部署和代理配置

    nginx负载均衡高可用配置 服务器A:172.16.100.2 服务器B:172.16.100.3 首先先在两台服务器上分别安装好nginx和keepalived后,再进行下面的操作 配置keepa ...

随机推荐

  1. 安装HBase(0.9)数据库

    基本知识: 1.hbase是一种基于列存储的数据库,也就是说它的一列的数据是存储在一个文件里面的,而传统的数据库存储都是一个文件存储多个行,这些行有不同的列,这些列的数据类型 不同. 2.基于HDFS ...

  2. Spark运行模式概述

    Spark编程模型的回顾 spark编程模型几大要素 RDD的五大特征 Application program的组成 运行流程概述 具体流程(以standalone模式为例) 任务调度 DAGSche ...

  3. opencv边缘检测的入门剖析(第七天)

    ---边缘检测概念理解--- 边缘检测的理解可以结合前面的内核,说到内核在图像中的应用还真是多,到现在为止学的对图像的操作都是核的操作,下面还有更神奇的! 想把边缘检测出来,从图像像素的角度去想,那就 ...

  4. python编程之禅

    在python界面输入 import this >>> import this The Zen of Python, by Tim Peters Beautiful is bette ...

  5. VisualSVN:强制必须填写日志信息

    上回将到怎么修改已提交的版本日志信息,而开发项目过程中团队中总是有人忘记添加日志信息注释直接提交,这样会后期维护带来不便. 现在先演示一下效果 当直接提交一个空白日志信息时 有填写日志信息时 那怎么实 ...

  6. 单网卡用双IP上内外网

    网络   2008-09-22 17:42   阅读44   评论1    字号: 大  中  小  PC机同时上内外网一例 一个公司内部,一台pc机在上内网的同时,还要求上外网,如何在一块网卡上实现 ...

  7. 在eclipse中创建maven项目,亲测有效,详细步骤

    一.想要使用maven,首先要配置本地maven的环境 1.在http://maven.apache.org/download.cgi中去下载maven 2. 3.下载完毕后将压缩包解压到自己记住的位 ...

  8. clientdataset 用法

    http://www.360doc.com/content/10/0709/01/2071424_37769962.shtml

  9. <c:forEach>取得集合数量

    这个问题曾经也困扰了我好久,不过以后都没有用过也都忘记了,不过今天在做项目 的时候又遇到了,花费了很久的时间都没有成功.也试了它的很多属性,例如:varStatus.last.${status.cou ...

  10. Scala语言学习笔记(4)

    高阶函数 // 高阶函数 map val salaries = Seq(20000, 70000, 40000) val doubleSalary = (x: Int) => x * 2 val ...