CodeIgniter: rrdtool library

Simple CodeIgniter rrdtool library for rrd file usage. With this library you can: create rrd files, display graphs from rrd files and update rrd files with data. Library has functions that enable monitoring network interface traffic and usage.

You can read all about rrd files and great rrdtool at http://oss.oetiker.ch/rrdtool/.


Library installation and usage instructions:

  1. rrd extensions for php - for library to work first you need to enable rrd support in your php installation. "How to" procedure and other details can be found here: www.ioncannon.net. Debian and Ubuntu users can read blog comments for instructions how to build .deb package. Using php_info() function in php, you can verify that rrd extension is installed and working.

  2. CodeIgniter installation - you need fully functional CI installation. You can get instructions and all files at www.codeigniter.com.

  3. Library installation - download library Rrd.php, remove trailing .txt and place it in CI library folder, usually something like: system/application/libraries

  4. Controller example - this example shows how to use rrd library in CI controller. "Index" function diplays graphs created from rrd files, and "insert_data" function updates rrd file with data. As parameters for this function you need to pass counters for interface traffic in bytes (for example: "http://localhost/rrdlib/index.php/rrdtest/insert_data/345677/456444)". Update time for this graph type is 5 minutes.
    
    <?php

    class Rrdtest extends Controller {

        function 
    Rrdtest()
        {
            
    parent::Controller();
        }
        
        function 
    index()
        {
            
    $this->load->library('rrd');
            
    #data_dir must be writeable by web browser
            
    $data_dir='/var/www/test/';
            
    $rrd_file=$data_dir.'data.rrd';

            
    #create graph images
            
    $this->rrd->create_image($rrd_file$data_dir.'day_graph.png''1d');
            
    $this->rrd->create_image($rrd_file$data_dir.'week_graph.png''1w');
            
    $this->rrd->create_image($rrd_file$data_dir.'month_graph.png''1m');
            
    $this->rrd->create_image($rrd_file$data_dir.'year_graph.png''1y');
            
            
    #display graph images
            
    print '<div style="text-align: center;">';
            print 
    '<h3>Day</h3><img src="/test/day_graph.png"></img><br>';
            print 
    '<h3>Week</h3><img src="/test/week_graph.png"></img><br>';
            print 
    '<h3>Month</h3><img src="/test/month_graph.png"></img><br>';
            print 
    '<h3>Year</h3><img src="/test/year_graph.png"></img><br>';
            print 
    '</div>';
        }
            
        function 
    insert_data($input$output)
        {
            
    $this->load->library('rrd');
            
    #data_dir must be writeable by web browser
            
    $data_dir='/var/www/test/';
            
    $rrd_file=$data_dir.'data.rrd';
            
            
    #if rrd file doesn't exist create one
            
    if (!file_exists($rrd_file)) $this->rrd->create($rrd_file);
            
    $this->rrd->update($rrd_file, array($input$output));
        }

    }
    Resulting page looks something like this:

  5. Improvements - for futher library extensions it would be nice to add new rrd and graph types. Better error control would be nice also :-).

If you have any questions please leave comment.

01.10.2009. 15:34