snmp笔记

参考: 我是参考这个安装的
https://blog.csdn.net/sinat_21302587/article/details/75127133
https://www.cnblogs.com/alanjl/p/11685064.html

在Ubuntu 14.04上配置SNMPv3
https://www.linuxidc.com/Linux/2014-10/108511.htm

点击查看代码

k8s下载configmap

Wget http://k8s-yaml.qytang.com/snmp-exporter/snmp.yml
kubectl create configmap snmp-config --from-file=snmp.yml -n monitoring

应用资源配置清单

kubectl apply -f http://k8s-yaml.qytang.com/snmp-exporter/dp.yaml
kubectl apply -f http://k8s-yaml.qytang.com/snmp-exporter/svc.yaml
kubectl apply -f http://k8s-yaml.qytang.com/snmp-exporter/ingress.yaml

配置DNS解析(dnsca)

cat > /var/named/qytang.com.zone<<'EOF'

$ORIGIN gytang.com.
$TTL 600    ;   10 minutes
@       IN SOA   dnsca.qytang.com. dnsadmin.qytang.com. (
                                    2020090906  ; serial
                                    10800       ; refresh (3 hours)
                                    900         ; retry   (15 minutes)
                                    604800      ; expire  (1 week)
                                    86400       ; minimum (1 day)
                                    )
         NS   dnsca.qytang.com.
$TTL 60     ;  1 minute
dnsca                 A    10.1.1.219
harbor                A    10.1.1.220
k8s-yaml              A   10.1.1.219

prometheus.yml的snmp_exporter配置

- job_name: snmp
  scrape_interval: 10s
  static_configs:
    - targets:
      - 10.1.1.215   #交换机IP地址
      - 10.1.1.216   #交换机工P地址
  metrics_path: /snmp
  params:
    module: [cisco_mib]
  relabel_configs:
  - source_labels: [__address_]
    target_label: __param_target
  - source_labels: [__param_target]
    target_label: instance
  - target_label:  __address__
    replacement: snmp-exporter.monitoring:9116   # snmp_exporter服务IP地址

安装依赖模块

yum install -y epel-release
yum group install -y "Development Tools"
yum install -y wget net-snmp net-snmp-utils net-snmp-libs net-snmp-devel golang p7zip*

测试SNMP

snmpwalk -v2c -c tcpipro 10.1.1.216 .1

git snmp_exporter

cd /
git clone https://github.com/prometheus/snmp_exporter.git

go国内代理

export G0111MODULE=on
export GOPROXY=https://goproxy.cn,direct

build generator

go get github.com/prometheus/snmp_exporter/generator
cd snmp_exporter/generator/
go build
make mibs

generator generate生成generater配置操作 generator.yml

modules:
  cisco_mib:
    walk:
      - sysUpTime
      - interfaces
      - ifXTable
      - sysDescr
      - sysName
      - cpmCPUTotalTable
    version:
    auth:
      community: tcpipro
    lookups:
      - source_indexes: [ifIndex]
        lookup: 1.3.6.1.2.1.31.1.1.1.18  #ifAlias
      - source_indexes: [ifIndex]
        # Use OID to avoid conflict with PaloAlto PAN-COMMON-MIB.
        lookup: 1.3.6.1.2.1.2.2.1.2 # ifDescr
      - source_indexes: [ifIndex]
        # Use OID to avoid conflict with Netscaler NS-ROOT-MIB.
        lookup: 1.3.6.1.2.1.31.1.1.1.1  # ifName

    overrides:
      ifAlias:
        ignore: true # Lookup metric
      ifDescr:
        ignore: true # Lookup metric
      ifName:
        ignore: true # Lookup metric
      ifType:
        type: EnumAsInfo

产生snmp.yml

export MIBDIRS=/snmp_exporter/generator/mibs:/snmp_exporter/generator/mibs/cisco_v2
./generator generate

export MIBDIRS

export MIBDIRS=/root/go/pkg/mod/github.com/prometheus/snmp_exporter@v0.20.0/generator/mibs:/root/go/pkg/mod/github.com/prometheus/snmp_exporter@v0.20.0/generator/mibs/cisco_v2:

自定义启动snmp

nohup ./snmp_exporter --config.file=snmp.yml --web.listen-address=:9116 > default.log 2>&1 &

generator.yml 提供模块列表。最简单的模块只是一个名称和一组要遍历的 OID。

