Integrates with the centreon monitoring platform to perform secondary analysis and highchart presentation on rrdtool data

Source: Internet
Author: User
Tags rrd rrdtool timedelta

I don't know, is it true that all companies are very hardworking every week to collect statistics on the company's IDC load. To meet this requirement, you can use the monitoring platforms such as cacti or centreon to manually select the service usage at a specific time, such as load and memory. The most recent requirement is to count the maximum and minimum network traffic values for six time periods a day and register them into the book. The general method is to log on to the monitoring platform, select a specific time to plot, and then perform statistics. In this way, I really want to say that it is weak, and I have been repeating this kind of work before. This time, I plan to simplify this process using my less sophisticated Django, Python, rrdtool, highcharts, and jquery.

The entire process:

1. Use rrdtool xport to export traffic data to XML format

2. Django views to return XML

3. The front-end parses XML through jquery to generate an array of metric values and a time array for six time periods.

4. With these 6 arrays, calculate the maximum and minimum values and insert them into the table.

5. If there is a table, use highcharts to plot the table.


:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3E/52/wKiom1PHPlehlCHFAALYj_3hp9k243.jpg "Title =" qq20140717110833.png "alt =" wkiom1phplehlchfaalyj_3hp9k243.jpg "/>

Code sharing:

1. A button

<button onclick="parsexml(‘/time/1/‘)" class="btn btn-primary" >1 day ago</button>

2. parsexml parsing functions:

Function parsexml (XML) {$. ajax ({type: 'get', URL: XML, datatype: 'xml', // note that the type of information to be processed is XML. The default value is HTML success: function (docxml) {var value0 = parseint ($ (docxml ). find ('V '). first (). text (); var timearray = new array (); var valuearray = new array (); $ (docxml ). find ('row '). each (function () {var time = parsefloat ($ (this ). children ('T '). text (); VaR value = parsefloat ($ (this ). children ('V '). text (); timearray. push (time) valuearray. push (value)}); // end each var starttime = getlocaltime (timearray [0]); var endtime = getlocaltime (timearray [1439]); var value10 = valuearray. slice (0,600); var value12 = valuearray. server Load balancer (600,720); var value14 = valuearray. slice (720,840); var value19 = valuearray. slice (840,1140); var value21 = valuearray. slice (1140,1260); var value24 = valuearray. slice (1260,1440); $ ("# 10max "). text (value10.max (); $("#10 min "). text (value10.min (); $ ("# 12max "). text (value12.max (); $("#12 min "). text (value12.min (); $ ("# 14max "). text (value14.max (); $("#14 min "). text (value14.min (); $ ("# 19max "). text (value19.max (); $("#19 min "). text (value19.min (); $ ("# 21max "). text (value21.max (); $("#21 min "). text (value21.min (); $ ("# 24max "). text (value24.max (); $("#24 min "). text (value24.min (); loadchart (starttime, endtime);} // end of success}); // end of Ajax} // end of parsexml

3. The highcharts function for generating images:

function loadchart(starttime,endtime){    $(document).ready(function(){    $(‘#TrafficHighChart‘).highcharts({        data: {            table: document.getElementById(‘datatable‘)        },        chart: {            type: ‘column‘        },        title: {            text: ‘From ‘ + starttime + ‘ To ‘ + endtime        },        yAxis: {            allowDecimals: false,            title: {                text: ‘IDC Traffic‘            }        },        tooltip: {            formatter: function() {                return ‘<b>‘+ this.series.name +‘</b><br>‘+                    this.y;            }        }    }); //end of highcharts});//end of documentready            } //end of loadchart

4. Calculate the maximum and minimum values of an array:

Array. prototype. min = function () {var min = This [0]; var Len = This. length; For (VAR I = 1; I <Len; I ++) {If (this [I] <min) {min = This [I] ;}} min = (min/1048576 ). tofixed (2); Return min;} array. prototype. max = function () {var max = This [0]; var Len = This. length; For (VAR I = 1; I <Len; I ++) {If (this [I]> MAX) {max = This [I] ;}} max = (max/1048576 ). tofixed (2) return Max;} function parsexml (XML) {$. ajax ({type: 'get', URL: XML, datatype: 'xml', // note that the type of processing information is XML, the default value is HTML} max = (max/1048576 ). tofixed (2) return Max;

5. Table format:

    <table id="datatable" style="margin-left:20px;" class = "table table-bordered table-striped">    <thead>      <tr>        <th>Time Range</th>        <th>max(Mb/s)</th>        <th>min(Mb/s)</th>      </tr>    </thead>    <tbody>      <tr>        <th>00:00-10:00</th>        <td id = "10max"></td>        <td id = "10min"></td>      </tr>      <tr>        <th>10:00-12:00</th>        <td id = "12max"></td>        <td id = "12min"></td>      </tr>      <tr>        <th>12:00-14:00</th>        <td id = "14max"></td>        <td id = "14min"></td>      </tr>      <tr>        <th>14:00-19:00</th>        <td id = "19max"></td>        <td id = "19min"></td>      </tr>      <tr>        <th>19:00-21:00</th>        <td id = "21max"></td>        <td id = "21min"></td>      </tr>      <tr>        <th>21:00-24:00</th>        <td id = "24max"></td>        <td id = "24min"></td>      </tr>    </tbody>  </table>

6. highchart <div>

<div id="TrafficHighChart" style="min-width:700px;height:400px"></div>


7. Django views:

Find the name of the rrdtool database file.

def trfrrdxport(request,offset):    try:        offset = int(offset)    except ValueError:        raise Http404()    rrd = ‘/var/lib/centreon/metrics/711.rrd‘    start = int(time.mktime((datetime.date.today() - datetime.timedelta(days = offset)).timetuple()))    end = int(time.mktime((datetime.date.today() - datetime.timedelta(days = offset - 1)).timetuple()))    step = ‘60‘    legend = ‘IDC_Traffic‘    xport = rrdxport(rrd,start,end,step,legend)    return HttpResponse(xport)

8. Django urls:

(r‘^time/(\d)/$‘,trfrrdxport),

9. rrdxport () XML export function:

def rrdxport(rrd,start,end,step,legend):    s = paramiko.SSHClient()    s.load_system_host_keys()    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())    try:        pkey_file=‘/home/tadu/.ssh/id_rsa‘        key = paramiko.RSAKey.from_private_key_file(pkey_file)        s.connect(‘xxx.xxx.xxx.xxx‘,22,‘root‘,pkey=key,timeout=5)        cmd = "/usr/bin/rrdtool xport --start %s --end %s DEF:value1=%s:traffic_out:AVERAGE:step=%s  XPORT:value1:‘%s‘" % (start,end,rrd,step,legend)        stdin,stdout,stderr = s.exec_command(cmd)        return stdout.read()    except Exception,e:        return e

Note: I used SSH to obtain this function on the monitoring host.


This is just a method for front-end analysis and presentation through rrdtool data. I use this example to prove its possibility. If you want to analyze the average memory usage of 100 servers per week, then draw a cursor to view the top 10 data. This method can also be implemented.




On July 22, July 17, 2014, today is a special day. All previous articles are cleared. I hope everything will be fine with this article!









This article is from the "Dreaming, zhonglin" blog, please be sure to keep this http://gaozhonglin.blog.51cto.com/801285/1439399

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: info-contact@alibabacloud.com 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.