返回
自定义监控网络层指标,掌握网络动态,防患于未然
开发工具
2023-12-07 12:40:08
监控服务器的网络状况,是运维工作中不可或缺的一环。通过监控网络层指标,可以掌握网络的运行状况,及早发现潜在问题,避免故障发生。
本文将介绍如何使用自定义监控,监控网络层指标。您将学习到:
- 如何使用 Shell 命令,将网络层指标数据上报至自定义监控
- 如何使用 SDK,将网络层指标数据上报至自定义监控
- 如何在自定义监控上,查看监控指标并配置告警
使用 Shell 命令,上报网络层指标数据
1. 安装 Python 2
在开始之前,您需要在云服务器上安装 Python 2。
sudo yum install python2
2. 安装自定义监控 Agent
接下来,您需要安装自定义监控 Agent。
curl -sSO https://cloud.tencent.com/monitor/agent/soft_repo/linux_monitor_repo/monitor_repo.rpm
sudo yum install -y monitor_repo.rpm
sudo yum update monitor-agent
sudo service monagent start
3. 创建自定义监控命名空间
在您开始上报数据之前,您需要创建一个自定义监控命名空间。
moncli namespace create --name <NAMESPACE_NAME>
4. 编写 Shell 脚本,上报网络层指标数据
现在,您可以编写 Shell 脚本,将网络层指标数据上报至自定义监控了。
#!/bin/bash
# 获取网络接口信息
interfaces=$(ifconfig | grep -oP '^\S+')
# 循环每个网络接口
for interface in $interfaces; do
# 获取网络接口的收包数和发包数
rx_packets=$(cat /sys/class/net/${interface}/statistics/rx_packets)
tx_packets=$(cat /sys/class/net/${interface}/statistics/tx_packets)
# 获取网络接口的字节数
rx_bytes=$(cat /sys/class/net/${interface}/statistics/rx_bytes)
tx_bytes=$(cat /sys/class/net/${interface}/statistics/tx_bytes)
# 获取网络接口的丢包数
rx_dropped=$(cat /sys/class/net/${interface}/statistics/rx_dropped)
tx_dropped=$(cat /sys/class/net/${interface}/statistics/tx_dropped)
# 获取网络接口的错误数
rx_errors=$(cat /sys/class/net/${interface}/statistics/rx_errors)
tx_errors=$(cat /sys/class/net/${interface}/statistics/tx_errors)
# 计算网络接口的吞吐量
rx_throughput=$(echo "scale=2; ${rx_bytes} * 8 / 1024 / 1024" | bc)
tx_throughput=$(echo "scale=2; ${tx_bytes} * 8 / 1024 / 1024" | bc)
# 组装上报数据
data=$(cat - | jq -c --arg name ${interface} --arg rx_packets ${rx_packets} --arg tx_packets ${tx_packets} --arg rx_bytes ${rx_bytes} --arg tx_bytes ${tx_bytes} --arg rx_dropped ${rx_dropped} --arg tx_dropped ${tx_dropped} --arg rx_errors ${rx_errors} --arg tx_errors ${tx_errors} --arg rx_throughput ${rx_throughput} --arg tx_throughput ${tx_throughput} --arg namespace ${NAMESPACE_NAME} '
{
"MetricName": "NetworkInterface",
"Namespace": "$namespace",
"Instance": "$name",
"Dimensions": [
{
"Name": "Interface",
"Value": "$name"
}
],
"Metrics": [
{
"Name": "RxPackets",
"Value": "$rx_packets",
"Unit": "Packets"
},
{
"Name": "TxPackets",
"Value": "$tx_packets",
"Unit": "Packets"
},
{
"Name": "RxBytes",
"Value": "$rx_bytes",
"Unit": "Bytes"
},
{
"Name": "TxBytes",
"Value": "$tx_bytes",
"Unit": "Bytes"
},
{
"Name": "RxDropped",
"Value": "$rx_dropped",
"Unit": "Packets"
},
{
"Name": "TxDropped",
"Value": "$tx_dropped",
"Unit": "Packets"
},
{
"Name": "RxErrors",
"Value": "$rx_errors",
"Unit": "Errors"
},
{
"Name": "TxErrors",
"Value": "$tx_errors",
"Unit": "Errors"
},
{
"Name": "RxThroughput",
"Value": "$rx_throughput",
"Unit": "Mbps"
},
{
"Name": "TxThroughput",
"Value": "$tx_throughput",
"Unit": "Mbps"
}
]
}
')
# 上报数据
echo "$data" | moncli datapoint create --stdin
done
5. 运行 Shell 脚本
现在,您可以运行 Shell 脚本,将网络层指标数据上报至自定义监控了。
./network_interface_metrics.sh
使用 SDK,上报网络层指标数据
1. 安装 Python SDK
在开始之前,您需要在云服务器上安装 Python SDK。
pip install tencentcloud-monitor
2. 创建 Python 脚本,上报网络层指标数据
现在,您可以编写 Python 脚本,将网络层指标数据上报至自定义监控了。
from tencentcloud.monitor.v20180729 import MonitorClient
client = MonitorClient()
# 获取网络接口信息
interfaces = client.get_network_interface_list()
# 循环每个网络接口
for interface in interfaces:
# 获取网络接口的收包数和发包数
rx_packets = client.get_network_interface_metric(
NetworkInterfaceId=interface.NetworkInterfaceId,
MetricName="RxPackets"
)
tx_packets = client.get_network_interface_metric(
NetworkInterfaceId=interface.NetworkInterfaceId,
MetricName="TxPackets"
)
# 获取网络接口的字节数
rx_bytes = client.get_network_interface_metric(
NetworkInterfaceId=interface.NetworkInterfaceId,
MetricName="RxBytes"
)
tx_bytes = client.get_network_interface_metric(
NetworkInterfaceId=interface.NetworkInterfaceId,
MetricName="TxBytes"
)
# 获取网络接口的丢包数
rx_dropped = client.get_network_interface_metric(
NetworkInterfaceId=interface.NetworkInterfaceId,
MetricName="RxD