Automatic deployment and monitoring of Linux development scripts
Automated deployment and monitoring of development scripts
First, scripting automatic deployment of reverse proxy, web, NFS;
Requirements:
1, the deployment of Nginx reverse proxy three Web services, scheduling algorithm using weighted polling;
#!/bin/shNgxstatus= ' PS aux | Grep-v grep |grep-c nginx ' function Ngxproxyinstall () {if[-e/usr/sbin/Nginx];then Echo"Nginx already installed"Exit110ElseYum 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"fiif[-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"fiif[$ngxStatus-lt 2];then systemctl start nginx echo"Start Nginx Successful"fi} function Nfsinstall () {if[-e/usr/sbin/rpcinfo];then Echo"NFS already installed"Exit111ElseYum Install rpcbind NFS-utils-y-Q Echo"Install NFS Successful"fiif[!-d/Share];then mkdir-P/share chmod-R o+w/Sharefiecho'/share 192.168.10.0/24 (rw,sync,fsid=0)'>/etc/Exportssystemctl enable Rpcbind.servicesystemctl enable NFS-server.servicesystemctl start Rpcbind.servicesystemctl start NFS-Server.serviceecho"Start NFS Successful"} ngxproxyinstallnfsinstall
Three Web Services weighted polling
2, all Web services use shared storage NFS, to ensure that all the web has read and write permissions to ensure data consistency;
#!/bin/shNgxstatus= ' 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"Exit110Else #Yum install gcc-* glibc-* OpenSSL openssl-devel pcre pcre-devel zlib zlib-devel-y-QYum Install nginx-y-Q Echo"Install Nginx Successful"fiif[-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"fiif[$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"Fisystemctl enable Rpcbind.servicesystemctl enable NFS-server.servicesystemctl start Rpcbind.servicesystemctl start NFS-Server.servicemount-T NFS $ipAddress:/share/data/www/html/Echo"Welcome Luchuangao">/data/www/html/test.html} ngxwebinstallnfsinstall
shared NFS, consistent data
Second, write a monitoring script, monitor the cluster of all service survival status, memory, disk residual rate detection, abnormal send alarm mail
Step One: Prepare the tool for sending mail
#!/usr/bin/python#-*-coding:utf-8-*-ImportSYSImportSmtplibImportEmail.mime.multipartImportEmail.mime.text Server='smtp.163.com'Port=' -' defSendMail (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('Mail sent successfully e-Mail has been sent!') if __name__=='__main__': Msg=Email.mime.multipart.MIMEMultipart () msg['Subject'] ='memory disk usage exceeds set standards, please hurry home and see'msg[' from'] ='[email protected]'msg[' to'] ='[email protected]'User='Python4_mail'pwd='sbalex3714'content='%s\n%s'%('\ n'. Join (Sys.argv[1:4]),' '. Join (Sys.argv[4:]))#format processing, specifically for our message formattxt= Email.mime.text.MIMEText (Content, _charset='Utf-8') Msg.attach (TXT)
Detecting Anomalies
Step two: Copy the above file contents to/usr/bin/my_mail
Chmod+x /usr/bin/my_mail
Step three: Then create a new monitoring script
Vim syscheck.sh
#!/bin/shfunction Ngxmonitor () {#Monitoring Nginx ServicePS aux | grep nginx| Grep-v grep &>/dev/NULLif[ $? -NE 0];then msg="time:$ (date +%f_%t)hostname:$ (HOSTNAME) ipaddr:$ (/usr/sbin/ifconfig |awk'Nr==2{print $}') Msg:nginx Program isCrash, waiting to restart"Echo $msg/usr/bin/my_mail $msg systemctl Restart Nginxfi} function Nfsmonitor () {#Monitoring NFS ServicesPS aux | grep nfs| Grep-v grep &>/dev/NULLif[ $? -NE 0];then msg="time:$ (date +%f_%t)hostname:$ (HOSTNAME) ipaddr:$ (/usr/sbin/ifconfig |awk'Nr==2{print $}') Msg:nfs Program isCrash, waiting to restart"Echo $msg/usr/bin/my_mail $msg systemctl Restart Nginxfi} function Memmonitor () {#Monitoring MemoryMem_use= ' Free | Awk'Nr==2{print}'' Mem_total= ' Free | Awk'Nr==2{print $}'' 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"fiif(($mem _per > 10 )); Then msg="time:$ (date +%f_%t)hostname:$ (HOSTNAME) ipaddr:$ (/usr/sbin/ifconfig |awk'Nr==2{print $}') msg:memory usage exceeds the Limit,current value is${mem_per}%"Echo $msg/usr/bin/my_mail $msgfi} function Diskmonitor () {#Monitoring DiskSpace_use= ' DF $disk |awk'Nr==2{print}'|cut-d%-F1 'if[$space _USE-GT 80];then msg="time:$ (date +%f_%t)hostname:$ (HOSTNAME) ipaddr:$ (/usr/sbin/ifconfig |awk'Nr==2{print $}') Msg:disk space usage exceeds the Limit,current value is${space_use}%"Echo $msg/usr/bin/my_mail $msgfi} ngxmonitor&>>/tmp/Monitor.lognfsmonitor&>>/tmp/Monitor.logmemmonitor&>>/tmp/Monitor.logdiskmonitor&>>/tmp/monitor.log
syscheck.sh
Third, write the scheduled task, run the monitoring script regularly, complete the monitoring operation
* * * * * */shell/syscheck.sh
Automatic deployment and monitoring of Linux development scripts