主题: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 "执行成功"
~
#!/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 "执行成功"
~