본문 바로가기
  • AI (Artificial Intelligence)
Security/Monitoring

[Monitoring] RRDTOOL + MRTG 트래픽 페이지 보기 (CentOS6.5)

by 로샤스 2014. 12. 3.

[전제사항]

당삼 설치하기전에 apache, php 는 깔려있어야한다. 알아서 설치 하세요


=> php 설치 확인

php  -- version, php  --modules

 => apache 설치

# wget http://apache.mirror.cdnetworks.com/httpd/httpd-2.2.29.tar.gz
# mkdir /usr/local/server/apache/
# mv ./httpd-2.2.29.tar.gz usr/local/server/apache/
# tar zxvf httpd-2.2.29.tar.gz
(알아서 푸세요..)
# cd httpd-2.2.29.tar.gz (안 옮기고 설치하셔도 됩니다)
# ./configure --prefix=/usr/local/server/apache/
# make && make install

=> apache 확인

# cd..
# ./bin/httpd -V

http://localhost/ 접속
 

 

1. MRTG설치

 

# yum install mrtg

또는 컴파일 설치 (아래설명은 컴파일로 설치한 디렉토리 기준으로 설명합니다.)


# rpm -qa zlib gd libpng       ===> 3가지 라이브러리 설치 확인.
--------------------------------------------------------------------------------------
gd-2.0.33-9.4.el5_4.2
libpng-1.2.10-7.1.el5_5.3
zlib-1.2.3-3
--------------------------------------------------------------------------------------
만약 미설치시 # yum install zlib gd libpng 설치.

# wget http://oss.oetiker.ch/mrtg/pub/mrtg-2.16.4.tar.gz
# tar xvfz mrtg-2.16.4.tar.gz
# cd mrtg-2.16.4
# ./configure --prefix=/usr/local/server/mrtg/
--------------------------------------------------------------------------------------
** Ooops, one of many bad things happened:

   a)  You don't have the GD library installed.
       Get it from http://www.boutell.com, compile it and
--------------------------------------------------------------------------------------
=> 위와 같은 문구가 나올 시, 아래와 같이 설치하고 다시 하세요

# yum install gd-devel
--------------------------------------------------------------------------------------

# make && make install

# cd /usr/local/server/mrtg/
# mkdir www conf
--------------------------------------------------------------------------------------
/usr/local/server/mrtg/www      mrtg 결과물 페이지(웹 홈디렉토리)
/usr/local/server/mrtg/conf     mrtg 설정파일(.cfg) 저장위치
/usr/local/server/mrtg/bin      mrtg 실행파일 (mrtg, cfgmaker, indexmaker) 저장위치
--------------------------------------------------------------------------------------


 

2. RRDTOOL설치

 

# rpm -qa freetype libart_lgpl     ===>2가지 라이브러리 설치확인.
--------------------------------------------------------------------------------------
libart_lgpl-2.3.17-4
freetype-2.2.1-28.el5_5
--------------------------------------------------------------------------------------
만약 미설치시 # yum install freetype libart_lgpl 설치.

# wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.4.tar.gz
# tar xvfz rrdtool-1.4.4.tar.gz
# cd rrdtool-1.4.4
# ./configure --prefix=/usr/local/server/rrdtool --disable-python --disable-tcl
# make && make install

--------------------------------------------------------------------------------------
/usr/local/server/rrdtool/      rrdtool 설치 경로
/usr/local/server/mrtg/bin      rrdtool 실행파일 저장위치
--------------------------------------------------------------------------------------

 

 

3. 트래픽 보기 설정.(여기서는 리눅스 서버 자기꺼 한대만 하기로한다.)

 

▶ 로컬 리눅스 서버 snmp 설정

 

# yum install net-snmp
# vi /etc/snmp/snmpd.conf

------젤위에 추가하고 저장-------------------------------------------------------
com2sec local     localhost          nforce
com2sec local     211.233.xxx.xxx    nforce

group MyROGroup  any        local

view all    included  .1                               80

view mib2   included  .iso.org.dod.internet.mgmt.mib-2 fc

