Prometheus(三):Prometheus監控交換機(snmp)


默認已安裝Prometheus服務,服務地址:192.168.56.200

一、獲取交換機snmp信息

snmp服務IP(交換機IP):172.20.2.83

snmp community:dfetest

二、部署 snmp_exporter

2.1 下載snmp_exporter

下載snmp_exporter安裝包,下載地址:https://github.com/prometheus/snmp_exporter/releases 

下載完成后,上傳至機器的  /usr/local 目錄下

 解壓安裝包

#   tar -zvxf snmp_exporter-0.15.0.linux-arm64.tar.gz # mv snmp_exporter-0.15.0.linux-arm64/ snmp_exporter

2.2 配置snmp_exporter

snmp_exporter的配置文件需要自己通過SNMP Exporter Config Generator 項目編譯生成,

參考方法: https://github.com/prometheus/snmp_exporter/tree/master/generator#file-format  

由於Prometheus使用go語言開發的,所以自己編譯生成snmp_exporter的配置文件需要go環境,Linux中安裝go環境可參考:https://www.cnblogs.com/guoxiangyue/p/11772717.html  中安裝go環境章節

go環境安裝以后,構建snmp exporter  config Generator,執行以下操作:

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

 編譯完成后,修改當前目錄下的generator.yml 文件,generator.yml文件的配置可以參考 https://github.com/prometheus/snmp_exporter/tree/master/generator#file-format ,generator.yml文件中只需要注入snmp的community即可,下面僅修改了modules.if_mib.auth字段,其余與 File Format 中一致。

#  vim generator.yml

generator.yml 中module.if_mib.auth字段:

modules: # Default IF-MIB interfaces table with ifIndex. if_mib: walk: [sysUpTime, interfaces, ifXTable] version: 1 auth: community: dfetest lookups: - source_indexes: [ifIndex] lookup: ifAlias - source_indexes: [ifIndex] lookup: 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=mibs # ./generator generate

將新生成的snmp.yml 替換掉原snmp_exporter中的snmp.yml

#   cp snmp.yml /usr/local/snmp_exporter

 2.3 驗證並設置snmp_exporter 服務開機啟動

#   cd /usr/local/snmp_exporter # ./snmp_exporter

開啟服務端口在9116上,瀏覽器訪問 http://192.168.56.200:9116 , 在 Target 輸入框中填入交換機IP地址,點擊 submit 按鈕,出現數據,在snmp_exporter 配置成功。

 

 2.4 設置snmp_exporter 以服務方式,開機啟動

Ctrl+C 結束掉 snmp_exporter 進程,創建 snmp_exporter 服務,讓 snmp_exporter 以服務的方式,開機自啟。

添加系統服務

#   vim /etc/systemd/system/snmp_exporter.service

將以下內容寫入文件中

[Unit] Description=node_exporter After=network.target [Service] ExecStart=/usr/local/snmp_exporter/snmp_exporter Restart=on-failure [Install] WantedBy=multi-user.target

啟動服務,設置開機自啟

#  systemctl daemon-reload # systemctl enable snmp_exporter # systemctl start snmp_exporter

三、修改Prometheus 配置

進入Prometheus的安裝文件夾,打開Prometheus配置文件

#  cd /usr/local/prometheus
#  vim prometheus.yml

在scrape_configs標簽下,添加以下內容,配置監控。params.module中可以配置需要抓取的模塊,不配置表示全部抓取。

- job_name: 'snmp' scrape_interval: 10s static_configs: - targets: - 172.20.2.83 # 交換機IP地址 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: 192.168.56.200:9116 # snmp_exporter 服務IP地址

以下是Prometheus.yml 文件全部內容

# my global config global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself.
scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: 'Linux' static_configs: - targets: ['192.168.56.201:9100'] labels: instance: Linux - job_name: 'Windows' static_configs: - targets: ['192.168.56.1:9182'] labels: instance: Windows - job_name: 'snmp' scrape_interval: 10s static_configs: - targets: - 172.20.2.83 # 交換機IP地址 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: 192.168.56.200:9116 # snmp_exporter 服務IP地址

保存退出,重啟Prometheus服務

#  systemctl restart prometheus

瀏覽器訪問 http://192.168.56.200:9090/targets  查看監控信息

可以看到,snmp已經加入進來。

 四、配置Grafana

添加dashboard

Grafana官方為我們提供了很多dashboard頁面,可直接下載使用。瀏覽器訪問 https://grafana.com/grafana/dashboards 下載所需要的dashboard頁面

此處我們使用  snmp 的監控dashboard,dashboard Id為:10523

然后打開我們的Grafana監控頁面,打開dashboard的管理頁面

 點擊【import】按鈕

然后將我們剛才的dashboard Id (10523) 復制進去

Grafana會自動識別dashboard Id 。

然后點擊【change】按鈕,生成一個隨機的UID,然后點擊下方輸入框,選擇我們之前創建的數據源Prometheus,最后點擊【Import】按鈕,即可完成導入。

 導入成功后,會自動打開該Dashboard,即可看到我們剛才設置好的snmp監控

至此Prometheus監控交換機,配置完成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM