linux开发脚本自动部署及监控
linux开发脚本自动部署及监控
开发脚本自动部署及监控
一、编写脚本自动部署反向代理、web、nfs;
要求:
1、部署nginx反向代理三个web服务,调度算法使用加权轮询;
#!/bin/sh
ngxStatus=`ps aux | grep -v grep |grep -c nginx`
function ngxProxyInstall() {
if [ -e /usr/sbin/nginx ];then
echo "nginx already installed"
exit 110
else
yum install epel-release -y -q
yum install gcc-* glibc-* openssl openssl-devel pcre pcre-devel zlib zlib-devel -y -q
yum install nginx -y -q
echo "install nginx successful"
fi
if [ -f /etc/nginx/nginx.conf ];then
/bin/cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sed -ri '/^http/a\\t upstream luchuangao { \n\t server 192.168.10.27 weight=3;\n\t server 192.168.10.28;\n\t server 192.168.10.29;\n\t }' /etc/nginx/nginx.conf
sed -ri '/^ *location \/ \{/a\\t\t proxy_pass http://luchuangao;' /etc/nginx/nginx.conf
echo "Configuration successful"
fi
if [ $ngxStatus -lt 2 ];then
systemctl start nginx
echo "Start nginx successful"
fi
}
function nfsInstall() {
if [ -e /usr/sbin/rpcinfo ];then
echo "nfs already installed"
exit 111
else
yum install rpcbind nfs-utils -y -q
echo "install NFS successful"
fi
if [ ! -d /share ];then
mkdir -p /share
chmod -R o+w /share
fi
echo '/share 192.168.10.0/24(rw,sync,fsid=0)' > /etc/exports
systemctl enable rpcbind.service
systemctl enable nfs-server.service
systemctl start rpcbind.service
systemctl start nfs-server.service
echo "Start NFS successful"
}
ngxProxyInstall
nfsInstall
三个web服务加权轮询
2、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;
#!/bin/sh ngxStatus=`ps aux | grep -v grep |grep -c nginx`
ipAddress="192.168.10.26" function ngxWebInstall(){
if [ -e /usr/sbin/nginx ];then
echo "nginx already installed"
exit 110
else
#yum install gcc-* glibc-* openssl openssl-devel pcre pcre-devel zlib zlib-devel -y -q
yum install nginx -y -q
echo "install nginx successful"
fi
if [ -f /etc/nginx/nginx.conf ];then
/bin/cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sed -ri '/^ *location \/ \{/a\\t\t root /data/www/html;\n\t\t index index.html;' /etc/nginx/nginx.conf
mkdir -p /data/www/html
echo `hostname` > /data/www/html/index.html
echo "Configuration successful"
fi
if [ $ngxStatus -lt 2 ];then
systemctl start nginx
echo "Start nginx successful"
fi
} function nfsInstall(){
if [ ! -e /usr/sbin/rpcinfo ];then
yum install rpcbind nfs-utils -y -q
echo "install NFS successful"
fi
systemctl enable rpcbind.service
systemctl enable nfs-server.service
systemctl start rpcbind.service
systemctl start nfs-server.service
mount -t nfs $ipAddress:/share /data/www/html/
echo "welcome luchuangao" > /data/www/html/test.html
} ngxWebInstall
nfsInstall
共享nfs,数据一致
二、编写监控脚本,监控集群内所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件
步骤一:准备发送邮件的工具
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text server = 'smtp.163.com'
port = '' 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('邮件发送成功 电子邮件已发送!') 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)
检测异常
步骤二:将上述文件内容拷贝到/usr/bin/my_mail
chmod+x /usr/bin/my_mail
步骤三:然后新建监控脚本
vim sysCheck.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
sysCheck.sh
三、编写计划任务,定时运行监控脚本,完成监控操作
* * * * * /shell/sysCheck.sh
linux开发脚本自动部署及监控的更多相关文章
- linux基础 -nginx和nfs代理 开发脚本自动部署及监控
开发脚本自动部署及监控 1.编写脚本自动部署反向代理.web.nfs: (1).部署nginx反向代理三个web服务,调度算法使用加权轮询: (2).所有web服务使用共享存储nfs,保证所有web ...
- 脚本自动部署及监控 web
1.编写脚本自动部署反向代理.web.nfs: I.部署nginx反向代理两个web服务,调度算法使用加权轮询 II.所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性: ...
- shell脚本自动部署及监控
一.shell脚本部署nginx反向代理和三个web服务 1 对反向代理服务器进行配置 #!/bin/bash #修改用户交互页面 用户输入参数执行相应的参数 #安装epel扩展包和nginx fun ...
- linux下实现自动部署tomcat的脚本
linux下实现自动部署tomcat的脚本 由于经常部署war到tomccat上,经常有一些重复的工作要做:停服务.备份war包.上传新的war包.启动服务.索性就写了一个自动部署的脚本. 脚本如下a ...
- shell脚本编写-自动部署及监控
1.编写脚本自动部署反向代理.web.nfs: I.部署nginx反向代理两个web服务,调度算法使用加权轮询 II.所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性: ...
- 010-- 开发脚本自动部署nginx_web和nfs及监控内存
1.编写脚本自动部署反向代理.web.nfs: #!/bin/bash #检测安装nginx function detection_nginx(){ if [ -f /etc/nginx/nginx. ...
- Azure vm 扩展脚本自动部署Elasticsearch集群
一.完整过程比较长,我仅给出Azure vm extension script 一键部署Elasticsearch集群的安装脚本,有需要的同学,可以邮件我,我给你完整的ARM Template 如果你 ...
- 10分钟实现dotnet程序在linux下的自动部署
背景 一直以来,程序署都是非常麻烦且无聊的事情,在公司一般都会有 devops 方案,整个 cicd 过程涉及的工具还是挺多的,搭建起来比较麻烦.那么对于一些自己的小型项目,又不想搭建一套这样的环境, ...
- ceph脚本-自动部署计算机节点
依然还在加班中,最近确实忙的脚打后脑勺! 又花了些时间丰富ceph脚本,可以连带着自动部署计算机节点了. 这一部分内容是后加的.可以关注我的公众号获取更多的项目代码和讲解!波神与你同行哦,加油!!!
随机推荐
- C语言复制数组
直接上代码,分别是栈上开辟内存,堆上开辟内存 #include <stdio.h> #include <stdlib.h> void test01(){ ]; printf(& ...
- 留下来做项目经理还是跳槽学Java
毕业两年了,曾经给自己计划工作两年后跳一次槽,去尝试学习更多的东西.2012年7月5日入职,现在整整两年,最近面临这样的一个抉择:是留在公司继续做项目经理,还是跳槽去学习Java. 我的基本情况:本科 ...
- Spring MVC(二)--Spring MVC登陆实例
本文通过一个简单的登陆实例实现Spring MVC的流程,同时整合 MyBatis使用,流程是这样的: 1.访问一个URL进入登陆界面 2.输入正确的用户名和密码,成功则进入index页面,否则留在登 ...
- CentOS 6.5 usb安装
我只说一下将CentOS安装作为服务器的情况,但是适用于普遍的CentOS安装过程,首先下载CentOS的安装镜像文件,有这么几种:CentOS-6.5-x86_64-LiveCD.CentOS-6. ...
- alter table 修改表结构规范
use database_name; ) )), ADD INDEX index_time ( `timeId` ); # 添加主键: alter table table_name add prima ...
- Liferay 7:Liferay DXP全套教程内附源码
分享是美德 都是英文教程,有不明白的问题可以随时咨询我. http://www.javasavvy.com/liferay-7-hooks-tutorials/
- Python 字符串与二进制串的相互转换
def encode(s): return ' '.join([bin(ord(c)).replace('0b', '') for c in s]) def decode(s): return ''. ...
- css上下左右居中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Git 对已经加入版本控制的文件,修改后希望不被提交办法
参考网址:http://my.oschina.net/zlLeaf/blog/197740 问题举例:假设网站有一个数据库配置文件db.php,通过git做版本控制,已经将这个文件提交到git库中.但 ...
- Windows 禁用Windows updata服务
方法一:禁用Windows updata服务 按WIN+R 打开运行,输入 services.msc 回车 然后找到 “Windows updata”服务,双击后设置为禁用 应用即可; 方法二:推迟自 ...