access MyROGroup ""      any       noauth    0      all    none   none
-------------------------------------------------------------------------

 

# service snmpd restart


 

▶ mrtg 설정

 

# /usr/local/server/mrtg/bin/cfgmaker nforce@211.233.xxx.xxx> /usr/local/server/mrtg/conf/nforce.cfg

설명 : nforce(커뮤니티)@211.233.xxx.xxx(서버ip)

 

# vi /usr/local/server/mrtg/conf/nforce.cfg       파란색 줄 추가 저장.
------------------------------------------------------------------------------------------------------
WorkDir: /usr/local/server/mrtg/www/

Language: Korean
LogFormat: rrdtool
PathAdd: /usr/local/server/rrdtool/bin/
LibAdd: /usr/local/server/rrdtool/lib/perl/


Target[211.233.xxx.xxx_2]: 2:nforce@211.233.xxx.xxx:
SetEnv[211.233.xxx.xxx_2]: MRTG_INT_IP="211.233.xxx.xxx" MRTG_INT_DESCR="eth0"
MaxBytes[211.233.xxx.xxx_2]: 12500000
Title[211.233.xxx.xxx_2]: Traffic Analysis for 2 -- sabzil
PageTop[211.233.xxx.xxx_2]:

               
                                
                                        System:
                                        sabzil in Unknown (edit /etc/snmp/snmpd.conf)
                               
                               
                                        Maintainer:
                                        Root (configure /etc/snmp/snmp
.local.conf)
                               
                               
                                        Description:
                                        eth0 
                               
                               
                                        ifType:
                                        ethernetCsmacd (6)
                               
                               
                                        ifName:
                                        eth0
                               
                               
                                        Max Speed:
                                        12.5 MBytes/s
                               
                               
                                        Ip:
                                        211.233.xxx.xxx ()
                               
                       
               

----------------------------------------------------------------------------------------------------

 

# crontab -e    아래를 추가해 주고 저장.(5분마다mrtg 실행)
----------------------------------------------------------------------------------------------------------
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/local/server/mrtg/bin/mrtg /usr/local/server/mrtg/conf/nforce.cfg
----------------------------------------------------------------------------------------------------------

 

첨부한 index.php 파일을 웹서버 홈디렉토리에 업로드 후
----------------------------------------------------------------------------------------------------------
<?php
/***************************************************************************\
*                 MRTS - MRTG RRDtool Total Statistics v0.1                 *
*****************************************************************************
* This program is free software; you can redistribute it and/or modify it   *
* under the terms of the GNU General Public License as published by the     *
* Free Software Foundation; either version 2 of the License, or (at your    *
* option) any later version.                                                *
*****************************************************************************
* This script is written by Thor Dreier                                     *
* More information can be found at  http://apt-get.dk/mrts/                 *
\***************************************************************************/

 

/* The directory where the rrd files are located */
$dir = '/var/log/mrtg';

 

/* List all devices that MRTS should'n display, */
$exclude = array('secret', 'topsecret');

 

/* RRDtool path - where are the the executable located */
$rrdcommand = '/usr/bin/rrdtool';

 

/* Change this to get another top on the site */
function top($name)
{ ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title><?php echo $GLOBALS['title']; ?></title>
  <style type="text/css">
   <!--

   .datahead,.total1head,.total2head,.data,.total1,.total2
   {
     border: 1px solid black;
     width: 50px;
   }

   .datahead,.total1head,.total2head
   {
    font-size: 12px;
    font-weight: bold;
   }

   .data,.total1,.total2
   {
    font-size: 10px;
   }

   .total1,.total1head
   {
    background-color: #dddddd;
   }

   .total2,.total2head
   {
    background-color: #bbbbbb;
   }

   h1,h2,h3
   {
    text-align: center;
   }

   -->
  </style>
 </head>
 <body>
 <h1><?php echo $GLOBALS['title']; ?></h1>
 <h2><?php echo $name; ?></h2>
<?php }

 

/* Change this to get another bottom on the site */
function bottom()
{ ?>
  <hr>
  Created by <a href="http://apt-get.dk/mrts/"><?php echo $GLOBALS['title'] ?></a>
 </body>
</html>
<?php }

 

