#!/bin/bash
#####################################################
# Name: change_nginx_upstream_conf.sh
# Version: V1.
# Author: 运维菜鸟
# Description: 更改nginx upstream配置文件
# Create Date: --
# Email:
##################################################### #function name
function_name=$
#pool name
pool_name=$
#pool corresponding ip list
pool_ip_lists=$
#pool corresponding tomcat port
pool_tomcat_port=$
#upstream file location
ngx_upstream_file=$ #检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
grep -E "${pool_name}[^-]" ${ngx_upstream_file} >> /dev/null
if [ $? -eq ];then
echo -e "\033[36m the ${pool_name} in ${ngx_upstream_file}. \033[0m"
else
echo -e "\033[31m the ${pool_name} not in ${ngx_upstream_file}. \033[0m"
exit
fi
} #显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
pool_name_first_line=`egrep -n "${pool_name}[^-]" ${ngx_upstream_file} | cut -d ":" -f1`
line_list=`grep -n "^}" ${ngx_upstream_file} | cut -d ":" -f1`
pool_name_end_line=${pool_name_first_line}
for line in ${line_list[*]};do
if [ $line -gt ${pool_name_first_line} ];then
pool_name_end_line=${line}
break;
fi
done
sed -n "${pool_name_first_line},${pool_name_end_line}p" ${ngx_upstream_file}
} #增加pool进nginx upstream配置文件
function add_pool_to_upstream() {
#pool对应ip地址列表,多个ip以逗号改开
pool_ip=`awk 'BEGIN{list="'${pool_ip_lists}'";split(list,ip_list,",");for(ip in ip_list){print ip_list[ip];}}'`
for ip in ${pool_ip[*]};do
echo "add ${pool_name} ${ip} in ${ngx_upstream_file}"
sed -i '/upstream '${pool_name}'[^-]*{/a\\tserver '${ip}':'${pool_tomcat_port}';' ${ngx_upstream_file}
done
echo -e "\033[31m ====添加完成如下:==== \033[0m"
} #在nginx upstream配置文件删除pool对应的ip地址
function delete_ip_from_upstream() {
pool_name_first_line=`egrep -n "${pool_name}[^-]" ${ngx_upstream_file} | cut -d ":" -f1`
line_list=`grep -n "^}" ${ngx_upstream_file} | cut -d ":" -f1`
pool_name_end_line=${pool_name_first_line}
for line in ${line_list[*]};do
if [ $line -gt ${pool_name_first_line} ];then
pool_name_end_line=${line}
break;
fi
done
#获取pool对应配置行数
line_count=`sed -n "${pool_name_first_line},${pool_name_end_line}p" ${ngx_upstream_file} | wc -l`
#如果某个pool的配置行数等于3,则不能进行删除操作
if [ ${line_count} -eq ];then
echo -e "\033[31m this is lowest configure. \033[0m"
fi
#删除pool_ip_lists中包含的ip地址
for ((i=${pool_name_first_line};i<=${pool_name_end_line};i++));do
pool_ip=`awk 'BEGIN{list="'${pool_ip_lists}'";split(list,ip_list,",");for(ip in ip_list){print ip_list[ip];}}'`
line_context=`sed -n ''${i}'p' ${ngx_upstream_file}`
for ip in ${pool_ip[*]};do
echo "this line ${line_context} has ${ip}" | egrep "${ip}:${pool_tomcat_port}"
if [ $? -eq ];then
#将包含删除ip的行,替换为空行
sed -i ''${i}'s/.*'${ip}':'${pool_tomcat_port}'.*//ig' ${ngx_upstream_file}
#sed -i ''${i}'d' ${ngx_upstream_file}
echo -e "\033[36m delete ${pool_name} from ${ngx_upstream_file} where ip = ${ip}. \033[0m"
fi
done
done
#删除文件中的空行
sed -i '/^$/d' ${ngx_upstream_file}
echo -e "\033[31m ====删除完成如下:==== \033[0m"
} #调用方法
if [ $# -eq ];then
case $ in
add)
check_pool_in_ngx_upstream;
show_pool_in_ngx_upstream;
add_pool_to_upstream;
show_pool_in_ngx_upstream;
;;
delete)
check_pool_in_ngx_upstream;
show_pool_in_ngx_upstream;
delete_ip_from_upstream;
show_pool_in_ngx_upstream;
;;
*)
$"Usage: {sh change_nginx_upstream_conf.sh add chat-frontier-web 10.10.13.194 8080 /etc/nginx/conf.d/upstream.conf|sh change_nginx_upstream_conf.sh add chat-frontier-web 10.10.13.194 8080 /etc/nginx/conf.d/upstream.conf}"
exit
esac
else
echo "variables count not eq 5.please check the usage."
fi

