Thursday, August 4, 2016

create rrd database, update and generate graph from it

1- install the necessary packages
$ sudo apt-get install rrdtool python-rrdtool


2- first thing, rrd's start date can not be older than 1970-01-01, then it will start data match with the second after that start date.

so if you want to create the file with the specific start date, you need to use julian date to facilitate with it.
for example you want to create rrd file with the start date  of 2016-08-02, you can use the script below as a sample

from astropy.time import Time
y = Time("1970-01-01")
print "iso to julian date"
y.format = "jd"
print y
secday = 24 * 60 * 60
print "total second"
print y.value * secday
olddate = y.value * secday

y = Time("2016-08-02")
print "iso to julian date"
print y
y.format = "jd"
print "total second after 1970-01-01"
print (y.value * secday) - olddate


and the result of total second from 1970-01-01 to 2016-08-02 is 1470096000.0


3- script to create rrd file, we use the total second from above, but if you want to create the start date now, you change 1470096000 to 0
import sys
import rrdtool

ret = rrdtool.create("net.rrd", "--step", "300", "--start", '1470096000',
 "DS:input:COUNTER:600:U:U",
 "DS:output:COUNTER:600:U:U",
 "RRA:AVERAGE:0.5:1:600",
 "RRA:AVERAGE:0.5:6:700",
 "RRA:AVERAGE:0.5:24:775",
 "RRA:AVERAGE:0.5:288:797",
 "RRA:MAX:0.5:1:600",
 "RRA:MAX:0.5:6:700",
 "RRA:MAX:0.5:24:775",
 "RRA:MAX:0.5:444:797")


if ret:
 print rrdtool.error()

4- to update the date, we can get the value from snmp, or anything that can provide the the value

either one of the 3 below scripts do the update, so choose one that meet your need.

a- update value and sleep for 300 second the same as the step value when you create the rrd
import sys
import time
import random
import rrdtool

total_input_traffic = 0
total_output_traffic = 0

while 1:
 total_input_traffic += random.randrange(1000, 1500)
 total_output_traffic += random.randrange(1000, 3000)
 ret = rrdtool.update('net.rrd','N:' + `total_input_traffic` + ':' + `total_output_traffic`);
 if ret:
  print rrdtool.error()
 time.sleep(300)



b- after every 300 seconds but no sleep time
import sys
import time
import random
import rrdtool

total_input_traffic = 0
total_output_traffic = 0
n1 = 30000
for n in range(300,3000000,300):
 n1 -= n
 sec = 1470096000
 total_input_traffic += random.randrange(1000, 200000)
 total_output_traffic += random.randrange(1000, 300000)
 ret = rrdtool.update('net.rrd',str(sec + n) +':' + `total_input_traffic` + ':' + `total_output_traffic`);

 if ret:
  print rrdtool.error()
 print n1



c- after data every second and with no sleep time
import sys
import time
import random
import rrdtool

total_input_traffic = 0
total_output_traffic = 0
n1 = 30000
for n in range(1,30000):
 n1 -= n
 sec = 1470096000
 total_input_traffic += random.randrange(1000, 200000)
 total_output_traffic += random.randrange(1000, 300000)
 ret = rrdtool.update('net.rrd',str(sec + n) +':' + `total_input_traffic` + ':' + `total_output_traffic`);
 if ret:
  print rrdtool.error()
 print n1



5- finally the the rrd has data inside, we can generate from the file and the script is below
import sys
import rrdtool

ret = rrdtool.graph( "net.png", "--start", "-1d", "--vertical-label=Bytes/s",
 "DEF:inoctets=net.rrd:input:AVERAGE",
 "DEF:outoctets=net.rrd:output:AVERAGE",
 "AREA:inoctets#00FF00:In traffic",
 "LINE1:outoctets#0000FF:Out traffic",
 "CDEF:inbits=inoctets,8,*",
 "CDEF:outbits=outoctets,8,*",
 "COMMENT:\r",
  )





screenshot the graph using script above



No comments:

Post a Comment