MRTG 流量統計安裝教學

 本篇環境以centos6.6版本進行測試

Install snmpd

yum -y install net-snmp net-snmp-devel

Install iftop / sysstat / mrtg

yum -y install iftop sysstat mrtg


Setup MRTG

Setup SNMP

mv /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.bak

vi /etc/snmp/snmpd.conf

# First, map the community name (COMMUNITY) into a security name

# (local and mynetwork, depending on where the request is coming

# from):

#       sec.name  source        community

com2sec notConfigUser  default  public    

 

####

# Second, map the security names into group names:

#               sec.model  sec.name

group   notConfigGroup v1            notConfigUser

group   notConfigGroup   v2c        notConfigUser

####

# Third, create a view for us to let the groups have rights to:

#           incl/excl subtree                       mask

view all     included  .1                               80

####

# Finally, grant the 2 groups access to the 1 view with different

# write permissions:

#               context sec.model sec.level match  read   write  notif

access notConfigGroup ""    any     noauth  exact  all  none   none

Setup SYSSTAT

mkdir /opt/mrtg

  i. CPU

vi /opt/mrtg/mrtg.cpu
  

#!/bin/bash

cpuusr=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $3}'`

cpusys=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $5}'`

UPtime=`/usr/bin/uptime | awk '{print $3.$4.$5}'`

echo $cpuusr

echo $cpusys

echo $UPtime

hostname

    

   # chmod 755 /opt/mrtg/mrtg.cpu

  ii. MEM

vi /opt/mrtg/mrtg.mem



#!/bin/bash

echo `/usr/bin/free -m | grep Mem | awk '{print $3}'`

echo `/usr/bin/free -m | grep Swap | awk '{print $3}'`

echo `/usr/bin/uptime | awk '{print $3 " " $4 " " $5}'`

hostname

   # chmod 755 /opt/mrtg/mrtg.mem

c. Setup MRTG

vi /etc/mrtg/mrtg.cfg

HtmlDir: /var/www/mrtg

ImageDir: /var/www/mrtg

LogDir: /var/lib/mrtg

ThreshDir: /var/lib/mrtg

Target[eth0]: 2:public@127.0.0.1

MaxBytes[eth0]: 12500000

Title[eth0]: Traffic Analysis

PageTop[eth0]: <H1>127.0.0.1 eth0</H1>

 

Target[cpu]: `/opt/mrtg/mrtg.cpu`

MaxBytes[cpu]: 100

Options[cpu]: gauge, nopercent

YLegend[cpu]: CPU loading (%)

ShortLegend[cpu]: %

LegendO[cpu]:   CPU us;

LegendI[cpu]:   CPU sy;

Title[cpu]: CPU Loading

PageTop[cpu]:<H1>CPU Loading</H1>

 

Target[mem]: `/opt/mrtg/mrtg.mem`

MaxBytes[mem]: 2048000

kmg[mem]: KB

kilo[mem]: 1024

Options[mem]: gauge,nopercent

YLegend[mem]: Memory Usage:

Legend1[mem]: Total Memory:

Legend2[mem]: Used Memory:

LegendI[mem]: Total Memory:

LegendO[mem]: Used Memory:

Title[mem]: MEMORYWORKLOAD

PageTop[mem]:<H1>Memory</H1>


# mkdir /var/lib/mrtg

# env LANG=C mrtg /etc/mrtg/mrtg.cfg

# env LANG=C mrtg /etc/mrtg/mrtg.cfg

# env LANG=C mrtg /etc/mrtg/mrtg.cfg

# indexmaker /etc/mrtg/mrtg.cfg > /var/www/mrtg/index.html

   #  vi /etc/httpd/conf.d/mrtg.conf



Alias /mrtg /var/www/mrtg


<Location /mrtg>

    Order deny,allow

    allow from all

</Location>


設定排程

透過 crontab -e 加上以下指令


*/5 * * * * env LANG=C mrtg /etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log


Optimizate TCP/IP configuration


# vi /etc/sysctl.conf

 

# 加上以下的設定 (要注意已經有的則調整參數即可)
 

# Use TCP syncookies when needed

net.ipv4.tcp_synack_retries=3

net.ipv4.tcp_syn_retries=3

net.ipv4.tcp_max_syn_backlog=2048
 

# Enable TCP window scaling

net.ipv4.tcp_window_scaling = 1
 

# Increase TCP max buffer size

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216
 

# Increase Linux autotuning TCP buffer limits

net.ipv4.tcp_rmem = 4096 87380 16777216

net.ipv4.tcp_wmem = 4096 65536 16777216
 

# Increase number of ports available

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 300

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range = 5000 65000

最後執行


sysctl -p

即可無須重開機便啟用此設定

如要監看更多設備可以參考另一篇sh檔名記得要修改
MRTG CPU,NIC,MEM,SWAP,DISK usage, DISK I/O.監控

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

使用windows CMD 時間自動校正