Python + Pexpect远程监控服务器
来源:互联网 发布:未来网络的发展趋势 编辑:程序博客网 时间:2024/06/01 08:01
导言:性能测试少不了监控服务器一些性能指标,上一家公司运维部已经用zabbix进行了监控,在性能测试时,直接去查看就好,python大屌丝也能监控呀
Python的Pexpect详解:http://blog.csdn.net/sdustliyang/article/details/23373485
闲来无聊玩了下:
# -*- coding:utf-8 -*-import pexpectimport reimport timeimport 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# memdef 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), "%"# iodef 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# cpudef 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_numif __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/
0 0
- Python + Pexpect远程监控服务器
- python pexpect ssh 远程登录服务器
- python Pexpect
- python pexpect
- python pexpect
- python Pexpect
- python 利用pexpect进行多机远程命令执行
- python 利用pexpect进行多机远程命令执行
- visualvm监控远程服务器
- LR远程监控服务器
- 利用python的pexpect模块,自动登陆服务器
- python远程监控
- JConsole远程监控JBoss服务器
- JConsole远程监控Tomcat服务器
- JConsole远程监控JBoss服务器
- JConsole远程监控JBoss服务器
- JConsole远程监控JBoss服务器
- JConsole监控远程Tomcat服务器
- Android JAVA 裁剪、压缩图片
- MATLAB.NET混合编程之一~~由MATLAB编译dll
- postgreSQL jdbc(java连接)
- iOS 大学列表检索
- 基于dropwizard/metrics ,kafka,zabbix构建应用统计数据收集展示系统
- Python + Pexpect远程监控服务器
- 1. Aeneas
- win7屏蔽ctrl+alt+up/down快捷键之解决方案
- 成为明星程序员的10个提示
- scala中的常用语法(上)
- PHP memcache扩展安装,memcahed服务器安装,项目上运用memcahed
- 处理umeditor编辑器给a标签自动添加http://办法
- 15分钟学会使用Git
- 基础类的DSP/BIOS API调用