shell脚本安装部署反向代理 监控进程 计划任务
1.编写脚本自动部署反向代理、web、nfs;
要求:
I、部署nginx反向代理三个web服务,调度算法使用加权轮询;
反向代理服务器脚本配置脚本
#!/bin/bash
#安装eple和nginx
function install(){
rpm -qa | grep epel &> /dev/null
if [ $? != 0 ]
then
yum install epel -y
fi
rpm -qa | grep nginx &> /dev/null
if [ $? != 0 ]
then
yum install nginx -y
fi
}
#启动nginx
function startng(){
ps aux | grep nginx | grep -v grep &> /dev/null
if [ $? -ne 0 ]
then
systemctl restart nginx
fi
}
#调用install()
install
echo 'install seccussful'
#nginx.conf追加反向代理客户端ip地址和权重
sed -r -i '/http[ ]*\{/a\upstream mynginx {\nserver 192.168.185.137 weight=3;\nserver 192.168.185.138;\nserver 192.168.185.139;\n}' /etc/nginx/nginx.conf
echo 'insert1 ok'
#追加location内容
sed -r -i '/location \/ \{/a\proxy_pass http://mynginx;' /etc/nginx/nginx.conf
echo 'insert1 ok'
#调用startng()
startng
echo 'start nginx'
反向代理客户端脚本
#!/bin/bash
#安装eple和nginx
function install(){
rpm -qa | grep epel &> /dev/null
if [ $? != 0 ]
then
yum install epel -y
fi
rpm -qa | grep nginx &> /dev/null
if [ $? != 0 ]
then
yum install nginx -y
fi
}
#启动nginx
function startng(){
ps aux | grep nginx | grep -v grep &> /dev/null
if [ $? -ne 0 ]
then
systemctl restart nginx
fi
}
#调用install()
install
#调用startng()
startng
II、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;
存储服务器脚本
#!/bin/bash
#yum安装nfs和RPC
function install(){
rpm -qa | grep rpcbind &> /dev/null
if [ $? != 0 ]
then
yum install rpcbind -y
fi
rpm -qa | grep nfs-utils &> /dev/null
if [ $? != 0 ]
then
yum install nfs-utils -y
fi
}
#调用install()
install
#新建输出目录share,增加写权限
mkdir /share_nfs
chmod -R o+w /share_nfs
#服务端修改配置文件
echo '/share 192.168.185.0/24(rw,sync,fsid=0)'>/etc/exports
#rpcbind和nfs服务开机启动
systemctl enable nfs-server.service
systemctl enable rpcbind.service
#启动rpcbind和nfs服务
function startrn(){
ps aux | grep nfs-server | grep -v grep &> /dev/null
if [ $? -ne 0 ]
then
systemctl restart nfs-server.service
fi
ps aux | grep rpcbind | grep -v grep &> /dev/null
if [ $? -ne 0 ]
then
systemctl restart rpcbind.service
fi
}
#调用startrn()
startrn
web端脚本
#!/bin/bash
#yum安装nfs和RPC
function install(){
rpm -qa | grep rpcbind &> /dev/null
if [ $? != 0 ]
then
yum install rpcbind -y
fi
rpm -qa | grep nfs-utils &> /dev/null
if [ $? != 0 ]
then
yum install nfs-utils -y
fi
}
#调用install()
install
#rpcbind和nfs服务开机启动
systemctl enable rpcbind.service
#启动rpcbind服务
function startr(){
ps aux | grep nfs-server | grep -v grep &> /dev/null
if [ $? -ne 0 ]
then
systemctl restart rpcbind.serive
fi
}
#调用startr()
startr
#挂载服务端/share目录
mount -t nfs 192.168.185.130:/share_nfs /var/www/html/
2.编写监控脚本,监控nginx,nfs状态,内存、磁盘剩余率检测,异常则发送报警邮件
监控脚本monitor.sh
#!/bin/bash
#monitor nginx
function monitor_nginx(){
ps aux | grep nginx | grep -v grep &> /dev/null
if [[ $? -ne 0 ]]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig | awk 'NR==2{print $2}')
MSG:nginx service stop"
echo $msg
/usr/bin/mail $msg
fi
}
#monitor nfs
function monitor_nfs(){
ps aux | grep nfs | grep -v grep &> /dev/null
if [[ $? -ne 0 ]]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig | awk 'NR==2{print $2}')
MSG:nfs service stop"
echo $msg
/usr/bin/mail $msg
fi
}
mem_limit=20
disk_space_limit=20
#monitor memory
function monitor_mem(){
mem_total=`free | awk 'NR==2{print $2}'`
mem_used=`free | awk 'NR==2{print $3}'`
mem_used_per=`echo "scale=2;$mem_used/$mem_total" |bc -l |cut -d. -f2`
if [[ mem_used_per -gt $mem_limit ]]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig | awk 'NR==2{print $2}')
MSG:Memory usage exceeds the limit,current value is ${mem_used_per}%"
echo $msg
/usr/bin/mail $msg
fi
}
function monitor_disk_space(){
space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1`
if [[ $space_use -gt $disk_space_limit ]]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:Disk space usage exceeds the limit,current value is ${space_use}%"
echo $msg
/usr/bin/mail $msg
fi
}
monitor_nginx &>> /tmp/monitor.log
monitor_nfs &>> /tmp/monitor.log
monitor_mem &>> /tmp/monitor.log
monitor_disk_space &>> /tmp/monitor.log
准备发送邮件的工具(将下面述文件内容拷贝到/usr/bin/mail并chmod +x /usr/bin/mail)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text
server = 'smtp.163.com'
port = '25'
def sendmail(server,port,user,pwd,msg):
smtp = smtplib.SMTP()
smtp.connect(server,port)
smtp.login(user, pwd)
smtp.sendmail(msg['from'], msg['to'], msg.as_string())
smtp.quit()
print('email has send out !')
if __name__ == '__main__':
msg = email.mime.multipart.MIMEMultipart()
msg['Subject'] = 'From your monitor server'
msg['From'] = 's*****6@163.com'
msg['To'] = 's*****ve@163.com'
user = 's*****6'
pwd = 's*****3'
content='%s\n%s' %('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:]))
txt = email.mime.text.MIMEText(content, _charset='utf-8')
msg.attach(txt)
sendmail(server,port,user,pwd,msg)
3.编写计划任务,定时运行监控脚本,完成监控操作
#用户(-u)root身份编辑(-e)计划任务
crontab -e -u root
#* * * * * [命令] 其中,五个星号表示:分钟 小时 日 月 周
25 14 * * 1-5 /shell/monitor.sh #每周1至周5的14点25分执行monitor脚本
#查看计划任务执行日志
tail -f /var/log/cron
shell脚本安装部署反向代理 监控进程 计划任务的更多相关文章
- Shell脚本-自动化部署反向代理、WEB、nfs
部署nginx反向代理三个web服务,调度算法使用加权轮询(由于物理原因只开启两台服务器) AutoNginxNfsService.sh #/bin/bash systemctl status ngi ...
- 编写脚本自动部署反向代理、web、nfs
服务器端 #!/bin/bash function nginx_install(){ if [[ -f /usr/sbin/nginx ]]; then echo 'Nginx has been in ...
- Shell脚本一键部署——源码编译安装MySQL及自动补全工具
Shell脚本一键部署--源码编译安装MySQL及自动补全工具 编译安装MySQL 1.软件包 Mysql安装包 将安装包拖至/opt目录下,编辑一个脚本文件,将以下内容复制进去,然后source或者 ...
- Apache 2.4.7在CentOS6.4中安装配置反向代理解决单外网IP对应多个内网主机的方法实践
欢迎转载,转载时请保留全文及出处. Apache 2.4.7在CentOS6.4中安装配置反向代理解决单外网IP对应多个内网主机的方法实践 Apache安装 下载源程序(http://httpd.ap ...
- Docker学习3-简单shell脚本安装mysql5.7与docker小技巧
前言 玩过Windows中的 .bat 的小伙伴是不是觉得很有意思呢,github中一键推送.同步拉取等等操作,哈哈,当然shell脚本也是很类似,可以运行一个脚本就可以自动给我们部署好环境啦!但是这 ...
- shell脚本自动化部署
由于公司技术部团队较小,没有专门的运维团队,所以运维工作技术部承包了. 一.纯人工部署是这样的: 1. 本地打包:一般 maven clean package 2. 借助xftp上传到服务器对应目录 ...
- Nginx的安装及反向代理设置
因为项目的缘故,接触到了Nginx的安装和反向代理设置,和大家分享下. 一.Nginx的下载.安装cd /homewget http://nginx.org/download/nginx-1.0.5. ...
- shell脚本自动化部署服务
shell脚本自动化部署 !/bin/bash #export PATH=$PATH:/export/maven/bin run_flag_dir="/data0/shell/deploy_ ...
- shell脚本安装python、pip--这种写法是错误的---每一个命令执行完都要判断是否执行成功,否则无法进行下一步
shell脚本安装python.pip--不需要选择安装项目--不管用总报错,必须带上判断符号,while没有这种用法,写在这里为了以后少走弯路,所以不要用下面的执行了 首先把pip-.tgz 安装包 ...
随机推荐
- PAT 1072 开学寄语
https://pintia.cn/problem-sets/994805260223102976/problems/994805263964422144 1072 开学寄语(20 分)提问 下图是上 ...
- windows多线程(三) 原子操作
一.分析上一篇程序的现象 我们先从上一篇文章中的最后一个程序开始分析. #include <stdio.h> #include <windows.h> const unsign ...
- 域名DNS解析说明
一直都对域名DNS 解析很懵逼,今天看到一个博客上面详细的介绍了域名解析. 特意记录下: 记录类型: A记录: 将域名指向一个IPv4地址(例如:8.8.8.8)CNAME:将域名指向另一个域名(例如 ...
- 快速用梯度下降法实现一个Logistic Regression 分类器
前阵子听说一个面试题:你实现一个logistic Regression需要多少分钟?搞数据挖掘的人都会觉得实现这个简单的分类器分分钟就搞定了吧? 因为我做数据挖掘的时候,从来都是顺手用用工具的,尤其是 ...
- jQuery Mobile页面跳转后未加载外部JS原因分析及解决
在使用jQuery Mobile进行Web开发中,当页面跳转时(pageA => pageB),在pageB中引用的JS并未成功运行.因为,JQM并为将整个页面加载到当前的dom中,仅将data ...
- BZOJ5020 THUWC2017在美妙的数学王国中畅游(LCT)
明摆着的LCT,问题在于如何维护答案.首先注意到给出的泰勒展开式,并且所给函数求导非常方便,肯定要用上这玩意.容易想到展开好多次达到精度要求后忽略余项.因为x∈[0,1]而精度又与|x-x0|有关,当 ...
- The Bells are Ringing UVALive - 4060(枚举求解)
输出整数N,使得 t1 <= N 统计有多少组t1,t2,t3,满足:1<t1<t2<t3<=1000000,t3-t1<=25,且t1,t2,t3的最小公倍数 ...
- MT【159】单调有界有极限
已知数列$\{a_n\}$满足:$a_n>0,a_{n+1}+\dfrac{1}{a_n}<2,n\in N^*$.求证:(1)$a_{n+2}<a_{n+1}<2 (n\in ...
- java多线程 -- ConcurrentHashMap 锁分段 机制
hashtable效率低ConcurrentHashMap 线程安全,效率高 Java 5.0 在 java.util.concurrent 包中提供了多种并发容器类来改进同步容器 的性能. Conc ...
- android studio gradle dependencies 包存放在哪儿?
在AndroidStudio中的"External Libraries"下有引用的library的列表, 选择某个library右键->"Library Prope ...