真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

一次公司需求記錄,python處理sysstat收集的sa性能數(shù)據(jù)

鑒于每個(gè)月需要把公司上千臺(tái)的服務(wù)的cpu使用利率,CPU負(fù)載,內(nèi)存使用率、網(wǎng)絡(luò)流量等數(shù)據(jù)取出,以前是手動(dòng)通過(guò)zabbix的平臺(tái)去獲取,每次都需要花費(fèi)1-2小時(shí)才能完成這個(gè)需求,太耗時(shí),因此優(yōu)化為systat軟件采集數(shù)據(jù),然后通過(guò)python腳本處理/var/log/sa下采集到數(shù)據(jù),然后入庫(kù)到MySQL數(shù)據(jù)庫(kù)中,以后每次取數(shù)據(jù),只需要一行sql代碼就能獲取到數(shù)據(jù),方便快捷,本文把采集數(shù)據(jù)的腳本截圖,如下所示:
一次公司需求記錄,python處理sysstat收集的sa性能數(shù)據(jù)
一次公司需求記錄,python處理sysstat收集的sa性能數(shù)據(jù)
一次公司需求記錄,python處理sysstat收集的sa性能數(shù)據(jù)
一次公司需求記錄,python處理sysstat收集的sa性能數(shù)據(jù)
一次公司需求記錄,python處理sysstat收集的sa性能數(shù)據(jù)
最后附上完整代碼:
#!/bin/python2.7
#coding: utf-8
#################腳本用法:要取哪天的數(shù)據(jù)就輸入哪天的時(shí)間,比如9月1輸入應(yīng)為20190901
import os
import sys
import datetime
import commands
import time

創(chuàng)新互聯(lián)建站成立十載來(lái),這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)、申請(qǐng)域名、網(wǎng)絡(luò)營(yíng)銷、VI設(shè)計(jì)、網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。網(wǎng)站是否美觀、功能強(qiáng)大、用戶體驗(yàn)好、性價(jià)比高、打開(kāi)快等等,這些對(duì)于網(wǎng)站建設(shè)都非常重要,創(chuàng)新互聯(lián)建站通過(guò)對(duì)建站技術(shù)性的掌握、對(duì)創(chuàng)意設(shè)計(jì)的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進(jìn)步。

def get_cpu(sdate,edate):
cpunum = int(commands.getoutput("cat /proc/cpuinfo |grep processor|wc -l"))
path = os.popen("find /var/log/sa/ -type f -newermt "+sdate+" ! -newermt "+edate+" |grep -v sar").readlines()
path = [m.rstrip("\n") for m in path]
for n in path:
idle = os.popen("sar -f "+n+" -u|awk '{print $NF}'|sed '1,3d'|sed '$d'|sed '/^$/d'").readlines()
data = list(idle)
data = [i.rstrip("\n") for i in idle]
data = [j for j in data if j != '']

    while '%idle' in data:
        data.remove("%idle")
    while 'RESTART' in data:
        data.remove("RESTART")

    data_tmp = list(map(float,data))
    data_num = [100-x for x in data_tmp]
    max_num = max(data_num)
    min_num = min(data_num)
    average_num=sum(data_num)/len(data_num)

    print("%s CPU useed max:%.4f,min:%.4f,average:%.4f"  % (n,max_num,min_num,average_num))

def get_dev(sdate,edate):
net = commands.getoutput("route -n|sed '1,2d'|awk -F' ' '{print $1,$8}'|grep 0.0.0.0|awk -F' ' '{print $2}'|sed -n '1p'")
rxkB=list()
txkB=list()
path = os.popen("find /var/log/sa/ -type f -newermt "+sdate+" ! -newermt "+edate+" |grep -v sar").readlines()
path = [m.rstrip("\n") for m in path]

for n in path:
    rxkB = commands.getoutput("sar -f "+n+" -n DEV|grep "+net+" |grep -v Average|awk -F' ' '{print $6}'|sed '$d'").split("\n")
    txkB = commands.getoutput("sar -f "+n+" -n DEV|grep "+net+" |grep -v Average|awk -F' ' '{print $7}'").split("\n")

    data_txkB = list(map(float,txkB))
    max_txkB=max(data_txkB)
    min_txkB=min(data_txkB)
    average_txkB=sum(data_txkB)/len(data_txkB)

    data_rxkB = list(map(float,rxkB))
    max_rxkB=max(data_rxkB)
    min_rxkB=min(data_rxkB)
    average_rxkB=sum(data_rxkB)/len(data_rxkB)
    print "%s rxval max:%.4f,min:%.4f,average:%.4f,dev:%s" %(n,max_rxkB,min_rxkB,average_rxkB,net)
    print "%s txval max:%.4f,min:%.4f,average:%.4f,dev:%s" %(n,max_txkB,min_txkB,average_txkB,net)

