collectd+pythonモニタディスクio情報


pythonコード
import subprocess
import collectd
import time

NAME = 'io_info'
VERBOSE = True

def get_stats():
    value = []
    io_value = [[]]
    subprocess.Popen("iostat -d -m -x 1 2 | grep -E 'dm-2' | tail -n1 | awk '{print $6,$7,$10,$13,$14}' > /tmp/io_info" ,shell=True)
    with open('/tmp/io_info', 'r') as f:
        lines = f.readlines()
    for line in lines:
        value=line.split( )
    #print value
    time.sleep(5)
    io_value_read_Mb = float(value[0])
    io_value_write_Mb = float(value[1])
    io_value_await = float(value[2])
    io_value_svctm = float(value[3])
    io_value_util = float(value[4])

    io_value[0] = ["read_Mb"]
    io_value[0].append(io_value_read_Mb)

    io_value.append(["write_Mab"])
    io_value[1].append(io_value_write_Mb)

    io_value.append(["util"])
    io_value[2].append(io_value_util)

    io_value_wait = io_value_await - io_value_svctm
    io_value.append(["wait"])
    io_value[3].append(io_value_wait)

    return io_value

def log(t, message):
    """ Log messages to collectd logger
    """
    if t == 'err':
        collectd.error('{0}: {1}'.format(NAME, message))
    elif t == 'warn':
        collectd.warning('{0}: {1}'.format(NAME, message))
    elif t == 'verb':
        if VERBOSE:
            collectd.info('{0}: {1}'.format(NAME, message))
    else:
        collectd.info('{0}: {1}'.format(NAME, message))


def configure_callback(conf):
    """ Config data from collectd
    """
    log('verb', 'configure_callback Running')
    global NAME, VERBOSE
    for node in conf.children:
        if node.key == 'Name':
            NAME = node.values[0]
        elif node.key == 'Verbose':
            if node.values[0] == 'False':
                VERBOSE = False
        else:
            log('warn', 'Unknown config key: {0}'.format(node.key))

def read_callback():
    """ Prepare data for collectd
    """
    log('verb', 'read_callback Running')

    stats = get_stats()

    if not stats:
        log('verb', 'No statistics received')
        return

    for metric, percent in stats:
        log('verb', 'Sending value: {0} {1}'.format(metric, percent))
        value = collectd.Values(plugin=NAME)
        value.type = 'percent'
        value.type_instance = metric
        value.values = [ str(percent) ]
        value.dispatch()

collectd.register_config(configure_callback)
collectd.warning('Initialising {0}'.format(NAME))
collectd.register_read(read_callback)

collectdプロファイルコード

    ModulePath "/etc/collectd/plugins"
    Import "io_info"
    <Module "io_info">
    Name "io_info"
    Verbose False
    Module>