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 ................ ...
随机推荐
- docker容器的启动、停止、运行、导入、导出、删除
原文:docker容器的启动.停止.运行.导入.导出.删除 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/jiang425776024/articl ...
- P2P平台投宝金融跑路?为什么我没有中雷!
编者按:市场有风险,投资需谨慎.最近,安徽合肥P2P平台"投保金融"跑路倒闭了. 今天(2016年11月8日)下午,在朋友圈看到一个分享,投宝金融跑路了,新闻媒体已经传开了. 这个 ...
- [BZOJ1185][HNOI2007]最小矩形覆盖-[凸包+旋转卡壳]
Description 传送门 Solution 感性理解一下,最小矩形一定是由一条边和凸包上的边重合的. 然后它就是模板题了..然而真的好难调,小于大于动不动就打错. Code #include&l ...
- day 1类 对象 属性 方法
1. 解决吃啤酒鸭的问题 第一种方式(面向过程): 1)养鸭子 2)鸭子长成 3)杀 4)作料 5)烹饪 6)吃 7)卒 第二种方式(面向对象): 1)找个卖啤酒鸭的人 2)给钱 交易 3)吃 4)胖 ...
- springboot 中controller 返回html界面或 jsp界面
参考链接:https://blog.csdn.net/qq_15260315/article/details/80907056 经尝试,返回html界面没问题,但是返回jsp界面是有问题的,just ...
- 深度学习:参数(parameters)和超参数(hyperparameters)
1. 参数(parameters)/模型参数 由模型通过学习得到的变量,比如权重和偏置 2. 超参数(hyperparameters)/算法参数 根据经验进行设定,影响到权重和偏置的大小,比如迭代次数 ...
- logstash处理@timestamp时区
input { stdin { } } filter { #ruby { # code => "event.set('timestamp', event.get('@timestamp ...
- Vue.js项目中,当图片无法显示时则显示默认图片
使用require将图片进入,写法如下: data: () => ({logo: 'this.src="' + require('../assets/img.png') + '&quo ...
- kubeadm构建k8s之Prometheus-operated监控(0.18.1)
介绍: 大家好,k8s的搭建有许多方式,也有许多快速部署的,为了简化部署的复杂度,官方也提供了开源的kubeadm快速部署,最新1.10.x版本已经可以实现部署集群, 如果你对k8s的原理已经非常了解 ...
- idea的快捷键(复制)
IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...