监控流量
#!/bin/bash
TIME=$(date +%s)
# 获取接收流量和发送流量
RX=$(cat /proc/net/dev | grep eth0 | awk '{print $2}')
TX=$(cat /proc/net/dev | grep eth0 | awk '{print $10}')
# 计算并转化流量单位为 Mbps
RX_MBPS=$(echo "scale=2; $RX/$TIME/1048576*8" | bc)
TX_MBPS=$(echo "scale=2; $TX/$TIME/1048576*8" | bc)
# 输出流量信息
echo "RX: $RX_MBPS Mbps"
echo "TX: $TX_MBPS Mbps"
#!/bin/bash
# Set variables
TIME=$(date +"%Y-%m-%d %H:%M:%S")
CPU=$(top -bn1 | grep %Cpu | awk '{print $2}')
MEMORY=$(free | awk '/Mem/{printf "%dMB/%dMB(%.2f%%)n", $3/1024,$2/1024,$3/$2*100.0}')
AVAILABLE_MEMORY=$(free -h | grep Mem: | awk '{print $7}')
CURR_ESTAB=$(netstat -an | grep ESTABLISHED | wc -l)
TCP_TW=$(netstat -an | grep TIME_WAIT | wc -l)
SOCKETS_USED=$(netstat -an | grep -v -e TIME_WAIT -e LISTEN | wc -l)
UDP_INUSE=$(netstat -anu | grep -v -e TIME_WAIT | wc -l)
TCP_ALLOC=$(cat /proc/sys/net/ipv4/tcp_max_orphans)
TCP_INSEGS=$(cat /proc/net/snmp | grep TcpInSegs | awk '{print $2}')
TCP_OUTSEGS=$(cat /proc/net/snmp | grep TcpOutSegs | awk '{print $2}')
TCP_RETRANSSEGS=$(cat /proc/net/snmp | grep TcpRetransSegs | awk '{print $2}')
result=$(sar -n DEV 1 2 | awk 'BEGIN {rx=0; tx=0;} $1 ~ /^[0-9]+(:.+)?$/ {iface=$1; rx=rx+$5; tx=tx+$6} END {printf "RX: %4.2f MbpsnTX: %4.2f Mbpsn", (rx/1048576*8), (tx/1048576*8)}')
# Send message to WeChat robot
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7c39d97e-23b5-4a6f-999d-b594beb14c20'
-H 'Content-Type: application/json'
-d "
{
"msgtype": "text",
"text": {
"content": "neo4j服务器资源使用情况如下:
- 当前时间: $TIME
- CPU使用量: ${CPU}%
- 内存使用量: $MEMORY
- 可用内存为: $AVAILABLE_MEMORY
- ESTABLISHED的TCP连接数: $CURR_ESTAB
- TIME_WAIT的TCP连接数: $TCP_TW
- 当前使用的Socket数: $SOCKETS_USED
- 当前使用的UDP连接数: $UDP_INUSE
- 发送流量: $(echo $result | awk '{print $2}')Mbps
- 接收流量: $(echo $result | awk '{print $5}')Mbps
- TCP最大孤立连接数: $TCP_ALLOC
- 收到的TCP段数量: $TCP_INSEGS
- 发送的TCP段数量: $TCP_OUTSEGS
- 重传的TCP段数量: $TCP_RETRANSSEGS"
}
}"