zoukankan      html  css  js  c++  java
  • 用python做自动化测试--Python实现远程性能监控

    http://blog.csdn.net/powerccna/article/details/8044222

    在性能测试中,监控被测试服务器的性能指标是个重要的工作,包括CPU/Memory/IO/Network,但大多数人估计都是直接在被测试服务器的运行监控程序。我们开始也是这样做的。但这样做带来一个问题是,测试人员需要在每台被测试服务器上部署监控程序,增加了部署的工作量,而且经常因为Python版本的问题,有些模块不兼容,或者第三方模块需要再次安装。

           改进性能测试监控工具:

    1. 能远程监控被测试服务器,这样测试人员就不需要在每个被测试机器安装监控工具了。

             2. 被测试服务器上不需要安装agent,监控结果能取到本地。

            3. 本地服务器上的python模块和兼容性问题,可以通过Python  virtualenv解决,每个测试人员维护自己的一套Python环境。

    Google了下,找到了pymeter(thttp://pymeter.sourceforge.net/), 看了下源代码,很多工作还没完成,但这个思路和我的是一样的。而且我在其他项目中已经实现了远程发送命令的模块。 所以不如直接在自己的项目上扩展。

    远程发送命令的模块开始是基于Pexpect(http://www.noah.org/wiki/Pexpect)实现的, Pexpect很强大,它是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块。用他来可以很容易实现telnet,ftp,ssh的操作。 但Pexpect无windows下的版本,这是我抛弃它的原因,无法达到测试工具兼容所有系统的要求。 所以就用telent模块替换了Pexpect,实现了远程发送命令和获取结果。

    #file name: telnetoperate.py

     
    1. #!/usr/bin/env python  
    2. #coding=utf-8  
    3.   
    4. import time,sys,logging,traceback,telnetlib,socket  
    5.   
    6.   
    7. class TelnetAction:  
    8.     def __init__(self,host,prompt,account,accountPasswd,RootPasswd=""):  
    9.         self.log=logging.getLogger()  
    10.         self.host=host  
    11.         self.account=account  
    12.         self.accountPasswd=accountPasswd  
    13.         self.RootPasswd=RootPasswd  
    14.         self.possible_prompt = ["#","$"]  
    15.         self.prompt=prompt  
    16.         self.default_time_out=20  
    17.         self.child =None  
    18.         self.login()  
    19.       
    20.     def expand_expect(self,expect_list):  
    21.         try:  
    22.             result=self.child.expect(expect_list,self.default_time_out)  
    23.         except EOFError:  
    24.             self.log.error("No text was read, please check reason")  
    25.         if result[0]==-1:  
    26.             self.log.error("Expect result"+str(expect_list)+" don't exist")  
    27.         else:  
    28.             pass  
    29.         return result  
    30.   
    31.     def login(self):  
    32.         """Connect to a remote host and login. 
    33.              
    34.         """  
    35.         try:  
    36.             self.child = telnetlib.Telnet(self.host)  
    37.             self.expand_expect(['login:'])  
    38.             self.child.write(self.account+ ' ')  
    39.             self.expand_expect(['assword:'])  
    40.             self.child.write(self.accountPasswd + ' ')  
    41.             self.expand_expect(self.possible_prompt)  
    42.             self.log.debug("swith to root account on host "+self.host)  
    43.             if self.RootPasswd!="":  
    44.                 self.child.write('su -'+' ')  
    45.                 self.expand_expect(['assword:'])  
    46.                 self.child.write(self.RootPasswd+' ')  
    47.                 self.expand_expect(self.possible_prompt)  
    48.             #self.child.write('bash'+' ')  
    49.             #self.expand_expect(self.possible_prompt)  
    50.             self.child.read_until(self.prompt)  
    51.             self.log.info("login host "+self.host+" successfully")  
    52.             return True  
    53.         except:  
    54.             print "Login failed,please check ip address and account/passwd"  
    55.             self.log.error("log in host "+self.host+" failed, please check reason")  
    56.             return False  
    57.   
    58.     def send_command(self,command,sleeptime=0.5):  
    59.         """Run a command on the remote host. 
    60.              
    61.         @param command: Unix command 
    62.         @return: Command output 
    63.         @rtype: String 
    64.         """  
    65.         self.log.debug("Starting to execute command: "+command)  
    66.         try:  
    67.             self.child.write(command + ' ')  
    68.             if self.expand_expect(self.possible_prompt)[0]==-1:  
    69.                 self.log.error("Executed command "+command+" is failed, please check it")  
    70.                 return False  
    71.             else:  
    72.                 time.sleep(sleeptime)  
    73.                 self.log.debug("Executed command "+command+" is successful")  
    74.                 return True  
    75.         except socket.error:  
    76.             self.log.error("when executed command "+command+" the connection maybe break, reconnect")  
    77.             traceback.print_exc()  
    78.             for i in range(0,3):  
    79.                 self.log.error("Telnet session is broken from "+self.host+ ", reconnecting....")  
    80.                 if self.login():  
    81.                     break  
    82.             return False  
    83.   
    84.     def get_output(self,time_out=2):  
    85.         reponse=self.child.read_until(self.prompt,time_out)  
    86.         #print "response:",reponse  
    87.         self.log.debug("reponse:"+reponse)  
    88.         return  self.__strip_output(reponse)  
    89.           
    90.     def send_atomic_command(self, command):  
    91.         self.send_command(command)  
    92.         command_output = self.get_output()  
    93.         self.logout()  
    94.         return command_output  
    95.       
    96.     def process_is_running(self,process_name,output_string):      
    97.         self.send_command("ps -ef | grep "+process_name+" | grep -v grep")  
    98.         output_list=[output_string]  
    99.         if self.expand_expect(output_list)[0]==-1:  
    100.             return False  
    101.         else:  
    102.             return True  
    103.       
    104.     def __strip_output(self, response):  
    105.         #Strip everything from the response except the actual command output.  
    106.           
    107.         #split the response into a list of the lines  
    108.           
    109.         lines = response.splitlines()  
    110.         self.log.debug("lines:"+str(lines))  
    111.         if len(lines)>1:  
    112.             #if our command was echoed back, remove it from the output  
    113.             if self.prompt in lines[0]:  
    114.                 lines.pop(0)  
    115.             #remove the last element, which is the prompt being displayed again  
    116.             lines.pop()  
    117.             #append a newline to each line of output  
    118.             lines = [item + ' ' for item in lines]  
    119.             #join the list back into a string and return it  
    120.             return ''.join(lines)  
    121.         else:  
    122.             self.log.info("The response is blank:"+response)  
    123.             return "Null response"  
    124.       
    125.     def logout(self):  
    126.           
    127.         self.child.close()  

    telnetoperate.py代码说明:

    1.  __init__(self,host,prompt,account,accountPasswd,RootPasswd="")

      这里用到了多个登陆账号(account,root),原因是我们的机器开始不能直接root登陆,需要先用普通用户登陆,才能切换到root账号,所以这里出现了account, rootPasswd这2个参数,如果你的机器可以直接root账号登陆,或者你不需要切换到root账号,可以就用account, accountPasswd就可以了。

      prompt是命令行提示符,机器配置不一样,可能是$或者#,用来判断一个命令执行是否完成。

    2.  send_command(self,command,sleeptime=0.5)

        这里sleeptime=0.5是为了避免很多机器性能不好,命令执行比较慢,命令还没返回,会导致获取命令后的结果失败。如果你嫌这样太慢了,可以调用的时候send_command(command,0)

    process_is_running(

    process_name 

     

    监控远程机器:

    #simplemonitor.py

    1. #!/usr/bin/env python  
    2. #coding=utf-8  
    3.   
    4. import time  
    5. import telnetoperate  
    6.   
    7.   
    8.   
    9. remote_server=telnetoperate.TelnetAction("192.168.23.235","#","user","passwd123")  
    10. #get cpu information  
    11. cpu=remote_server.get_output("sar 1 1 |tail -1")  
    12. memory=remote_server.get_output("top | head -5 |grep -i memory")  
    13. io=remote_server.get_output("iostat -x 1 2|grep -v '^$' |grep -vi 'dev'")  
    14.       

     

    这样在任何一台机器上就可以实现监控远程多个机器了,信息集中化管理,方便进一步分析。如果你想cpu, memory, io独立的监控,可以多线程或者起多个监控进程,在多线程中需要注意的时候,必须对每个监控实例建立一个telnet连接,get_output是从telnet 建立的socket里面去获取数据,如果多个监控实例用同一个socket会导致数据混乱。

  • 相关阅读:
    c# 暴力破解中文编码方式
    vs调试 不能进入断点
    shell-的bash内部命令变量介绍与shift等
    shell-的特殊变量-难点理论
    shell-的特殊变量-进程状态变量$$ $! $? $_详解
    shell-的特殊变量-位置变量$0 $n $* $# $@详解
    shell-的变量-局部变量
    shell-的变量-全局变量
    shell-脚本开发基本规范及习惯
    shell-脚本的执行
  • 原文地址:https://www.cnblogs.com/feifeidxl/p/5705738.html
Copyright © 2011-2022 走看看