栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

python snmp 交换机 配置文件

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python snmp 交换机 配置文件

使用Prometheus监控华为交换机主要是利用snmp_exporter来获取开启了snmp agent的网络设备上的信息。snmp具体的协议和prometheus的搭建不做详细介绍了,主要介绍下node_exporter的使用方法,主要是snmp.yaml文件自定义是个坎儿。

1. 交换机开启snmp
snmp-agent        #开启snmp客户端snmp-agent community read  abc                 #设置读团体名:自己定义一个字符串snmp-agent community write  cde               #设置写团体名:自己定义一个字符串snmp-agent sys-info version all   #配置SNMP版本 (all是所有版本)snmp-agent sys-info contact MR.LI-tel:8002          #设置联系方式snmp-agent sys-info location 3rd-flood                #设置设备位置snmp-agent target-host trap address udp-domain 172.16.149.253 params securityname abc     #允许向172.16.149.253发送Trap报文,使用的团体名为public

2. 下载snmp_exporter

GitHub地址 https://github.com/prometheus/snmp_exporter

我们可以下载编译好的二进制文件,下载地址如下:

https://github.com/prometheus/snmp_exporter/releases/download/v0.19.0/snmp_exporter-0.19.0.linux-amd64.tar.gz

3.配置文件snmp.yml 需要使用 Generator生成器

安装编译环境工具

sudo yum install gcc gcc-g++ make net-snmp net-snmp-utils net-snmp-libs net-snmp-devel gogo get github.com/prometheus/snmp_exporter/generatorexport GOPROXY=https://goproxy.cn #设置下代理,否正会提示golang.org无法连接cd ${GOPATH-$HOME/go}/src/github.com/prometheus/snmp_exporter/generatorgo build   #生成二级制文件generator

或者使用docker (需要解决一些网络问题)

make mibsdocker build -t snmp-generator .docker run -ti   -v "${PWD}:/opt/"   snmp-generator generate

4. 寻找Mibs,修改generator.yml

华为的网络设备mib文件下载地址,这个GitHub上主流的网络设备都有,大家可以收藏下。

https://github.com/librenms/librenms/tree/master/mibs/huawei

建议mib文件保存到 $HOME/.snmp/mibs 或者/usr/local/share/snmp/mibs 下。

对应厂商的OID有些区别,OID寻找有些耗时,可以在官网或者mib文件下寻找,同时我们可以测试下OID是否可用,可以使用测试工具snmpwalk

sudo yum -y install net-snmp-utils  #安装snmpwalk

使用snmpwalk一步步测试oid是否可用,例如

snmpwalk -v 2c 192.168.2.132 -c abc 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7.67108873

修改generator.yml 文件

自定义的华为交换机generator.yml的简单示例

modules:  hw:    version: 2    auth:      community: abc    walk:      - 1.3.6.1.4.1.2011.6.3.4.1.2       # hwCpuDevDuty      - 1.3.6.1.4.1.2011.6.3.4.1.3      # hwAvgDuty1min      - 1.3.6.1.4.1.2011.6.3.4.1.4       # hwAvgDuty5min      - 1.3.6.1.4.1.2011.6.3.5.1.1.2    # hwMemoryDevSize      - 1.3.6.1.4.1.2011.6.3.5.1.1.3    # hwMemoryDevFree      - 1.3.6.1.4.1.2011.6.3.5.1.1.4    # hwMemoryDevRawSliceUsed      - 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7  # hwEntityMemUsage    retries: 3    timeout: 10s

官网上的详细示例说明

modules:  module_name:  # 模块名称。您可以根据需要拥有任意数量的模块。    walk:       # OID列表 walk. 也可以是SNMP对象名称或特定实例      - 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版本。默认值为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   # 重试失败请求的次数, defaults to 3.    timeout: 5s  # 每个SNMP请求的超时, defaults to 5s.    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' # The first entry whose regex matches and whose value parses wins.             - 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.                             #   Bits: An RFC 2578 BITS construct, which produces a StateSet with a time series per bit.

挺华为芯

执行snmp.yaml生成器

generator generate 当前目录下

generator generate --output.path="$OMD_ROOT/etc/prometheus_snmp_exporter/snmp.yml"

新生成的snmp.yaml文件如下:

