iT邦幫忙

DAY 23
5

MySQL漫談,由使用Python撰寫之MySQL工具程式出發系列 第 23

MySQL漫談,由使用Python撰寫之MySQL工具程式出發(23)

今天將介紹系統CPU使用情況的蒐集及繪圖程式.
系統CPU使用情況,是系統效能相關資訊中,大多數人第一優先想到的,
其重要性不言而喻.
以下是建立資料檔的程式:

#!/usr/bin/env python
# --------------------
# Python RRDTool MySQL
# CPU Usage
# create rrd file
# --------------------

import rrdtool
import Linux_Quick_Report

def create_cpu_rrd(filename):
    rrdtool.create(
    filename, '--step', '60',
    'DS:usr:GAUGE:120:0:100',
    'DS:nice:GAUGE:120:0:100',
    'DS:sys:GAUGE:120:0:100',
    'DS:iowait:GAUGE:120:0:100',
    'DS:irq:GAUGE:120:0:100',
    'DS:sirq:GAUGE:120:0:100',
    'DS:idle:GAUGE:120:0:100',
    'RRA:AVERAGE:0.5:1:2880',
    'RRA:AVERAGE:0.5:30:672',
    'RRA:AVERAGE:0.5:60:744',
    'RRA:AVERAGE:0.5:720:732',
    'RRA:MAX:0.5:1:2880',
    'RRA:MAX:0.5:30:672',
    'RRA:MAX:0.5:60:744',
    'RRA:MAX:0.5:720:732',
    'RRA:MIN:0.5:1:2880',
    'RRA:MIN:0.5:30:672',
    'RRA:MIN:0.5:60:744',
    'RRA:MIN:0.5:720:732',
    'RRA:LAST:0.5:1:2880',
    'RRA:LAST:0.5:30:672',
    'RRA:LAST:0.5:60:744',
    'RRA:LAST:0.5:720:732')
#
def create_sys_cpu_rrd():
    cpu_quantity = Linux_Quick_Report.get_cpus_quantity()
    create_cpu_rrd('cpu_all.rrd')
    for i in range(cpu_quantity):
        fname = 'cpu_%d.rrd' % i
        create_cpu_rrd(fname)
#

if __name__ == '__main__':
    create_sys_cpu_rrd()

可以注意到,本程式有使用之前開發系統概觀報表時得知系統CPU數量
的函式.
再來就是蒐集效能資料並將其存檔的程式.

#!/usr/bin/env python
# --------------------
# Python RRDTool MySQL
# CPU Usage
# update rrd file
# --------------------
import time
import subprocess
import rrdtool
import Linux_Quick_Report

def get_cpu_util():
    """ Report CPU utilization with mpstat """
    cpus_list = []
    cpu_item = ('cpuname', 'usr', 'nice', 'sys', 'iowait', 'irq', 'soft_irq', 'idle')
    cpu_quantity = Linux_Quick_Report.get_cpus_quantity()
    output = subprocess.check_output(
        'mpstat -P ALL', shell=True,)
    eachlines = output.splitlines()
    cpu_all = eachlines[3]
    cpu_data = cpu_all.split('   ')
    cpuname = cpu_data[0].split('  ')
    cpuname = cpuname[1]
    usr  = float(cpu_data[1])
    nice = float(cpu_data[2])
    sys  = float(cpu_data[3])
    iowait = float(cpu_data[4])
    irq = float(cpu_data[5])
    soft_irq = float(cpu_data[6])
    idle = float(cpu_data[9])
    cpu_val = (cpuname, usr, nice, sys, iowait, irq, soft_irq, idle)
    cpu_dict = dict(zip(cpu_item, cpu_val))
    cpus_list.append(cpu_dict)
    # ------------------------
    eachlines = eachlines[4:]
    for cpus in eachlines:
        cpu_data = cpus.split('   ')
        cpuname = cpu_data[1].split()[0]
        usr  = float(cpu_data[2])
        nice = float(cpu_data[3])
        sys  = float(cpu_data[4])
        iowait = float(cpu_data[5])
        irq = float(cpu_data[6])
        soft_irq = float(cpu_data[7])
        idle = float(cpu_data[10])
        cpu_val = (cpuname, usr, nice, sys, iowait, irq, soft_irq, idle)
        cpu_dict = dict(zip(cpu_item, cpu_val))
        cpus_list.append(cpu_dict)
    #
    return cpus_list
