iT邦幫忙

DAY 19
5

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

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

今天繼續繪製流量圖形,增加變化.昨天的圖形與一般常見的流量圖
除了線圖改為區塊以外,還有增加一條平均值的水平線,有網友提出討論
說到 95 percent line.所以今天將要以 95 percent line為主角,
來變化圖形.
先來看圖吧.


這就是加了95 percent line的流量圖.

以下是程式:

#!/usr/bin/env python
# ---------------------------
# Python RRDTool 
# Net Data
# Create Image from rrd file
# For IT Help
# 95% line
# ---------------------------
import rrdtool
import datetime

def net_graph(rrdfile, nettype, period):
    timenow = datetime.datetime.now()
    disptime = datetime.datetime.strftime(timenow, '%Y-%m-%d %H-%M-%S')
    vlabel = '--vertical-label=Bytes / Sec'
    title = 'eth0_%s_%s' % (nettype, period)
    filename = title + '.png'
    if period == 'yesterday':
        start = 'end-1d'
        end = '00:00'
    if period == 'today':
        start = '00:00'
        end = '23:59'
    
    if nettype == 'traffic_in':
        showstr = 'In'
        ds = 'net0_bytes_in'
        data = 'rx'
        kdata = 'krx'
        color = '#1E90FFAA'
    if nettype == 'traffic_out':
        showstr = 'Out'
        ds = 'net0_bytes_out'
        data = 'tx'
        kdata = 'ktx'
        color = '#FF1493AA'
    #    
    def_ds = 'DEF:%s=%s:%s:AVERAGE' % (data, rrdfile, ds)
    cdef = 'CDEF:%s=%s,1024,/' % (kdata, data)
    vdefavg = 'VDEF:avg=%s,AVERAGE' % data
    vdefmax95 = 'VDEF:m95=%s,95,PERCENT' % data
    graph_cmd = 'AREA:%s%s:%s' % (data, color, showstr)
    avgrule = 'HRULE:avg#CBFF75:Average'
    max95rule = 'HRULE:m95#F4A460:95 Percent\\n'
    last_print = 'GPRINT:%s' % kdata
    last_print = last_print + ':LAST:Current\\: %.2lfKBytes'
    avg_print = 'GPRINT:%s' % kdata
    avg_print = avg_print + ':AVERAGE:Average\\: %.2lfKBytes'
    min_print = 'GPRINT:%s' % kdata
    min_print = min_print + ':MIN:Min\\: %.2lfKBytes'
    max_print = 'GPRINT:%s' % kdata
    max_print = max_print + ':MAX:Max\\: %.2lfKBytes\\n' 
    # -------------------
    rrdtool.graph(
        filename,
        '--start', start,
        '--end', end,
        '--title', title,
        '-a', 'PNG',
        '-W', 'Hitomitanaka for ITHelp',
        '--slope-mode',
        vlabel,
        '--rigid',
        '--width', '500',
        '--height', '150',
        '--x-grid', 'HOUR:1:HOUR:2:HOUR:2:0:%H',
        '--alt-y-grid',
        '--color', 'BACK#000000',
        '--color', 'CANVAS#000000',
        '--color', 'FONT#FFF978',
        '--font=LEGEND:7',
        '--font', 'TITLE:8:',
        '--font', 'UNIT:7:',
        '--font', 'WATERMARK:9',
        # ---------------------------------
        def_ds,
        cdef,
        vdefavg,
        vdefmax95,
        graph_cmd,
        avgrule,
        max95rule,
        last_print,
        avg_print,
        min_print,
        max_print,
        'COMMENT:Update Time %s' % disptime
        )
#

def draw_net(period):
    net_graph('net.rrd', 'traffic_in', period)
    net_graph('net.rrd', 'traffic_out', period)
 
if __name__ == '__main__':
    draw_net('yesterday')

接著是變化,除了 95% line 以外,若是 95%以上的部份,顏色也不同,
就更能區分了.


以下是程式:

