默認已安裝Prometheus服務,服務地址:192.168.56.200html
snmp服務IP(交換機IP):172.20.2.83node
snmp community:dfetestlinux
下載snmp_exporter安裝包,下載地址:https://github.com/prometheus/snmp_exporter/releases git
下載完成後,上傳至機器的 /usr/local 目錄下github
解壓安裝包vim
# tar -zvxf snmp_exporter-0.15.0.linux-arm64.tar.gz # mv snmp_exporter-0.15.0.linux-arm64/ snmp_exporter
snmp_exporter的配置文件須要本身經過SNMP Exporter Config Generator 項目編譯生成,瀏覽器
參考方法: https://github.com/prometheus/snmp_exporter/tree/master/generator#file-format ide
因爲Prometheus使用go語言開發的,因此本身編譯生成snmp_exporter的配置文件須要go環境,Linux中安裝go環境可參考:http://www.javashuo.com/article/p-hbhrudbi-kb.html 中安裝go環境章節ui
go環境安裝之後,構建snmp exporter config Generator,執行如下操做:this
# 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
# cd /usr/local/snmp_exporter # ./snmp_exporter
開啓服務端口在9116上,瀏覽器訪問 http://192.168.56.200:9116 , 在 Target 輸入框中填入交換機IP地址,點擊 submit 按鈕,出現數據,在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配置文件
# 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已經加入進來。
添加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監控交換機,配置完成。