1. 程式人生 > >使用python獲取整月每一天的系統監控資料生成報表

使用python獲取整月每一天的系統監控資料生成報表

1.安裝阿里開源監控工具tsar

tsar官方網站 

wget -O tsar.zip https://github.com/alibaba/tsar/archive/master.zip --no-check-certificate
unzip tsar.zip
cd tsar
make && make install

安裝後,您可能會看到以下檔案:

  • /etc/tsar/tsar.conf,這是tsar的主要配置檔案;
  • /etc/cron.d/tsar,用來執行tsar每分鐘收集資訊;
  • /etc/logrotate.d/tsar 將每月輪換tsar的日誌檔案;
  • /usr/local/tsar/modules 是所有模組庫(* .so)所在的目錄;

用法

  執行tsar -l以檢視實時監控是否有效,例如,每秒顯示狀態(-i 1)

 

  • null:檢視預設mods歷史資料, tsar
  • --modname:指定要顯示的模組, tsar --cpu
  • -L / - list:列出可用的moudule, tsar -L
  • -l / - live:顯示實時資訊, tsar -l --cpu
  • -i / - interval:設定報告的間隔, tsar -i 1 --cpu
  • -s / - spec:指定模組詳細資訊欄位, tsar --cpu -s sys,util
  • -D / - detail:不要將資料轉換為K / M / G, tsar --mem -D
  • -m / - merge:將multiply項合併為一, tsar --io -m
  • -I / - item:顯示規格項資料, tsar --io -I sda
  • -d / - date:指定資料,YYYYMMDD或n表示n天前
  • -C / - 檢查:顯示最後的收集資料
  • -h / - help:顯示幫助, tsar -h

2.寫python指令碼

 python 2.7.5 的環境

 

#!/bin/python
# encoding=utf-8
# Author By:BenjaminYang 
import calendar
import time
import os
date=str(input('請輸入你要查詢的年月:\n(格式如:201811)\n----------------\n'))
print('###############################################################################################')
year=int(date[0:4])  #資料切片取得年
month=int(date[4:6]) #資料切片取得月
total_days=calendar.monthrange(year,month)[1] #根據年月組合獲取該月共有多少天
start_time=time.time()
def report_check():
    i=0
    #定義開始時間
#遍歷每一天的tsar監控資料
    while i<total_days:
        if i<9:
              i=i+1
              date=str(year)+str(month)+str(0)+str(i)
        else:
            i=i+1
            date=str(year)+str(month)+str(i)
        os.environ['date']=date
        #將獲取到的資料寫入檔案
        cpu=os.popen("tsar -d $date|grep MEAN|awk '{print $2}'").read()
        mem=os.popen("tsar -d $date|grep MEAN|awk '{print $3}'").read()
        tcp=os.popen("tsar -d $date|grep MEAN|awk '{print $4}'").read()
        bytein=os.popen("tsar -d $date|grep MEAN|awk '{print $5}'").read()
        byteout=os.popen("tsar -d $date|grep MEAN|awk '{print $6}'").read()
        sda=os.popen("tsar -d $date|grep MEAN|awk '{print $7}'").read()
        sdb=os.popen("tsar -d $date|grep MEAN|awk '{print $8}'").read()
        load=os.popen("tsar -d $date|grep MEAN|awk '{print $9}'").read()
        print date
        print os.popen("tsar -d $date|grep MEAN").read()
        with open("data.csv","a+") as f:
            if cpu.strip()=='':
                f.write(str(date)+','+'have null data'+'\n')
            else:    
                f.write(str(date+','+cpu+','+mem+','+tcp+','+bytein+','+byteout+','+sda+','+sdb+','+load).replace("\n","")+"\n")    
os.system('>data.csv')
ag=['adsag','dockerag','ecsag','odpsag','opsag','ossag','rdsag','slbag']
with open("data.csv","a+") as f:
       f.write(str('check_date'+','+'cpu'+','+'mem'+','+'tcp'+','+'bytein'+','+'byteout'+','+'sda'+','+'sdb'+','+'load').replace("\n","")+"\n")

report_check()

end_time=time.time()
total_time=end_time-start_time
print "此次生成報表總共耗時:",total_time

3.執行效果

會生成一個data.csv的報表

將他匯出到我們本地