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脚本安装部署反向代理 监控进程 计划任务的更多相关文章

  1. Shell脚本-自动化部署反向代理、WEB、nfs

    部署nginx反向代理三个web服务,调度算法使用加权轮询(由于物理原因只开启两台服务器) AutoNginxNfsService.sh #/bin/bash systemctl status ngi ...

  2. 编写脚本自动部署反向代理、web、nfs

    服务器端 #!/bin/bash function nginx_install(){ if [[ -f /usr/sbin/nginx ]]; then echo 'Nginx has been in ...

  3. Shell脚本一键部署——源码编译安装MySQL及自动补全工具

    Shell脚本一键部署--源码编译安装MySQL及自动补全工具 编译安装MySQL 1.软件包 Mysql安装包 将安装包拖至/opt目录下,编辑一个脚本文件,将以下内容复制进去,然后source或者 ...

  4. Apache 2.4.7在CentOS6.4中安装配置反向代理解决单外网IP对应多个内网主机的方法实践

    欢迎转载,转载时请保留全文及出处. Apache 2.4.7在CentOS6.4中安装配置反向代理解决单外网IP对应多个内网主机的方法实践 Apache安装 下载源程序(http://httpd.ap ...

  5. Docker学习3-简单shell脚本安装mysql5.7与docker小技巧

    前言 玩过Windows中的 .bat 的小伙伴是不是觉得很有意思呢,github中一键推送.同步拉取等等操作,哈哈,当然shell脚本也是很类似,可以运行一个脚本就可以自动给我们部署好环境啦!但是这 ...

  6. shell脚本自动化部署

    由于公司技术部团队较小,没有专门的运维团队,所以运维工作技术部承包了. 一.纯人工部署是这样的: 1. 本地打包:一般 maven clean package 2. 借助xftp上传到服务器对应目录 ...

  7. Nginx的安装及反向代理设置

    因为项目的缘故,接触到了Nginx的安装和反向代理设置,和大家分享下. 一.Nginx的下载.安装cd /homewget http://nginx.org/download/nginx-1.0.5. ...

  8. shell脚本自动化部署服务

    shell脚本自动化部署 !/bin/bash #export PATH=$PATH:/export/maven/bin run_flag_dir="/data0/shell/deploy_ ...

  9. shell脚本安装python、pip--这种写法是错误的---每一个命令执行完都要判断是否执行成功,否则无法进行下一步

    shell脚本安装python.pip--不需要选择安装项目--不管用总报错,必须带上判断符号,while没有这种用法,写在这里为了以后少走弯路,所以不要用下面的执行了 首先把pip-.tgz 安装包 ...

随机推荐

  1. 软工网络15团队作业8——Beta阶段项目总结

    1.新成员 姓名 风格 擅长 角色 宣言 李家俊 乱写 都有所涉猎 测试 混就完事了 丁树乐 潇洒 与人沟通 测试 与其临渊羡鱼,不如退而结网 2.是否需要更换团队的PM 不需要 3.下一阶段需要改进 ...

  2. 软工网络15团队作业8——敏捷冲刺日志的集合贴(Beta阶段)

    Beta阶段 第 1 篇 Scrum 冲刺博客 第 2 篇 Scrum 冲刺博客 第 3 篇 Scrum 冲刺博客 第 4 篇 Scrum 冲刺博客 第 5 篇 Scrum 冲刺博客 第 6 篇 Sc ...

  3. [C/C++] multimap查找一个key对应的多个value

    在multimap中,同一个键关联的元素必然相邻存放.基于这个事实,就可以将某个键对应的值一一输出. 1.使用find和count函数.count函数求出某个键出现的次数,find函数返回一个迭代器, ...

  4. SQLServer 重建索引前后对比 (转)

    https://www.cnblogs.com/mingl12/p/5730178.html

  5. [AT1999] [agc002_e] Candy Piles

    题目链接 AtCoder:https://agc002.contest.atcoder.jp/tasks/agc002_e 洛谷:https://www.luogu.org/problemnew/sh ...

  6. iptables之NAT代理-内网访问外网

    1.前言 本文使用NAT功能:内网服务器,想上网又不想被攻击. 工作原理:内网主机向公网发送数据包时,由于目的主机跟源主机不在同一网段,所以数据包暂时发往内网默认网关处理,而本网段的主机对此数据包不做 ...

  7. 20135319zl软件破解报告

    编写一个简单的C程序.要求只有输入a,才能通过. 现在,使用objdump –d po反汇编这个程序 找到main函数,可以发现movb $0x61,0x1f(%esp)这句语句中是将字符a(对应0x ...

  8. 为smokeping添加日志开启debug

    用包管理工具安装smokeping没有自带日志输出,为了定位问题开启日志就成为第一需求. 1.修改smokeping的配置 # vim /etc/smokeping/config.d/General ...

  9. 单点登录(五)-----遇到问题-----cas server 源码部署tomcat运行报错BeanCreationException:Error creating bean with name 's

    我们在上一篇文章已经解决了把下载好的cas server源码部署到tomcat缺少子项目编辑文件或者jar包导致找不到class报错的问题 单点登录(四)-----遇到问题-----cas serve ...

  10. WinForm查询大数据界面假死,使用异步调用解决

    用DataGridView无分页绑定一个几千条数据的查询,查询的时候界面直接卡死十几秒,用户体验非常不好,因此用异步操作解决界面卡死的问题原本场景:点击[查询]后,界面直接卡死优化场景:点击[查询]后 ...