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 #执行启动服务函数
#!/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/
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
#!/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 %s' %(' '.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.编写计划任务,定时运行监控脚本,完成监控操作
#!/user/bin/env python #coding:utf-8 crontab -e -u root * * * * * /mem.sh restart #每分钟检测一次
python2.6.6升级2.7:生产环境
python升级导致yum无法使用方法:传送门