1、安装依赖模块
1、yum install -y epel-release.noarch
2、yum -y groupinstall Development tools
3、yum install -y wget net-snmp net-snmp-utils net-snmp-libs net-snmp-devel golang p7zip*
2、测试snmp连通性
[root@node-2 ~]# snmpwalk -v 3 -u 5gmec -l authPriv -a md5 -A 5G-MEC-net -x aes -X 5G-MEC-net 1.41.X.X sysname
SNMPv2-MIB::sysName.0 = STRING: xxx-xxx-xxx
[root@node-2 ~]#
经验证,snmp可正常获取设备信息
3、通过git 下载snmp_exporter
切换至根目录下,git下载完成后可以看到根目录下多了一个 snmp_exporter的目录
[root@node-2 ~]# cd /opt
[root@node-2 /opt]# git clone https://github.com/prometheus/snmp_exporter.git
Cloning into 'snmp_exporter'...
remote: Enumerating objects: 7038, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7038 (delta 1), reused 1 (delta 0), pack-reused 7032
Receiving objects: 100% (7038/7038), 8.90 MiB | 2.03 MiB/s, done.
Resolving deltas: 100% (3877/3877), done.
[root@node-2 /opt]# ls -al | grep snmp
drwxr-xr-x 11 root root 4096 May 17 11:18 snmp_exporter
[root@node-2 /opt]#
4、设置go国内代理
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
5、编译生成snmp_exporter
[root@node-2 snmp_exporter]# go build
go: downloading github.com/prometheus/exporter-toolkit v0.7.1
go: downloading github.com/prometheus/client_model v0.2.0
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/cespare/xxhash/v2 v2.1.2
go: downloading github.com/golang/protobuf v1.5.2
go: downloading github.com/prometheus/procfs v0.7.3
go: downloading google.golang.org/protobuf v1.26.0
go: downloading github.com/pkg/errors v0.9.1
go: downloading golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
go: downloading golang.org/x/sys v0.0.0-20220114195835-da31bd327af9
go: downloading github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f
go: downloading golang.org/x/net v0.0.0-20220225172249-27dd8689420f
go: downloading golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b
go: downloading github.com/jpillora/backoff v1.0.0
go: downloading golang.org/x/text v0.3.7
[root@node-2 snmp_exporter]# ls -al | grep snmp_exporter
-rwxr-xr-x 1 root root 15312819 May 18 13:02 snmp_exporter
[root@node-2 snmp_exporter]#
6、snmp_exporter目录结构与snmp_generator编译
[root@node-1 snmp_exporter]# tree
.
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── collector
│ ├── collector.go
│ └── collector_test.go
├── config
│ └── config.go
├── config_test.go
├── CONTRIBUTING.md
├── Dockerfile
├── examples
│ └── systemd
│ └── snmp_exporter.service
├── generator
│ ├── config.go
│ ├── Dockerfile
│ ├── FORMAT.md
│ ├── generator.yml
│ ├── main.go
│ ├── Makefile
│ ├── net_snmp.go
│ ├── README.md
│ ├── tree.go
│ └── tree_test.go
├── go.mod
├── go.sum
├── LICENSE
├── main.go
├── MAINTAINERS.md
├── Makefile
├── Makefile.common
├── NOTICE
├── README.md
├── SECURITY.md
├── snmp-mixin
│ ├── alerts
│ │ ├── snmp_general.yml
│ │ └── snmp_ubiquiti_wifi.yml
│ ├── dashboards
│ │ ├── snmp_ubiquiti_access_point.json
│ │ └── snmp_ubiquiti_wifi.json
│ ├── Makefile
│ ├── mixin.libsonnet
│ ├── README.md
│ └── rules
│ └── rules.yaml
├── snmp.yml
├── testdata
│ ├── snmp-auth.yml
│ └── snmp-with-overrides.yml
└── VERSION
10 directories, 41 files
[root@node-1 snmp_exporter]#
至此,可以看到 /snmp_exporter路径下有一个generator的目录,该目录下在编译前是没有generator文件和mibs文件夹的,编译,生成mibs文件夹后
[root@node-2 /]# cd opt/snmp_exporter/generator/
go build
make mibs
[root@node-2 generator]# ls -al
total 6564
drwxr-xr-x 3 root root 210 May 18 12:40 .
drwxr-xr-x 11 root root 4096 May 18 13:02 ..
-rw-r--r-- 1 root root 1931 May 17 11:15 config.go
-rw-r--r-- 1 root root 255 May 17 11:15 Dockerfile
-rw-r--r-- 1 root root 2492 May 17 11:15 FORMAT.md
-rwxr-xr-x 1 root root 6569264 May 17 11:29 generator
-rw-r--r-- 1 root root 13282 May 18 10:30 generator.yml
-rw-r--r-- 1 root root 4920 May 17 11:15 main.go
-rw-r--r-- 1 root root 10950 May 17 11:15 Makefile
drwxr-xr-x 5 root root 110 May 17 18:08 mibs
-rw-r--r-- 1 root root 6722 May 17 11:15 net_snmp.go
-rw-r--r-- 1 root root 9789 May 17 11:15 README.md
-rw-r--r-- 1 root root 14326 May 17 11:15 tree.go
-rw-r--r-- 1 root root 58192 May 17 11:15 tree_test.go
[root@node-2 generator]#
可以看到/opt/snmp_exporter/generator目录下多了一个generator文件和mibs文件夹,然后我们可以去H3C官网 link把需要的mib文件全部下载下来,然后放到mibs文件夹里,总共是有439个文件之多。
[root@node-2 mibs]# ls -al | wc -l
439
[root@node-2 mibs]#
7、创建新的generator.yml文件,并删除原文件
[root@node-2 generator]# rm -rf generator.yml
[root@node-2 generator]# vim generator.yml
[root@node-2 generator]#
附上完整的generator.yml,该模板用的是snmp v3协议。
modules:
H3C:
walk:
- 1.3.6.1.2.1.1.1 #sysDescr
- 1.3.6.1.2.1.1.3 #sysUpTime