Basic usage of rrdtool module in Python

Source: Internet
Author: User
Tags rrd rrdtool mrtg
This article mainly introduces the basic usage of the rrdtool module in Python and is often used for data visualization functions, if you need it, you can refer to the recently used python to plot based on the collected data and decide to use the rrd database. Then, you can use rrdtool to plot the data, so you can learn how to use rrdtool.

The usage is as follows:

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: the name of the rrd file. It can end with rrd.
-- Step: the number of events that rrdtool expects to receive at intervals. The default value is 5 minutes.
-- Start: the start time of the first record of rrdtool. rrdtool does not receive any data at a time earlier than the start time. For example, if the time given during update is earlier than the start time, rrdtool rejects the value.
DS: DS is used to define the data source and is the variable name used to store the results. This variable name is used for drawing.
DST: Used to set data source types, including COUNTER, GUAGE, DERIVE, ABSOLUTE, and COMPUTE.
COUNTER: The value must be incremental.
DERIVE: similar to COUNTER, but data can be decreased progressively
ABSOLUTE: it assumes that the value of the previous interval is 0, and then calculates the average value.
GAUGE: directly stored in rra without any changes
COMPUTE: instead of receiving a specific value, it defines an expression and calculates a value.
RRA: rra specifies how data is stored. It can store data at different intervals. We try to generate a piece of data every five minutes. In this way, 12 pieces of data will be generated every hour. If there is one piece of data every 30 minutes, two pieces of data will be generated every hour. In this way, a lot of data will be generated every day, however, the size of the rrd database is fixed, so there is a way to merge data. This is the rra function. The merge mode is specified by CF.
CF: CF is used to set the data merging function. It can be divided into four types: AVERAGE, MAX, MIN, and LAST, indicating the AVERAGE, maximum, minimum, and current values of PDP.
Xff: The xff field sets a proportional value. rrdtool combines multiple PDP into one CDP. If this parameter is set to the PDP with an UNKNOW value, how can we calculate the CDP value, xff is to set a ratio. When the ratio of UNKNOW in PDP exceeds the limit, the CPD cannot be synthesized normally, and the CPD value of the synthesis is also UNKNOW.
Steps: Number of PDP members to synthesize one CDP
Rows: How many CDP instances are stored in total? That is, how much data can be stored?

PDP: rrdtool receives a value within each specified time interval. After receiving the value, rrdtool calculates another value based on DST settings. This value is PDP.
CDP: rrdtool uses multiple PDP to merge one CDP, that is, the result of executing CF, and then stores the CDP value in rra.

View the 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

View rrd structure information:
Info (...)
Info (filename): extract header information from an rrd

Obtain the rrd value:
Fetch (...)
Fetch (args...): fetch data from an rrd.
Fetch filename CF [-- resolution |-r resolution] [-- start |-s start] [-- end |-e end]
Filename: the rrd file to retrieve data
-- Start: optional, end-1day by default
-- End: Optional. The default value is now.
CF: AVERAGE, MAX, MIN, and LAST. It is required that you have rra of this type in rrd.

Update rrd data:
Update (...)
Update (args...): Store a new set of values into the rrd
Update filename [-- template |-t ds-name [: ds-name]...] N | timestamp: value [: value...] [timestamp: value [: value...]
Filename: the rrd file to be updated.
-- Template:
-T ds-name [: ds-name]: sets to update the DS data.
N | timestamp: timestamp, indicating the time at which the data is collected, and N indicates now.
Value [: value...]: An rrd file can have multiple DS, So multiple values can be updated at one update.

After the insert value is updated, no response is returned. You cannot know whether the insert is normal. You can use 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.

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 label] [-- 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] [DEF: vname = rrd: ds-name: CF] [CDEF: vname = rpn-expression] [PRINT: vname: CF: format] [uplint: 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: image name. The default format is PNG.
-- Start: start Time of the x-axis data of the image
-- End: the end time of the x-axis data of the image.
-- 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 Y-axis text description
-- Width pixels x axis size
-- Height pixels Y axis size
-- Interlaced
-- Imginfo formatstring
-- Imgformat GIF | PNG | GD image format
-- Background value
-- Overlay value
-- Unit value
-- Lazy
-- Logarithmic
-- Upper-limit value: the upper limit of the Y-axis data value. By default, the y-axis Number is automatically adjusted.
-- Lower-limit value lower limit of Y-axis data value
-- No-legend cancel the legend below the chart
-- Rigid is drawn strictly according to upper-limit/lower-limit.
-- Step value
-- The default value of base value is 1 K = 1000. The value can be adjusted to 1 K = 1024.
-- Color COLORTAG # rrggbb
-- Title the title above the chart
DEF: vname = rrd: ds-name: CF defines the data source used for plotting, that is, to retrieve data from that RRD. Because RRA has multiple types, you must specify
CDEF: vname = rpn-expression defines the merged value,
PRINT: vname: CF: format
The maximum value, minimum value, and the like are output under the chart in the format.
COMMENT: text is used to output some strings.
HRULE: value # rrggbb draw a horizontal line on the chart
VRULE: time # rrggbb draw a vertical line on the chart
LINE {1 | 2 | 3}: vname uses a LINE to draw a vname Data graph.
AREA: vname uses squares to draw vname data charts
STACK: vname

An example is provided below:

### Create rrd #! /Usr/bin/pythonimport rrdtool rrdb = rrdtool. create ('rest. rrd ',' -- step', '60', '-- start', '000000', 'ds: input: GAUGE: 1369982786: U: U', 'ds: output: GAUGE: 120: U: U', 'rra: LAST: 0.5: 1: 8080', 'rra: AVERAGE: 600: 5: 80', 'rra: MAX: 0.5: 5: 600 ', 'rra: MIN: 0.5: 5: 600') if rrdb: print rrdtool. error () ### insert data into rrd #! /Usr/bin/pythonimport timeimport psutilimport rrdtool for keys in psutil. network_io_counters (pernic = True): if keys = 'em1': sent = psutil. network_io_counters (pernic = True) [keys] [0] recv = psutil. network_io_counters (pernic = True) [keys] [1] up = rrdtool. updatev ('rest. rrd ', 'N': % d: % d' % (sent, recv) print up ### Drawing Based on rrd #! /Usr/bin/pythonimport rrdtool rrdtool.graph('rest.png ',' -- start', '123', '-- title', 'My rrd graph test',' -- vertical-label ', 'bits ', 'def: input = rest. rrd: input: last', 'def: output = rest. rrd: output: LAST ', 'line1: input # 0000FF: In traffic', 'line1: output #00FF00: Out traffic \ R', 'cdef: bytes_in = input, 8, * ', 'cdef: bytes_out = output, 8, *', 'comment: \ n', 'uplint: bytes_in: LAST in traffic \: % 6.2lf % Sbps ', 'comment:', 'uplint: bytes_out: LAST out traffic \: % 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: 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.