modules:
  module_name:  # The module name. You can have as many modules as you want.
    walk:       # List of OIDs to walk. Can also be SNMP object names or specific instances.
      - 1.3.6.1.2.1.2              # Same as "interfaces"
      - sysUpTime                  # Same as "1.3.6.1.2.1.1.3"
      - 1.3.6.1.2.1.31.1.1.1.6.40  # Instance of "ifHCInOctets" with index "40"

    version: 2  # SNMP version to use. Defaults to 2.
                # 1 will use GETNEXT, 2 and 3 use GETBULK.
    max_repetitions: 25  # How many objects to request with GET/GETBULK, defaults to 25.
                         # May need to be reduced for buggy devices.
    retries: 3   # How many times to retry a failed request, defaults to 3.
    timeout: 10s # Timeout for each walk, defaults to 10s.

    auth:
      # Community string is used with SNMP v1 and v2. Defaults to "public".
      community: public

      # v3 has different and more complex settings.
      # Which are required depends on the security_level.
      # The equivalent options on NetSNMP commands like snmpbulkwalk
      # and snmpget are also listed. See snmpcmd(1).
      username: user  # Required, no default. -u option to NetSNMP.
      security_level: noAuthNoPriv  # Defaults to noAuthNoPriv. -l option to NetSNMP.
                                    # Can be noAuthNoPriv, authNoPriv or authPriv.
      password: pass  # Has no default. Also known as authKey, -A option to NetSNMP.
                      # Required if security_level is authNoPriv or authPriv.
      auth_protocol: MD5  # MD5 or SHA, defaults to MD5. -a option to NetSNMP.
                          # Used if security_level is authNoPriv or authPriv.
      priv_protocol: DES  # DES or AES, defaults to DES. -x option to NetSNMP.
                          # Used if security_level is authPriv.
      priv_password: otherPass # Has no default. Also known as privKey, -X option to NetSNMP.
                               # Required if security_level is authPriv.
      context_name: context # Has no default. -n option to NetSNMP.
                            # Required if context is configured on the device.

    lookups:  # Optional list of lookups to perform.
              # The default for `keep_source_indexes` is false. Indexes must be unique for this option to be used.

      # If the index of a table is bsnDot11EssIndex, usually that'd be the label
      # on the resulting metrics from that table. Instead, use the index to
      # lookup the bsnDot11EssSsid table entry and create a bsnDot11EssSsid label
      # with that value.
      - source_indexes: [bsnDot11EssIndex]
        lookup: bsnDot11EssSsid
        drop_source_indexes: false  # If true, delete source index labels for this lookup.
                                    # This avoids label clutter when the new index is unique.

     overrides: # Allows for per-module overrides of bits of MIBs
       metricName:
         ignore: true # Drops the metric from the output.
         regex_extracts:
           Temp: # A new metric will be created appending this to the metricName to become metricNameTemp.
             - regex: '(.*)' # Regex to extract a value from the returned SNMP walks's value.
               value: '$1' # The result will be parsed as a float64, defaults to $1.
           Status:
             - regex: '.*Example'
               value: '1'
             - regex: '.*'
               value: '0'
         type: DisplayString # Override the metric type, possible types are:
                             #   gauge:   An integer with type gauge.
                             #   counter: An integer with type counter.
                             #   OctetString: A bit string, rendered as 0xff34.
                             #   DateAndTime: An RFC 2579 DateAndTime byte sequence. If the device has no time zone data, UTC is used.
                             #   DisplayString: An ASCII or UTF-8 string.
                             #   PhysAddress48: A 48 bit MAC address, rendered as 00:01:02:03:04:ff.
                             #   Float: A 32 bit floating-point value with type gauge.
                             #   Double: A 64 bit floating-point value with type gauge.
                             #   InetAddressIPv4: An IPv4 address, rendered as 1.2.3.4.
                             #   InetAddressIPv6: An IPv6 address, rendered as 0102:0304:0506:0708:090A:0B0C:0D0E:0F10.
                             #   InetAddress: An InetAddress per RFC 4001. Must be preceded by an InetAddressType.
                             #   InetAddressMissingSize: An InetAddress that violates section 4.1 of RFC 4001 by
                             #       not having the size in the index. Must be preceded by an InetAddressType.
                             #   EnumAsInfo: An enum for which a single timeseries is created. Good for constant values.
                             #   EnumAsStateSet: An enum with a time series per state. Good for variable low-cardinality enums.

参考:
https://blog.csdn.net/weixin_39172380/article/details/106812717
net-snmp服务参考
https://blog.csdn.net/zqtsx/article/details/25150821

posted @ 2022-02-28 23:09  ty1539  阅读(242)  评论(0编辑  收藏  举报