#!/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 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-h|grep '/app ' |wc-l"
Command_apache_mount = "sudo su-c ' mount-a '-"
Command_apache_umount = "sudo su-c ' umount/app '-"
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) apache_info_write(info_apache_stop) try: apache_connect_perform(ip,command_apache_restart) except Exception as a: apache_info_write(a)
Print result
Time.sleep (3) apache_connect_perform (ip,command_apache_status) if int (result)! = 1:info_ Apache_stop_again = "%s,%s Apache restart fail"% (Datetime.datetime.now (), IP) apache_info_write (info_apache_sto P_again) for Ip_nginx in Nginx_list:command_nginx_zhushi = "grep%s/etc/nginx/conf.d/panda.sai Cmotor.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, "/E Tc/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_co Mmand) Apache_connect_perform (ip_nginx,new_command_split) time.sleep (2) Apache_connect_peRform (ip_nginx,command_nginx_reload) Else:pass else:info_apache_rest art_sucess = "%s,%s Apache now was rerunning now"% (Datetime.datetime.now (), IP) apache_info_write (info_apache_res tart_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) == 2: 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_umount) time.sleep (4) Apache_connect_perform (Ip,command_apache_mount) except Exception as B:apache_in Fo_write (b) time.sleep (2) apache_connect_perform (ip,command_apache_mount_check) if Int (re Sult) = = 1:info_remount = "%s,%s Apache is remounting now"% (Datetime.datetime.now (), IP) APA Che_info_write (info_remount) Else:info_mount_fail = "%s,%s Apache is remount fail"% (DATETIME.D Atetime.now (), IP) apache_info_write (info_mount_fail) for ip_nginx1 in Nginx_list: Command_nginx_zhushi = "grep%s/etc/nginx/conf.d/panda.saicmotor.com.conf|grep ' # ' |wc-l"%ip ApacHe_connect_perform (Ip_nginx1,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_nginx1,new_command_split) time.sleep (2) Apache_connect_perform (ip_nginx1,command_nginx_reload) Else:pass
Lamp environment plus GLUSTERFS storage Cluster automation monitoring automatically fix Python scripts