RRDtool draws MRTG Log Data

Source: Internet
Author: User
Tags rrd rrdtool mrtg

Summary: The company has drawn a flow chart using mrtg. here we need to summarize the existing flow chart. I want to use rrdtool to plot the total traffic diagram.
1. Understand the mrtg log format:

1233645600 1543128 37293414 1724764 39253194
1233645300 1585731 38744665 1794511 41541920
1233645000 2006102 47017612 2433360 53782126
......
MRTG logs are divided into two parts:

+ Part 1

The first line of the log is the first part. There are three columns, representing the previous running timestamp of MRTG.
Total traffic and total output traffic.

+ Part 2

The remaining part of the first row is the second part, with a total of five columns representing:
1. The timestamp of the related data in column A (first column). Note that the time between each row at the beginning
Every five minutes, the last day.
Perl-e 'print scalar localtime (x), "\ n "'

2. average input (average incoming) traffic per second in column B (second), in bytes.

3. average output (average outgoing) traffic per second in Column C, in bytes.

4. D (column 4) maximum incoming (in bytes)
Bit. This is calculated from all updates in the current interval. Assume that the current interval is 1 hour, every 5 minutes
Once, this value is the largest of all 12 data records.

5. E (column 5) maximum outgoing per second during the current interval, in bytes
Bit. The calculation method is the same as above.

