Linux下针对服务器网卡流量和磁盘的监控脚本

 

1)实时监控网卡流量的通用脚本:

[root@ceph-node1 ~]# cat /root/net_monit.sh 
#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin;
export PATH
function traffic_monitor {

  OS_NAME=$(sed -n '1p' /etc/issue)

  eth=$1

  if [ ! -d /sys/class/net/$eth ];then
      echo -e "Network-Interface Not Found"
      echo -e "You system have network-interface:\n`ls /sys/class/net`"
      exit 5
  fi
  while [ "1" ]
  do
   
    STATUS="fine"
  
    RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
    TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')

    sleep 1
    RXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
    TXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
    clear
 
    RX=$((${RXnext}-${RXpre}))
    TX=$((${TXnext}-${TXpre}))

    if [[ $RX -lt 1024 ]];then
      RX="${RX}B/s"
    elif [[ $RX -gt 1048576 ]];then
      RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
      $STATUS="busy"
    else
      RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
    fi

    if [[ $TX -lt 1024 ]];then
      TX="${TX}B/s"
      elif [[ $TX -gt 1048576 ]];then
      TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
    else
      TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
    fi
  
    echo -e "==================================="
    echo -e "Welcome to Traffic_Monitor stage"
    echo -e "version 1.0"
    echo -e "Since 2018.7.2"
    echo -e "Created by wangshibo"
    echo -e "BLOG: http://www.cnblogs.cn/kevingrace"
    echo -e "==================================="
    echo -e "System: $OS_NAME"
    echo -e "Date:   `date +%F`"
    echo -e "Time:   `date +%k:%M:%S`"
    echo -e "Port:   $1"
    echo -e "Status: $STATUS"
    echo -e  " \t     RX \tTX"
    echo "------------------------------"
   
    echo -e "$eth \t $RX   $TX "
    echo "------------------------------"

    echo -e "Press 'Ctrl+C' to exit"
  done
}

if [[ -n "$1" ]];then
  traffic_monitor $1
else
  echo -e "None parameter,please add system netport after run the script! \nExample: 'sh traffic_monitor eth0'"
fi



[root@ceph-node1 ~]# chmod 755 /root/net_monit.sh
[root@ceph-node1 ~]# sh /root/net_monit.sh eth0               #eth0是网卡设备名称,如果是网卡绑定bond0,后面就跟bond0
===================================
Welcome to Traffic_Monitor stage
version 1.0
Since 2018.7.2
Created by wangshibo
BLOG: http://www.cnblogs.cn/kevingrace
===================================
System: CentOS release 6.9 (Final)
Date:   2018-07-02
Time:   15:19:34
Port:   eth0
Status: fine
        RX  TX
------------------------------
eth0   417B/s   390B/s 
------------------------------
Press 'Ctrl+C' to exit
===================================
Welcome to Traffic_Monitor stage
version 1.0
Since 2018.7.2
Created by wangshibo
BLOG: http://www.cnblogs.cn/kevingrace
===================================
System: CentOS release 6.9 (Final)
Date:   2018-07-02
Time:   15:19:35
Port:   eth0
Status: fine
        RX  TX
------------------------------
eth0   1.49902KB/s   1.3252KB/s 
------------------------------
Press 'Ctrl+C' to exit

2)监控磁盘的监控脚本

