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 ................ ...
随机推荐
- IDEA 运行报错 failed to create a child event loop
背景 在IDEA中写了测试代码,但是运行的时候一直提示 java.lang.IllegalStateException: failed to create a child event loop ... ...
- Maven学习(十三)-----Maven 构建生命周期
Maven 构建生命周期 构建生命周期是什么? 构建生命周期阶段的目标是执行顺序是一个良好定义的序列. 这里使用一个例子,一个典型的 Maven 构建生命周期是由下列顺序的阶段: 阶段 处理 描述 准 ...
- win7升级到win10,出现算术运算溢出问题
前台winform,后台java代码是: OutputStream ou=(OutputStream)response.getOutputStream(); ou.write(rightSet.get ...
- 设置PNG图片DPI 信息,保存为PDF(使用Magick),与OpenCV转换
目录 任务描述 解决方案 Magick++ Talk is cheap, show me the code. 与 Opencv 配合 相关链接 任务描述 我有这样一个需求,读取一张格式为PNG 或者 ...
- VMware安装的Windows10下Docker的安装
1.前言 开启学习Docker之旅,首先在VMware中安装了windows10,因为Docker for windows要Win10专业或者企业版,现在台式机是win7,不想动主机系统.嘻嘻 不过, ...
- phpcms v9如何给父级单页栏目添加内容
对于phpcms单页的调用相信大家都应该没问题,那么如果我们在后台添加的单页有二层甚至更多的时候,这样在管理内容上是没有给父级栏目添加内容这一功能的!那么我们该怎么实现这个功能并调用呢? 首先我们要修 ...
- 如何使用phpredis连接Redis的方法
本文跟大家介绍使用同一VPC内弹性云服务器ECS上的phpredis连接Redis的方法. 更多的客户端的使用方法,请参考https://redis.io/clients 前提条件 已成功申请Redi ...
- mysql group by 取第一条
select * from table where id in (select max(id) from table group by sku) 说明:id是自增序列,sku是表中的一个字段
- 第四次c++作业
一,GitHub地址 https://github.com/ronghuijun/3Elevators-scheduling 二,命令行和文件读写 百度有时候有点蒙,命令行用的是D:>Eleva ...
- 软工1816 · Alpha冲刺(7/10)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 学会了POSTMAN的使用,对后端已经完成的接口进行了收发消息正确性的验证 推 ...