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. ORACLE:一列的多行数据拼成字符串

    查询表中的一个字段,返回了多行,就把这么多行的数据都拼成一个字符串. 例:   id  name        1   aa        2   bb        3   cc 要的结果是&quo ...

  2. springmvc上传文件报错org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]

    在用springmvc+mybatis进行项目开发时,上传文件抛异常... org.springframework.beans.BeanInstantiationException: Could no ...

  3. Caffe使用step by step:faster-rcnn目标检测matlab代码

    faster-rcnn是MSRA在物体检测最新的研究成果,该研究成果基于RCNN,fast rcnn以及SPPnet,对之前目标检测方法进行改进,faster-rcnn项目地址.首先,faster r ...

  4. POJ1815_Friendship

    一个无向图,问你删除多少点后,可以隔断起点到终点的所有路径?输出字典序最小的删点方案. 求最小点割,先拆点,容量为1,普通边容量无穷,最大流即为应删点数. 需要求出字典序最小的方案,可以从小到大枚举所 ...

  5. UVAlive5135_Mining Your Own Business

    好题.给一个无向图,求最少染黑多少个点后,使得任意删除一个点,每一个点都有与至少一个黑点联通. 一开始的确不知道做.看白书,对于一个联通分量,如果它有两个或以上的割点,那么这个分量中间的任何一个点都是 ...

  6. 【bzoj1087】互不侵犯King

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  7. USACO Section 1.5 Prime Palindromes 解题报告

    题目 题目描述 题目就是给定一个区间[a,b]((5 <= a < b <= 100,000,000)),我们需要找到这个区间内所有既是回文串又是素数的数字. 输入样例 5 500 ...

  8. 第三周 构造一个简单的Linux系统

    20135331文艺 首先 在上周内容中我们学习了 计算机三个法宝: 1.存储程序计算机 2.函数调用堆栈 3.中断 本周中得知 操作系统两把宝剑: 1.中断上下文的切换:保存现场和恢复现场 2.进程 ...

  9. 配置nginx为FastDFS的storage server提供http访问接口

    1.拉取模块代码 # git clone https://github.com/happyfish100/fastdfs-nginx-module.git 2.编译安装nginx,添加支持fastdf ...

  10. 解题:CQOI 2013 和谐矩阵

    题面 踩踩时间复杂度不正确的高斯消元 首先可以发现第一行确定后就可以确定整个矩阵,所以可以枚举第一行的所有状态然后$O(n)$递推检查是否合法 $O(n)$递推的方法是这样的:设$pre$为上一行,$ ...