# --------------------------
def update_rrd(filename, usr, nice, sys, iowait, irq, soft_irq, idle):
    rrdtool.update(filename, 'N:' + `usr` + ':' + `nice` + ':' \
                   + `sys` + ':' + `iowait` + ':' + `irq` + \
                   ':' + `soft_irq` + ':' + `idle`)
# 
# ------------------------
def resfresh():
    cpus_list = get_cpu_util()
    for cpu_data in cpus_list:
        #print 'CPU Name:%s usr:%f idle:%f' % (cpu_data['cpuname'], cpu_data['usr'], cpu_data['idle'])
        #print '-' * 20
        filename = 'cpu_%s.rrd' % str(cpu_data['cpuname'])
        usr = cpu_data['usr']
        nice = cpu_data['nice']
        sys = cpu_data['sys']
        iowait = cpu_data['iowait']
        irq = cpu_data['irq']
        soft_irq = cpu_data['soft_irq']
        idle = cpu_data['idle']
        update_rrd(filename, usr, nice, sys, iowait, irq, soft_irq, idle)
#


if __name__ == '__main__':
    while 1:
        resfresh()
        time.sleep(60)

本程式是呼叫 mpstat 指令來獲取CPU使用量,並將其存檔.
置於背景執行,會每分鐘更新資料.

再來就是繪圖的程式.

#!/usr/bin/env python
# ----------------------------
# Python RRDTool 
# CPU Data
# Create Image from rrd file
# For ITHelp
# ----------------------------
import rrdtool
import datetime
import Linux_Quick_Report

