使用cacti为交换机流量绘图

1740阅读 0评论2014-08-08 cxmfyl
分类:系统运维

环境介绍:交换机没有公网IP,只有内网IP。cacti服务器位于另外一个机房。交换机所在的机房有一台服务器A,设有公网IP,使用该服务器查询交换机的snmp数据,cacti服务器通过A获取交换机的snmp数据。
交换机上行做了端口绑定,所以,要获得该交换机的上行带宽数据,需要将绑定的端口的流量相加。


服务器A:
1、从交换机的mib中找到自己需要的oid。
snmpwalk -v 2c -c public 192.168.0.233 1.3.6.1
试试上面的通用oid,如果不可用,需要咨询交换机厂商。
一个不错的查看mib的网站
OID assignments from 1.3.6.1 - Internet
找到我们需要的三个端口的OID,交换机的22,23,24端口做了端口绑定

traffic in:
IF-MIB::ifInOctets.122 = Counter32: 316156901
IF-MIB::ifInOctets.123 = Counter32: 3143937565
IF-MIB::ifInOctets.124 = Counter32: 1974868671

traffic out:
IF-MIB::ifOutOctets.122 = Counter32: 2413048227
IF-MIB::ifOutOctets.123 = Counter32: 3097128860
IF-MIB::ifOutOctets.124 = Counter32: 3697399942

2、脚本,将3个端口的数据求和
/opt/switch_flow.sh
  1. #!/bin/bash
  2. switch_ip=192.168.0.233
  3. sum_Out=0
  4. sum_In=0
  5. for port in 22 23 24
  6. do
  7. out=`snmpwalk -v 2c -c public $switch_ip IF-MIB::ifOutOctets.1$port|awk '{print $4}'`
  8. sum_Out=`expr $out + $sum_Out`
  9. in=`snmpwalk -v 2c -c public $switch_ip IF-MIB::ifInOctets.1$port|awk '{print $4}'`
  10. sum_In=`expr $in + $sum_In`
  11. done

  12. echo -n "$sum_In $sum_Out"

cacti服务器:
以下为在cacti服务器上的操作,参考:
1、创建data input method
我们执行cacti服务器上的脚本,通过服务器A来获取交换机的snmp数据

 Scripts that output more than one value should be formatted like the following:

: : ... :
  1. #!/usr/bin/perl -w
  2. use Net::OpenSSH;

  3. if ( exists $ARGV[0]) {
  4.         $ip=$ARGV[0];
  5.         $user="cacti";
  6.         my $ssh = Net::OpenSSH->new (
  7.                         $ip,
  8.                         user => $user,
  9.         );
  10.         die "Can't ssh to $ip: " . $ssh->error if $ssh->error;

  11.         my $bytes=$ssh->capture("/opt/switch_flow.sh");
  12.         die "run scripts failed: " . $ssh->error if $ssh->error;
  13.         @result=split /\s/,$bytes;
  14.         print "BytesIN:$result[0] BytesOUT:$result[1]";
  15.         print "\n";
  16. }
  1. 登录cacti控制台,创建新的data input method
Scripts/Command
Input String: perl /scripts/switch.pl 
Input Fields: 
Friendly Name: 随便添,自己可以看懂就可以
Output Fields:
add:
Field [Output]:BytesIN     Friendly Name: 随便添,自己可以看懂就可以  勾选:Update RRD File
Field [Output]:BytesOUT     Friendly Name: 随便添,自己可以看懂就可以  勾选:Update RRD File

2、创建数据模板
登录cacti控制台-
其中需要注意的地方:
Data Input Method:选择上面创建的data input method
Associated RRA's:全选
添加多个data soure item,点击右边的new
为防止异常的大数据影响所绘流量图的可观看性,应去掉异常的大数据,定义下面的值:(参考interface - traffic数据模板)
Maximum Value
 Use Per-Data Source Value (Ignore this Value)  100000000

Data Source Type:COUNTER
COUNTER: is for continuous incrementing counters like the ifInOctets counter in a router. The COUNTER data source assumes that the counter never decreases, except when a counter overflows. It is always a whole INTEGER, floating point numbers are invalid. The update function takes the overflow into account. The counter is stored as a per-second rate. When the counter overflows, RRDTool checks if the overflow happened at the 32bit or 64bit border and acts accordingly by adding an appropriate value to the result
更多信息:

3、创建图形模板(参考 Interface - Traffic (bits/sec)模板)
Name:             switch-traffic-bits
Title (--title): switch traffic bits/sec
勾选:Rigid Boundaries Mode (--rigid)

点击save后可以添加:Graph Item和Graph Item Inputs
Graph Item Inputs会根据graph item所选择的data source 来自动添加。


添加第一个graph item,用于将数据以图形的方式显示出来(area:填充区域,line:画线)
Data Source:下拉菜单,选到switch_flow - (BytesIN)
Color:                                随便选
Graph Item Type:           Area (后面有介绍)
Consolidation Function:    AVERAGE
CDEF Function:   Turn Bytes into Bits
GPRINT Type:   Normal
Text Format:   Inbound traffic
Graph Item Type: AREA, STACK, LINE1, LINE2, and LINE3 用于将数据再图形上显示, COMMENT and GPRINT 用于将数据在图例上显示.
添加该图形对应的三个图例,分别为Current:、 Average:、Maximum:
添加完成后,添加BytesOUT对应的graphy item,完成后,如下图:

其中
的作用是使图例中的内容强制换行。
Forces the legend to the next line after this item.

4、至此,配置完成,可以为设备绘图了。

上一篇:Cacti监控
下一篇:查看一个文件在被哪个程序调用