def get_mem(sdate,edate):
path = os.popen("find /var/log/sa/ -type f -newermt "+sdate+" ! -newermt "+edate+" |grep -v sar").readlines()
path = [m.rstrip("\n") for m in path]
for n in path:

    a=int(commands.getoutput("sar -f "+n+" -r|awk 'END{print NF}'"))
    if(a >= 11):
         memused = os.popen("sar -f "+n+" -r|grep -v ^$|grep -iv Linux|grep -iv memused|grep -iv Average|awk '{print(($(NF-8)-$(NF-5))/($(NF-9)+$(NF-8)))}'").readlines()
    else:
         memused = os.popen("sar -f "+n+" -r|grep -v ^$|grep -iv Linux|grep -iv memused|grep -iv Average|awk '{print(($(NF-5)-$(NF-2))/($(NF-6)+$(NF-5)))}'").readlines()
    data = list(memused)
    data = [i.rstrip("\n") for i in memused]
    data_num = list(map(float,data))
    max_num=max(data_num)
    min_num=min(data_num)
    average_num=sum(data_num)/len(data_num)
    print "%s memused max:%.4f,min:%.4f,average:%.4f" %(n,max_num,min_num,average_num)

def get_ldavg(sdate,edate):
cpunum = int(commands.getoutput("cat /proc/cpuinfo |grep processor|wc -l"))
path = os.popen("find /var/log/sa/ -type f -newermt "+sdate+" ! -newermt "+edate+" |grep -v sar").readlines()
path = [m.rstrip("\n") for m in path]
for n in path:

    a=int(commands.getoutput("sar -f "+n+" -q|sed -n '10p'|awk 'END{print NF}'"))
    if a == 7:
        ldavg_1 = os.popen("sar -f "+n+" -q|awk -F' ' '{print $5}'|sed '1,3d'|sed '$d'|sed '/^$/d'|grep -v ldavg-1").readlines()
        ldavg_15 = os.popen("sar -f "+n+" -q|awk -F' ' '{print $7}'|sed '1,3d'|sed '$d'|sed '/^$/d'|grep -v ldavg-15").readlines()
    elif a == 8:
        ldavg_1 = os.popen("sar -f "+n+" -q|awk -F' ' '{print $5}'|sed '1,3d'|sed '$d'|sed '/^$/d'|grep -v ldavg-1").readlines()
        ldavg_15 = os.popen("sar -f "+n+" -q|awk -F' ' '{print $7}'|sed '1,3d'|sed '$d'|sed '/^$/d'|grep -v ldavg-15").readlines()
    elif a == 6:
        ldavg_1 = os.popen("sar -f "+n+" -q|awk -F' ' '{print $4}'|sed '1,3d'|sed '$d'|sed '/^$/d'|grep -v ldavg-1").readlines()
        ldavg_15 = os.popen("sar -f "+n+" -q|awk -F' ' '{print $6}'|sed '1,3d'|sed '$d'|sed '/^$/d'|grep -v ldavg-15").readlines()

    data1 = [i.rstrip("\n") for i in ldavg_1]
    data15 =[j.rstrip("\n") for j in ldavg_15]
    data1 = list(map(float,data1))
    data15 = list(map(float,data15))
    max_data1=max(data1)
    min_data1=min(data1)
    average_data1=sum(data1)/len(data1)
    max_data15=max(data15)
    min_data15=min(data15)
    average_data15=sum(data15)/len(data15)
    print "%s cpu 1minute load max:%.4f,min:%.4f,average:%.4f" %(n,max_data1/cpunum,min_data1/cpunum,average_data1/cpunum)
    print "%s cpu 15minute load max:%.4f,min:%.4f,average:%.4f" %(n,max_data15/cpunum,min_data15/cpunum,average_data15/cpunum)

if name== "main":
sdate=sys.argv[1]
sdate=datetime.datetime.strptime(sdate,'%Y%m%d')
edate=sdate+datetime.timedelta(days=1)
sdate=sdate.strftime('%Y%m%d')
edate=edate.strftime('%Y%m%d')
get_cpu(sdate,edate)
get_ldavg(sdate,edate)
get_mem(sdate,edate)
get_dev(sdate,edate)


網(wǎng)站題目:一次公司需求記錄,python處理sysstat收集的sa性能數(shù)據(jù)
當(dāng)前地址:http://weahome.cn/article/isjgoc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部