Shell脚本修改Nginx upstream配置文件的更多相关文章

  1. Linux下添加shell脚本使得nginx日志每天定时切割压缩

    Linux下添加shell脚本使得nginx日志每天定时切割压缩一 简介 对于nginx的日志文件,特别是access日志,如果我们不做任何处理的话,最后这个文件将会变得非常庞大 这时,无论是出现异常 ...

  2. shell脚本分析nginx日志

    shell脚本分析nginx日志: name=`awk -F ',' '{print $13":"$32}' $file | awk -F ':' '{print $4}'`ech ...

  3. Busybox shell脚本修改密码

    /****************************************************************************** * Busybox shell脚本修改密 ...

  4. Shell脚本创建Nginx的upstream及location配置文件

    #!/bin/sh ##################################################### # Name: create_nginx_conf.sh # Versi ...

  5. shell脚本编写nginx部署脚本

    下面为shell脚本编写的nginx的安装及修改nginx.conf的脚本,脚本比较简单: #!/bin/bash function yum_install(){ yum install epel-r ...

  6. shell脚本之nginx的安装

           为了编写nginx自动部署的脚本而刚学习的shell脚本语言.写文章只是为了记录,有错误勿喷. 一.创建shell脚本程序        操作系统是Linux的 CentOS 7 版本. ...

  7. 利用shell脚本实现nginx 的logs日志分割

    Nginx 是一个非常轻量的 Web 服务器,体积小.性能高.速度快等诸多优点.但不足的是也存在缺点,比如其产生的访问日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将 导致日志文件容量 ...

  8. shell脚本分析 nginx日志访问次数最多及最耗时的页面

    当服务器压力比较大,跑起来很费力时候.我们经常做站点页面优化,会去查找那些页面访问次数比较多,而且比较费时. 找到那些访问次数高,并且比较耗时的地址,就行相关优化,会取得立竿见影的效果的. 下面是我在 ...

  9. Mac下通过shell脚本修改properties文件

    通过shell脚本替换属性文件中的某行记录 假设有如下属性文件 demo.properties user.name=test user.password=123456 ................ ...

随机推荐

  1. PostgreSQL的checkpoint能否并行

    对于此问题,在社区进行了提问,并得到了一些大牛的解答: http://postgresql.1045698.n5.nabble.com/Can-checkpoint-creation-be-paral ...

  2. 【LG3234】[HNOI2014]抄卡组

    题面 题解 分三种情况: 若所有串都没有通配符,直接哈希比较即可. 若所有串都有通配符, 把无通配符的前缀 和 无通配符的后缀哈希后比较即可. 中间部分由于通配符的存在,一定可以使所有串匹配. 若部分 ...

  3. 如何解决Django与Vue语法的冲突

    当我们在django web框架中,使用vue的时候,会遇到语法冲突.因为vue使用{{}},而django也使用{{}},因此会冲突. 解决办法1:在django1.5以后,加入了标签:{% ver ...

  4. sql server 按月对数据表进行分区

    当某张数据表数据量较大时,我们就需要对该表进行分区处理,以下sql语句,会将数据表按月份,分为12个分区表存储数据,废话不多说,直接上脚本: use [SIT_L_TMS] --开启 XP_CMDSH ...

  5. ython进阶06 循环对象

    这一讲的主要目的是为了大家在读Python程序的时候对循环对象有一个基本概念. 循环对象的并不是随着Python的诞生就存在的,但它的发展迅速,特别是Python 3x的时代,循环对象正在成为循环的标 ...

  6. socket_tcp协议_loadrunner测试

    1.lrs_create_socket("socket0", "TCP", "RemoteHost=127.0.0.1:8888", Lrs ...

  7. 会声会影2018提示dll文件丢失怎么办?

    一些会声会影2018用户,在安装.使用软件的过程中,会出现dll缺失的提示,导致软件无法打开,那么,出现这一问题要怎么解决.接下来小编为大家具体介绍下两种解决方法. 图1:dll丢失提示 打开会声会影 ...

  8. MATLAB 笔记

    MATLAB的学习 Matlab 主要有5大部分构成,分别是MATLAB语言,桌面工具与开发环境,数学函数库 ,图形系统和应用程序接口.以及众多的专业工具.

  9. ExpressJS基础概念及简单Server架设

    NodeJS Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效.Node.js 的包 ...

  10. Python爬虫入门(7):正则表达式

    下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对字符串操作的一种公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串” ...