rrdweb/rrd.py
author Tero Marttila <terom@fixme.fi>
Sun, 23 Jan 2011 13:50:13 +0200
changeset 29 c756e522c9ac
parent 5 e716718482c3
child 32 47e977c23ba2
permissions -rw-r--r--
pmacct: load pmacct data to rrd
1
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Friendly wrapper around the rrdtool python interface
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
29
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
     5
import rrdtool
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
     6
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
     7
import logging
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
     8
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
     9
log = logging.getLogger('rrdweb.rrd')
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    10
1
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
def normalize_option_key (key) :
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    """
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
        Normalize the given option key.
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
        A -- is prepended, and _'s are converted to -
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    """
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    return '--' + str(key).replace('_', '-')
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
def normalize_option_multi (key, values) :
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    """
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        Normalize a list of option values, returning a series of --opt, val1, --opt, val2, ...
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    """
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    for value in values :
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
        yield key
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        yield str(value)
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
def normalize_option (key, value) :
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    """
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
        Normalize the given option to a series of cmd-args.
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
        If value is None or False, no cmd-args are emitted. If value is True, only --opt is emitted. If value is a list,
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
        --opt and value are emitted for each item.
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
        
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
        Otherwise, both --opt and str(value) are emitted.
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    """
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
    key = normalize_option_key(key)
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    if value is None or value is False :
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
        # omit
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        return ()
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    elif value is True :
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
        # flag
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        return (key, )
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
    
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    elif isinstance(value, list) :
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        # list of option values
29
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    51
        return tuple(normalize_option_multi(key, value))
1
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    else :
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        # option value
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        return (key, str(value))
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
5
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    57
def merge_opts (*all_opts) :
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    58
    """
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    59
        Merge the given series of opt dicts
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    60
    """
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    61
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    62
    out = dict()
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    63
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    64
    for opts in all_opts :
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    65
        # XXX: not strictly true, merge lists
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    66
        out.update(opts)
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    67
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    68
    return out
e716718482c3 hack hack hack some HTML output
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    69
1
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
def run_cmd (func, pre_args, opts, post_args) :
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
    """
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
        Run the given rrdtool.* function, formatting the given positional arguments and options.
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
    """
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    
29
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    75
    # series of (cmd-arg, cmd-arg, ...) tuples, giving all '--opt' and 'value' arguments for each keyword argument
1
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    opt_items = (normalize_option(key, value) for key, value in opts.iteritems())
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
29
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    78
    # decomposed series of cmd-args for options
1
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
    opt_args = [item for items in opt_items for item in items]
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
    # positional arguments
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
    pre_args = [str(arg) for arg in pre_args]
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
    post_args = [str(arg) for arg in post_args]
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
    # full arguments
29
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    86
    args = pre_args + opt_args + ['--'] + post_args
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    87
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    88
    log.debug('rrdtool %s %s', func.__name__, args)
1
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
    return func(*args)
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
def graph (out_path, *args, **opts) :
29
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    93
    """
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    94
        Create a graph from data stored in one or several RRDs.
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    95
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    96
        Graph image output is written to the given path.
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    97
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    98
        Returns... something to do with the image's dimensions, or even the data itself?
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    99
    """
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   100
1
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
    return run_cmd(rrdtool.graph, (out_path, ), opts, args)
18787b57ba46 rrd module to wrap rrdtool, providing a nicer command argument style
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
29
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   103
def create (rrd_path, *args, **opts) :
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   104
    """
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   105
        Set up a new Round Robin Database (RRD).
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   106
    """
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   107
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   108
    return run_cmd(rrdtool.create, (rrd_path, ), opts, args)
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   109
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   110
def update (rrd_path, *args, **opts) :
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   111
    """
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   112
        Store new data values into an RRD.
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   113
    """
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   114
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   115
    return run_cmd(rrdtool.update, (rrd_path, ), opts, args)
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   116
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   117
c756e522c9ac pmacct: load pmacct data to rrd
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
   118