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

I、部署nginx反向代理两个web服务,调度算法使用加权轮询

II、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;

proxy      192.168.11.62     #nginx代理,nfs服务端
web01 192.168. 11.74 #web01,nfs客户端
web02 192.168.11.75 #web02,nfs客户端 #poxy上安装nginx代理,nfs客户端,脚本如下
[root@proxy mnt]# cat install_nginx.sh
#!/bin/bash
#insatll nginx proxy IP=`ifconfig | awk -F" " '/inet/{print $2}' | head -1` #insyall nginx proxy
function install_nginx() {
yum remove nginx -y
yum install nginx -y
touch /etc/nginx/conf.d/proxy.conf
cat > /etc/nginx/conf.d/proxy.conf <<EOF
upstream web {
server 192.168.11.74;
server 192.168.11.75;
} server {
listen 80;
server_name $IP;
location / {
proxy_pass http://web;
}
}
EOF
systemctl start nginx
} #install nfs server
function install_nfs_server() {
yum install rpcbind nfs-utils -y
[ -d /share ] || mkdir /share && chmod o+w /share
echo "/share 192.168.11.62/24(rw,sync,fsid=0)" > /etc/exports
systemctl start rpcbind.service
systemctl start nfs-server.service
}
while :
do
read -p "please choice your install server{nginx|nfs}: " server
if [ $server = "nginx" ]
then
install_nginx
elif [ $server = "nfs" ]
then
install_nfs_server
else
exit
fi
done #web01安装nginx web 和nfs客户端
[root@web01 mnt]# cat install_nginx.sh
#!/bin/bash
#insatll nginx proxy IP=`ifconfig | awk -F" " '/inet/{print $2}' | head -1` #insyall nginx proxy
function install_nginx() {
yum install nginx -y
echo "welcome to web01" >/usr/share/nginx/html/index.html
systemctl start nginx
} #install nfs server
function install_nfs_server() {
yum install rpcbind nfs-utils -y
systemctl start rpcbind.service
systemctl start nfs-server.service
mount -t nfs 192.168.11.62:/share /usr/share/nginx/html/
}
while :
do
read -p "please choice your install server{nginx|nfs}: " server
if [ $server = "nginx" ]
then
install_nginx
elif [ $server = "nfs" ]
then
install_nfs_server
else
exit
fi
done

2.编写监控脚本,监控集群内所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件

步骤一:准备发送邮件的工具

#!/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'] = '你是风儿我是沙,缠缠绵绵回我家'
msg['From'] = 'python4_mail@163.com'
msg['To'] = 'python4_recvmail@163.com'
user = 'python4_mail'
pwd = 'sbalex3714'
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)

步骤二:将上述文件内容拷贝到/usr/bin/mail并chmod+x /usr/bin/mail

步骤三:然后新建监控脚本servermonitor.sh

#!/bin/sh

