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. PAT 甲级 1155 Heap Paths

    https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 In computer science, ...

  2. TADOConnection.Close - connection still active on MS-SQL server

    I have several Delphi programs (XE3), that use a TADOConnection to connect to a MS-SQL Server. I rec ...

  3. web.config文件详解[转]

    一).Web.Config是以XML文件规范存储,配置文件分为以下格式1.配置节处理程序声明特点: 位于配置文件的顶部,包含在<configSections>标志中.2.特定应用程序配置特 ...

  4. linux下安装jenkins

    我们不用离线安装方式 第一步.必须验证java环境 第二步.我们这里使用yum命令进行在线安装,使用service命令进行启动 1.wget -O /etc/yum.repos.d/jenkins.r ...

  5. SPOJ_LCS

    经典题目,求两个串的最长公共子串. 是这样来做的. 以第一个串构造SAM,第二个串在自动机上跟新一遍就可以了. 更新的过程是这样的,假设当前到达的状态点为x(初始状态为0点),下一个字符是c,如果当前 ...

  6. JVM学习笔记(一):Java内存区域

    由于Java程序是交由JVM执行的,所以我们在谈Java内存区域划分的时候事实上是指JVM内存区域划分.在讨论JVM内存区域划分之前,先来看一下Java程序具体执行的过程: 首先Java源代码文件(. ...

  7. linux 实践到的命令 collection

    查看文件夹/文件 大小:du   :(disk usage) 要通过 1024 字节块概述一个目录树及其每个子树的磁盘使用情况,请输入: du -k /home/fran/filename 这在/ho ...

  8. [AT1999] [agc002_e] Candy Piles

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

  9. UVA.10791 Minimum Sum LCM (唯一分解定理)

    UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...

  10. 解题:POI 2007 Tourist Attractions

    题面 事实上这份代码在洛谷过不去,因为好像要用到一些压缩空间的技巧,我并不想(hui)写(捂脸) 先预处理$1$到$k+1$这些点之间相互的最短路和它们到终点的最短路,并记录下每个点能够转移到时的状态 ...