开发脚本自动部署及监控
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 0 ] then echo 'epel_package install right' else echo 'epel_package install ERROR!!!' fi yum clean all yum install nginx -y if [ $? -eq 0 ] then echo 'nginx_package install right' else echo 'nginx_pacage install ERROR' fi } function Close_Wall(){ systemctl stop firewalld #close firewall setenforce 0 #close selinux iptables –L #clear tables systemctl disable firewalld #disable } function Restart_service(){ systemctl start nginx #start nginx if [ $? -eq 0 ] then echo 'nginx_service start!!!' else systemctl restart nginx if [ $? -eq 0 ] 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 0 ] 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 -1 /etc/exports chmod -R 777 /share } function Client_conf(){ # mkdir /data_disk mount -t nfs 192.168.0.118:/share /usr/share/nginx/html if [ $? -eq 0 ] 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 0 ] then echo 'rpcbind start!!!' else systemctl restart rpcbind.service if [ $? -eq 0 ] then echo 'rpcbind_service start!!!' else echo 'rpcbind_servive is not good!!!' fi fi systemctl start nfs-server.service if [ $? -eq 0 ] then echo 'nfs-server start!!!' else systemctl restart nfs-server.service if [ $? -eq 0 ] 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 0 ] 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=50 #cpu使用超过50% mem_limit=50 #内存使用超过50%发送邮件 disk='/dev/sda1' #需要监控的磁盘名 disk_inode_limit=50 #磁盘inode使用 disk_space_limit=50 #磁盘空间使用 function m_nginx(){ ps aux |grep nginx|grep -v grep &> /dev/null if [ $? -eq 0 ] 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 0 ] 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 -ld.监控正在执行的计划任务
[root@ym script]# tail -f /var/log/cron
e. 邮件验证