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

#!/bin/bash
#检测安装nginx
function detection_nginx(){
if [ -f /etc/nginx/nginx.conf ]
then
echo 'nginx has been installed'
exit
else
then
yum install epel-release -y
yum install nginx -y
echo 'nginx successfully installed'
fi
} #改写配置文件
function rewrite_nginx_conf(){
msg='upstream my_upstream{ server 192.168.19.129;server 192.168.19.130;server 192.168.19.131}'
sed -ri "/^http/a $msg" /etc/nginx/nginx.conf #增加upstream
sed -ri "/^ *location \/ \{$/a proxy_pass http://my_upstream\;" /etc/nginx/nginx.conf #修改localtion
#重新加载nginx
systemctl reload nginx
} #检测安装nfs和rpcbind
function detection_nfs(){
if [ -d /var/lib/nfs ]
then
echo 'nfs has been installed'
exit
else
then
yum install rpcbind nfs-utils -y
echo 'rpcbind nfs-utils successfully installed'
fi
} function start_service(){
#创建共享目录
mkdir /share
#给用户增加写的权限
chmod -R o+w /share/
#改写nfs的配置文件
echo '/share 192.168.19.0/24(rw,sync,fsid=0)' >> /etc/exports
#启动服务
systemctl start rpcbind.server
systemctl start nfs-utils.server
#设置开机启动
systemctl enable nfs-server.service
systemctl enable rpcbind.service
} detection_nginx #执行检测安装nginx函数
rewrite_nginx_conf #执行改写nginx.conf函数
detection_nfs #执行检测安装nfs函数
start_service #执行启动服务函数

服务端 Code

#!/bin/bash
#检测安装nginx
function detection_nginx(){
if [ -f /etc/nginx/nginx.conf ]
then
echo 'nginx has been installed'
exit
else
then
yum install epel-release -y
yum install nginx -y
echo 'nginx successfully installed'
fi
} #改写配置文件
function rewrite_nginx_conf(){
sed -ri "/^ *location \/ \{$/a root /var/www/html\;index index.html\;" /etc/nginx/nginx.conf
touch /var/www/html/index.html
echo 'web1 hello world' >> /var/www/html/index.html #写入文件内容
systemctl restart nginx
} #检测安装nfs和rpcbind
function detection_nfs(){
if [ -d /var/lib/nfs ]
then
echo 'nfs has been installed'
exit
else
then
yum install rpcbind nfs-utils -y
echo 'rpcbind nfs-utils successfully installed'
fi
} detection_nginx #执行检测安装nginx函数
rewrite_nginx_conf #执行改写nginx.conf函数
detection_nfs #执行检测安装nfs函数 #挂载共享目录
showmount -e 192.168.19.128
mount -t nfs 192.168.19.128:/share /var/www/html/

客户端 Code

2.编写监控脚本,监控集群内所有服务存活状态,内存,异常则发送报警邮件

#!/bin/bash
mem_limit=30 #内存使用超过30%则报警
#监控内存
mem_total=`free |awk 'NR==2{print $2}'`
mem_use=`free |awk 'NR==2{print $3}'`
memory_usage=`echo "scale=2;$mem_use/$mem_total" |bc -l|cut -d. -f2`
IP=`ifconfig |awk 'NR==2{print $2}'`
if [ $memory_usage -gt $mem_limit ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$IP
MSG:Memory usage exceeds the limit,current value is ${memory_usage}%"
echo $msg
/usr/bin/my_mail $msg fi

监控 Code

#!/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('邮件发送成功 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)

my_mail Code

3.编写计划任务,定时运行监控脚本,完成监控操作

#!/user/bin/env python
#coding:utf-8
crontab -e -u root * * * * * /mem.sh restart #每分钟检测一次

crontab Code


python2.6.6升级2.7:生产环境

python升级导致yum无法使用方法:传送门

最新文章

  1. 文件描述符、文件表项指针、inode节点的关系
  2. MySQL 分区介绍总结
  3. 简析 .NET Core 构成体系
  4. Discuz!用户注册,登陆,生成帖子功能实现
  5. 使用rem来开发你的移动端网站
  6. 多年前写的文本框扩展控件(有ValueChanging事件等),已放github
  7. JS函数arguments数组获得实际传参数个数
  8. iOS nib file owner
  9. C# IEnumerable,Lambda表达式和 Parallel并行编程的用法
  10. checkbox与说明文字无法对齐的问题
  11. struts2:JSP页面及Action中获取HTTP参数(parameter)的几种方式
  12. [vim]设置vim语法高亮显示和自动缩进
  13. Spring Boot实现一个监听用户请求的拦截器
  14. springMVC整合memcached,以注解形式使用
  15. [BZOJ 3791] 作业 【DP】
  16. 源码(08) -- java.util.ListIterator<E>
  17. python报错“UnicodeEncodeError: 'ascii' codec can't encode characters in position 22-26: ordinal not in range(128)”问题解决
  18. java 面向对象 2
  19. nginx 用来做什么?
  20. spring+shiro+ehcache整合

热门文章

  1. 【sourcetree】sourcetree连接远程仓库需要登陆但是一直登陆不上的问题 解决方法
  2. 取汉子拼音首字母的C#方法
  3. [React] Create and import React components with Markdown using MDXC
  4. Python常用的模块
  5. android-----JNI中的log打印
  6. location.href用法总结
  7. 浅谈JavaScript的Canvas(绘制图形)
  8. HUNNU-10307-最优分解问题
  9. Python 001- 将URL中的汉字转换为url编码
  10. rpm的gpg key