# WARNING: This file was auto-generated using snmp_exporter generator, manual changes will be lost.hw:  walk:  - 1.3.6.1.4.1.2011.6.3.4.1.2  - 1.3.6.1.4.1.2011.6.3.4.1.3  - 1.3.6.1.4.1.2011.6.3.4.1.4  - 1.3.6.1.4.1.2011.6.3.5.1.1.2  - 1.3.6.1.4.1.2011.6.3.5.1.1.3  - 1.3.6.1.4.1.2011.6.3.5.1.1.4  get:  - 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7.67108873  metrics:  - name: hwEntityMemUsage    oid: 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7    type: gauge    help: The memory usage for the entity - 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7    indexes:    - labelname: entPhysicalIndex      type: gauge  - name: hwCpuDevDuty    oid: 1.3.6.1.4.1.2011.6.3.4.1.2    type: gauge    help: The overall CPU duty percentage in the last 5-second period. - 1.3.6.1.4.1.2011.6.3.4.1.2    indexes:    - labelname: hwFrameIndex      type: gauge    - labelname: hwSlotIndex      type: gauge    - labelname: hwCpuDevIndex      type: gauge  - name: hwAvgDuty1min    oid: 1.3.6.1.4.1.2011.6.3.4.1.3    type: gauge    help: The overall CPU duty percentage in the last 1 minute period. - 1.3.6.1.4.1.2011.6.3.4.1.3    indexes:    - labelname: hwFrameIndex      type: gauge    - labelname: hwSlotIndex      type: gauge    - labelname: hwCpuDevIndex      type: gauge  - name: hwAvgDuty5min    oid: 1.3.6.1.4.1.2011.6.3.4.1.4    type: gauge    help: The overall CPU duty percentage in the last 5-minute period. - 1.3.6.1.4.1.2011.6.3.4.1.4    indexes:    - labelname: hwFrameIndex      type: gauge    - labelname: hwSlotIndex      type: gauge    - labelname: hwCpuDevIndex      type: gauge  - name: hwMemoryDevSize    oid: 1.3.6.1.4.1.2011.6.3.5.1.1.2    type: gauge    help: Indicates the total size of the memory module which is on the managed object.Its      unit is byte. - 1.3.6.1.4.1.2011.6.3.5.1.1.2    indexes:    - labelname: hwFrameIndex      type: gauge    - labelname: hwSlotIndex      type: gauge    - labelname: hwMemoryDevModuleIndex      type: gauge  - name: hwMemoryDevFree    oid: 1.3.6.1.4.1.2011.6.3.5.1.1.3    type: gauge    help: Indicates the free size of the memory, in the unit of byte. - 1.3.6.1.4.1.2011.6.3.5.1.1.3    indexes:    - labelname: hwFrameIndex      type: gauge    - labelname: hwSlotIndex      type: gauge    - labelname: hwMemoryDevModuleIndex      type: gauge  - name: hwMemoryDevRawSliceUsed    oid: 1.3.6.1.4.1.2011.6.3.5.1.1.4    type: gauge    help: Indicates the used size of the raw slice memory, in the unit of byte. -      1.3.6.1.4.1.2011.6.3.5.1.1.4    indexes:    - labelname: hwFrameIndex      type: gauge    - labelname: hwSlotIndex      type: gauge    - labelname: hwMemoryDevModuleIndex      type: gauge  version: 2  retries: 3  timeout: 10s  auth:    community: abc

5.启动node_exporter

./snmp_exporter

浏览器打开检测,node_exporter的默认端口是9116

模块选择snmp下自定义的hw

能够获取到数据,说明已经可以获取到华为交换机上的数据了,目前只采集cpu和内存的信息

# HELP hwAvgDuty1min The overall CPU duty percentage in the last 1 minute period. - 1.3.6.1.4.1.2011.6.3.4.1.3# TYPE hwAvgDuty1min gaugehwAvgDuty1min{hwCpuDevIndex="0",hwFrameIndex="0",hwSlotIndex="0"} 17# HELP hwAvgDuty5min The overall CPU duty percentage in the last 5-minute period. - 1.3.6.1.4.1.2011.6.3.4.1.4# TYPE hwAvgDuty5min gaugehwAvgDuty5min{hwCpuDevIndex="0",hwFrameIndex="0",hwSlotIndex="0"} 17# HELP hwCpuDevDuty The overall CPU duty percentage in the last 5-second period. - 1.3.6.1.4.1.2011.6.3.4.1.2# TYPE hwCpuDevDuty gaugehwCpuDevDuty{hwCpuDevIndex="0",hwFrameIndex="0",hwSlotIndex="0"} 17# HELP hwEntityMemUsage The memory usage for the entity - 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7# TYPE hwEntityMemUsage gaugehwEntityMemUsage{entPhysicalIndex="67108873"} 50# HELP hwMemoryDevFree Indicates the free size of the memory, in the unit of byte. - 1.3.6.1.4.1.2011.6.3.5.1.1.3# TYPE hwMemoryDevFree gaugehwMemoryDevFree{hwFrameIndex="0",hwMemoryDevModuleIndex="0",hwSlotIndex="0"} 7.2067592e+07# HELP hwMemoryDevRawSliceUsed Indicates the used size of the raw slice memory, in the unit of byte. - 1.3.6.1.4.1.2011.6.3.5.1.1.4# TYPE hwMemoryDevRawSliceUsed gaugehwMemoryDevRawSliceUsed{hwFrameIndex="0",hwMemoryDevModuleIndex="0",hwSlotIndex="0"} 3.9048208e+07# HELP hwMemoryDevSize Indicates the total size of the memory module which is on the managed object.Its unit is byte. - 1.3.6.1.4.1.2011.6.3.5.1.1.2# TYPE hwMemoryDevSize gaugehwMemoryDevSize{hwFrameIndex="0",hwMemoryDevModuleIndex="0",hwSlotIndex="0"} 1.46873864e+08# HELP snmp_scrape_duration_seconds Total SNMP time scrape took (walk and processing).# TYPE snmp_scrape_duration_seconds gaugesnmp_scrape_duration_seconds 2.212691979# HELP snmp_scrape_pdus_returned PDUs returned from walk.# TYPE snmp_scrape_pdus_returned gaugesnmp_scrape_pdus_returned 7# HELP snmp_scrape_walk_duration_seconds Time SNMP walk/bulkwalk took.# TYPE snmp_scrape_walk_duration_seconds gaugesnmp_scrape_walk_duration_seconds 2.212509975

6.prometheus参考配置
scrape_configs:  - job_name: 'snmp'    static_configs:      - targets:        - 192.168.2.132  # SNMP device华为交换机(开启snmp agent的)    metrics_path: /snmp    scrape_timeout: 30s  #若snmp响应慢,增大此值,默认是10s    params:      module: [hw]  # snmp.yaml文件自定义模块名字    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.

7.grafana上绘图

华为交换机cpu和内存使用率趋势图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965701.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号