/***************************************************************************\
*                It should be no need to edit anything below                *
*                   this point, unless there are problems                   *
*****************************************************************************
* - or said in another way - if you change anything radical below here, you *
* have to make the changes public, as this code is GNU licensed code        *
\***************************************************************************/

 

 


/***************************************************************************\
*                                 Variables                                 *
\***************************************************************************/

 

 /* File extension of the MRTG-RRD-files */
 $extension = '.rrd';

 /* This version */
 $version = 'v0.1.1';

 /* The title */
 $title = "MRTS - MRTG RRDtool Total Statistics $version";

 

 


/***************************************************************************\
*                                 Functions                                 *
\***************************************************************************/

 

 /* Checks if a name is a valid device name */
 function validname($name)
 {
  return in_array($name, $GLOBALS['legalnames']);
 } //function validname($name)

 

 /* Convert a device name to a file name */
 function filename($name)
 {
  return $GLOBALS['dir'].'/'.$name.$GLOBALS['extension'];
 } //function filename($name)

 

 /* Formats a number with KB, MB etc. */
 function humanreadable($size)
 {
  $names = array('B', 'KB', 'MB', 'GB', 'TB');
  $times = 0;
  while($size>1024)
  {
   $size = round(($size*100)/1024)/100;
   $times++;
  }
  return "$size " . $names[$times];
 } //function humanreadable($size)

 

 /* Convert a month number to a month name */
 function monthname($no)
 {
  $names = array(1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  return $names[$no];
 } //function monthname($no)

 

 /* Convert year and month number to a string useful for rrdtool  */
 function monthstartend($year, $month)
 {
  $start = mktime(0, 0, 0, $month, 1, $year);
  if($month==12)
   $end = mktime(0, 0, 0, 1, 1, $year+1);
  else
   $end = mktime(0, 0, 0, $month+1, 1, $year);
  return " -s $start -e $end ";
 }

 

 /* Output HTML for a year */
 function showyear($year, $months)
 {

  $sumyear = array();
  $sumquater = array();

  printf("<h3>Year: %s</h3>\n", $year);
  printf("<table><tr><td></td>\n");


  for($quater=1; $quater<=4; $quater++)
  {
   /* month */
   for($i=($quater-1)*3+1; $i<=($quater-1)*3+3; $i++)
   {
    printf("<td class=\"datahead\"><a href=\"%s?name=%s&amp;year=%s&amp;month=%s\">%s (%s)</a></td>\n", $_SERVER['SCRIPT_NAME'], $_GET['name'], $year, $i, monthname($i), $i);
   }
   /* quater */
   printf("<td class=\"total1head\">Quater %s</td>\n", $quater);
  }
  /* year */
  printf("<td class=\"total2head\">Year</td>\n");


  printf("</tr><tr><td class=\"datahead\">In</td>\n");


  for($quater=1; $quater<=4; $quater++)
  {
   /* month */
   for($i=($quater-1)*3+1; $i<=($quater-1)*3+3; $i++)
   {
    printf("<td class=\"data\">%s</td>\n", humanreadable($months[$i]['in']));
    $sumyear['in'] += $months[$i]['in'];
    $sumquater[$quater]['in'] += $months[$i]['in'];
   }
   /* quater */
   printf("<td class=\"total1\">%s</td>\n", humanreadable($sumquater[$quater]['in']));
  }
  /* year */
  printf("<td class=\"total2\">%s</td>\n", humanreadable($sumyear['in']));


  printf("</tr><tr><td class=\"datahead\">Out</td>\n");


  for($quater=1; $quater<=4; $quater++)
  {
   /* month */
   for($i=($quater-1)*3+1; $i<=($quater-1)*3+3; $i++)
   {
    printf("<td class=\"data\">%s</td>\n", humanreadable($months[$i]['out']));
    $sumyear['out'] += $months[$i]['out'];
    $sumquater[$quater]['out'] += $months[$i]['out'];
   }
   /* quater */
   printf("<td class=\"total1\">%s</td>\n", humanreadable($sumquater[$quater]['out']));
  }
  /* year */
  printf("<td class=\"total2\">%s</td>\n", humanreadable($sumyear['out']));


  printf("</tr><tr><td class=\"datahead\">Max</td>\n");


  for($quater=1; $quater<=4; $quater++)
  {
   /* month */
   for($i=($quater-1)*3+1; $i<=($quater-1)*3+3; $i++)
   {
    printf("<td class=\"data\">%s</td>\n", humanreadable(max($months[$i]['in'], $months[$i]['out'])));
   }
   /* quater */
   printf("<td class=\"total1\">%s</td>\n", humanreadable(max($sumquater[$quater]['in'], $sumquater[$quater]['out'])));
  }
  /* year */
  printf("<td class=\"total2\">%s</td>\n", humanreadable(max($sumyear['in'], $sumyear['out'])));


  printf("</tr><tr><td class=\"datahead\">Sum</td>\n");


  for($quater=1; $quater<=4; $quater++)
  {
   /* month */
   for($i=($quater-1)*3+1; $i<=($quater-1)*3+3; $i++)
   {
    printf("<td class=\"data\">%s</td>\n", humanreadable($months[$i]['in'] + $months[$i]['out']));
   }
   /* quater */
   printf("<td class=\"total1\">%s</td>\n", humanreadable($sumquater[$quater]['in'] + $sumquater[$quater]['out']));
  }
  /* year */
  printf("<td class=\"total2\">%s</td>\n", humanreadable($sumyear['in'] + $sumyear['out']));


  printf("</tr></table>\n");


 } //function showyear($year, $months)

 

 /* Output HTML for a month */
 function showmonth($year, $month, $days)
 {

  $summonth = array();
  $daysinmonth = date("t", mktime(0, 0, 0, $month, 1, $year));

  printf("<h3>Month: %s %s</h3>\n", $year, monthname($month));

  for($j=1; $j<=2; $j++)
  {
   
   if($j==1)
   {
    $start = 1;
    $end = 16;
   }
   else
   {
    $start = 17;
    $end = $daysinmonth;
   }
   
   printf("<table><tr><td></td>\n");

   for($i=$start; $i<=$end; $i++)
   {
    printf("<td class=\"datahead\">%s</td>\n", $i);
   }
   if($j==2)
   {
    printf("<td class=\"total2head\">Month</td>\n");
   }

   printf("</tr><tr><td class=\"datahead\">In</td>\n");

   for($i=$start; $i<=$end; $i++)
   {
    printf("<td class=\"data\">%s</td>\n", humanreadable($days[$i]['in']));
    $summonth['in'] += $days[$i]['in'];
   }
   if($j==2)
   {
    printf("<td class=\"total2\">%s</td>\n", humanreadable($summonth['in']));
   }

   printf("</tr><tr><td class=\"datahead\">Out</td>\n");

   for($i=$start; $i<=$end; $i++)
   {
    printf("<td class=\"data\">%s</td>\n", humanreadable($days[$i]['out']));
    $summonth['out'] += $days[$i]['out'];
   }
   if($j==2)
   {
    printf("<td class=\"total2\">%s</td>\n", humanreadable($summonth['out']));
   }

   printf("</tr><tr><td class=\"datahead\">Max</td>\n");

   for($i=$start; $i<=$end; $i++)
   {
    printf("<td class=\"data\">%s</td>\n", humanreadable(max($days[$i]['in'], $days[$i]['out'])));
   }
   if($j==2)
   {
    printf("<td class=\"total2\">%s</td>\n", humanreadable(max($summonth['in'], $summonth['out'])));
   }

   printf("</tr><tr><td class=\"datahead\">Sum</td>\n");

   for($i=$start; $i<=$end; $i++)
   {
    printf("<td class=\"data\">%s</td>\n", humanreadable($days[$i]['in'] + $days[$i]['out']));
   }
   if($j==2)
   {
    printf("<td class=\"total2\">%s</td>\n", humanreadable($summonth['in'] + $summonth['out']));
   }

   printf("</tr></table>\n");
   
  } //for($j=1; $j<=2; $j++)


 } //showmonth($year, $month, $days)

 

 

/***************************************************************************\
*                                All the rest                               *
\***************************************************************************/

 

 /* Find legalnames */
 $legalnames = array();
 if($dirhandler = @opendir($dir))
 {
  while(($filename = readdir($dirhandler)) !== false)
  {
   if(ereg("$extension$", $filename))
   {
    $filename = substr($filename, 0, -strlen($extension));
    if(!in_array($filename, $exclude))
    {
     $legalnames[] = $filename;
    }
   }
  }
  closedir($dirhandler);
 }

 

 

 /* If a device have been chosen */
 if(isset($_GET['name']))
 {

  /* If the device name is valid */
  if(validname($_GET['name']))
  {

   /* If the script should generate a picture */
   if(isset($_GET['picture']))
   {

    $name = filename($_GET['name']);

    header("content-type: image/png");

    $rrdcommand = "$rrdcommand graph - -v 'Bytes/s' -b 1024 -w 390 DEF:avgin=$name:ds0:AVERAGE AREA:avgin#00CC00:'Traffic in' DEF:avgout=$name:ds1:AVERAGE LINE2:avgout#0000FF:'Traffic out'";

    /* Last day */
    if($_GET['period']=='day')
    {
     $rrdcommand .= ' -t "Traffic the last day" -s -86400';
    }
    /* Last week */
    else if($_GET['period']=='week')
    {
     $rrdcommand .= ' -t "Traffic the last week" -s -604800';
    }
    /* Last month */
    else if($_GET['period']=='month')
    {
     $rrdcommand .= ' -t "Traffic the last month" -s -2678400';
    }
    /* Last year */
    else if($_GET['period']=='year')
    {
     $rrdcommand .= ' -t "Traffic the last year" -s -31622400';
    }
    /* If year and month is supplied, then generate picture for that month */
    else if(is_numeric($_GET['year']) && is_numeric($_GET['month']))
    {
     $name = monthname($_GET['month']) . ' ' . $_GET['year'];
     $rrdcommand .= " -t 'Traffic for $name' " . monthstartend($_GET['year'], $_GET['month']);
     $rrdcommand .= " -x DAY:1:WEEK:1:DAY:1:86400:%d ";
    }

    echo  `$rrdcommand`;

   } //if(isset($_GET['picture']))
   /* If year and month is supplied, then generate page for that month */
   else if(is_numeric($_GET['year']) && is_numeric($_GET['month']))
   {

    echo top($_GET['name']);

    $name = monthname($_GET['month']) . ' ' . $_GET['year'];

    printf("<img src=\"%s?name=%s&amp;year=%s&amp;month=%s&amp;picture=yes\" alt=\"%s\">", $_SERVER['SCRIPT_NAME'], $_GET['name'], $_GET['year'], $_GET['month'], $name);


    $lastdate = 0;
    $days = array();

    /* Get statistics for the selected month */
    if($fp = popen("$rrdcommand fetch " . filename($_GET['name']) . " AVERAGE -r 864000 ".monthstartend($_GET['year'], $_GET['month']), 'r'))
    {
     fgets($fp, 4096);
     while(!feof($fp))
     {
      $line = trim(fgets($fp, 4096));

      if($line != '')
      {

       list($date, $in, $out) = split('( )+', $line);
       list($date) = split(':', $date);
       if($lastdate != 0)
       {

        if(!is_numeric($in))
         $in = 0;
        if(!is_numeric($out))
         $out = 0;

        $in  = $in*($date-$lastdate);
        $out = $out*($date-$lastdate);

        if($_GET['month'] == date('n', $lastdate) && $_GET['year'] == date('Y', $lastdate))
        {
         $day = date('j', $lastdate);
         $days[$day]['in']  += $in;
         $days[$day]['out'] += $out;
        }

       } //if($lastdate != 0)

       $lastdate = $date;

      } //if($line != '')
     } //while(!feof($fp))

     showmonth($_GET['year'], $_GET['month'], $days);

     pclose($fp);

    } //if($fp = popen($test, 'r'))


    echo bottom();

   }
   /* Else generate main device page */
   else
   {

    echo top($_GET['name']);

    /* Find out when the database was last updated */
    if($fp = popen("$rrdcommand info " . filename($_GET['name']), 'r'))
    {
     $key = '';
     while(!feof($fp))
     {
      list($key, $value) = split(' = ', trim(fgets($fp, 4096)));
      if($key == 'last_update')
      {
       printf("Last updated: %s<br>\n", date("Y-m-d H:i:s", $value));
       break;
      }
     }
     pclose($fp);
    }

    printf("<img src=\"%s?name=%s&amp;period=day&amp;picture=yes\" alt=\"Dayly\">", $_SERVER['SCRIPT_NAME'], $_GET['name']);
    printf("<img src=\"%s?name=%s&amp;period=week&amp;picture=yes\" alt=\"Weekly\">", $_SERVER['SCRIPT_NAME'], $_GET['name']);
    printf("<img src=\"%s?name=%s&amp;period=month&amp;picture=yes\" alt=\"Monthly\">", $_SERVER['SCRIPT_NAME'], $_GET['name']);
    printf("<img src=\"%s?name=%s&amp;period=year&amp;picture=yes\" alt=\"Yearly\">\n", $_SERVER['SCRIPT_NAME'], $_GET['name']);


    $lastdate = 0;
    $months = array();

    /* Get statistics for the last two year */
    if($fp = popen("$rrdcommand fetch " . filename($_GET['name']) . " AVERAGE -s -63331200 -e +31622400", 'r'))
    {
     fgets($fp, 4096);
     while(!feof($fp))
     {
      $line = trim(fgets($fp, 4096));
      if($line != '')
      {

       list($date, $in, $out) = split('( )+', $line);
       list($date) = split(':', $date);
       if($lastdate != 0)
       {

        if(!is_numeric($in))
         $in = 0;
        if(!is_numeric($out))
         $out = 0;

        $in  = $in*($date-$lastdate);
        $out = $out*($date-$lastdate);

        $year = date('Y', $lastdate);
        $month = date('n', $lastdate);
        $months[$year][$month]['in']  += $in;
        $months[$year][$month]['out'] += $out;

       } //if($lastdate != 0)

       $lastdate = $date;

      } //if($line != '')
     } //while(!feof($fp))

     $year = date('Y');
     showyear($year, $months[$year]);

     $year = date('Y')-1;
     showyear($year, $months[$year]);

     pclose($fp);

    } //if($fp = popen($test, 'r'))


    echo bottom();

   } //else


  } //if(validname($_GET['name']))
  /* If device name has been provided, but it is not valid */
  else
  {
   printf("Don't do that");
  } //else


 } //if(isset($_GET['name']))
 /* If device name has been given, show the main page */
 else
 {

  echo top('All devices');

  foreach($legalnames as $name)
  {
   printf("<a href=\"%s?name=%s\">%s</a><br>\n", $_SERVER['SCRIPT_NAME'], $name, $name);
  }

  echo bottom();

 } //else


?>
----------------------------------------------------------------------------------------------------------

 

# vi index.php    아래처럼 변경후 저장.
----------------------------------------------------
/* MRTG+RRDTool 그래프가 그려질 디렉토리 */

$dir = '/usr/local/server/mrtg/www';


/* RRDTool 바이너리 파일 지정 */

$rrdcommand = '/usr/local/server/rrdtool/bin/rrdtool';

-----------------------------------------------------

 

아파치 vhost 설정에서 홈디렉토리 설정을 변경후 저장.

# vi /usr/local/server/apache/conf/httpd.conf

<Directory "/usr/local/server/mrtg/www/">
php_admin_value safe_mode 0
AllowOverride FileInfo
</Directory>
 

 

# /usr/local/server/apache/bin/apachectl restart


아파치 리스타트후  웹페이지 확인.

 



 

 

 

 

 

 

 

 

 

 

 

 

 

 

참조 : http://blog.naver.com/nforce7050/140119501677

 

 

 

 

 

 

 

 

댓글