2016-03-24 28 views
0

我有一个奇怪的问题,我使用下面的python脚本,当我在一台主机上运行它工作正常,在另一台机器上它卡在65左右的值,即之后它冻结。我还包括strace。两台机器都是Centos,同一个网络。python从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() 

这里是堆栈跟踪:

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 +++ 

在此先感谢。更多信息请告诉我们。

回答

0

我不知道为什么,但你应该使用sys.exit,除了例外。

#!/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) 

你得到了一个错误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 

我想你应该,如果你正在使用python绑定 “正确” 的尝试。