Basic usage of the RRDtool module under Python

Source: Internet
Author: User
It is recently necessary to use Python to draw on the data collected, to decide on using the RRD database, and then to draw with RRDtool, so learn the usage of RRDtool.

Use the following:

Create:
Create (...)
Create (args.): Set up a new Round Robin Database
Create filename [--start|-b start time] [--step|-s step] [Ds:ds-name:dst:heartbeat:min:max] [RRA:CF:xff:steps:rows]
FileName: is the file name of RRD and can end with RRD.
--step:rrdtool expects to receive a value for every number of events, the default being 5 minutes.
--start:rrdtool the start time of the first record, RRDtool will not receive any data for less than the start time. For example, if the time you give at update is less than the start time, RRDtool will reject the value
Ds:ds is used to define a data source, which is the variable name used to hold the result. The variable name is used when drawing.
DST: Used to set the type of data source, there are 5 types of counter, guage, DERIVE, ABSOLUTE, compute, and so on.
COUNTER: The value must be incremented
DERIVE: Similar to counter, but data can be incremented and decremented
ABSOLUTE: Every time he assumes the value of the previous interval is 0, then the average is calculated
GAUGE: Do not make any changes, directly deposited in the RRA
COMPUTE: Instead of receiving a specific value, he defines an expression and then calculates a value
Heartbeat
Min
Max
Rra:rra specify how the data is stored, he can save data at different intervals. Try to produce a data every 5 minutes, so that an hour is 12, if every 30 minutes a piece of data, such an hour is two data, so one day, a year down there will be a lot of data, but the RRD database size is fixed, so there is a way to merge data. This is the function of RRA. The merge method is specified by CF.
CF:CF is used to set the data merge function, there are average, max, Min, last and other 4 types, respectively, to the PDP averaging, maximum, minimum, the current value.
Xff:xff field to set a scale value, RRDtool will combine multiple PDP into a CDP, if the PDP has a value of Unknow PDP, then the value of the CDP is how to calculate, Xff is to set a scale, when the ratio of unknow in the PDP more than how much is, The normal CPD cannot be synthesized and the CPD value of the synthesized is unknow.
Steps: Indicates how many PDP to synthesize a CDP
Rows: How many CDP are saved in total? How much data can be saved?

PDP: RRDtool receives a value within each specified interval, and after RRDtool receives the value, another value is computed based on the DST setting, which is the PDP.
Cdp:rrdtool uses multiple PDP to merge a CDP, which is the consequence of performing CF, and then depositing the value of this CDP into RRA.


View first/Last Data update time:
First (...)
First (filename): Return The timestamp of the first Data sample in an RRD

Last (...)
Last (filename): Return The timestamp of the last data sample in an RRD


To view the RRD structure information:
Info (...)
info (filename): Extract header information from an RRD

Remove the value of the RRD:
Fetch (...)
Fetch (args.): Fetch data from an RRD.
Fetch filename CF [--resolution|-r resolution] [--start|-s start] [--end|-e end]
FileName: rrd file to remove data from
--start: Optional, default is End-1day
--end: Optional, default is now
Cf:average, MAX, MIN, last. The premise is that you have the type of RRA in your RRD.


