监控网卡流量的shell脚本

简介: 监控网卡流量的shell脚本

本脚本来自有学习阿铭的博文学习:
公司网络偶尔出现网卡问题,将陷入业务瘫痪,造成不可挽回的损失。所以需要时常监控。
提示:本文中的S全部都$符,不要问为什么,马云爸爸的社区就这样。

#!/bin/bash
#用途:监控网卡流量的shell脚本。
#作者:Caron maktini
#日期:2018年11月23日
#版本:v0.1
#要求如下:每分钟检测一次网卡的流量;当网卡流量为0时,重启网卡。

mail_user=admin@admin.com 
dir=/tmp/netlog

[ -d  Sdir ] II mkdir  $dir 
s_m=`lsattr -d  Sdir l awk ' {print $1} ' I  sed  's/[^a]//g'` 
if  [  $s_m  != "a" ]
then 
    chattr +a  $dir 
fi

if  ! rpm -q sysstat &> /dev/null 
then 
    yum install -y sysstat 
fi 

sar -n DEV 10 I grep 'eth0 ' > /tmp/eth0_sar. Iog 
net_in=`grep ' ^Average: ' /tmp/eth0_sar.log l awk '{ print $5}'` 
net_out=`grep ' ^Average: ' /tmp/eth0_sar.log l awk '{ print $6}'` 


if [ ! -f $dir/net.log ] 
then 
    echo"net_in $ net_in">> S dir/net. Iog 
    echo"net_out $ net_out">> S dir/net. Iog 
    exit 0
fi 

net_in_last=`tail-2  Sdir/net.Iog l grep 'net_in'` 
net_out_last=`tail-2  Sdir/net.Iog l grep 'net_out'` 
net_in_diff=`S [Snet_in-$net_in_last]`
net_out_diff=`S [S net_out-Snet_out_last]' 

if [  Snet_in_diff  -gt  $net_in_last ]
then 
      python mail.py $mail_user "网卡入口流量増幅异常" "增幅Snet_in_dff"       
fi 

if [ Snet_out_diff -gt Snet_out_last ] 
then 

      python mail.py $mail_user "网卡出口流量增幅异常" "增幅Snet_out_dff"      
fi 

echo  "net_in Snet_in" >> $ dir/net.Iog 
echo   "net_out  Snet_out">> $ dir/net.Iog 


相关文章
|
3天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
27 3
|
6天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
26 0
|
7天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
8天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
26 1
|
12天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
143 0
|
12天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
136 0
|
监控 Shell
监控网卡实时流量shell脚本
监控网卡实时流量shell脚本
142 0
|
23天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
25天前
|
Java Shell
SpringBoot启动脚本Shell
SpringBoot启动脚本Shell
17 0
|
16天前
|
弹性计算 Shell Linux
ecs服务器shell常用脚本练习(六)
【4月更文挑战第4天】shell代码训练(六)
109 0