#!/usr/bin/env python
# ---------------------------
# Python RRDTool 
# Net Data
# Create Image from rrd file
# For IT Help
# 95% area
# ---------------------------
import rrdtool
import datetime

def net_graph(rrdfile, nettype, period):
    timenow = datetime.datetime.now()
    disptime = datetime.datetime.strftime(timenow, '%Y-%m-%d %H-%M-%S')
    vlabel = '--vertical-label=Bytes / Sec'
    title = 'eth0_%s_%s' % (nettype, period)
    filename = title + '.png'
    if period == 'yesterday':
        start = 'end-1d'
        end = '00:00'
    if period == 'today':
        start = '00:00'
        end = '23:59'
    
    if nettype == 'traffic_in':
        showstr = 'In'
        ds = 'net0_bytes_in'
        data = 'rx'
        kdata = 'krx'
        color = '#1E90FFAA'
    if nettype == 'traffic_out':
        showstr = 'Out'
        ds = 'net0_bytes_out'
        data = 'tx'
        kdata = 'ktx'
        color = '#FF1493AA'
    #    
    def_ds = 'DEF:%s=%s:%s:AVERAGE' % (data, rrdfile, ds)
    cdef = 'CDEF:%s=%s,1024,/' % (kdata, data)
    vdefavg = 'VDEF:avg=%s,AVERAGE' % data
    vdefmax95 = 'VDEF:m95=%s,95,PERCENT' % data
    cdef_low = 'CDEF:low=%s,m95,MIN' % data
    graph_cmd = 'AREA:%s#942894:%s' % (data, showstr)
    graph_low = 'AREA:low%s:Under 95 Percent' % color
    avgrule = 'HRULE:avg#CBFF75:Average'
    max95rule = 'HRULE:m95#F4A460:95 Percent Line\\n'
    last_print = 'GPRINT:%s' % kdata
    last_print = last_print + ':LAST:Current\\: %.2lfKBytes'
    avg_print = 'GPRINT:%s' % kdata
    avg_print = avg_print + ':AVERAGE:Average\\: %.2lfKBytes'
    min_print = 'GPRINT:%s' % kdata
    min_print = min_print + ':MIN:Min\\: %.2lfKBytes'
    max_print = 'GPRINT:%s' % kdata
    max_print = max_print + ':MAX:Max\\: %.2lfKBytes\\n' 
    # -------------------
    rrdtool.graph(
        filename,
        '--start', start,
        '--end', end,
        '--title', title,
        '-a', 'PNG',
        '-W', 'Hitomitanaka for ITHelp',
        '--slope-mode',
        vlabel,
        '--rigid',
        '--width', '500',
        '--height', '150',
        '--x-grid', 'HOUR:1:HOUR:2:HOUR:2:0:%H',
        '--alt-y-grid',
        '--color', 'BACK#000000',
        '--color', 'CANVAS#000000',
        '--color', 'FONT#FFF978',
        '--font=LEGEND:7',
        '--font', 'TITLE:8:',
        '--font', 'UNIT:7:',
        '--font', 'WATERMARK:9',
        # ---------------------------------
        def_ds,
        cdef,
        vdefavg,
        vdefmax95,
        cdef_low,
        graph_cmd,
        graph_low,
        avgrule,
        max95rule,
        last_print,
        avg_print,
        min_print,
        max_print,
        'COMMENT:Update Time %s' % disptime
        )
#

def draw_net(period):
    net_graph('net.rrd', 'traffic_in', period)
    net_graph('net.rrd', 'traffic_out', period)
 
if __name__ == '__main__':
    draw_net('yesterday')

上一篇
MySQL漫談,由使用Python撰寫之MySQL工具程式出發(18)
下一篇
MySQL漫談,由使用Python撰寫之MySQL工具程式出發(20)
系列文
MySQL漫談,由使用Python撰寫之MySQL工具程式出發30

1 則留言

0
baiyuan
iT邦新手 4 級 ‧ 2012-10-23 10:02:31

超專業~灑花

我要留言

立即登入留言