2016-03-24 23 views
0

Ich habe ein seltsames Problem, wobei ich das folgende Python-Skript verwenden und wenn ich es auf einem Host läuft es gut funktioniert, auf einem anderen Computer bleibt es bei etwa 65 Werte stecken, dh danach friert es ein. Ich habe auch den Strace mit einbezogen. Beide Maschinen sind Centos, dasselbe Netzwerk. HierSeltsames Problem mit Python beim Sammeln von Daten von XenServer

#!/usr/bin/python 

import XenAPI 
import string, time, os, ConfigParser, sys, re 
def errorAndExit(message): 
    print "ERROR: Something went wrong! -", message 
    exit(1) 

def grabXenData(session, config): 

    try: 
    vms = session.xenapi.VM.get_all() 
    except: 
    errorAndExit("Couldn't retrieve all VM's") 
    for vm in vms: 
    record = session.xenapi.VM.get_record(vm) 
    if (record["power_state"] == "Running") and not (record["is_control_domain"]): 
     if (record["actions_after_crash"] != "destroy"): 
      print record["name_label"] 


if __name__ == '__main__': 

    CONFIG_FILE = (os.getcwd() + "/config.txt") 
    config = ConfigParser.ConfigParser() 
    config.read([CONFIG_FILE]) 

    wait = config.get('XENAPI', 'WAIT') 

    # time.sleep expects a float, convert the var here. 
    wait = float(wait) 

    url = "http://" + config.get('XENAPI', 'HOSTNAME') 
    print url 
    username = config.get('XENAPI', 'USERNAME') 
    password = config.get('XENAPI', 'PASSWORD') 
    while True: 
    session = XenAPI.Session(url) 
    print session 
    try: 
     session.xenapi.login_with_password(username, password) 
    except: 
     errorAndExit("Could not connect to host, CHECK: URL, username, password") 

    grabXenData(session, config) 
    session.xenapi.session.logout() 
    exit() 

ist der Stack-Trace:

open("/usr/lib64/python2.7/httplib.py", O_RDONLY) = 4 
fstat(4, {st_mode=S_IFREG|0644, st_size=48234, ...}) = 0 
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa108b7e000 
read(4, "r\"\"\"HTTP/1.1 client library\n\n<in"..., 4096) = 4096 
read(4, "THENTICATION_REQUIRED = 407\nREQU"..., 4096) = 4096 
read(4, "nread'):\n   unread = se"..., 4096) = 4096 
write(2, " ", 4)      = 4 
write(2, "line = self.fp.readline(_MAXLINE"..., 38) = 38 
close(4)        = 0 
munmap(0x7fa108b7e000, 4096)   = 0 
write(2, " File \"/usr/lib64/python2.7/soc"..., 63) = 63 
open("/usr/lib64/python2.7/socket.py", O_RDONLY) = 4 
fstat(4, {st_mode=S_IFREG|0644, st_size=20512, ...}) = 0 
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa108b7e000 
read(4, "# Wrapper module for _socket, pr"..., 4096) = 4096 
read(4, "oo long.\"\n errorTab[10064] = "..., 4096) = 4096 
read(4, "sed\" is a property, see below\n "..., 4096) = 4096 
read(4, "  try:\n     "..., 4096) = 4096 
read(4, "   nl = data.find('\\n')"..., 4096) = 4096 
write(2, " ", 4)      = 4 
write(2, "data = self._sock.recv(self._rbu"..., 39) = 39 
close(4)        = 0 
munmap(0x7fa108b7e000, 4096)   = 0 
write(2, "KeyboardInterrupt", 17)  = 17 
write(2, "\n", 1)      = 1 
rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7fa10838d130}, {0x7fa1086ab6a0, [], SA_RESTORER, 0x7fa10838d130}, 8) = 0 
close(3)        = 0 
close(8)        = 0 
exit_group(1)       = ? 
+++ exited with 1 +++ 

Vielen Dank im Voraus. Bitte geben Sie uns weitere Informationen.

Antwort

0

Ich weiß nicht warum, aber Sie sollten sys.exit verwenden, auch außer Ausnahme.

#!/usr/bin/python 

import XenAPI 
import string, time, os, ConfigParser, sys, re 
def errorAndExit(message): 
    print "ERROR: Something went wrong! -", message 
    sys.exit(1) 

def grabXenData(session, config): 
    vms = [] 
    try: 
    vms = session.xenapi.VM.get_all() 
    except Exception: 
    errorAndExit("Couldn't retrieve all VM's") 
    for vm in vms: 
    record = session.xenapi.VM.get_record(vm) 
    if (record["power_state"] == "Running") and not (record["is_control_domain"]): 
     if (record["actions_after_crash"] != "destroy"): 
      print record["name_label"] 


if __name__ == '__main__': 

    CONFIG_FILE = (os.getcwd() + "/config.txt") 
    config = ConfigParser.ConfigParser() 
    config.read([CONFIG_FILE]) 

    wait = config.get('XENAPI', 'WAIT') 

    # time.sleep expects a float, convert the var here. 
    wait = float(wait) 

    url = "http://" + config.get('XENAPI', 'HOSTNAME') 
    print url 
    username = config.get('XENAPI', 'USERNAME') 
    password = config.get('XENAPI', 'PASSWORD') 
    while True: 
    session = XenAPI.Session(url) 
    print session 
    try: 
     session.xenapi.login_with_password(username, password) 
    except Exception: 
     errorAndExit("Could not connect to host, CHECK: URL, username, password") 

    grabXenData(session, config) 
    session.xenapi.session.logout() 
    sys.exit(0) 

Sie haben einen Fehler ATHENTICATION_REQUIRED:

open("/usr/lib64/python2.7/httplib.py", O_RDONLY) = 4 
fstat(4, {st_mode=S_IFREG|0644, st_size=48234, ...}) = 0 
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa108b7e000 
read(4, "r\"\"\"HTTP/1.1 client library\n\n<in"..., 4096) = 4096 
read(4, "THENTICATION_REQUIRED = 407\nREQU"..., 4096) = 4096 

Ich denke, Sie sollten versuchen, wenn Sie Python-Bindings "rechts" verwenden.