def cpu_graph(cpu, period):
    timenow = datetime.datetime.now()
    disptime = datetime.datetime.strftime(timenow, '%Y-%m-%d %H-%M-%S')
    title = cpu + '_usage_' + period
    filename = title + '.png'
    rrdfile = cpu + '.rrd'
    # -------------------
    usr = 'DEF:usr=%s:usr:AVERAGE' % rrdfile
    nice = 'DEF:nice=%s:nice:AVERAGE' % rrdfile
    sys = 'DEF:sys=%s:sys:AVERAGE' % rrdfile
    iowait = 'DEF:iowait=%s:iowait:AVERAGE' % rrdfile
    irq = 'DEF:irq=%s:irq:AVERAGE' % rrdfile
    sirq = 'DEF:sirq=%s:sirq:AVERAGE' % rrdfile
    # -------------------
    area_usr = 'LINE2:usr#0000FF:User'
    area_nice = 'LINE1:nice#EEEE:Nice'
    area_sys = 'LINE2:sys#FFFF00:System'
    area_iowait = 'LINE2:iowait#FF0000:I/O wait'
    area_irq = 'LINE1:irq#888888:IRQ'
    area_sirq = 'LINE1:sirq#E29136:softIRQ'
    # -------------------
    if period == 'yesterday':
        start = 'end-1d'
        end = '00:00'
        xgrid = 'HOUR:1:HOUR:2:HOUR:2:0:%H'
    if period == 'today':
        start = '00:00'
        end = '23:59'
        xgrid = 'HOUR:1:HOUR:2:HOUR:2:0:%H'
    if period == 'lastweek':
        start = 'end-1w'
        end = '00:00'
        xgrid = 'HOUR:6:DAY:1:DAY:1:0:%m%d'
        
    rrdtool.graph(
        filename,
        '--start', start,
        '--end', end,
        '--title', title,
        '-a', 'PNG',
        '-W', 'Hitomitanaka for ITHelp',
        '--slope-mode',
        '--vertical-label=Percent (%)',
        '--rigid',
        '--lower-limit', '0',
        '--width', '1024',
        '--height', '150',
        '--x-grid', xgrid,
        '--alt-y-grid',
        '--color', 'BACK#000000',
        '--color', 'CANVAS#000000',
        '--color', 'FONT#FFF978',
        '--font=LEGEND:7',
        '--font', 'TITLE:8:',
        '--font', 'UNIT:7:',
        '--font', 'WATERMARK:9',
        # ---------------------------------
        usr,
        nice,
        sys,
        iowait,
        irq,
        sirq,
        #----------------------------------
        
        area_usr,
        'GPRINT:usr:LAST:      Current\\: %4.1lf%%',
        'GPRINT:usr:AVERAGE:    Average\\: %4.1lf%%',
        'GPRINT:usr:MIN:    Min\\: %4.1lf%%',
        'GPRINT:usr:MAX:    Max\\: %4.1lf%%\\n',
        area_sys,
        'GPRINT:sys:LAST:    Current\\: %4.1lf%%',
        'GPRINT:sys:AVERAGE:    Average\\: %4.1lf%%',
        'GPRINT:sys:MIN:    Min\\: %4.1lf%%',
        'GPRINT:sys:MAX:    Max\\: %4.1lf%%\\n',
        area_iowait,
        'GPRINT:iowait:LAST:  Current\\: %4.1lf%%',
        'GPRINT:iowait:AVERAGE:    Average\\: %4.1lf%%',
        'GPRINT:iowait:MIN:    Min\\: %4.1lf%%',
        'GPRINT:iowait:MAX:    Max\\: %4.1lf%%\\n',
        area_nice,
        'GPRINT:nice:LAST:      Current\\: %4.1lf%%',
        'GPRINT:nice:AVERAGE:    Average\\: %4.1lf%%',
        'GPRINT:nice:MIN:    Min\\: %4.1lf%%',
        'GPRINT:nice:MAX:    Max\\: %4.1lf%%\\n',
        area_irq,
        'GPRINT:irq:LAST:       Current\\: %4.1lf%%',
        'GPRINT:irq:AVERAGE:    Average\\: %4.1lf%%',
        'GPRINT:irq:MIN:    Min\\: %4.1lf%%',
        'GPRINT:irq:MAX:    Max\\: %4.1lf%%\\n',
        area_sirq,
        'GPRINT:sirq:LAST:   Current\\: %4.1lf%%',
        'GPRINT:sirq:AVERAGE:    Average\\: %4.1lf%%',
        'GPRINT:sirq:MIN:    Min\\: %4.1lf%%',
        'GPRINT:sirq:MAX:    Max\\: %4.1lf%%\\n',
        'COMMENT:\t\t\t\tUpdate Time %s' % disptime)
#
if __name__ == '__main__':
    cpu_quantity = Linux_Quick_Report.get_cpus_quantity()
    period_list = ['today', 'yesterday', 'lastweek']
    for period in period_list:
        cpu_graph('cpu_all', period)
        for i in range(cpu_quantity):
            cpu = 'cpu_%d' % i
            cpu_graph(cpu, period)

因為同時繪製數個數據,故採用線圖,執行後將繪製一周內,昨天,今天的
CPU_ALL,及個別CPU的usr,sys,iowait等情況.
圖形很多,就舉數個參考:
cpu_all_uasge_lastweek:

cpu_0_usage_lastweek:

cpu0_usage_yesterday:


上一篇
MySQL漫談,由使用Python撰寫之MySQL工具程式出發(22)
下一篇
MySQL漫談,由使用Python撰寫之MySQL工具程式出發(24)
系列文
MySQL漫談,由使用Python撰寫之MySQL工具程式出發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
ted99tw
iT邦高手 1 級 ‧ 2012-10-26 11:53:50

讚讚讚

老大有沒有算過一天太概寫多少行程式碼?

沒有特別去算,不過上面的程式其實可以改成比較精簡的方式.

我要留言

立即登入留言