在Linux服务器运维工作中,网络流量监控是保障系统稳定运行的重要环节。本文将详细介绍在CentOS 7.x系统上搭建CATI网络流量监控系统的完整方案。CATI(Computer Assisted Telephone Interviewing)系统通常用于电话调研和数据采集,其网络流量监控需求具有特殊性——既需要监控常规的网络吞吐量,又需要关注呼叫数据的传输质量。
首先确保CentOS 7.x系统已更新至最新稳定版本:
bash复制yum update -y && yum upgrade -y
reboot
安装基础开发工具链:
bash复制yum groupinstall "Development Tools" -y
yum install epel-release -y
CATI流量监控需要以下核心组件:
推荐安装组合:
bash复制yum install -y nfdump influxdb grafana
注意:如果企业有安全合规要求,建议使用本地yum源安装,避免直接从公网获取软件包。
安装nfdump工具套件:
bash复制yum install -y nfdump nfcapd
创建采集服务:
bash复制cat > /etc/systemd/system/nfcapd.service <<EOF
[Unit]
Description=nfcapd netflow collector
After=network.target
[Service]
ExecStart=/usr/bin/nfcapd -p 9995 -l /var/netflow -P /var/run/nfcapd.pid -t 60 -S 1 -w -D
Restart=always
[Install]
WantedBy=multi-user.target
EOF
关键参数说明:
-p 9995:监听端口-l /var/netflow:数据存储目录-t 60:文件滚动间隔(秒)-S 1:子目录层级启动服务:
bash复制systemctl daemon-reload
systemctl enable --now nfcapd
在交换机或路由器上配置NetFlow导出:
code复制flow-export destination 192.168.1.100 9995
flow-export version 9
flow-export template timeout 60
提示:不同网络设备配置命令可能不同,请参考设备厂商文档。
安装最新稳定版InfluxDB:
bash复制cat <<EOF | tee /etc/yum.repos.d/influxdb.repo
[influxdb]
name = InfluxDB Repository
baseurl = https://repos.influxdata.com/rhel/7/x86_64/stable/
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdb.key
EOF
yum install -y influxdb
systemctl enable --now influxdb
创建专用数据库:
bash复制influx -execute "CREATE DATABASE netflow"
influx -execute "CREATE RETENTION POLICY one_month ON netflow DURATION 30d REPLICATION 1 DEFAULT"
使用nf2influx工具将NetFlow数据导入InfluxDB:
bash复制git clone https://github.com/phaag/nfdump2influxdb.git
cd nfdump2influxdb
pip install -r requirements.txt
配置转换服务:
bash复制cat > /etc/systemd/system/nf2influx.service <<EOF
[Unit]
Description=NFdump to InfluxDB converter
After=network.target nfcapd.service
[Service]
ExecStart=/usr/bin/python3 /opt/nfdump2influxdb/nf2influx.py -l /var/netflow -H localhost -d netflow -i 60
Restart=always
[Install]
WantedBy=multi-user.target
EOF
安装Grafana:
bash复制yum install -y grafana
systemctl enable --now grafana-server
配置数据源:
http://localhost:8086netflow推荐监控指标:
导入预置仪表板:
bash复制wget https://grafana.com/api/dashboards/10795/revisions/1/download -O /tmp/netflow.json
sed -i 's/${DS_INFLUXDB}/InfluxDB/g' /tmp/netflow.json
curl -X POST -H "Content-Type: application/json" -d @/tmp/netflow.json http://admin:admin@localhost:3000/api/dashboards/db
对于高流量环境,建议配置采样:
bash复制# 修改nfcapd服务配置
ExecStart=/usr/bin/nfcapd -p 9995 -l /var/netflow -P /var/run/nfcapd.pid -t 60 -S 1 -s 100 -w -D
-s 100表示1:100采样率,可根据实际负载调整。
调整InfluxDB存储策略:
bash复制influx -execute "ALTER RETENTION POLICY one_month ON netflow DURATION 90d SHARD DURATION 7d DEFAULT"
配置数据压缩:
bash复制vim /etc/influxdb/influxdb.conf
[data]
index-version = "tsi1"
series-id-set-cache-size = 100
现象:Grafana无数据显示
排查步骤:
bash复制systemctl status nfcapd
bash复制tcpdump -i eth0 port 9995 -vv
bash复制ls -lh /var/netflow
高负载场景下的调优:
增加nfcapd内存缓存
bash复制ExecStart=/usr/bin/nfcapd -p 9995 -l /var/netflow -P /var/run/nfcapd.pid -t 60 -S 1 -b 1024000 -w -D
-b参数指定缓存大小(字节)
调整InfluxDB写入参数
bash复制vim /etc/influxdb/influxdb.conf
[http]
max-concurrent-writes = 32
write-timeout = "30s"
Grafana访问限制:
bash复制vim /etc/grafana/grafana.ini
[auth.anonymous]
enabled = false
[auth.basic]
enabled = true
InfluxDB认证配置:
bash复制vim /etc/influxdb/influxdb.conf
[http]
auth-enabled = true
配置SSL加密:
bash复制# 生成自签名证书
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/grafana/server.key \
-out /etc/grafana/server.crt
# 配置Grafana
vim /etc/grafana/grafana.ini
[server]
protocol = https
cert_file = /etc/grafana/server.crt
cert_key = /etc/grafana/server.key
数据清理脚本:
bash复制cat > /usr/local/bin/clean_netflow.sh <<'EOF'
#!/bin/bash
# 保留最近7天数据
find /var/netflow -type f -mtime +7 -delete
# 优化InfluxDB存储
influx -execute "DELETE FROM flows WHERE time < now() - 90d" -database netflow
EOF
chmod +x /usr/local/bin/clean_netflow.sh
配置Grafana自身监控:
bash复制vim /etc/grafana/grafana.ini
[metrics]
enabled = true
添加Prometheus数据源监控系统指标:
bash复制yum install -y prometheus2
systemctl enable --now prometheus
Grafana告警设置:
示例:流量突增告警
sql复制SELECT mean("bytes") FROM "flows" WHERE $timeFilter GROUP BY time(1m)
Grafana API使用示例:
bash复制# 获取仪表板列表
curl -s http://admin:admin@localhost:3000/api/search | jq
InfluxDB数据查询API:
bash复制curl -G 'http://localhost:8086/query?db=netflow' \
--data-urlencode "q=SELECT sum(bytes) FROM flows WHERE time > now() - 1h GROUP BY protocol"
在实际部署过程中,我发现网络设备的NetFlow版本兼容性是需要特别注意的。曾经遇到某品牌交换机只支持NetFlow v5,而我们的采集器默认使用v9,导致数据丢失。解决方案是在交换机上明确指定版本号,并在nfcapd启动时添加-T参数指定兼容模式。