[root@ceph-node1 ~]# cat disk_monit.sh 
#!/bin/bash
#filename:Monitor_Disk
Monitor_Disk(){
mkdir -p /mnt/Monitor_Disk
fdisk -l|grep "Disk /dev/" | awk '{print $2,$3$4}'|tr -d ',:'>/mnt/Monitor_Disk/device_list.log
N=1;ECHO 90
while read device_line
do
Device=`echo $device_line|awk '{print $1}'`
Sum=`echo $device_line|awk '{print $2}'`
df -h |grep "$Device"|sort>/mnt/Monitor_Disk/${N}_partitions.log
echo
echo "** 第$N块硬盘($Device):${Sum} **"| grep -E "$Device|$Sum|$N" --color=yes
echo "------------------------------------"
echo -e "linux分区 挂载目录 总大小 已用 剩余 已用百分比 文件系统 ID system  \
块大小  预留空间  ">/mnt/Monitor_Disk/${N}_Over.log
echo -e "========= ======== =====  ===  ===  ==========  ======= == ======  \
======  ======== ">>/mnt/Monitor_Disk/${N}_Over.log
Num_Partition=`cat /mnt/Monitor_Disk/${N}_partitions.log|wc -l`
n=0
while read partition_line
do
    Partition_Name=`echo $partition_line|awk '{print $1}'`
    Mount_Dir=`echo $partition_line|awk '{print $6}'`
    Partition_Sum=`echo $partition_line|awk '{print $2}'`
    Partition_Used=`echo $partition_line|awk '{print $3}'`
    Partition_Leave=`echo $partition_line| awk '{print $4}'`
    Partition_Percent=`echo $partition_line|awk '{print $5}'`
    Partition_Type=`mount|grep $Partition_Name|awk '{print $5$6}'`
    Partition_Id=`fdisk -l | grep $Partition_Name|tr -d '\*'|awk '{print $5}'`
    Partition_System=`fdisk -l | grep $Partition_Name|tr -d '\*'|awk '{print $6}'`
    Part_Block_Size_B=`tune2fs -l $Partition_Name|grep "Block size"|awk '{print $3}'`
    Part_Lift_For_Root_Blocks=`tune2fs -l $Partition_Name|grep "Reserved block count:"|\
    awk '{print $4}'`
    Part_Block_Size=`echo $Part_Block_Size_B/1024|bc`
    Part_Lift_For_Root=`echo ${Part_Lift_For_Root_Blocks}*${Part_Block_Size}/1024|bc`
    echo -e "$Partition_Name $Mount_Dir $Partition_Sum $Partition_Used $Partition_Leave \
$Partition_Percent $Partition_Type  $Partition_Id $Partition_System  \
${Part_Block_Size}K ${Part_Lift_For_Root}M">>/mnt/Monitor_Disk/${N}_Over.log
    let n++

    [ $n -eq $Num_Partition ]&&(cat /mnt/Monitor_Disk/${N}_Over.log|column -t;echo)
done</mnt/Monitor_Disk/${N}_partitions.log
let N++
done</mnt/Monitor_Disk/device_list.log
ECHO 90
rm -fr /mnt/Monitor_Disk
}
ECHO(){
for ((i=1;i<=$1;i++))
do
echo -n "#"
[ $i -eq $1 ]&&(echo;echo)
done
}
Monitor_Disk


[root@ceph-node1 ~]# chmod 755 disk_monit.sh
[root@ceph-node1 ~]# sh disk_monit.sh
##########################################################################################


** 第1块硬盘(/dev/sdb):577.4GB **
------------------------------------
linux分区  挂载目录  总大小  已用  剩余  已用百分比  文件系统  ID  system  块大小  预留空间
=========  ========  =====   ===   ===   ==========  =======   ==  ======  ======  ========
/dev/sdb1  /data     530G    42G   461G  9%          ext4(rw)  83  Linux   4K      27532M


** 第2块硬盘(/dev/sda):322.1GB **
------------------------------------
linux分区  挂载目录  总大小  已用  剩余  已用百分比  文件系统  ID  system  块大小  预留空间
=========  ========  =====   ===   ===   ==========  =======   ==  ======  ======  ========
/dev/sda1  /boot     283M    76M   193M  29%         ext4(rw)  83  Linux   1K      15M
/dev/sda3  /         265G    3.1G  248G  2%          ext4(rw)  83  Linux   4K      13744M

##########################################################################################

==================================原文链接===此文章由博客转发小工具转发==================================

posted on 2018-07-27 09:25  五光十色  阅读(234)  评论(0编辑  收藏  举报

导航