prometheus监控系统之snmp-exporter部署来监控交换机端口流量
Posted sfccl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了prometheus监控系统之snmp-exporter部署来监控交换机端口流量相关的知识,希望对你有一定的参考价值。
一、背景:实验使用prometheus系统来监控交换机端口流量
二、环境:
1、主机1:192.168.0.208,centos7.6,已安装prometheus服务,拟安装snmp-exporter。
2、主机2:GNS3网络仿真系统,已配置交换机提供snmp信息服务,通过getif软件成功读取了交换机的snmp信息。
三、步骤
1、安装go语言环境
[root@ELK prometheus]# yum install go #这种方式比较简单,也可参考https://golang.org/doc/install,通过下载go语言安装包进行安装。
2、编译生成generator,并由generator和generator.yml制作snmp.yml
(1)安装配套软件包
[root@ELK prometheus]# yum install gcc gcc-g++ make net-snmp net-snmp-utils net-snmp-libs net-snmp-devel
(2)获取snmp-exporter源码包,包括generator源码包。
[root@ELK prometheus]# go get github.com/prometheus/snmp_exporter/generator
[root@ELK prometheus]# echo ${GOPATH-$HOME}
/root
安装go语言环境时,变量${GOPATH-$HOME}被配置为/root目录,因此,获取的snmp-exporter和gererator源码包在/root目录下。
[root@ELK prometheus]# cd ${GOPATH-$HOME/go}/src/github.com/prometheus/snmp_exporter/generator
[root@ELK generator]# ls
config.go FORMAT.md main.go net_snmp.go tree.go
Dockerfile generator.yml Makefile README.md tree_test.go
[root@ELK generator]# pwd
/root/go/src/github.com/prometheus/snmp_exporter/generator
(3)编译generator
[root@ELK generator]# go build
go: golang.org/x/sys@v0.0.0-20200212091648-12a6c2dcc1e4: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
上述错误是网站无法访问导致,需设置代理进行解决
启用 Go Modules功能: 参考https://www.cnblogs.com/sage-blog/p/10640947.html
[root@ELK generator]# export GO111MODULE=on
配置GOPROXY环境变量:
[root@ELK generator]# export GOPROXY=https://goproxy.io
再进行编译:
[root@ELK generator]# go build
go: downloading gopkg.in/alecthomas/kingpin.v2 v2.2.6
go: downloading github.com/prometheus/common v0.9.1
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/go-kit/kit v0.9.0
go: downloading github.com/soniah/gosnmp v1.23.1-0.20200214014533-6d3944030084
go: extracting gopkg.in/yaml.v2 v2.2.8
go: extracting gopkg.in/alecthomas/kingpin.v2 v2.2.6
go: downloading github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
go: downloading github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
go: extracting github.com/go-kit/kit v0.9.0
go: extracting github.com/prometheus/common v0.9.1
go: extracting github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
go: extracting github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
go: downloading github.com/go-logfmt/logfmt v0.5.0
go: downloading github.com/pkg/errors v0.9.1
go: extracting github.com/pkg/errors v0.9.1
go: extracting github.com/go-logfmt/logfmt v0.5.0
go: extracting github.com/soniah/gosnmp v1.23.1-0.20200214014533-6d3944030084
go: finding github.com/go-kit/kit v0.9.0
go: finding github.com/prometheus/common v0.9.1
go: finding gopkg.in/alecthomas/kingpin.v2 v2.2.6
go: finding gopkg.in/yaml.v2 v2.2.8
go: finding github.com/soniah/gosnmp v1.23.1-0.20200214014533-6d3944030084
go: finding github.com/pkg/errors v0.9.1
go: finding github.com/go-logfmt/logfmt v0.5.0
go: finding github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
go: finding github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
编译完成,生成generator执行程序。
(4)生成mibs
[root@ELK generator]# make mibs
mkdir: 已创建目录 "mibs"
>> Downloading apc-powernet-mib
>> Downloading ARISTA-ENTITY-SENSOR-MIB
>> Downloading ARISTA-SMI-MIB
>> Downloading ARISTA-SW-IP-FORWARDING-MIB
>> Downloading cisco_v2
make: *** [mibs/.cisco_v2] 错误 56 #这里有错,可暂时忽略
(5)制作generator.yml配置文件
新建generator.yml文件 (#原generator.yml改名备份为generator.yml.bak):
[root@ELK generator]# vi generator.yml
内容如下:
modules:
# Default IF-MIB interfaces table with ifIndex.
if_mib:
walk:
- 1.3.6.1.2.1.2 # OID of "interfaces"
version: 2
auth:
community: ccl123
lookups:
- source_indexes: [ifIndex]
lookup: ifAlias
- source_indexes: [ifIndex]
lookup: ifDescr
- source_indexes: [ifIndex]
lookup: ifName
overrides:
ifAlias:
ignore: true # Lookup metric
ifDescr:
ignore: true # Lookup metric
ifName:
ignore: true # Lookup metric
ifType:
type: EnumAsInfo
(6)运行generator以产生snmp.yml
[root@ELK generator]# export MIBDIRS=mibs
[root@ELK generator]# ./generator generate
level=info ts=2020-06-01T13:28:40.885Z caller=net_snmp.go:142 msg="Loading MIBs" from=$HOME/.snmp/mibs:/usr/share/snmp/mibs
level=info ts=2020-06-01T13:28:40.937Z caller=main.go:52 msg="Generating config for module" module=if_mib
level=info ts=2020-06-01T13:28:40.941Z caller=main.go:67 msg="Generated metrics" module=if_mib metrics=40
level=info ts=2020-06-01T13:28:40.958Z caller=main.go:92 msg="Config written" file=/root/go/src/github.com/prometheus/snmp_exporter/generator/snmp.yml
3、编译生成snmp_exporter服务运行程序:
[root@ELK snmp_exporter]# pwd
/root/go/src/github.com/prometheus/snmp_exporter
[root@ELK snmp_exporter]# go build
go: downloading github.com/prometheus/client_golang v1.4.1
go: extracting github.com/prometheus/client_golang v1.4.1
go: downloading github.com/golang/protobuf v1.3.3
go: downloading github.com/cespare/xxhash/v2 v2.1.1
go: downloading github.com/prometheus/client_model v0.2.0
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
go: downloading github.com/prometheus/procfs v0.0.8
go: extracting github.com/beorn7/perks v1.0.1
go: extracting github.com/prometheus/client_model v0.2.0
go: extracting github.com/cespare/xxhash/v2 v2.1.1
go: extracting github.com/matttproud/golang_protobuf_extensions v1.0.1
go: extracting github.com/prometheus/procfs v0.0.8
go: extracting github.com/golang/protobuf v1.3.3
go: finding github.com/prometheus/client_golang v1.4.1
go: finding github.com/beorn7/perks v1.0.1
go: finding github.com/cespare/xxhash/v2 v2.1.1
go: finding github.com/prometheus/client_model v0.2.0
go: finding github.com/golang/protobuf v1.3.3
go: finding github.com/matttproud/golang_protobuf_extensions v1.0.1
go: finding github.com/prometheus/procfs v0.0.8
编译完成,生成snmp_exporter服务执行程序
4、把generator目录下新生成的snmp.yml拷贝到snmp_exporter服务程序所在目录
[root@ELK snmp_exporter]# pwd
/root/go/src/github.com/prometheus/snmp_exporter
[root@ELK snmp_exporter]# cp generator/snmp.yml .
5、运行snmp_exporter服务
[root@ELK snmp_exporter]# ./snmp_exporter
level=info ts=2020-06-01T13:46:32.453Z caller=main.go:149 msg="Starting snmp_exporter" version="(version=, branch=, revision=)"
level=info ts=2020-06-01T13:46:32.453Z caller=main.go:150 build_context="(go=go1.13.6, user=, date=)"
level=info ts=2020-06-01T13:46:32.460Z caller=main.go:243 msg="Listening on address" address=:9116
6、测试
[root@ELK generator]# snmpwalk -v 2c 192.168.18.5 -c ccl123 ifDescr #测试是否能从交换机获取数据
IF-MIB::ifDescr.1 = STRING: FastEthernet0/0
IF-MIB::ifDescr.2 = STRING: FastEthernet1/0
IF-MIB::ifDescr.3 = STRING: FastEthernet1/1
IF-MIB::ifDescr.4 = STRING: GigabitEthernet2/0
IF-MIB::ifDescr.5 = STRING: Null0
[root@ELK generator]# curl http://192.168.0.208:9116/snmp?target=192.168.18.5
# HELP ifAdminStatus The desired state of the interface - 1.3.6.1.2.1.2.2.1.7
# TYPE ifAdminStatus gauge
ifAdminStatus{ifAlias="",ifDescr="FastEthernet0/0",ifIndex="1",ifName="Fa0/0"} 1
ifAdminStatus{ifAlias="",ifDescr="FastEthernet1/0",ifIndex="2",ifName="Fa1/0"} 1
ifAdminStatus{ifAlias="",ifDescr="FastEthernet1/1",ifIndex="3",ifName="Fa1/1"} 1
ifAdminStatus{ifAlias="",ifDescr="GigabitEthernet2/0",ifIndex="4",ifName="Gi2/0"} 1
ifAdminStatus{ifAlias="",ifDescr="Null0",ifIndex="5",ifName="Nu0"} 1
...
也可web页访问:http://192.168.0.208:9116
填入交换机地址后,点击Submit按钮,显示有键值对信息,就表明获取交换机信息成功。
7、prometheus服务添加对snmp_exporter的监控
(1)修改prometheus.yml
[root@ELK ~]# cd prometheus
[root@ELK prometheus]# vi prometheus.yml
...
- job_name: ‘snmp‘
scrape_interval: 10s
static_configs:
- targets:
- 192.168.18.5 #SNMP device.
metrics_path: /snmp
params:
module: [if_mib]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.0.208:9116 #snmp_exporter
(2)修改完成后重启服务:docker restart prometheus
(3)web登录prometheus查看targets:http://192.168.0.208:9090/targets
或者终端中输入:curl http://192.168.0.208:9090/targets
显示有如下信息即表明配置成功。
...
<td class="endpoint">
<a href="http://192.168.0.208:9116/snmp?module=if_mib&target=192.168.18.5">http://192.168.0.208:9116/snmp</a><br>
<span class="badge badge-primary">module="if_mib"</span>
<span class="badge badge-primary">target="192.168.18.5"</span>
</td>
8、使用方法:
(1)查看各接口1分钟内进口流量变化,单位为Mbps(ifInOctets单位为Bytes)。
(rate(ifInOctets[1m])*8)/(1024*1024)
(2)查看接口FastEthernet0/0在20秒内进口流量的变化。
(rate(ifInOctets{ifDescr="FastEthernet0/0"}[20s])*8)/(1024*1024)
以上是关于prometheus监控系统之snmp-exporter部署来监控交换机端口流量的主要内容,如果未能解决你的问题,请参考以下文章