0

包括/machine.py

def cpu_info(ip):
        cpuinfo = []
        cmd_list = ["cat /proc/cpuinfo |grep 'processor' |wc -l","cat /proc/cpuinfo |grep 'physical id' |sort |uniq |wc -l","cat /proc/cpuinfo |grep 'core id' |sort |uniq |wc -l","awk -F: '/model name/ {print $2}' /proc/cpuinfo |t
ail -n 1"]
        try:
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(ip,22,timeout=20)
                for cmd in cmd_list:
                        stdin,stdout,stderr = ssh.exec_command(cmd)
                        cpuinfo.append(stdout.read().strip())
                ssh.close()
                return cpuinfo
        except:
                msg = 'When collect cpuinfo Connection Error %s ' % ip
                print msg




 def mem_info(ip):
        meminfo = []
        cmd_list = ["free -m|grep Mem|awk -F: '{print $2}'| awk '{print $1}'","free -m|grep -w cache |awk -F: '{print $2}'|awk '{print $2}'"]
        try:
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(ip,22,timeout=20)
                for cmd in cmd_list:
                        stdin,stdout,stderr = ssh.exec_command(cmd)
                        meminfo.append(stdout.read().strip())

                ssh.close()
                return meminfo
        except:
                msg = 'When collect meminfo Connection Error %s ' % ip
                print msg

视图.py

from birdman.include.machine import cpu_info,mem_info
import threading,os,sys,time
from django.shortcuts import render_to_response
from birdman.models import Machineinfo

workdir=os.path.dirname(globals()["__file__"])
server_list = '%s/serverlist/host.conf' % workdir





def machine_info(ip):
        host_ip = []
        machineinfo = []
        host_ip.append(ip.strip())
        machineinfo = host_ip + cpu_info(ip) + mem_info(ip) + disk_info(ip) + load_info(ip)
        p = Machineinfo.objects.filter(host=ip.strip())
        if(p.exists()):
                p.update(logic_core=machineinfo[1])
                p.update(phy_cpu=machineinfo[2])
                p.update(core_per_phy=machineinfo[3])
                p.update(cpu_model=machineinfo[4])
                p.update(phy_mem=machineinfo[5])
                p.update(free_mem=machineinfo[6])
                p.update(max_disk=machineinfo[7])
                p.update(load=machineinfo[8])
        else:
                Machineinfo.objects.create(host=machineinfo[0],logic_core=machineinfo[1],phy_cpu=machineinfo[2],core_per_phy=machineinfo[3],cpu_model=machineinfo[4],
                                phy_mem=machineinfo[5],free_mem=machineinfo[6])



  def all_machine_info():
        f = open(server_list)
        lines = f.readlines()
        for ip in lines:
                a = threading.Thread(target = machine_info,args = (ip,))
                a.start()
        f.close()
#all_machine_info()




def html_machine_info(request):
        all_machine_info()
        result = Machineinfo.objects.all()
        return render_to_response('machine_info.html',{'result': result})

网址.py

    url(r'^html_machine_info/$',html_machine_info)

当我访问:http://XXXX/html_machine_info/ 时,我发现 all_machine_info 函数没有执行,但我使用

#python manage.py shell
>>> from birdman.views import all_machine_info 
>>> all_machine_info()

它确实执行,为什么?因为与线程有关?


包括/machine.py

def cpu_info(ip):
        #paramiko.util.log_to_file('paramiko.log')
        cpuinfo = []
        cmd_list = ["cat /proc/cpuinfo |grep 'processor' |wc -l","cat /proc/cpuinfo |grep 'physical id' |sort |uniq |wc -l","cat /proc/cpuinfo |grep 'core id' |sort |uniq |wc -l","awk -F: '/model name/ {print $2}' /proc/cpuinfo |t
ail -n 1"]
        try:
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(ip,22,username='root',timeout=20,password=None)
                for cmd in cmd_list:
                        stdin,stdout,stderr = ssh.exec_command(cmd)
                        cpuinfo.append(stdout.read().strip())
                ssh.close()
                return cpuinfo
        except:
                msg = 'When collect cpuinfo Connection Error %s ' % ip
                logging.debug('FUCK FUCK FUCK FUCK')
                print msg

如果我使用http://xxxx/html_machine_info/,我发现 ssh.connect(ip,22,username='root',timeout=20,password=None) 它不起作用,这是我的日志文件:

2013-05-28 18:00:56,644 DEBUG starting thread (client mode): 0x16de7690L
2013-05-28 18:00:56,649 INFO Connected (version 2.0, client OpenSSH_4.3)
2013-05-28 18:00:56,650 DEBUG kex algos:['diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr'
, 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', '
arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] client mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac
-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'
] client lang:[''] server lang:[''] kex follows?False
2013-05-28 18:00:56,651 DEBUG Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
2013-05-28 18:00:56,651 DEBUG using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none
2013-05-28 18:00:56,681 DEBUG Switch to new keys ...
2013-05-28 18:00:56,696 DEBUG Adding ssh-rsa host key for X.X.X.X
: 7679319647a2d5f231faf60391e37737
2013-05-28 18:00:56,697 DEBUG FUCK FUCK FUCK FUCK
2013-05-28 18:00:56,782 DEBUG EOF in transport thread

我使用#python machine.py,它可以工作,这里是 paramiko.log:

DEB [20130528-18:03:24.031] thr=1   paramiko.transport: starting thread (client mode): 0x1258ec10L
INF [20130528-18:03:24.035] thr=1   paramiko.transport: Connected (version 2.0, client OpenSSH_4.3)
DEB [20130528-18:03:24.037] thr=1   paramiko.transport: kex algos:['diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-ctr',
'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] server encrypt:['aes128-ctr', 'aes192-ctr', 'ae
s256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] client mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-rip
emd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', 'zlib@openssh.com'] server compress:[
'none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEB [20130528-18:03:24.037] thr=1   paramiko.transport: Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
DEB [20130528-18:03:24.037] thr=1   paramiko.transport: using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, r
emote none
DEB [20130528-18:03:24.067] thr=1   paramiko.transport: Switch to new keys ...
DEB [20130528-18:03:24.082] thr=2   paramiko.transport: Adding ssh-rsa host key for X.X.X.X.: 4a5e4aafac28c0d035815c777956f4c8
DEB [20130528-18:03:24.095] thr=2   paramiko.transport: Trying discovered key 4a5344eded57cfbd2fecb1e6ac4f5f17 in /root/.ssh/id_rsa
DEB [20130528-18:03:24.107] thr=1   paramiko.transport: userauth is OK
INF [20130528-18:03:24.910] thr=1   paramiko.transport: Authentication (publickey) successful!
DEB [20130528-18:03:24.912] thr=2   paramiko.transport: [chan 1] Max packet in: 34816 bytes
DEB [20130528-18:03:24.912] thr=1   paramiko.transport: [chan 1] Max packet out: 32768 bytes
INF [20130528-18:03:24.913] thr=1   paramiko.transport: Secsh channel 1 opened.
DEB [20130528-18:03:24.915] thr=1   paramiko.transport: [chan 1] Sesch channel 1 request ok
DEB [20130528-18:03:24.955] thr=1   paramiko.transport: [chan 1] EOF received (1)
DEB [20130528-18:03:24.956] thr=2   paramiko.transport: [chan 2] Max packet in: 34816 bytes
DEB [20130528-18:03:24.956] thr=1   paramiko.transport: [chan 1] EOF sent (1)
DEB [20130528-18:03:24.956] thr=1   paramiko.transport: [chan 2] Max packet out: 32768 bytes
INF [20130528-18:03:24.957] thr=1   paramiko.transport: Secsh channel 2 opened.
DEB [20130528-18:03:24.998] thr=1   paramiko.transport: [chan 2] Sesch channel 2 request ok
DEB [20130528-18:03:25.038] thr=1   paramiko.transport: [chan 2] EOF received (2)
DEB [20130528-18:03:25.039] thr=2   paramiko.transport: [chan 3] Max packet in: 34816 bytes
DEB [20130528-18:03:25.039] thr=1   paramiko.transport: [chan 2] EOF sent (2)
DEB [20130528-18:03:25.039] thr=1   paramiko.transport: [chan 3] Max packet out: 32768 bytes
INF [20130528-18:03:25.040] thr=1   paramiko.transport: Secsh channel 3 opened.
DEB [20130528-18:03:25.081] thr=1   paramiko.transport: [chan 3] Sesch channel 3 request ok
DEB [20130528-18:03:25.122] thr=1   paramiko.transport: [chan 3] EOF received (3)
DEB [20130528-18:03:25.123] thr=2   paramiko.transport: [chan 4] Max packet in: 34816 bytes
DEB [20130528-18:03:25.123] thr=1   paramiko.transport: [chan 3] EOF sent (3)
DEB [20130528-18:03:25.123] thr=1   paramiko.transport: [chan 4] Max packet out: 32768 bytes
INF [20130528-18:03:25.123] thr=1   paramiko.transport: Secsh channel 4 opened.
DEB [20130528-18:03:25.165] thr=1   paramiko.transport: [chan 4] Sesch channel 4 request ok
DEB [20130528-18:03:25.206] thr=1   paramiko.transport: [chan 4] EOF received (4)

信任关系已经建立。

4

1 回答 1

0

挖掘您的源代码,您似乎正在启动新线程而没有真正等待它们完成。尝试这样的事情

def all_machine_info():
    f = open(server_list)
    lines = f.readlines()

    jobs = [] # List of background jobs

    for ip in lines:
        a = threading.Thread(target = machine_info,args = (ip,))
        a.start()
        jobs.append(a)

    for a in jobs:
        a.join() # Wait for job to finish
    f.close()
于 2013-05-27T19:04:21.460 回答