**前置文章:**[Centos7.6安装Prometheus(普罗米修斯)监控](https://www.xiaoleizhang.com/index.php/archives/10/ "Centos7.6安装Prometheus(普罗米修斯)监控") **前置文章:**[Prometheus(普罗米修斯)安装Grafana可视化图形工具](https://www.xiaoleizhang.com/index.php/archives/23/ "Prometheus(普罗米修斯)安装Grafana可视化图形工具") **环境配置** |服务器|IP地址| | :------------: | :------------: | |Prometheus服务器|172.100.48.199| |网络设备|172.100.100.100| |grafana服务器|172.100.48.199| |snmp_exporter服务器|172.100.48.199| ------------ ------------ # 一、准备 ## 1、环境安装 在snmp_exporter服务器上安装go环境 ```shell yum install go -y ``` 安装依赖模块 ```shell yum install -y epel-release yum install git -y yum group install -y "Development Tools" yum install -y wget net-snmp net-snmp-utils net-snmp-libs net-snmp-devel golang p7zip* ``` # 二、配置snmp_exporter ## 1、测试SNMP (test是团体名,.1表示查看所有信息) ```shell snmpwalk -v 2c -c test 172.100.100.100 .1 ``` 看到类似如下画面,或者有正常的信息输出就表示没有问题 ![snmp测试.png](https://www.xiaoleizhang.com/usr/uploads/2022/10/3734528070.png) ## 2、git snmp_exporter ```shell cd / git clone https://github.com/prometheus/snmp_exporter.git ``` ## 3、go国内代理 ```shell export GO111MODULE=on export GOPROXY=https://goproxy.cn,direct ``` ## 4、build generator ```shell go get github.com/prometheus/snmp_exporter/generator cd snmp_exporter/generator/ go build ``` ## 5、编译generator.yml生成snmp.yml snmp_exporter的配置文件snmp.yml需要自己通过SNMP Exporter Config Generator 项目编译生成 我这里没有使用下载的generator.yml文件,我是按照自己的需要写的,需要注意的两个点: (1)walk下面写的是你需要查询的信息对应的oid,这个像华为交换机都是可以在原厂文档上查的到的; (2)还有一点就是community这个要写自己设置的团体名的,如果不知道可以自己手动设置一个,我这里设置的是test。 其他的地方基本不需要修改,huawei_mib这个也可以改,后面使用snmp查的时候、写prometheus配置文件的时候对应也写自己刚刚修改的mib名称就好。 将原先的generator配置保存 ```shell mv generator.yml generator.bak.yml ``` 新建generator.yml ```shell vim generator.yml ``` ```shell modules: test1_mib: walk: - 1.3.6.1.2.1.2.2.1.2 - 1.3.6.1.2.1.2.2.1.3 - 1.3.6.1.2.1.2.2.1.10 - 1.3.6.1.2.1.2.2.1.16 version: auth: community: test lookups: - source_indexes: [ifIndex] lookup: ifAlias - source_indexes: [ifIndex] lookup: 1.3.6.1.2.1.2.2.1.2 - source_indexes: [ifIndex] lookup: 1.3.6.1.2.1.31.1.1.1.1 overrides: ifAlias: ignore: true ifDescr: ignore: true ifName: ignore: true ifType: type: EnumAsInfo test2_mib: walk: - 1.3.6.1.2.1.2.2.1.2 - 1.3.6.1.2.1.2.2.1.3 - 1.3.6.1.2.1.2.2.1.10 - 1.3.6.1.2.1.2.2.1.16 version: auth: community: H3C lookups: - source_indexes: [ifIndex] lookup: ifAlias - source_indexes: [ifIndex] lookup: 1.3.6.1.2.1.2.2.1.2 - source_indexes: [ifIndex] lookup: 1.3.6.1.2.1.31.1.1.1.1 overrides: ifAlias: ignore: true ifDescr: ignore: true ifName: ignore: true ifType: type: EnumAsInfo ``` 这里我根据我自己的需求创建了两个mib,以下是网络接口常用的oid值 |网络接口(1.3.6.1.2.1.2)| | :------------: | :------------: | |OID|描述|备注|请求方式| |.1.3.6.1.2.1.2.1.0|网络接口的数目|IfNumber|GET| |.1.3.6.1.2.1.2.2.1.2|网络接口信息描述|IfDescr|WALK| |.1.3.6.1.2.1.2.2.1.3|网络接口类型|IfType|WALK| |.1.3.6.1.2.1.2.2.1.4|接口发送和接收的最大IP数据报[BYTE]|IfMTU|WALK| |.1.3.6.1.2.1.2.2.1.5|接口当前带宽[bps]|IfSpeed|WALK| |.1.3.6.1.2.1.2.2.1.6|接口的物理地址|IfPhysAddress|WALK| |.1.3.6.1.2.1.2.2.1.8|接口当前操作状态[up/down]|IfOperStatus|WALK| |.1.3.6.1.2.1.2.2.1.10|接口收到的字节数|IfInOctet|WALK| |.1.3.6.1.2.1.2.2.1.16|接口发送的字节数|IfOutOctet|WALK| |.1.3.6.1.2.1.2.2.1.11|接口收到的数据包个数|IfInUcastPkts|WALK| |.1.3.6.1.2.1.2.2.1.17|接口发送的数据包个数|IfOutUcastPkts|WALK| 生成snmp.yml文件 ```shell ./generator generate ``` ![snmp.yml生成.png](https://www.xiaoleizhang.com/usr/uploads/2022/10/3587859355.png) 这样你就会在/snmp_exporter/generator目录下得到一个snmp.yml文件 ## 6、启动snmp_exporter服务 下载snmp_exporter并做成service ```shell cd ~ wget https://github.com/prometheus/snmp_exporter/releases/download/v0.19.0/snmp_exporter-0.19.0.linux-amd64.tar.gz tar -xzvf snmp_exporter-0.19.0.linux-amd64.tar.gz mv /root/snmp_exporter-0.19.0.linux-amd64 /usr/local/snmp_exporter ``` 接着将刚才生成的snmp.yml拷贝到snmp_exporter目录下替换原来的snmp.yml文件 ```shell mv /snmp_exporter/generator/snmp.yml /usr/local/snmp_exporter/snmp.yml ``` 做成service ```shell vim /etc/systemd/system/snmp_exporter.service ``` 内容如下 ```shell [Unit] Description=SNMP Exporter After=network-online.target [Service] User=root Restart=on-failure ExecStart=/usr/local/snmp_exporter/snmp_exporter --config.file=/usr/local/snmp_exporter/snmp.yml [Install] WantedBy=multi-user.target ``` 启动service,并设置成开机自启 ```shell systemctl daemon-reload systemctl enable snmp_exporter.service systemctl start snmp_exporter.service systemctl status snmp_exporter.service ``` 查看监听端口9116是否正常,可以看到监听正常 ```shell [root@cpe-172-100-48-199 snmp_exporter]# netstat -tuplan | grep 9116 tcp6 0 0 :::9116 :::* LISTEN 26676/snmp_exporter [root@cpe-172-100-48-199 snmp_exporter]# ``` ## 7、测试snmp_exporter是否能够采集到数据(成功采集) Target填写交换机ip地址 Module是写的自己设置的mib名称,交换机的团体名和mib要一样 ![snmp数据测试.png](https://www.xiaoleizhang.com/usr/uploads/2022/10/1850919691.png) 后面就会得到类似如下图的采集结果 ![snmp数据测试2.png](https://www.xiaoleizhang.com/usr/uploads/2022/10/85278037.png) # 三、修改prometheus.yml配置文件 在prometheus.yml文件中添加job监控交换机,根据自己的prometheus.yml文件的位置来添加 ```shell vim /usr/local/prometheus/prometheus.yml ``` 添加如下内容,注意yml的格式要对齐(-job_name那行) ```shell - job_name: 'snmp1' scrape_interval: 10s static_configs: - targets: - 172.100.100.100 #交换机ip metrics_path: /snmp params: module: [test1_mib] #修改成2.4步自己设置的mib名称 relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 127.0.0.1:9116 # 安装snmp_exporter主机的ip和端口号 - job_name: 'snmp2' scrape_interval: 10s static_configs: - targets: - 172.100.100.101 #交换机ip metrics_path: /snmp params: module: [test2_mib] #修改成2.4步自己设置的mib名称 relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 127.0.0.1:9116 # 安装snmp_exporter主机的ip和端口号 ``` 重启Prometheus服务 ```shell systemctl restart prometheus.service ``` 浏览器访问http://172.100.48.199:9090/targets?search= 查看snmp监控信息 ![snmp_exporter_prometheus.png](https://www.xiaoleizhang.com/usr/uploads/2022/10/2828135173.png) # 四、配置Grafana可视化 添加数据源 ![添加数据源.png](https://www.xiaoleizhang.com/usr/uploads/2022/10/3368800162.png) 添加监控图标 irate和rate都会用于计算某个指标在一定时间间隔内的变化速率。但是它们的计算方法有所不同:irate取的是在指定时间范围内的最近两个数据点来算速率,而rate会取指定时间范围内所有数据点,算出一组速率,然后取平均值作为结果 所以官网文档说:irate适合快速变化的计数器(counter),而rate适合缓慢变化的计数器(counter) 根据以上算法我们也可以理解,对于快速变化的计数器,如果使用rate,因为使用了平均值,很容易把峰值削平。除非我们把时间间隔设置得足够小,就能够减弱这种效应 以下是grafana的表达式rate的意思在上面已经写了,乘8是因为字节的单位换算,ifInOctets是你需要的的oid值 ```shell irate(ifInOctets{instance="填写自己交换机的ip", ifName="GigabitEthernet0/0/0"}[30s])*8 ``` 实际结果如图所示 ![snmp_exporter图表展示.png](https://www.xiaoleizhang.com/usr/uploads/2022/10/1732290575.png) 再根据自己的实际需求填写交换机和端口就可以,增加图表也是这样 这样监控就做好了!!! 最后修改:2022 年 10 月 24 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