Lamp environment plus GLUSTERFS storage cluster automated monitoring automatic repair Python script

Source: Internet
Author: User
Tags python script glusterfs

#!/usr/bin/python
#coding: Utf-8
Import OS
Import Sys
Import re
Import Paramiko
Import commands
Import time
Import datetime

def apache_connect_perform (Ip,command):
Client = Paramiko. Sshclient ()
Client.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Client.connect (ip,username= ' Gluster_zabbix ', password= ' Gluster ')
Stdin,stdout,stdeer = Client.exec_command (command)
Global result
result = Stdout.read ()

Print Stdeer.read ()
client.close()

#apache_connect_perform ()

def apache_info_write (info):
With open ('/opt/scripts/apache-stop.log ', ' a ') as F:
F.write (str (info) + ' \ n ')

If name = = 'main':
Command_apache_status = "sudo su-c '/etc/init.d/apache2 status|grep running|wc-l '-"
Command_apache_restart = "sudo su-c ' service apache2 restart '-"
Command_nginx_reload = "Setsid script-c ' sudo nginx-s reload '/dev/null"
Command_apache_mount_check = "Df-th|grep '/app ' |wc-l"
Command_apache_mount = "sudo su-c ' mount-a '-"
Apache_list = [' 10.133.72.116 ', ' 10.133.72.118 ']
Nginx_list = [' 10.133.72.110 ', ' 10.133.72.111 ']
For IP in apache_list:
Apache_connect_perform (Ip,command_apache_status)

Print Result,ip
    if int (result)! = 1:info_apache_stop = "%s,%s Apache is not running"% (Datetime.datetime.now (), IP) APAC He_info_write (info_apache_stop) try:apache_connect_perform (ip,command_apache_restart) except EXC Eption as A:apache_info_write (a) print result Time.sleep (3) apache_connect_perform (ip,com mand_apache_status) if int (result)! = 1:info_apache_stop_again = "%s,%s Apache restart fail"% (datetime                . DateTime.Now (), IP) apache_info_write (info_apache_stop_again) for Ip_nginx in Nginx_list: Command_nginx_zhushi = "grep%s/etc/nginx/conf.d/panda.saicmotor.com.conf|grep ' # ' |wc-l"%ip apache_c Onnect_perform (Ip_nginx,command_nginx_zhushi) if int (result) = = 0:NEW_DST = "' S/server                    %s/#&/' "%ip dst = [" sed ","-I ", NEW_DST,"/etc/nginx/conf.d/panda.saicmotor.com.conf "] Command_nginx_apache_stop = "". Join (DST) New_command = ["sudo su-c", ' "', Command_nginx_apache_stop, '" ', "-"]                    New_command_split = "". Join (New_command) Apache_connect_perform (ip_nginx,new_command_split)                    Time.sleep (2) apache_connect_perform (ip_nginx,command_nginx_reload) Else: Pass else:info_apache_restart_sucess = "%s,%s Apache now was rerunning now"% (datetime . DateTime.Now (), IP) apache_info_write (info_apache_restart_sucess) Else:
Print Datetime.datetime.now (), "%s Apache is running"%ip
        apache_connect_perform(ip,command_apache_mount_check)        if int(result) == 1:            for ip_nginx_zhushi in nginx_list:
Print Datetime.datetime.now (), "%s Apache mount is already now"%ip
                command_nginx_zhushi = "grep %s /etc/nginx/conf.d/panda.saicmotor.com.conf|grep ‘#‘|wc -l"%ip                            apache_connect_perform(ip_nginx_zhushi,command_nginx_zhushi)                            if int(result) == 1:                    new_dst = "‘/%s/s/#//g‘"%ip                                        dst = ["sed","-i",new_dst,"/etc/nginx/conf.d/panda.saicmotor.com.conf"]                                    command_nginx_apache_stop = " ".join(dst)                                        new_command = ["sudo su -c",‘"‘,command_nginx_apache_stop,‘"‘,"-"]                                        new_command_split = " ".join(new_command)                                        apache_connect_perform(ip_nginx_zhushi,new_command_split)                                        time.sleep(2)                                        apache_connect_perform(ip_nginx_zhushi,command_nginx_reload)        else:
Print "Not mount"
            Info_app_mount = '%s ',%s Apache is not mount now '% (Datetime.datetime.now (), IP) apache_info_write (in Fo_app_mount) Try:apache_connect_perform (ip,command_apache_mount) except Exception a s B:apache_info_write (b) time.sleep (2) apache_connect_perform (Ip,command_apache_moun T_check) if int (result) = = 1:info_remount = "%s,%s Apache is remounting now"% (datetime.datetime  . Now (), IP) apache_info_write (info_remount) Else:info_mount_fail = "%s,%s Apache Is remount fail "% (Datetime.datetime.now (), IP) apache_info_write (info_mount_fail) for Ip_ngi NX1 in Nginx_list:command_nginx_zhushi = "grep%s/etc/nginx/conf.d/panda.saicmotor.com.conf|grep ' # ' |                                        Wc-l "%ip apache_connect_perform (Ip_nginx,command_nginx_zhushi) If INT (Result) = = 0:NEW_DST = "' S/server%s/#&/'"%ip dst = ["sed", "-I", new_                        DST, "/etc/nginx/conf.d/panda.saicmotor.com.conf"] command_nginx_apache_stop = "". Join (DST) New_command = ["sudo su-c", ' "', Command_nginx_apache_stop, '" ', "-"] new_command_spli                        t = "". Join (New_command) Apache_connect_perform (ip_nginx,new_command_split)                        Time.sleep (2) apache_connect_perform (ip_nginx,command_nginx_reload) Else: Pass

Lamp environment plus GLUSTERFS storage Cluster automation monitoring automatically fix Python scripts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.