Home > Database > Mysql Tutorial > body text

将RRD数据库中数据导入MYSQL中_MySQL

WBOY
Release: 2016-06-01 13:41:07
Original
1604 people have browsed it

bitsCN.com
将RRD数据库中数据导入MYSQL中 一、RRD数据库及RRDTOOL简介意为Round Robin Database。设计理念为按照round-robin的方式进行存储,在一个周期之后(可自己定义),新的数据会覆盖掉原来的数据。所以RRD数据库适合用来存储动态数据,并且不需长期存储。因为是周期性的覆盖旧的数据所以数据库的大小基本上就会固定下来,并不会随着时间而增大。 RRDTOOL是由Tobias Oetiker开发的自由软件,使用RRD作为存储格式。RRDTOOL提供了很多工具用来对RRD数据库    进行操作,包括创建,更新,查询,以及生成显示图等。RRDTOOL同时也提供了很多语言的API以方便操作。 Ganglia是一个分布式的监控系统,采用RRD数据库进行数据存储和可视化。Hadoop源码包里即有一个与ganglia相关的配置文件,修改一些参数和对ganglia进行一些设置即可对hadoop集群进行监控。每个不同的属性的数据都存在一个RRD数据库里。 二、将数据导入MYSQL中也会存在这样的情况,可能想对rrdtool采集到的数据进行长期存储,从而进行一些分析。而RRD数据库的数据是不断更新的,虽然也可以保留长期的数据,但精度不够。比如说一个RRD数据库的步长为15秒,也就是说,每隔15秒,就会有一个新的值存入(比如内存使用率),同时覆盖一个旧的值。一个RRD数据库存储5761个这样的数据(一天+15    秒).而且随着时间的推移总是存储最近一天的数据。然后在通过这些值不断地计算步长更高的值,比如我们可以通过这些15秒的数据算出360s的数据(平均值),然后以360s为步长将这些值再存进去,不过这时候可以存储的时间区间就更长了,同样的行数可以存储24天的数据。以此类推,也可以以一天为单位存储一年的数据,不过这时候的精度就只有一天了,那些旧的15s的数据都已经被覆盖掉了。如果想要把这些数据都存储起来,就需要通过脚本定时进行数据导入。LINUX上做这些是很方便的,perl,python,lua,ruby都是不错的选择,shell也可以。然后用crond设置在一定时间定时执行即可。以下是python的示例代码:(注:python学的一般,基本上是边看书,边写的代码,问题不少,请各位指正。) 首先是初始化,创建数据库及相应的表:import osimport MySQLdbimport string root="/var/lib/ganglia/rrds/hap-clu"dirs=os.listdir(root) map1=string.maketrans('.','_')map2=string.maketrans('-','_') conn=MySQLdb.connect(host='localhost', user='root',passwd='123456')cursor=conn.cursor()     for onedir in dirs:    dbname=onedir.translate(map1).translate(map2)    cursor.execute("create database if not exists "+dbname)    conn.commit()    conn.select_db(dbname)    # print onedirname    print "DB:"+dbname+" ."    files=os.listdir(root+"/"+onedir)    for onefile in files:        tablename=onefile[:-4].translate(map1)        if(dbname=="__SummaryInfo__"):            cursor.execute("create table if not exists "+tablename+"(time_id int not null primary key,value varchar(30),num varchar(30))")        else:            cursor.execute("create table if not exists "+tablename+"(time_id int not null primary key,value varchar(30))")        conn.commit()       # print "CREATE TABLE "+tablename    print "CREATE DATABASE "+dbname+" " cursor.close(); 这里面有不少说明的地方:1.存储的目录:ganglia里面默认是这个目录,不过可以修改。其他不同应用也应该不同。最后的那个hap-clu是集群    的名字。在这个目录下,每个节点占一个目录,目录名一般为IP地址,最后还有一个summary的目录。对应着,为每个目录(节点)创建一个数据库,每个属性一个表。2.MYSQL数据库和表的命名规则中不允许有"."和"-",所以对应的数据库名和表名要做相应的转换。这里使用的是translate函数。 3.原本以为这个脚本只需执行一次,不过在实际应用过程中,发现表的数量和数据库的数量可能会增加。比如有新添加的节点,就需要及时为它创建数据库。对于一些已存在的节点,有可能有些属性的数据是后来才检测到的。比如我碰到的情况就是运行了一段时间之后关于swap的统计信息才出来,RRD数据库也才创建。我不知道这是配置的问题还是常态。但为了顺利运行,这个脚本也要每天和插入数据的脚本一样定时运行,并且在后者之前。 插入数据的脚本:import osimport commandsimport MySQLdbimport stringimport rrdtool#from xml.etree.ElementTree import ElementTree    #working directoryroot="/var/lib/ganglia/rrds/hap-clu"dirs=os.listdir(root) #mysql table name limitmap1=string.maketrans('.','_')map2=string.maketrans('-','_') conn=MySQLdb.connect(host='localhost', user='root',passwd='123456')cursor=conn.cursor() for onedir in dirs:    dbname=onedir.translate(map1).translate(map2)    conn.select_db(dbname)     print "DB:"+dbname+" ."    files=os.listdir(root+"/"+onedir)    os.chdir(root+"/"+onedir)    for onefile in files:        # it seems that all is AVERAGE        tablename=onefile[:-4].translate(map1)         data=rrdtool.fetch(onefile,"AVERAGE")        firsttime=data[0][0]            count=0        while count

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!