python获取服务器性能
最近在学习python,首先学习一下性能指标的监控,平时做性能测试都是在服务器上部署测试脚本进行监控,比较麻烦,现在有了这个脚本就可以直接抓取远程的性能指标写到报告中
#!/usr/bin/python
#coding:utf-8
import paramiko,string,time
remote_m = [
# db server
('10.10.10.29', 'zhanghao', '!qaz@wsx3edc'),
# tomcat
('10.10.10.30', 'zhanghao', '!qaz@wsx3edc')
]
def getcpuload(host, user, pwd):
cmd='uptime |awk \'{print $10 $11 $12}\''
#cmd='top -n 1'
ssh = paramiko.sshclient()
ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())
ssh.connect(host,22,user, pwd)
stdin, stdout, stderr = ssh.exec_command(cmd)
load_info=stdout.readlines()
erro_info=stderr.readlines()
erro_message=('erro_messag: %s' %erro_info + '\n')
sstr1=string.join(load_info)
#load_info = sstr1[sstr1.index('average')+9:len(sstr1)]
return sstr1
ssh.colse()
if __name__ == "__main__":
report_name = 'report_' + time.strftime('%y%m%d%h%m',time.localtime(time.time())) + '.rpt'
debug_name = 'default.debug'
report_file = open('./reports/' + report_name, 'a')
debugfile = open(debug_name, 'a')
print 'benchmark start, generate report:' + report_name
report_file.write('start at ' + time.strftime('%y-%m-%d %h:%m:%s',time.localtime(time.time())) + '\n')
for m in remote_m:
print ('%s load: %s' %(m[0], getcpuload(m[0], m[1], m[2])))
report_file.write('%s load: %s' %(m[0], getcpuload(m[0], m[1], m[2])))
使用shell调用测试结果
#/bin/bash
python /home/shell/python_log/test.py
echo "执行成功"
~