I recently studied python. I first learned how to monitor performance indicators. I usually deploy a test script on the server to perform performance tests, which is troublesome, now with this script, you can directly capture remote performance indicators and write them to the report.
#! /Usr/bin/python
# Coding: UTF-8
Import paramiko, string, time
Remote_m = [
# Db server
('10. 10.10.29 ', 'hanghao ','! QAZ @ WSX3edc '),
# Tomcat
('10. 10.10.30', 'hanghao ','! 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.exe c_command (cmd)
Load_info = stdout. readlines ()
Erro_info = stderr. readlines ()
Erro_message = ('erro _ mescript: % 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])
Use shell to call test results
#/Bin/bash
Python/home/shell/python_log/test. py
Echo "executed successfully"
~