
ทำไมต้อง Monitor Network ด้วย Prometheus?
การ Monitor อุปกรณ์ Network เช่น Router, Switch ของ Cisco และ MikroTik เป็นสิ่งจำเป็นสำหรับ IT Team ทุกองค์กร Prometheus + SNMP Exporter เป็นวิธีที่ Modern, Open-Source และ Scalable ที่สุดในการเก็บ Metrics จากอุปกรณ์เหล่านี้
| เครื่องมือ | ข้อดี | ข้อเสีย |
|---|---|---|
| Prometheus + SNMP | ฟรี, Scalable, Alert ดี, Grafana Dashboard | ต้อง Setup เอง |
| Zabbix | ฟรี, GUI ครบ, SNMP Built-in | Resource หนัก, ซับซ้อน |
| PRTG | ใช้ง่าย, Auto-Discovery | License แพง (100 Sensor ฟรี) |
| LibreNMS | ฟรี, SNMP เก่ง, Auto-Discovery | UI ไม่ Modern, ไม่มี Alerting ดี |
Architecture Overview
# Architecture:
#
# Cisco/MikroTik → SNMP → SNMP Exporter → Prometheus → Grafana
# → Alertmanager → Email/LINE
#
# Components:
# 1. SNMP Agent: ทำงานบน Cisco/MikroTik (เปิด SNMP)
# 2. SNMP Exporter: แปลง SNMP Data เป็น Prometheus Metrics
# 3. Prometheus: เก็บ Time-Series Data + Alert Rules
# 4. Grafana: Dashboard แสดงผล
# 5. Alertmanager: จัดการ Alert (Email, LINE, Slack)
#
# Flow:
# 1. Prometheus เรียก SNMP Exporter ทุก 60 วินาที
# 2. SNMP Exporter ส่ง SNMP Query ไปยังอุปกรณ์
# 3. อุปกรณ์ตอบ SNMP Response
# 4. SNMP Exporter แปลงเป็น Prometheus Format
# 5. Prometheus เก็บข้อมูล + ตรวจ Alert Rules
# 6. Grafana อ่านข้อมูลจาก Prometheus แสดง Dashboard
ติดตั้ง SNMP Exporter
# ติดตั้ง SNMP Exporter บน Ubuntu:
# วิธี 1: Docker (แนะนำ):
docker run -d --name snmp-exporter \
-p 9116:9116 \
-v ./snmp.yml:/etc/snmp_exporter/snmp.yml:ro \
prom/snmp-exporter:latest
# วิธี 2: Binary:
wget https://github.com/prometheus/snmp_exporter/releases/download/v0.26.0/snmp_exporter-0.26.0.linux-amd64.tar.gz
tar xzf snmp_exporter-0.26.0.linux-amd64.tar.gz
cd snmp_exporter-0.26.0.linux-amd64
sudo cp snmp_exporter /usr/local/bin/
sudo cp snmp.yml /etc/snmp_exporter/
# สร้าง Systemd Service:
sudo cat > /etc/systemd/system/snmp-exporter.service << 'EOF'
[Unit]
Description=SNMP Exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/bin/snmp_exporter --config.file=/etc/snmp_exporter/snmp.yml
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now snmp-exporter
# ทดสอบ:
curl "http://localhost:9116/snmp?target=192.168.1.1&module=if_mib"
# → ได้ Metrics ของอุปกรณ์
เปิด SNMP บน Cisco
# Cisco IOS — เปิด SNMP v2c:
enable
configure terminal
snmp-server community public2026 RO
snmp-server community private2026 RW
snmp-server location "Bangkok Office"
snmp-server contact "IT Team"
# จำกัด Access (แนะนำ):
access-list 99 permit 192.168.1.100 # IP ของ SNMP Exporter
snmp-server community public2026 RO 99
# SNMP v3 (ปลอดภัยกว่า):
snmp-server group SNMPv3Group v3 priv
snmp-server user SNMPv3User SNMPv3Group v3 \
auth sha AuthPass2026! priv aes 128 PrivPass2026!
# ตรวจสอบ:
show snmp
show snmp community
show snmp user
# Cisco NX-OS (Nexus):
feature snmp
snmp-server community public2026 group network-operator
เปิด SNMP บน MikroTik
# MikroTik RouterOS — เปิด SNMP:
# CLI:
/snmp set enabled=yes
/snmp community set 0 name=public2026 addresses=192.168.1.100/32
/snmp set trap-community=public2026 trap-version=2
# หรือ ผ่าน Winbox:
# IP → SNMP → Enable
# Community: public2026
# Addresses: 192.168.1.100/32
# SNMP v3:
/snmp community
add name=v3user security=private \
authentication-protocol=SHA1 authentication-password=AuthPass2026! \
encryption-protocol=AES encryption-password=PrivPass2026!
# ทดสอบจาก Linux:
snmpwalk -v2c -c public2026 192.168.1.1 sysDescr
# → MikroTik RouterOS ...
snmpwalk -v2c -c public2026 192.168.1.1 ifDescr
# → ether1, ether2, wlan1, ...
Prometheus Configuration
# /etc/prometheus/prometheus.yml:
global:
scrape_interval: 60s
evaluation_interval: 60s
rule_files:
- "rules/network-alerts.yml"
alerting:
alertmanagers:
- static_configs:
- targets: ['localhost:9093']
scrape_configs:
# SNMP Exporter สำหรับ Cisco Switches:
- job_name: 'snmp-cisco'
scrape_interval: 60s
scrape_timeout: 30s
static_configs:
- targets:
- 192.168.1.1 # Core Switch
- 192.168.1.2 # Distribution Switch
- 192.168.1.3 # Access Switch
labels:
vendor: cisco
metrics_path: /snmp
params:
module: [if_mib]
community: [public2026]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: localhost:9116
# SNMP Exporter สำหรับ MikroTik:
- job_name: 'snmp-mikrotik'
scrape_interval: 60s
static_configs:
- targets:
- 192.168.1.10 # Main Router
- 192.168.1.11 # Branch Router
labels:
vendor: mikrotik
metrics_path: /snmp
params:
module: [if_mib]
community: [public2026]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: localhost:9116
SNMP Generator — สร้าง Custom snmp.yml
# Default snmp.yml ครอบคลุม MIB พื้นฐาน
# แต่ถ้าต้องการ Monitor Metrics เฉพาะ Vendor:
# 1. ติดตั้ง SNMP Exporter Generator:
git clone https://github.com/prometheus/snmp_exporter.git
cd snmp_exporter/generator
# 2. สร้าง generator.yml:
modules:
cisco_all:
walk:
- sysDescr
- sysUpTime
- ifDescr
- ifOperStatus
- ifInOctets
- ifOutOctets
- ifInErrors
- ifOutErrors
- ifSpeed
- cpmCPUTotal5minRev # Cisco CPU
- ciscoMemoryPoolUsed # Cisco Memory
- ciscoEnvMonTemperatureValue # Temperature
auth:
community: public2026
mikrotik_all:
walk:
- sysDescr
- sysUpTime
- ifDescr
- ifOperStatus
- ifInOctets
- ifOutOctets
- hrProcessorLoad # CPU Load
- hrStorageUsed # Storage/Memory
- mtxrWlApFreq # WiFi Frequency
- mtxrWlApClientCount # WiFi Clients
- mtxrWlApBand # WiFi Band
auth:
community: public2026
# 3. Generate snmp.yml:
make generate
# → สร้าง snmp.yml ที่ครอบคลุม Cisco/MikroTik Metrics
Alert Rules สำหรับ Network
# /etc/prometheus/rules/network-alerts.yml:
groups:
- name: network-alerts
rules:
# Interface Down:
- alert: InterfaceDown
expr: ifOperStatus{ifDescr!~".*Null.*|.*Loopback.*"} != 1
for: 2m
labels:
severity: critical
annotations:
summary: "Interface {{ $labels.ifDescr }} is DOWN on {{ $labels.instance }}"
# High Bandwidth Usage:
- alert: HighBandwidthUsage
expr: |
(rate(ifInOctets[5m]) * 8 / ifSpeed) * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "Interface {{ $labels.ifDescr }} bandwidth > 80% on {{ $labels.instance }}"
# Device Unreachable:
- alert: DeviceUnreachable
expr: up{job=~"snmp-.*"} == 0
for: 3m
labels:
severity: critical
annotations:
summary: "Device {{ $labels.instance }} is unreachable"
# High Interface Errors:
- alert: HighInterfaceErrors
expr: rate(ifInErrors[5m]) > 10
for: 5m
labels:
severity: warning
annotations:
summary: "High errors on {{ $labels.ifDescr }} ({{ $labels.instance }})"
# High CPU (Cisco):
- alert: HighCPU
expr: cpmCPUTotal5minRev > 80
for: 5m
labels:
severity: warning
annotations:
summary: "CPU > 80% on {{ $labels.instance }}"
Grafana Dashboard สำหรับ Network
# Grafana Dashboard Panels:
#
# 1. Network Overview:
# - Total Devices (Up/Down)
# - Total Interfaces (Up/Down)
# - Alerts Active
#
# 2. Bandwidth Per Interface:
# - Inbound: rate(ifInOctets[5m]) * 8
# - Outbound: rate(ifOutOctets[5m]) * 8
# - Unit: bits/sec
#
# 3. Interface Status Table:
# - Device | Interface | Status | Speed | In | Out | Errors
#
# 4. Top Talkers:
# - Top 10 Interfaces by Bandwidth Usage
#
# 5. Error Rate:
# - rate(ifInErrors[5m]) + rate(ifOutErrors[5m])
#
# Grafana Dashboard IDs (Import):
# - 11169: SNMP Network Overview
# - 1860: Node Exporter Full (สำหรับ Server)
# - 14857: MikroTik Dashboard
#
# Import Dashboard:
# Grafana → Dashboards → Import → ใส่ Dashboard ID
Alertmanager — ส่ง Alert ไป LINE/Email
# /etc/alertmanager/alertmanager.yml:
global:
smtp_smarthost: 'smtp.gmail.com:587'
smtp_from: '[email protected]'
smtp_auth_username: '[email protected]'
smtp_auth_password: 'AppPassword2026'
smtp_require_tls: true
route:
group_by: ['alertname', 'instance']
group_wait: 30s
group_interval: 5m
repeat_interval: 4h
receiver: 'team-email'
routes:
- match:
severity: critical
receiver: 'team-all'
- match:
severity: warning
receiver: 'team-email'
receivers:
- name: 'team-email'
email_configs:
- to: '[email protected]'
- name: 'team-all'
email_configs:
- to: '[email protected]'
webhook_configs:
- url: 'http://localhost:8088/line-notify'
# ใช้ LINE Notify Webhook Proxy
# LINE Notify Integration (ผ่าน Webhook Proxy):
# → ใช้ Prometheus-to-LINE Bridge
# → หรือเขียน Python Script รับ Webhook แล้วส่ง LINE
Troubleshooting SNMP
# ปัญหาที่พบบ่อย:
#
# 1. SNMP Exporter ไม่ได้ข้อมูล:
# → ตรวจ Community String ตรงกันไหม
# → ตรวจ Firewall: UDP 161 เปิดไหม
# → ทดสอบ: snmpwalk -v2c -c public2026 TARGET sysDescr
#
# 2. Metrics ไม่ขึ้นใน Prometheus:
# → ตรวจ prometheus.yml: relabel_configs ถูกไหม
# → ทดสอบ: curl "http://snmp-exporter:9116/snmp?target=TARGET&module=if_mib"
# → ดู Prometheus Targets: http://prometheus:9090/targets
#
# 3. Grafana ไม่แสดงข้อมูล:
# → ตรวจ Data Source: Prometheus URL ถูกไหม
# → ตรวจ Query: ลองใน Prometheus ก่อน
# → ตรวจ Time Range: เลือก Last 1h
#
# 4. SNMP Timeout:
# → เพิ่ม scrape_timeout ใน prometheus.yml
# → ตรวจ Network Latency
# → ลด Walk OIDs (เลือกเฉพาะที่ต้องการ)
#
# 5. MikroTik SNMP ช้า:
# → MikroTik SNMP Response ช้ากว่า Cisco
# → เพิ่ม timeout เป็น 30s
# → ลด OIDs ที่ Walk
สรุป: Prometheus SNMP Exporter สำหรับ Network Monitoring
Prometheus + SNMP Exporter + Grafana เป็น Stack ที่ทรงพลังสำหรับ Monitor อุปกรณ์ Network ทั้ง Cisco และ MikroTik ให้ Visibility ครบถ้วน ตั้งแต่ Bandwidth, Interface Status, CPU, Memory ไปจนถึง Alert แบบ Real-time ทั้งหมดนี้เป็น Open-Source ไม่มีค่า License ทำให้เป็นทางเลือกที่ดีที่สุดสำหรับ IT Team ที่ต้องการ Network Monitoring ระดับ Enterprise