function ngxMonitor(){  #监控nginx服务
ps aux | grep nginx| grep -v grep &>/dev/null
if [ $? -ne 0 ];then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
MSG:Nginx program is crash, Waiting to restart"
echo $msg
/usr/bin/my_mail $msg
systemctl restart nginx
fi
} function nfsMonitor(){ #监控nfs服务
ps aux | grep nfs| grep -v grep &>/dev/null
if [ $? -ne 0 ];then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
MSG:NFS program is crash, Waiting to restart"
echo $msg
/usr/bin/my_mail $msg
systemctl restart nginx
fi
} function memMonitor(){ #监控内存
mem_use=`free | awk 'NR==2{print $3}'`
mem_total=`free | awk 'NR==2{print $2}'`
mem_per=`echo "scale=2;$mem_use/$mem_total"|bc -l |cut -d . -f2` if [ ! -e /usr/bin/bc ];then
yum install bc -y -q
echo "bc install successful"
fi
if (( $mem_per > 10 )); then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
MSG:Memory usage exceeds the limit,current value is ${mem_per}%"
echo $msg
/usr/bin/my_mail $msg
fi
} function diskMonitor(){ #监控磁盘
space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1` if [ $space_use -gt 80 ];then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
MSG:Disk space usage exceeds the limit,current value is ${space_use}%"
echo $msg
/usr/bin/my_mail $msg
fi
} ngxMonitor &>>/tmp/monitor.log
nfsMonitor &>>/tmp/monitor.log
memMonitor &>>/tmp/monitor.log
diskMonitor &>>/tmp/monitor.log

步骤四:计划任务

* * * * * /shell/sysCheck.sh
 

shell脚本编写-自动部署及监控的更多相关文章

  1. shell脚本编写nginx部署脚本

    下面为shell脚本编写的nginx的安装及修改nginx.conf的脚本,脚本比较简单: #!/bin/bash function yum_install(){ yum install epel-r ...

  2. Shell脚本——DNS自动部署

    详细说明查看: (一)跟我一起玩Linux网络服务:DNS服务——BIND(/etc/named.conf./var/named)设置实现和解释 #! /bin/bash IP="10.10 ...

  3. Shell脚本——DHCP自动部署

    详细说明参考: (三)跟我一起玩Linux网络服务:DHCP服务配置之主服务器配置 #! /bin/bash IPSAG="10.10.10" DNSIP="10.10. ...

  4. Shell awk文本处理,shell脚本编写

    Shell awk文本处理,shell脚本编写 一:内容包含awk.变量.运算符.if多分支 <a>语法糖: awk [options] 'commands' files option - ...

  5. linux开发脚本自动部署及监控

    linux开发脚本自动部署及监控 开发脚本自动部署及监控一.编写脚本自动部署反向代理.web.nfs:要求:1.部署nginx反向代理三个web服务,调度算法使用加权轮询: #!/bin/sh ngx ...

  6. linux基础 -nginx和nfs代理 开发脚本自动部署及监控

    开发脚本自动部署及监控 1.编写脚本自动部署反向代理.web.nfs: (1).部署nginx反向代理三个web服务,调度算法使用加权轮询:  (2).所有web服务使用共享存储nfs,保证所有web ...

  7. Dubbo入门到精通学习笔记(二):Dubbo管理控制台、使用Maven构建Dubbo的jar包、在Linux上部署Dubbo privider服务(shell脚本)、部署consumer服务

    文章目录 Dubbo管理控制台 1.Dubbo管理控制台的主要作用: 2.管理控制台主要包含: 3.管理控制台版本: 安装 Dubbo 管理控制台 使用Maven构建Dubbo服务的可执行jar包 D ...

  8. shell脚本编写mind

    首先我们要缩小这个问题的范围 如果在面试中被问到这样的问题,说说shell脚本编写思路 如:你是在公司主要负责zabbix监控的 对答如下:shell脚本这个说法挺广的,您看我这么跟您说吧,我在平时工 ...

  9. shell脚本编写自动启动服务方法

    shell脚本编写自动启动服务方法 前言 ln :创建连接文件 默认创建的是硬连接,好比复制 ,但是两个文件会同步命令:ln ./java/android/aa.txt aaa s :创建的是软连接变 ...

随机推荐

  1. CS231n笔记 Lecture 3 Loss Functions and Optimization

    这一讲总体上就是引入Loss Function的概念,以及让大家对优化有一个初步的认识,和其他课程里面说的内容大同小异. Loss function Multiclass svm loss multi ...

  2. 【Luogu】P2389电脑班的裁员(DP)

    题目链接 sbt交了三遍才过是我的耻辱…… 就是设f[i][j]搞个三重循环DP一下,以上. #include<cstdio> #include<cstdlib> #inclu ...

  3. 二进制<2>

    位运算简介及实用技巧(二):进阶篇(1) =====   真正强的东西来了!   ===== 二进制中的1有奇数个还是偶数个    我们可以用下面的代码来计算一个32位整数的二进制中1的个数的奇偶性, ...

  4. hihoCoder #1072 辅导

    题意 $\DeclareMathOperator{\lcm}{lcm}$选 $k$ ($k\le 10$) 个 $1$ 到 $n$($n\le 10^9$)之间的整数(可以相同),使得 $\lcm(a ...

  5. BZOJ 4818 [Sdoi2017]序列计数 ——矩阵乘法

    发现转移矩阵是一个循环矩阵. 然后循环矩阵乘以循环矩阵还是循环矩阵. 据说还有FFT并且更优的做法. 之后再看吧 #include <map> #include <cmath> ...

  6. 【Visual Studio】error C2220: 警告被视为错误 - 没有生成“object”文件 (转)

    原文转自 http://www.cnblogs.com/kex1n/archive/2011/10/19/2217266.html [错误原因] 该文件的代码页为英文,而我们系统中的代码页为中文. [ ...

  7. 22深入理解C指针之---通过指针传递函数

    一.通过指针传递函数与通过指针传递数据的本质是一样的,区别就是此时的数据是函数指针(函数的开始的地址) 1.定义:通过函数指针将函数传入函数:通过函数返回函数指针实现函数返回函数的目标 2.特征: 1 ...

  8. LeetCode OJ--Median of Two Sorted Arrays ***

    http://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 找两个有序数组的中位数,因为有序数组,并且复杂度要求O(lg(m+n))所以想 ...

  9. Objective_C与Swift混编遇到的坑(一)

    swift推出已经很长一段时间了,前段时间突然想尝试一些简单的类用swift编写于是便开始了混编的路程. 1.在oc代码里引用swift类:找了很多资料需要添加头文件格式为 #import " ...

  10. Vijos——P1137 组合数

    https://vijos.org/p/1137 描述 组合公式 C=N!/(M!*(N-M)!). 问题是求 C 中不同的质因子的个数例如 N=7, M=4. C=7!/(3!*4!)=5040/( ...