Python之路-shell&计划任务
开发脚本自动部署及监控
1.编写脚本自动部署反向代理、web、nfs;
要求: I、部署nginx反向代理三个web服务,调度算法使用加权轮询;
II、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;
编写的部署脚本有两个,分别为nginx部署脚本和nfs部署脚本,根据IP检测要部署到的主机是服务器或者是客户端,识别出来后,执行不同的安装配置操作。
a.部署nginx
#!/bin/bash
#Deploy_Web to : deploy nginx
function Pre_Action(){
yum clean all
yum install epel* -y
if [ $? -eq ]
then
echo 'epel_package install right'
else
echo 'epel_package install ERROR!!!'
fi
yum clean all
yum install nginx -y
if [ $? -eq ]
then
echo 'nginx_package install right'
else
echo 'nginx_pacage install ERROR'
fi
}
function Close_Wall(){
systemctl stop firewalld #close firewall
setenforce #close selinux
iptables –L #clear tables
systemctl disable firewalld #disable
}
function Restart_service(){
systemctl start nginx #start nginx
if [ $? -eq ]
then
echo 'nginx_service start!!!'
else
systemctl restart nginx
if [ $? -eq ]
then
echo 'nginx_service start!!!'
else
echo 'nginx_servive is not good!!!'
fi
fi
}
function Cilent_conf(){
Pre_Action
Close_Wall
Restart_service
}
function Server_conf(){
Cilent_conf
msg='upstream mserver{ server 192.168.0.115 weight=3;server 192.168.0.116;server 192.168.0.117; }' #访问web115的权重为3
sed -ri "/^http/a $msg" /etc/nginx/nginx.conf #增加upstream
sed -ri "/^ *location \/ \{$/a proxy_pass http://mserver\;" /etc/nginx/nginx.conf #修改localtion
systemctl restart nginx #重启nginx服务
} ipaddr=`ifconfig|awk 'NR==2{print $2}'`
echo $ipaddr
if [ "$ipaddr" = '192.168.0.114']
then
Server_conf
else
Cilent_conf
fib.部署nfs
#!/bin/bash
#start nfs_service
function nfs_pre(){
yum clean all
yum install rpcbind nfs-utils -y
if [ $? -eq ]
then
echo 'nfs_package install right!!!'
else
echo 'nfs_package install ERROR!!!'
exit
fi
} function Server_conf(){
mkdir /share
echo '/share 192.168.0.0/24(rw,sync,fsid=0)' > /etc/exports
# head - /etc/exports
chmod -R /share
}
function Client_conf(){
# mkdir /data_disk
mount -t nfs 192.168.0.118:/share /usr/share/nginx/html
if [ $? -eq ]
then
echo 'mount successful!!!'
else
echo 'mount failed!!!'
exit
fi
} function nfs_start(){
systemctl enable nfs-server.service
systemctl enable rpcbind.service systemctl start rpcbind.service
if [ $? -eq ]
then
echo 'rpcbind start!!!'
else
systemctl restart rpcbind.service
if [ $? -eq ]
then
echo 'rpcbind_service start!!!'
else
echo 'rpcbind_servive is not good!!!'
fi
fi
systemctl start nfs-server.service
if [ $? -eq ]
then
echo 'nfs-server start!!!'
else
systemctl restart nfs-server.service
if [ $? -eq ]
then
echo 'nfs-server start!!!'
else
echo 'nfs-server is not good!!!'
fi
fi
exportfs
}
ipaddr=`ifconfig|awk 'NR==2{print $2}'`
echo $ipaddr
if [ "$ipaddr" = '192.168.0.118' ]
then
nfs_pre
Server_conf
nfs_start
else
nfs_pre
nfs_start
Client_conf
systemctl restart nginx
if [ $? -eq ]
then
echo 'nfs-server restart!!!'
else
echo 'nfs-server restart fail!!!'
fi
fic.在部署了一台nginx反向代理服务器,三台web服务器和一台nfs共享存储服务器后,在共享服务器的/share目录下创建了文件suc.txt。用浏览器访问nginx反向代理服务器的IP验证。
2.编写监控脚本,监控集群内所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件
a.要监控的服务有nginx和nfs是否关闭,还监控着内存和磁盘剩余率,其中磁盘剩余分为inode使用情况和内存空间使用情况
b.第三方邮件
[root@ym script]# vim /usr/bin/my_mail具体使用参考 http://www.cnblogs.com/linhaifeng/p/6602149.html#_label7
c.监控脚本如下
#!/bin/bash
cpu_limit= #cpu使用超过50%
mem_limit= #内存使用超过50%发送邮件
disk='/dev/sda1' #需要监控的磁盘名
disk_inode_limit= #磁盘inode使用
disk_space_limit= #磁盘空间使用
function m_nginx(){
ps aux |grep nginx|grep -v grep &> /dev/null
if [ $? -eq ]
then
echo 'nginx is normal'
else
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(/usr/sbin/ifconfig|awk 'NR==2{print $2}')
MSG:nginx is stop!!!"
echo $msg
/usr/bin/my_mail $msg
fi
} function m_nfs(){
ps aux |grep nfs|grep -v grep &> /dev/null
if [ $? -eq ]
then
echo 'nfs is normal'
else
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(/usr/sbin/ifconfig|awk 'NR==2{print $2}')
MSG:nfs is stop!!!"
echo $msg
/usr/bin/my_mail $msg
fi
}
function m_mem(){
mem_total=`free |awk 'NR==2{print $2}'`
mem_use=`free |awk 'NR==2{print $3}'`
mem_per=`echo "scale=2;$mem_use/$mem_total" |bc -l|cut -d. -f2`
if [ $mem_per -gt $mem_limit ]
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 m_disk_inode(){
inode_use=`df -i $disk |awk 'NR==2{print $5}' |cut -d% -f1`
if [ $inode_use -gt $disk_inode_limit ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
MSG:Disk inode usage exceeds the limit,current value is ${inode_use}%"
echo $msg
/usr/bin/my_mail $msg
fi
} function m_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:$(/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
}
m_nginx &>> /tmp/monitor.log
m_nfs &>> /tmp/monitor.log
m_mem &>> /tmp/monitor.log
m_disk_inode &>> /tmp/monitor.log
m_disk_space &>> /tmp/monitor.logd.执行验证
3.编写计划任务,定时运行监控脚本,完成监控操作
a.制定计划任务
[root@ym script]# crontab -e -u rootb.计划任务内容
* * * * * command
分 时 日 月 周 具体参考 man 5 crontab
注意:尽量全部使用绝对路径(包括命令,也使用命令的绝对路径)
c.显示已经制定的计划任务
[root@ym script]# crontab -l
d.监控正在执行的计划任务
[root@ym script]# tail -f /var/log/cron
e. 邮件验证
Python之路-shell&计划任务的更多相关文章
- python之路,Day24 常用设计模式学习
python之路,Day24 常用设计模式学习 本节内容 设计模式介绍 设计模式分类 设计模式6大原则 1.设计模式介绍 设计模式(Design Patterns) --可复用面向对象软件的基础 ...
- Python之路,Day21 - 常用算法学习
Python之路,Day21 - 常用算法学习 本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...
- Python之路,Day15 - Django适当进阶篇
Python之路,Day15 - Django适当进阶篇 本节内容 学员管理系统练习 Django ORM操作进阶 用户认证 Django练习小项目:学员管理系统设计开发 带着项目需求学习是最有趣 ...
- Python之路,Day14 - It's time for Django
Python之路,Day14 - It's time for Django 本节内容 Django流程介绍 Django url Django view Django models Django ...
- 小白的Python之路 day1
Python之路,Day1 - Python基础1 本节内容 Python介绍 发展史 Python 2 or 3? 一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van ...
- NO.3:自学python之路------集合、文件操作、函数
引言 本来计划每周完成一篇Python的自学博客,由于上一篇到这一篇遇到了过年.开学等杂事,导致托更到现在.现在又是一个新的学期,春天也越来越近了(冷到感冒).好了,闲话就说这么多.开始本周的自学Py ...
- python之路-进程
博客园 首页 新随笔 联系 管理 订阅 随笔- 31 文章- 72 评论- 115 python之路——进程 阅读目录 理论知识 操作系统背景知识 什么是进程 进程调度 进程的并发与并行 ...
- Python之路【第一篇】python基础
一.python开发 1.开发: 1)高级语言:python .Java .PHP. C# Go ruby c++ ===>字节码 2)低级语言:c .汇编 2.语言之间的对比: 1)py ...
- Python之路
Python学习之路 第一天 Python之路,Day1 - Python基础1介绍.基本语法.流程控制 第一天作业第二天 Python之路,Day2 - Pytho ...
随机推荐
- Java排序小算法(冒泡和选择)
package MyTest; import java.util.Scanner; public class BubbleSort { public void Init(int array[]) { ...
- JavaScript中的this关键字的用法和注意点
JavaScript中的this关键字的用法和注意点 一.this关键字的用法 this一般用于指向对象(绑定对象); 01.在普通函数调用中,其内部的this指向全局对象(window); func ...
- TJOI2015 day1解题报告
博客园的编辑器真的是太蛋疼了= =,想用tex然后上jpg又贴不了链接,真的很纠结啊= = T1:[TJOI2015]线性代数 描述:戳上面吧= = 首先这道题我觉得是这套题最漂亮的一道题了(虽然说学 ...
- 基于ES6模块标准通过webpack打包HTM5项目
本篇主要演示通过webpack打包phaser项目,webpack安装方法在此处就不一一赘述了 经常用phaser来写html5游戏的朋友可能会发现,当游戏场景比较多时,如果都写在一个js文件中那么将 ...
- angular : ngModel 内部流程
angular 1.5 beta link NgModelController provides API for the ngModel directive. The controller conta ...
- 【Zookeeper】源码分析之Leader选举(二)
一.前言 前面学习了Leader选举的总体框架,接着来学习Zookeeper中默认的选举策略,FastLeaderElection. 二.FastLeaderElection源码分析 2.1 类的继承 ...
- python打印表格式数据,留出正确的空格和段落星号或注释
python打印表格式数据,留出正确的空格,格式化打出 代码如下: def printPicnic(itemsDict,leftWidth,rightWidth): print('PICNIC ITE ...
- [PKU2389]Bull Math (大数运算)
Description Bulls are so much better at math than the cows. They can multiply huge integers together ...
- AR入门系列-07-Vuforia柱形体识别
今天为大家带来Vuforia柱形体识别的使用教程 首先我们要进入Vuforia官网在TargetManager中添加Target,这次我们添加的类型为Cylinder圆柱 Bottom Diamete ...
- HttpServletRequest 各种方法总结
HttpServletRequest HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象 ...




