Shell脚本修改Nginx upstream配置文件
#!/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配置文件的更多相关文章
- Linux下添加shell脚本使得nginx日志每天定时切割压缩
Linux下添加shell脚本使得nginx日志每天定时切割压缩一 简介 对于nginx的日志文件,特别是access日志,如果我们不做任何处理的话,最后这个文件将会变得非常庞大 这时,无论是出现异常 ...
- shell脚本分析nginx日志
shell脚本分析nginx日志: name=`awk -F ',' '{print $13":"$32}' $file | awk -F ':' '{print $4}'`ech ...
- Busybox shell脚本修改密码
/****************************************************************************** * Busybox shell脚本修改密 ...
- Shell脚本创建Nginx的upstream及location配置文件
#!/bin/sh ##################################################### # Name: create_nginx_conf.sh # Versi ...
- shell脚本编写nginx部署脚本
下面为shell脚本编写的nginx的安装及修改nginx.conf的脚本,脚本比较简单: #!/bin/bash function yum_install(){ yum install epel-r ...
- shell脚本之nginx的安装
为了编写nginx自动部署的脚本而刚学习的shell脚本语言.写文章只是为了记录,有错误勿喷. 一.创建shell脚本程序 操作系统是Linux的 CentOS 7 版本. ...
- 利用shell脚本实现nginx 的logs日志分割
Nginx 是一个非常轻量的 Web 服务器,体积小.性能高.速度快等诸多优点.但不足的是也存在缺点,比如其产生的访问日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将 导致日志文件容量 ...
- shell脚本分析 nginx日志访问次数最多及最耗时的页面
当服务器压力比较大,跑起来很费力时候.我们经常做站点页面优化,会去查找那些页面访问次数比较多,而且比较费时. 找到那些访问次数高,并且比较耗时的地址,就行相关优化,会取得立竿见影的效果的. 下面是我在 ...
- Mac下通过shell脚本修改properties文件
通过shell脚本替换属性文件中的某行记录 假设有如下属性文件 demo.properties user.name=test user.password=123456 ................ ...
随机推荐
- [SDOI2010]地精部落 DP
LG传送门 DP好题 题意很简单,就是求1-n的排列,满足一个数两边的数要么都比它大要么都比它小,求这样的排列个数对\(p\)取膜的值(为了表述简单,我们称这样的排列为波动序列). 这个题我第一眼看到 ...
- HTML基本代码教学,第三天
HTML 今天由于个人情况,身体不适,但是为了大家的学习进度,咱们以纯文字得形式来简单了解下今天的学习内容 今儿咱们来了解下表单 <form id=" " name=&qu ...
- javaweb(七)——HttpServletResponse对象(一)
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...
- 用php实现简单的自制计算器
存档: <!DOCTYPE html> <html> <head> <title>PHP实现计算器</title> </head> ...
- Ansible开发之路
一.初识Ansible 链接:https://www.cnblogs.com/baishuchao/articles/9164083.html 二.Ansible的架构 链接:https://www. ...
- 《零基础学JavaScript(全彩版)》学习笔记
<零基础学JavaScript(全彩版)>学习笔记 二〇一九年二月九日星期六0时9分 前期: 刚刚学完<零基础学HTML5+CSS3(全彩版)>,准备开始学习JavaScrip ...
- linux-sftp-指定端口号登录远程主机
sftp -oPort=60001 root@192.168.0.254 -o选项来指定端口号 -oPort=远程端口号
- Python中的构造函数
Python中的构造函数是__init__函数.在Python中,子类如果定义了构造函数,而没有调用父类的,那么Python不会自动调用,也就是说父类的构造函数不会执行. 比如有test.py的mod ...
- 7. B+树
一.B+树是应文件系统所需而产生的一种B树的变形树 1. 定义(使用阶数m来定义) 除了根结点外,其他非终端结点最多有m个关键字,最少有⌈m/2⌉个关键字 结点中的每个关键字对应一个子树 所有的非终端 ...
- 王者荣耀交流协会——第7次Scrum会议
照片由刘耀泽同学拍摄 ,王露芝同学(外援)没有参加本次会议. 要求2 : 时间跨度:2017年10月19日 15:05 - 15:20 共计15分钟 要求3 : 地点:计算机楼107教室 要求4 : ...