• monitor


    #!/usr/bin/python3
    """
    监控采集多台主机的load io数据,被监控主机在hosts列表按要求添加
    tab_header:
    datetime ip loadaverage_5 loadaverage_10 loadaverage_15 mem_all mem_use mem_idle mem_use% disk_kb_read/s disk_kb_write/s network_rxkb/s network_txkb/s
    """
    
    
    import paramiko
    import time
    import os
    
    dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    #print(dt)
    
    d = time.strftime("%Y-%m-%d",time.localtime())
    t = time.strftime("%H:%M:%S",time.localtime())
    dtshow = d + "_"+ t
    #print(dtshow)
    
    #tab head handle
    logName = "rui.qin.o.logs"
    tab_header = "datetime ip loadaverage_5 loadaverage_10 loadaverage_15 mem_all mem_use mem_idle mem_use% disk_kb_read/s disk_kb_write/s network_rxkb/s network_txkb/s\n"
    
    if not os.path.exists(logName):
        with open(logName,"w") as log_w:
            log_w.write(tab_header)
    else:
        pass
    
    
    #logName = "/opt/rui.qin.o/NetTools.logs"
    
    #remote exec ping hosts,remote exec ping aim host use username/passwd or passwdless
    hosts=[
    #'hostname="10.112.17.53",port=22,username="jenkins",password=""',
    #'hostname="10.110.93.11",port=22,username="jenkins",password=""',
    'hostname="10.110.176.192",port=22,username="root",password="n"',
    #'hostname="10.110.73.74",port=22,username="rui.qin",password=""',
    #'hostname="10.110.92.125",port=22,username="jenkins",password=""',
    #'hostname="10.110.92.123",port=22,username="jenkins",password=""',
    ]
    
    #for ips in pingAimIps:
    #    tabstr = tabstr + " " + ips
    #print(tabstr)
    
    #time.sleep(10)
    #if not os.path.exists(logName):
    #    cmd = "touch " + logName
    #    print(cmd)
    #    #os.system(touch logName)
    
    #time.sleep(10)
    #if tabstr not in open('/opt/rui.qin.o/NetTools.logs').read():
    #    openfs = open("/opt/rui.qin.o/NetTools.logs","a+")
    #    openfs.seek(0,0)
    #    openfs.write(tabstr)
        #print("True")
    #else:
        #print("False")
    #    pass
    
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    #counts=10
    #showcontent01 = ""
    #showcontent03 = ""
    for i in hosts:
        #showcontent02 = ""
        varHeader = "当前监测主机:"
        varHost = varHeader + str(i.split(','))
        #print(varHost)
        #print(f"{varHeader}" +  i.split(','))
        #HN='"' + i.split(',')[0].split('=')[1].replace('"',"") + '"'
        HN=i.split(',')[0].split('=')[1].replace('"','')
        #print(HN)
        #print("{} {}".format(varHeader,HN))
        #print(dtshow + " " + HN)
        showcontent01 = dtshow + " "  + HN
        #print(showcontent01)
        Port=int(i.split(',')[1].split('=')[1])
        #print(Port)
        userName=i.split(',')[2].split('=')[1].replace('"','')
        #print(userName)
        passWord=i.split(',')[3].split('=')[1].replace('"','')
        #print(passWord)
        #
        ssh.connect(hostname=HN,port=Port,username=userName,password=passWord)
        #++stdin,stdout,stderr = ssh.exec_command("du -sh /home/jenkins/rui.qin.o/")
        #++result = stdout.read()
        #++print(bytes.decode(result))
        #获取主机挂载点list
        #print(os.system("df -h |awk ' NR>1 {if ($1==$NF){printf $1}else{print $6}}'"))
        #stdin,stdout,stderr = ssh.exec_command("df -h | awk 'NR>1 {if ($1==$NF){print $1}else {print($5,$6)}}'")
        #stdin,stdout,stderr = ssh.exec_command("df -h | awk 'NR>1 {if ($1==$NF){print $1}else {print($6)}}'")
        #stdin,stdout,stderr = ssh.exec_command("ping -c %s 100.64.43.12 |grep rtt |awk '{print $4}' |awk -F'/' '{print $2}'")
        #stdin,stdout,stderr = ssh.exec_command("sudo apt-get install sysstat && sudo apt-get install iotop")
        #软件组件安装
        #print(HN)
        stdin_init,stdout_init,stderr_init = ssh.exec_command("sudo apt-get -y install sysstat iotop")
        time.sleep(1)
    
    
        '''
        stdin,stdout,stderr = ssh.exec_command("iostat -d -k 1 1")
        res_0661 = bytes.decode(stdout.read())
        print(res_0661)
        time.sleep(1)
    
        stdin,stdout,stderr = ssh.exec_command("iostat -c 1 1")
        res_0662 = bytes.decode(stdout.read())
        print(res_0662)
        time.sleep(1)
        stdin,stdout,stderr = ssh.exec_command("sudo iotop -b -d 1 -n 1 -o -u jenkins -qqq")
        res_0663 = bytes.decode(stdout.read())
        print(res_0663)
    time.sleep(1)
    
    
        stdin,stdout,stderr = ssh.exec_command("iostat -d -x -k 1")
        res_0664 = bytes.decode(stdout.read())
        print(res_0664)
        time.sleep(1)
        '''
    
        stdin,stdout,stderr = ssh.exec_command("/usr/bin/w  | grep average | awk '{print $10,$11,$12}'")
        cpu_la = bytes.decode(stdout.read())
        cpu_la_res = cpu_la.split(',')
        #print(cpu_la_res)
        cpu_la_5 = cpu_la_res[0]
        cpu_la_10 = cpu_la_res[1]
        cpu_la_15 = cpu_la_res[2]
        #print(cpu_la_5 + cpu_la_10 + cpu_la_15)
        #print(stderr.read())
        #res_0665 = bytes.decode(stdout.read())
        #print(HN + "\t" + res_0665)
        #time.sleep(19)
    
    
        """
        tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
        """
        #stdin,stdout,stderr = ssh.exec_command("iostat -d -m | sed -n '3P' && iostat -d -m | grep sda")
        stdin,stdout,stderr = ssh.exec_command("iostat -d -k | grep nvme0n1 | awk '{print $3,$4}'")
        #print(bytes.decode(stdout.read()))
        disk_io_res = bytes.decode(stdout.read()).lstrip()
        #print(disk_io_res)
        #time.sleep(19)
        #disk_io_read_kb_s = disk_io_res.split("\t")[1]
        #print(len(disk_io_read_kb_s))
        #disk_io_write_kb_s = disk_io_res.split(" ")[1]
        #print(disk_io_res_read_kb_s + disk_io_write_kb_s)
        #time.sleep(1)
    
    
        """
        stdin,stdout,stderr = ssh.exec_command("iostat -d -m")
        res_0666 = bytes.decode(stdout.read())
        print(res_0666)
        time.sleep(1)
        """
    
        """
        rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
        """
        #stdin,stdout,stderr = ssh.exec_command("sar -n DEV 1 2 | grep 'Average' | sed -n '1P' && sar -n DEV 1 2 | grep ens4f0| grep Average || sar -n DEV 1 2 | grep ens160|grep Average ")
        #stdin,stdout,stderr = ssh.exec_command("sar -n DEV 1 2 | grep ens4f0| grep Average | awk '{print $5,$6}' || sar -n DEV 1 2 | grep ens160|grep Average | awk '{print $5,$6}'")
        stdin,stdout,stderr = ssh.exec_command("sar -n DEV 1 2 | grep enx000ec670d28f| grep 平均时间 | awk '{print $5,$6}'&& sar -n DEV 1 2 | grep ens160|grep Average | awk '{print $5,$6}'")
        network_io_res = bytes.decode(stdout.read()).lstrip().rstrip().strip()
        #print(network_io_res)
        time.sleep(1)
    
        #memory
        stdin_m,stdout_m,stderr_m = ssh.exec_command("free -m | grep '内存'| awk '{print $2,$3,$4,$3/$2}'")
    mem = bytes.decode(stdout_m.read())
        time.sleep(1)
        #print(mem)
    
    
        results = dtshow + "\t" + HN + "\t" + cpu_la_5 + '\t' + cpu_la_10  + '\t' + cpu_la_15.replace('\n','').replace('\r','') + '\t' + mem.replace('\n','') + '\t' + disk_io_res.replace('\n','').replace('\n','') + '\t' + network_io_res.replace("\r","").replace("\n","") + '\n'
        print(results)
        time.sleep(0.5)
    
        if os.path.exists(logName):
            with open(logName,"a+") as log_w:
                log_w.write(results)
    
        """
        stdin,stdout,stderr = ssh.exec_command("sar -n DEV 1 2 | grep 'Average' | sed -n '1P' && sar -n DEV 1 2 | grep ens160| grep Average")
        res_0777 = bytes.decode(stdout.read())
        print(res_0777)
        time.sleep(1)
        """
    
    """
        #valueX[] = ""
        for ii in range(len(pingAimIps)):
            #print(ii)
            #valueX[ii] = ""
            #cmd="ping -c {} {}|grep rtt |awk '{print \$4}' |awk -F'/' '{print \$2}'".format(counts,ii)
            #print(cmd)
            #cmd="ping -c 10 %s |grep rtt |awk '{print $4}' |awk -F'/' '{print $2}'"%(ii)
            #cmd="ping -c 10 %s |grep round-trip |awk '{print $4}' |awk -F'/' '{print $2}'"%(ii)
            cmd="ping -c 6 %s |grep dev |awk '{print $4}' |awk -F'/' '{print $2}'"%(pingAimIps[ii])
            #print(ii)
            #print(showcontent02)
            #print(cmd)
            stdin,stdout,stderr = ssh.exec_command(cmd)
            res = stdout.read()
            #print(res)
            #print(type(res))
            result = res.split(b'\n')
            result01 = result[0]
            res = result01.split(b'\n')[0]
            resok = bytes.decode(res)
            #showcontent02 = showcontent02 + " " + pingAimIps[ii] + " " + resok
            showcontent02 = showcontent02 + " " + resok
            #print(showcontent02)
            #valueX[ii] = valueX[ii] + " " + pingAimIps[ii] + " " + resok
            #print(showcontent02)
            #print(valueX[ii])
    
        #print(showcontent01 + showcontent02)
            #print(showcontent02)
            ##print(resok)
            #showceontent02 =  showcontent02 + " " +resok
            #showcontent02 = showcontent01 + " " + ii + " " + resok
            #showcontent02 = HN + " " + ii + " " + resok
            #print(showcontent02)
            #print(showcontent02)
            #print(showcontent02)
            #print("\n")
    #print(type(res))
            #print(type(result01))
    #showcontent03 = showcontent01 + showcontent02
    #print(showcontent03)
    #showcontent03 = showcontent01 + " " + showcontent02
    #print(showcontent03)
    """
    

      

  • 相关阅读:
    Pycharm的快捷键
    【Python】笔记1:Spyder常用快捷键
    npm常用命令整理
    MVC4 IIS7.5 403.14错误的解决办法
    AO 获取系统自带的工具条
    CentOS 7.3下安装Redis
    CentOS 7.3下安装MySql
    安装sql server 2005时出现“安装汇编”错误的解决办法
    解决IIS下localhost访问需要输入用户名和密码的问题
    不断沟通,反复确认
  • 原文地址:https://www.cnblogs.com/ruiy/p/16494621.html
Copyright © 2020-2023  润新知