Update RRD Data:
Update (...)
Update (args.): Store A new set of values into the RRD
Update filename [--template|-t ds-name[:d s-name] ...] N|timestamp:value[:value ...] [Timestamp:value[:value ...]
FileName: The rrd file to update
--template:
-T ds-name[:d S-name]: Set update data for that DS
N|timestamp: A timestamp that indicates at what point the data was collected, and N indicates now.
Value[:value ...] : An RRD file can have multiple DS, so a single update can update multiple value.

After the update is inserted, there is no return and no way to know if the insert is normal and can be used Updatev

Insert with return value:
Updatev (...)
Updatev is called in the same manner as update
Similar to update, but a status code is returned after each insert.


Drawing:
Graph (...)
Graph (args.): Create A graph based on data from one or several RRD
Graph filename [-s|--start seconds] [-e|--end seconds] [-x|--x-grid x-axis grid and label] [-y|--y-grid y-axis grid and LA BEL] [--alt-y-grid] [--ALT-Y-MRTG] [--alt-autoscale] [--alt-autoscale-max] [--units-exponent] value [-v|-- Vertical-label text] [-w|--width pixels] [-h|--height pixels] [-i|--interlaced] [-f|--imginfo formatstring] [-a|-- Imgformat gif| PNG|GD] [-b|--background value] [-o|--overlay value] [-u|--unit value] [-z|--lazy] [-o|--logarithmic] [-u|--upper-limit Value] [-l|--lower-limit value] [-g|--no-legend] [-r|--rigid] [--step value] [-b|--base value] [-c|--color colortag# RRGGBB] [-t|--title title] [DEF:VNAME=RRD:DS-NAME:CF] [cdef:vname=rpn-expression] [PRINT:vname:CF:format] [Gprint: Vname:CF:format] [Comment:text] [hrule:value#rrggbb[:legend] [vrule:time#rrggbb[:legend]] [line{1|2|3}:vname[# Rrggbb[:legend]] [area:vname[#rrggbb [: Legend]] [stack:vname[#rrggbb [: Legend]]

FileName: The name of the picture, which is the PNG format by default
--start: Image X-axis data start time
--end: Picture x-axis data end time
--x-grid X-axis grid and label:
--y-grid Y-axis grid and label:
--alt-y-grid
--alt-y-mrtg
--alt-autoscale
--alt-autoscale-max
--units-exponent
--vertical-label text Description of the text y-axis
--width pixels x-Axis size
--height pixels y-Axis size
--interlaced
--imginfo formatstring
--imgformat gif| PNG|GD Picture Format
--background value
--overlay value
--unit value
--lazy
--logarithmic
--upper-limit the upper limit of the value y-axis data values, which automatically adjusts the y-axis numbers by default
--lower-limit value y-axis data value lower bound
--no-legend suppress the legend below the chart
--rigid in strict accordance with Upper-limit/lower-limit to draw
--step value
--base value default 1k=1000 can be adjusted 1k=1024
--color COLORTAG#RRGGBB
--title title at the top of the title chart
DEF:VNAME=RRD:DS-NAME:CF defines a data source for drawing, that is, fetching data from that RRD, because RRA has multiple types, so you need to specify CF
Cdef:vname=rpn-expression Define merge values,
PRINT:vname:CF:format
GPRINT:vname:CF:format output maximum, minimum, and so on below the chart
Comment:text used to output some strings
HRULE:VALUE#RRGGBB draw a horizontal line on a chart
VRULE:TIME#RRGGBB drawing a vertical line on a chart
Line{1|2|3}:vname using lines to draw VName data graphs
Area:vname using blocks to plot vname data graphs
Stack:vname


An example is attached below:

# # #创建rrd #!/usr/bin/pythonimport rrdtool rrdb=rrdtool.create (' rest.rrd ', '--step ', ' Max ', '--start ', ' 1369982786 ', ' DS: Input:gauge:120:u:u ', ' ds:output:gauge:120:u:u ', ' rra:last:0.5:1:600 ', ' rra:average:0.5:5:600 ', ' RRA:MAX:0.5:5 : + ', ' rra:min:0.5:5:600 ') if Rrdb:print rrdtool.error () # # #rrd插入数据 #!/usr/bin/pythonimport timeimport Psutilimport RR Dtool for Keys in Psutil.network_io_counters (pernic=true): if keys = = ' Em1 ': sent=psutil.network_io_counters (pernic=tr UE) [keys][0] Recv=psutil.network_io_counters (pernic=true) [keys][1] Up=rrdtool.updatev (' rest.rrd ', ' N:%d:%d '% (Sent , recv)) print up # # #根据rrd绘图 #!/usr/bin/pythonimport rrdtool rrdtool.graph (' rest.png ', '--start ', ' 1369983960 ', '--title ', ' My rrd graph test ', '--vertical-label ', ' bits ', ' def:input=rest.rrd:input:last ', ' Def:output=rest.rrd:output:la ST ', ' line1:input#0000ff:in traffic ', ' line1:output#00ff00:out traffic\\r ', ' cdef:bytes_in=input,8,* ', ' CDEF:b ytes_out=output,8,* ', ' comment:\\n ', ' GPRINT:bytes_in:LAST:LAST in traffic\:%6.2lf%sbps ', ' COMMENT: ', ' GPRINT:bytes_out:LAST:LAST out Traff Ic\:%6.2lf%sbps ')
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.