Python下rrdtool模块的基本使用方法
最近需要用python根据收集到的数据进行绘图,决定使用rrd数据库,然后配合rrdtool来绘图,故学习一下rrdtool的用法。
用法如下:
创建:
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:是rrd的文件名,可以以rrd结尾。
--step:rrdtool期望每隔多长事件收到一个值,默认是5分钟。
--start:rrdtool第一个记录的起始时间,rrdtool不会接收任何数据的时间小于起始时间。例如你在update时给出的时间小于起始时间,则rrdtool会拒绝该值
DS:DS用于定义数据源,是用来存放结果的变量名。该变量名在画图时会用到。
DST:用来设置数据源的类型,有COUNTER、GUAGE、DERIVE、ABSOLUTE、COMPUTE等5种类型。
COUNTER:数值必须是递增的
DERIVE:和COUNTER类似,不过数据可递增可递减
ABSOLUTE:他每次假定前一个时间间隔的值是0,然后计算平均值
GAUGE:不做任何变化,直接存入rra中
COMPUTE:他不接收具体值,而是定义一个表达式,然后算出某个值
heartbeat:
min:
max:
RRA:rra指定数据如何存放,他可以保存不同时间间隔的数据。力图每5分钟产生一条数据,这样一小时就是12条,如果每30分钟一条数据,这样一小时就是两条数据,这样一天,一年下来会有很多条数据,但是rrd数据库的大小是固定的,所以就有一个合并数据的方式了。这就是rra的功能。合并方式由CF指定。
CF:CF就是用来设置数据合并的功能,有AVERAGE、MAX、MIN、LAST等4种类型,分别表示对PDP取平均、最大、最小、当前值。
xff:xff字段设置一个比例值,rrdtool会把多个PDP合并为一个CDP,如果这写PDP中有值为UNKNOW的PDP,那么CDP的值该怎么计算,xff就是设置一个比例,当PDP中UNKNOW的比例超过多少是,就无法合成正常的CPD,合成的CPD值也为UNKNOW。
steps:表示多少个PDP合成一个CDP
rows:表示总共存多少个CDP?也就是可以保存多少数据?
PDP:在每个规定的时间间隔内rrdtool都会收到一个值,rrdtool在收到该值后,根据DST的设置,会计算出另一个值,这个值就是PDP。
CDP:rrdtool使用多个PDP合并出一个CDP,也就是执行CF的后果,然后把这个CDP的值存入rra中。
查看第一次/最后一次数据更新时间:
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
查看rrd结构信息:
info(...)
info(filename): extract header information from an rrd
取出rrd的值:
fetch(...)
fetch(args..): fetch data from an rrd.
fetch filename CF [--resolution|-r resolution] [--start|-s start] [--end|-e end]
filename:要取出数据的rrd文件
--start:可选,默认是end-1day
--end:可选,默认是now
CF:AVERAGE、MAX、MIN、LAST。前提是你的rrd中有该类型的rra才可以。
更新rrd数据:
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:要更新的rrd文件
--template:
-t ds-name[:ds-name]:设置更新那个DS的数据
N|timestamp:时间戳,表示数据在哪个时间点采集的,N表示now。
value[:value...]:一个rrd文件可以有多个DS,所以一次update可以更新多个value。
update插入值后,并不会有返回,无法知道插入是否正常,可以用updatev
带返回值的插入:
updatev(...)
updatev is called in the same manner as update
和update类似,不过每次插入后会返回一个状态码。
画图:
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 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:图片的名称,默认是PNG格式
--start:图片X轴数据的起始时间
--end:图片X轴数据的结束时间
--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轴的文字说明
--width pixels X轴的大小
--height pixels Y轴的大小
--interlaced
--imginfo formatstring
--imgformat GIF|PNG|GD 图片格式
--background value
--overlay value
--unit value
--lazy
--logarithmic
--upper-limit value Y轴数据值的上限,默认会自动调整Y轴的数字
--lower-limit value Y轴数据值的下限
--no-legend 取消图表下方的图例
--rigid 严格按照upper-limit/lower-limit来绘制
--step value
--base value 默认1K=1000 可以调整1K=1024
--color COLORTAG#rrggbb
--title title 图表上方的标题
DEF:vname=rrd:ds-name:CF 定义绘图用的数据源,也就是从那个RRD中取数据,因为RRA有多种类型,所以需要指定CF
CDEF:vname=rpn-expression 定义合并值,
PRINT:vname:CF:format
GPRINT:vname:CF:format 在图表的下方输出最大值、最小值之类
COMMENT:text 用来输出一些字符串
HRULE:value#rrggbb 在图表上绘制水平线
VRULE:time#rrggbb 在图表上绘制垂直线
LINE{1|2|3}:vname 使用线条来绘制vname的数据图
AREA:vname 使用方块来绘制vname的数据图
STACK:vname
下面附上一个例子:
###创建rrd #!/usr/bin/python import rrdtool rrdb=rrdtool.create('rest.rrd','--step','60','--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:600', 'RRA:MIN:0.5:5:600') if rrdb: print rrdtool.error() ###rrd插入数据 #!/usr/bin/python import time import psutil import 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 ###根据rrd绘图 #!/usr/bin/python import 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:LAST', 'LINE1:input#0000FF:In traffic', 'LINE1:output#00FF00:Out traffic\\r', 'CDEF:bytes_in=input,8,*', 'CDEF:bytes_out=output,8,*', 'COMMENT:\\n', 'GPRINT:bytes_in:LAST:LAST in traffic\: %6.2lf %Sbps', 'COMMENT: ', 'GPRINT:bytes_out:LAST:LAST out traffic\: %6.2lf %Sbps')

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

MySQL download file is corrupt, what should I do? Alas, if you download MySQL, you can encounter file corruption. It’s really not easy these days! This article will talk about how to solve this problem so that everyone can avoid detours. After reading it, you can not only repair the damaged MySQL installation package, but also have a deeper understanding of the download and installation process to avoid getting stuck in the future. Let’s first talk about why downloading files is damaged. There are many reasons for this. Network problems are the culprit. Interruption in the download process and instability in the network may lead to file corruption. There is also the problem with the download source itself. The server file itself is broken, and of course it is also broken when you download it. In addition, excessive "passionate" scanning of some antivirus software may also cause file corruption. Diagnostic problem: Determine if the file is really corrupt

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

MySQL refused to start? Don’t panic, let’s check it out! Many friends found that the service could not be started after installing MySQL, and they were so anxious! Don’t worry, this article will take you to deal with it calmly and find out the mastermind behind it! After reading it, you can not only solve this problem, but also improve your understanding of MySQL services and your ideas for troubleshooting problems, and become a more powerful database administrator! The MySQL service failed to start, and there are many reasons, ranging from simple configuration errors to complex system problems. Let’s start with the most common aspects. Basic knowledge: A brief description of the service startup process MySQL service startup. Simply put, the operating system loads MySQL-related files and then starts the MySQL daemon. This involves configuration

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.
