Shell脚本修改Nginx upstream配置文件
#!/bin/bash ##################################################### # Name: change_nginx_upstream_conf.sh # Version: V1.0 # Author: 运维菜鸟 # Description: 更改nginx upstream配置文件 # Create Date: 2017-07-03 # Email: ######################################################function name function_name=$1 #pool name pool_name=$2 #pool corresponding ip list pool_ip_lists=$3 #pool corresponding tomcat port pool_tomcat_port=$4 #upstream file location ngx_upstream_file=$5#检测pool在nginx upstream配置文件中是否存在 function check_pool_in_ngx_upstream() {grep -E "${pool_name}[^-]" ${ngx_upstream_file} >> /dev/nullif [ $? -eq 0 ];thenecho -e "\033[36m the ${pool_name} in ${ngx_upstream_file}. \033[0m"elseecho -e "\033[31m the ${pool_name} not in ${ngx_upstream_file}. \033[0m"exit 1fi }#显示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[*]};doif [ $line -gt ${pool_name_first_line} ];thenpool_name_end_line=${line}break;fidonesed -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[*]};doecho "add ${pool_name} ${ip} in ${ngx_upstream_file}"sed -i '/upstream '${pool_name}'[^-]*{/a\\tserver '${ip}':'${pool_tomcat_port}';' ${ngx_upstream_file}doneecho -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[*]};doif [ $line -gt ${pool_name_first_line} ];thenpool_name_end_line=${line}break;fidone#获取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 3 ];thenecho -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++));dopool_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[*]};doecho "this line ${line_context} has ${ip}" | egrep "${ip}:${pool_tomcat_port}"if [ $? -eq 0 ];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"fidonedone#删除文件中的空行sed -i '/^$/d' ${ngx_upstream_file}echo -e "\033[31m ====删除完成如下:==== \033[0m" }#调用方法 if [ $# -eq 5 ];thencase $1 inadd)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 3esac elseecho "variables count not eq 5.please check the usage." fi
转载于:https://www.cnblogs.com/crysmile/p/7116421.html
Shell脚本修改Nginx upstream配置文件相关推荐
- 开发shell脚本检查Nginx实战分享
开发shell脚本检查Nginx实战分享 点这里查看视频讲解 一.本脚本实现功能: 1.自动检查Nginx下面的代理节点是否正常 2.通过页面显示状态,有问题的节点给出页面报警及声音报警. 3.增加新 ...
- 宝塔linux shell定时访问url,bt(宝塔)面板添加shell 脚本通过nginx日志封访问频率过高的IP...
额,这篇文章是bt(宝塔)面板来写的文章,其他的程序如果是wdcp管理面板也有添加shell脚本的功能,大鸟自己用的是bt(宝塔)面板,所以说说如何添加shell 脚本通过nginx日志封访问频率过高 ...
- nginx日志报警|微信公众号推送预警消息|PHP+shell脚本实现nginx错误日志报警恶意访问报警,实时通知到微信公众号 | 小型网站nginx日志报警轻量级方案
1.shell脚本 命名:run.sh #!/bin/bash #!/usr/bin/python # -*- coding:utf-8 -*- #日志文件路径 accesslog=/www/wwwl ...
- shell脚本使得nginx日志每天定时切割压缩
Linux下添加shell脚本使得nginx日志每天定时切割压缩 对于nginx的日志文件,特别是access日志,如果我们不做任何处理的话,最后这个文件将会变得非常庞大 这时,无论是出现异常时查日志 ...
- Nginx学习笔记3:Shell脚本检测Nginx服务状态
前言 nginx 服务启动后,我们需要对其服务状态进行监控,今天学习过程中了解到一段非常有用的小脚本,不仅可以监控Nginx,也可以用来监控其他服务 脚本 A=`ps -C nginx –no-hea ...
- python定时任务执行shell脚本切割Nginx日志-慎用
Python定时任务执行shell脚本切割Nginx日志(慎用) 缘起 我们有一个Nginx服务用来接收埋点上报数据,输出的日志文件比较大,Nginx没有自带日志分割组件,这样输出的日志文件就比较大, ...
- shell脚本之nginx的安装
为了编写nginx自动部署的脚本而刚学习的shell脚本语言.写文章只是为了记录,有错误勿喷. 一.创建shell脚本程序 操作系统是Linux的 CentOS 7 版本.解释器默认使用 bash . ...
- shell脚本分析 nginx日志访问次数最多及最耗时的页面(慢查询)
当服务器压力比较大,跑起来很费力时候.我们经常做站点页面优化,会去查找那些页面访问次数比较多,而且比较费时. 找到那些访问次数高,并且比较耗时的地址,就行相关优化,会取得立竿见影的效果的. 下面是我在 ...
- Hadoop【部署 02】hadoop-3.1.3 单机版YARN(配置、启动停止shell脚本修改及服务验证)
1. 修改配置 进入 ${HADOOP_HOME}/etc/hadoop/ 目录下,修改以下配置: mapred-site.xml [root@tcloud ~]# vim /usr/local/ha ...
最新文章
- 都996了,需求还是没法按时交付,怎么办?
- ABAP RTTC动态编程在SAP gateway中的应用
- SVG矢量绘图 path路径详解(基本画法)
- 怎样借助营销圈帮助企业扩大品牌知名度呢?
- 拓端tecdat|在R语言中轻松创建关联网络
- Mycat分库分表的简单实践 / 用Mycat,学会数据库读写分离、分表分库
- centos7安装mysql教程详解(含常见问题的解决方案)
- 如何安装uclient_UClient客户端下载_UClient客户端官方下载-太平洋下载中心
- matlab汽车牌的识别,matlab车牌号识别
- 软件测试预演环境,什么是软件测试用例预演 有何优点?-软件水平考试-考试吧...
- viper4android 2.1,viper4android fx 2.2.1.1 调解
- java hypot_Java StrictMath hypot()用法及代码示例
- 注塑机网关 HFCL-EDGE
- java编程英语词汇_Java编程基础常见英语词汇
- Oh-my-Posh V3更新说明
- String中的assign函数
- Shader的合并同类项
- python笔记6-python官方文档之format()格式化详解
- VS2015——Qt工程不能生成moc文件的解决办法
- 化学绘图软件ChemDraw真的什么都能干!
热门文章
- orb-slam a versatile and accurate monocular slam system
- c语言验证鼓角猜想,患者,男,43岁,交通警察。下肢酸胀、沉重6年,活动或休息后减轻。体格检:小腿外侧有蚓状团块,足靴区...
- 计算机视觉(二)——深度学习进阶
- Datawhale 零基础入门数据挖掘-Task4 建模调参
- 不是方阵有逆矩阵吗_Lecture 3 | 乘法和逆矩阵
- nsq php,NSQ 最佳实践
- 修改mysql限制ip_MySQL 修改账号的IP限制条件
- fpga挂一片ddr2_FPGA片外存储器之DDR2控制器2
- http请求头获取 -python
- 构造一条二次bezier曲线_那些处处连续,处处不可导的曲线