Python + Pexpect远程监控服务器

导言:性能测试少不了监控服务器一些性能指标,上一家公司运维部已经用zabbix进行了监控,在性能测试时,直接去查看就好,python大屌丝也能监控呀

Python的Pexpect详解:http://blog.csdn.net/sdustliyang/article/details/23373485

闲来无聊玩了下:

# -*- coding:utf-8 -*-
import pexpect
import re
import time
import threading


# 主方法
def ssh_command(user, host, password, command):
    ssh_new_key = 'Are you sure you want to continue connecting'
    child = pexpect.spawn('ssh -l %s %s %s' % (user, host, command))
    i = child.expect([pexpect.TIMEOUT, ssh_new_key, 'password: '])
    if i == 0:
        print 'ERROR!'
        print 'SSH could not login. Here is what SSH said:'
        print child.before, child.after
        return None
    if i == 1:
        child.sendline('yes')
        child.expect('password: ')
        i = child.expect([pexpect.TIMEOUT, 'password: '])
        if i == 0:
            print 'ERROR!'
            print 'SSH could not login. Here is what SSH said:'
            print child.before, child.after
            return None
    child.sendline(password)
    return child


# mem
def mem_info():
    print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    child = ssh_command("root", "120.24.239.214", "******", "cat /proc/meminfo")
    child.expect(pexpect.EOF)
    mem = child.before
    mem_values = re.findall("(\d+)\ kB", mem)
    MemTotal = mem_values[0]
    MemFree = mem_values[1]
    Buffers = mem_values[2]
    Cached = mem_values[3]
    SwapTotal = mem_values[13]
    SwapFree = mem_values[14]
    if int(SwapTotal) == 0:
        print u"交换内存总共为:0"
    else:
        Rate_Swap = 100 - 100*int(SwapFree)/float(SwapTotal)
        print u"交换内存利用率:", Rate_Swap
    Free_Mem = int(MemFree) + int(Buffers) + int(Cached)
    Used_Mem = int(MemTotal) - Free_Mem
    Rate_Mem = 100*Used_Mem/float(MemTotal)
    print u"内存利用率:", str("%.2f" % Rate_Mem), "%"


# io
def vm_stat_info():
    print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    child = ssh_command("root", "120.24.239.214", "******", "vmstat 1 2 | tail -n 1")
    child.expect(pexpect.EOF)
    vmstat_info = child.before.strip().split()
    processes_waiting = vmstat_info[0]
    processes_sleep = vmstat_info[1]
    io_bi = vmstat_info[8]
    io_bo = vmstat_info[9]
    system_interrupt = vmstat_info[10]
    system_context_switch = vmstat_info[11]
    cpu_user = vmstat_info[12]
    cpu_sys = vmstat_info[13]
    cpu_idle = vmstat_info[14]
    cpu_wait = vmstat_info[15]
    print "processes_waiting:", processes_waiting
    print "processes_sleep:", processes_sleep
    print "io_bi:", io_bi
    print "io_bo:", io_bo
    print "system_interrupt:", system_interrupt
    print "system_context_switch:", system_context_switch
    print "cpu_user:", cpu_user
    print "cpu_sys:", cpu_sys
    print "cpu_idle:", cpu_idle
    print "cpu_wait:", cpu_wait


# cpu
def cpu_info():
    print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    child = ssh_command("root", "120.24.239.214", "******", "cat /proc/cpuinfo")
    child.expect(pexpect.EOF)
    cpuinfo = child.before
    cpu_num = re.findall('processor.*?(\d+)', cpuinfo)[-1]
    cpu_num = str(int(cpu_num) + 1)
    print u"CPU数目:", cpu_num


if __name__ == '__main__':
    try:
        threads = []
        t1 = threading.Thread(target=mem_info)
        threads.append(t1)
        t2 = threading.Thread(target=vm_stat_info)
        threads.append(t2)
        t3 = threading.Thread(target=cpu_info)
        threads.append(t3)
        for n in range(len(threads)):
            threads[n].start()
    except Exception, e:
        print str(e)

总结:

pexpect + django应该可以做一个监控网站,类似zabbix一样

本人利用Bootstrap + EasyUI + Django开发网站:http://www.xuyangting.com/ 欢迎来访

阳台测试: 239547991(群号)

本人博客:http://xuyangting.sinaapp.com/

你可能感兴趣的:(python,性能测试,pexpect)