2. Collect mrtg logs:
[Root @ hj addflow] # cat addflow. sh
#! /Bin/sh
If [$ #-ne 3]; then
Echo "usage: $0 log_file log_dir swfile"
Echo "Example usage: $0 all_sw.log all all_sw"
Exit 1
Fi
Curdir = "/usr/local/NetFlow/All_mrtg_log"
Vlan1 = "$ curdir/$1"
Swfile = "$ curdir/$2"
Swlog = 'cat $ swfile'
Log_dir = $3
For I in $ swlog
Do
Rsync-avL -- timeout = 120 $ curdir/log/$ I $ curdir/addflow/$ log_dir
Done
# Getti ()
#{
# Ti = $1
#}
# While read LINE
# Do
# Getti $ LINE
# Break
# Done <$ vlan1
Addto ()
{
To1 = 'expr $ to1 + $2'
To2 = 'expr $ to3 + $3'
}
Addport ()
{
Ti1 = 'expr $ ti1 + $2'
Ti2 = 'expr $ ti2 + $3'
Ti3 = 'expr $ ti3 + $4'
Ti4 = 'expr $ ti4 + $5'
}
Sumport ()
{
File = "$ dir/$1"
Num = 0
While read PORT
Do
If [$ num-gt 1]; then
Break
Fi
If [$ num-lt 1]; then
Addto $ PORT
Else
Addport $ PORT
Fi
Num = 'expr $ num + 1'
Done <$ file
}
Ti = 'date + % s'
Port = 'ls $ curdir/addflow/$ log_dir/*. Log'
For I in $ port
Do
Sumport $ I
Done
Sed-I '1, 2d '$ vlan1
Sed-I "1 I $ ti $ ti1 $ ti2 $ ti3 $ ti4" $ vlan1
Sed-I "1 I $ ti $ to1 $ to2" $ vlan1

3. Create an rrd File
The GAUGE type is used here, and the calculated data is directly stored in rrd.
[Root @ hj NetFlow] # cat create_rrd.sh
#! /Bin/bash
/Usr/bin/rrdtool create all_sw.rrd -- step 300 DS: input: GAUGE: 600: 0: u ds: output: GAUGE: 600: 0: u rra: AVERAGE: 0.5: 1: 2400 RRA: AVERAGE: 0.5: 4: 2400 RRA: AVERAGE: 0.5: 24: 2400 RRA: AVERAGE: 0.5: 288: 2400

Update rrd and drawing are written to a bash script.
4. Update the rrd file Function
Update_rrd ()
{
Rrdfile = $1
Log_file = $2
STEP = 2
HEARTBEAT = 4
Now = 'date + % s'
Input = 'less $ log_file | awk '{if (NR = 2) print $2 }''
Output = 'less $ log_file | awk '{if (NR = 2) print $3 }''
Echo-e "input: $ input \ noutput: $ output"
/Usr/bin/rrdtool updatev $ rrdfile $ now: $ input: $ output
}

5. rrdtool draws the diagram of the past 24 hours
Update_png ()
{
DIR = $1
Image_path = "$ {DIR}/$2"
Rrdfile = "$ {DIR}/$3"
PIC =$ {image_path}/$4
Dk = $5
Title1 = $6
Now = 'date "+ % Y/% m/% d % H \: % M \: % S "'
Rrdtool graph $ PIC \
-- Title "$ title1 "\
-- Vertical-label "unit: (Bits per Second )"\
-- End now -- start end-86400 \
-W 700-h 200 \
-Y-X 9-B 1000 \
-- X-grid MINUTE: 12: HOUR: 1: HOUR: 1: 0: '% H '\
-- Lower-limit = 0 \
-- Font TITLE: 10 :\
-- Font AXIS: 7 :\
-- Font LEGEND: 8 :\
-- Font UNIT: 7 :\
DEF: in = $ rrdfile: input: AVERAGE \
DEF: out = $ rrdfile: output: AVERAGE \
CDEF: v1 = in, 8 ,*\
CDEF: v2 = out, 8 ,*\
AREA: v1 #00FF00: "Upload "\
Uplint: v1: LAST: "Current: % 8.2lf % s "\
Uplint: v1: MAX: "MAX: % 8.2lf % s "\
Uplint: v1: MIN: "MIN: % 8.2lf % s "\
Uplint: v1: AVERAGE: "AVERAGE: % 8.2lf % s \ n "\
LINE1: v2 # 0000ff: "Download "\
Uplint: v2: LAST: "Current: % 8.2lf % s "\
Uplint: v2: MAX: "MAX: % 8.2lf % s "\
Uplint: v2: MIN: "MIN: % 8.2lf % s "\
Uplint: v2: AVERAGE: "AVERAGE: % 8.2lf % s \ n "\
COMMENT: "total purchased bandwidth $ {dk} G \ t \ tLast Updated \: $ now"
}

6. rrdtool draws the diagram of the past week
Update_week_png ()
{
DIR = $1
Image_path = "$ {DIR}/$2"
Rrdfile = "$ {DIR}/$3"
PIC =$ {image_path}/$4
Dk = $5
Title1 = $6
Now = 'date "+ % Y/% m/% d % H \: % M \: % S "'
Etime = 'date + % s'
Stime = 'expr $ etime-604800'
Echo "etime: $ etime stime: $ stime"
Rrdtool graph $ PIC \
-- Title "$ title1 "\
-- Vertical-label "unit: (Bits per Second )"\
#-S 'date-d "-1 week" + % s '\
-- Start = $ stime \
-- End = $ etime \
-W 700-h 200 \
-Y-X 9-B 1000 \
-- Lower-limit = 0 \
-- Font TITLE: 10 :\
-- Font AXIS: 7 :\
-- Font LEGEND: 8 :\
-- Font UNIT: 7 :\
DEF: in = $ rrdfile: input: AVERAGE \
DEF: out = $ rrdfile: output: AVERAGE \
CDEF: v1 = in, 8 ,*\
CDEF: v2 = out, 8 ,*\
AREA: v1 #00FF00: "Upload "\
Uplint: v1: LAST: "Current: % 8.2lf % s "\
Uplint: v1: MAX: "MAX: % 8.2lf % s "\
Uplint: v1: MIN: "MIN: % 8.2lf % s "\
Uplint: v1: AVERAGE: "AVERAGE: % 8.2lf % s \ n "\
LINE1: v2 # 0000ff: "Download "\
Uplint: v2: LAST: "Current: % 8.2lf % s "\
Uplint: v2: MAX: "MAX: % 8.2lf % s "\
Uplint: v2: MIN: "MIN: % 8.2lf % s "\
Uplint: v2: AVERAGE: "AVERAGE: % 8.2lf % s \ n "\
COMMENT: "total purchased bandwidth $ {dk} G \ t \ tLast Updated \: $ now"
}

7. function call
M_DIR =/usr/local/NetFlow
Update_rrd $ M_DIR/all_sw.rrd $ M_DIR/All_mrtg_log/all_sw.log
Update_png $ M_DIR images all_sw.rrd all_sw.png 1024 "Total offline and cloud streaming traffic in the past 24 hours"
Update_week_png $ M_DIR images all_sw.rrd all_sw_week.png 1024 "Total offline and cloud broadcast traffic in the past week"

8. display:

Use RRDtool to customize plotting to monitor Oracle databases

Simple Image Rendering using RRDtool

When installing RRDtool, the following error occurs: configure: error: Please fix the library issues listed above and try again.

RHEL5.5 installing RRDtool solves many Dependencies

Use yum to install RRDtool in CentOS

Related Article

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.