środa, 5 kwietnia 2017

check docker OOM state via python + paramiko

import sys
import time
import select
import paramiko
import time

hosty = ['app01.prod.crm.polsatc','app02.prod.crm.polsatc','app03.prod.crm.polsatc','app04.prod.crm.polsatc','app05.prod.crm.polsatc','app06.prod.crm.polsatc','app07.prod.crm.polsatc']
for h in hosty:

        i = 0
        imax = 5
        while True:
                print "Trying to connect to %s (%i/%i)" % (h, i, imax)

                try:
                        ssh = paramiko.SSHClient()
                        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                        ssh.connect(h, port=3300)
                        print "Connected to %s" % h
                        break
                except paramiko.AuthenticationException:
                        print "Authentication failed when connecting to %s" % h
                        sys.exit(1)
                except:
                        print "Could not SSH to %s, waiting for it to start" % h
                        i += 1
                        time.sleep(2)

                        # If we could not connect within time limit
                if i == 5:
                        print "Could not connect to %s. Giving up" % h
                        sys.exit(1)


        stdin, stdout, stderr = ssh.exec_command("docker inspect -f '{{.Config.Hostname}} {{.State.ExitCode}} {{.State.OOMKilled}}' $(docker ps -q)")
        while not stdout.channel.exit_status_ready():
                if stdout.channel.recv_ready():
                        rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
                        if len(rl) > 0:
                                print stdout.channel.recv(1024),
        print "Command done, closing SSH connection"
        ssh.close()



ansible-> python testParamiko.py
Trying to connect to app01.prod.crm.polsatc (0/5)
Connected to app01.prod.crm.polsatc
crm-prd-n1-3 0 false
crm-prd-n1-2 0 false
crm-prd-n1-1 0 false
Command done, closing SSH connection
Trying to connect to app02.prod.crm.polsatc (0/5)
Connected to app02.prod.crm.polsatc
crm-prd-n2-3 0 false
crm-prd-n2-2 0 false
crm-prd-n2-1 0 false
Command done, closing SSH connection
Trying to connect to app03.prod.crm.polsatc (0/5)
Connected to app03.prod.crm.polsatc
crm-prd-n3-3 0 false
crm-prd-n3-2 0 false
crm-prd-n3-1 0 false
Command done, closing SSH connection
Trying to connect to app04.prod.crm.polsatc (0/5)
Connected to app04.prod.crm.polsatc
crm-prd-n4-3 0 false
crm-prd-n4-2 0 false
crm-prd-n4-1 0 false
Command done, closing SSH connection
Trying to connect to app05.prod.crm.polsatc (0/5)
Connected to app05.prod.crm.polsatc
crm-prd-n5-3 0 false
crm-prd-n5-2 0 false
crm-prd-n5-1 0 false
Command done, closing SSH connection
Trying to connect to app06.prod.crm.polsatc (0/5)
Connected to app06.prod.crm.polsatc
crm-prd-n6-3 0 false
crm-prd-n6-2 0 false
crm-prd-n6-1 0 false
konfigurator-prd-n1 0 false
Command done, closing SSH connection
Trying to connect to app07.prod.crm.polsatc (0/5)
Connected to app07.prod.crm.polsatc
crm-prd-n7-3 0 false
crm-prd-n7-2 0 false
crm-prd-n7-1 0 false
Command done, closing SSH connection

Brak komentarzy:

Prześlij komentarz