Prometheus Operator 网络设备监控:SNMP Exporter

  • A+
所属分类:Kubernetes 容器技术


关于SNMP协议原理版本差异、MIB(管理信息库)、OID(对象标识符)请自行百度。

SNMP Exporter

https://github.com/prometheus/snmp_exporter

snmp-exporter是prometheus官方开源的一款网络设备监控工具,从SNMP收集的信息,供Prometheus监控系统使用。它有两个部分。一个执行实际抓取的snmp-exporter和一个generator(它依赖于NetSNMP)创建供导出器使用的配置。

安装go

官网:https://golang.org/dl/

随便一台机器部署go,通过genterator生成snmp-exporter的配置文件。

wget https://dl.google.com/go/go1.15.2.linux-amd64.tar.gz
tar zxvf go1.15.2.linux-amd64.tar.gz
mv go /usr/local

配置环境变量

vim /etc/profile
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
source /etc/profile
go version

安装genterator

https://github.com/prometheus/snmp_exporter/tree/master/generator#file-format

通过该生成器生成snmp.yml

sudo yum -y install git
sudo yum install gcc gcc-g++ make net-snmp net-snmp-utils net-snmp-libs net-snmp-devel # RHEL-based distros
go get github.com/prometheus/snmp_exporter/generator
cd ${GOPATH-$HOME/go}/src/github.com/prometheus/snmp_exporter/generator
go build
make mibs

查看常用的MIB组和OID

https://blog.51cto.com/yzmlinux/1874138

http://www.ttlsa.com/monitor/snmp-oid/

生成snmp_exporter 配置文件

cat << EOF >generator.yml 
modules:
  if_mib:
    walk:
     - sysUpTime
     - 1.3.6.1.2.1.2   ###等同于 interfaces
     - 1.3.6.1.2.1.31.1.1.1.6.40  # Instance of "ifHCInOctets" with index "40"
    version: 2
    auth:
      community: Prometheus
EOF
./generator  generate
cat snmp.yml

使用轻量级的snmp_exporter镜像

docker pull ricardbejarano/snmp_exporter

创建configmap

snmp_exporter配置导入到configmap中

kubectl -n monitoring create configmap snmp-exporter  --from-file=./snmp.yml
kubectl -n monitoring get cm snmp-exporter  -o yaml

创建deployment

[root@master01 snmp_exporter]# vim snmp-exporter.yaml 
apiVersion: v1
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    name: snmp-exporter
  name: snmp-exporter
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      name: snmp-exporter
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        name: snmp-exporter
    spec:
      nodeSelector:
        kubernetes.io/hostname: master01
      containers:
      - image: ricardbejarano/snmp_exporter
        imagePullPolicy: IfNotPresent
        name: snmp-exporter
        ports:
        - containerPort: 9116
        volumeMounts:
        - name: config
          mountPath: /snmp.yml
          subPath: snmp.yml
      volumes:
      - name: config
        configMap:
          name: snmp-exporter
---
apiVersion: v1
kind: Service
metadata:

  labels:
    name: snmp-exporter
  name: snmp-exporter
  namespace: monitoring
spec:

  selector:
    name: snmp-exporter
  ports:
  - name: snmp-metrics
    port: 9116
    targetPort: 9116
  type: NodePort

检查pod

kubectl  -n monitoring get pod,svc|grep snmp-exporter

浏览器访问查看snmp-exporter服务配置,页面探测snmp设备连通性

http://192.168.8.11:31603/config

集成prometheus operator servicemonitor

scrape_configs:
  - job_name: 'snmp'
    static_configs:
      - targets:
        - 192.168.1.2  # 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: 127.0.0.1:9116  # The SNMP exporter's real hostname:port.




YaLei

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: