linux基础监控

#!/usr/bin/bash
#-*- coding:utf-8 -*-

#cpu的使用率
a=`cat /proc/stat | grep -E "cpu\b" | awk -v total=0 '{$1="";for(i=2;i<=NF;i++){total+=$i};used=$2+$3+$4+$7+$8+$9}END{print total,used}'`
sleep 1
b=`cat /proc/stat | grep -E "cpu\b" | awk -v total=0 '{$1="";for(i=2;i<=NF;i++){total+=$i};used=$2+$3+$4+$7+$8+$9 }END{print total,used}'`
str=${a// / }
arr=($str)
a_total=${arr[0]}
a_use=${arr[1]}
str=${b// / }
arr=($str)
b_total=${arr[0]}
b_use=${arr[1]}
cpu_usage=$[($b_use-$a_use)*100/($b_total-$a_total)]
echo ${cpu_usage}


#采集内存使用占比
awk '/MemTotal/{total=$2}/MemFree/{free=$2}/Buffers/{buffers=$2}/^Cached/{cached=$2}END{print (total-free-buffers-cached)*100/total}'  /proc/meminfo

#各磁盘存储使用占比
dir_list=$(df -h | grep -v "Filesystem"  | awk '{print $NF}')
for dir in ${dir_list[*]}
do
    df -h | grep -v "Filesystem" | grep "${dir}$" | awk '{split($5,array,"%");print $NF,array[1]}'
done

#各磁盘inode使用占比

dir_list=$(df -i | grep -v "Filesystem"  | awk '{print $NF}')
for dir in ${dir_list[*]}
do
    df -i | grep -v "Filesystem" | grep "${dir}$" | awk '{split($5,array,"%");print $NF,array[1]}'
done

#系统盘io
sys_io_util=$(iostat -d -x -k 1 2 |grep 'sda'|awk '{total+=$12};{size+=1}END{print total/size}')

#存储盘io
data_io_util=$(iostat -d -x -k 1 2 |egrep -v 'sda|Device|Linux|^$'|awk '{total+=$12};{size+=1}END{print total/size}')

#设备最近1分钟和最近5分钟负载
w | awk 'NR==1{split($(NF-2),array,",");print array[1]}'
w | awk 'NR==1{split($(NF-1),array,",");print array[1]}'

#连接数
tcp_link=$(ss -t -a | grep "ESTAB" | grep -v "Port" | wc -l)
udp_link=$(ss -u -a  | grep "ESTAB" | grep -v "Port" | wc -l)
http_link=$(ss -o state established '( dport = :http or sport = :http )' | wc -l)
https_link=$(ss -o state established '( dport = :https or sport = :https )' | wc -l)

#hcs采集网卡流量
for (( i = 0; i < 1; i=(i+1) ));do a=`cat /proc/net/dev| grep bond1_v1|awk '{print $10}'`;sleep 5;b=`cat /proc/net/dev | grep bond1_v1 | awk '{print $10}'`;let c=${b}-${a};echo ${c};done


#判断系统是否重启
mark=$(uptime | awk '{print $4}')
start_time=$(uptime | awk '{print $3}')
if [ ${mark}" == "min" ];then
    if [ ${start_time} < 2 ];then
        echo -e "机器在2分钟内重启过"    
    else
        echo -e "机器在1小时内重启过"
    fi
else
    echo -e "机器没有重启"
fi

你可能感兴趣的:(linux基础监控)