zoukankan      html  css  js  c++  java
  • linux服务器性能(网卡流量、CPU、内存、磁盘使用率)监控

      转载请注明:http://www.cnblogs.com/zishengY/p/6819606.html 

      广义的网站的监控涵盖所有的非业务行为的数据采集与管理,包括数据分析师和产品设计师使用的网站用户行为日志、业务运行数据,以及供运维工程师和开发工程师使用的性能统计数据等。

         本文主要是通过shell脚本来收集服务器性能指标,如系统load、内存占用、磁盘IO、CPU占用,并将其写入一个文件中,及时判断应用情况,防患于未然 。

      实现步骤如下:

    第一步:编写shell脚本

    vim check.sh

    添加下面脚本之后保存 

    #!/bin/bash
    #这个脚本使用来统计CPU、磁盘、内存使用率、带宽的
    total=0
    system=0
    user=0
    i=0
    
    #带宽使用情况
    time=`date "+%Y-%m-%d %k:%M"`
    day=`date "+%Y-%m-%d"`
    minute=`date "+%k:%M"`
    echo  "*************************************************************************" >> 123.txt
    echo "统计开始时间:$day $minute" >> 123.txt
    
    #循环五次,避免看到的是偶然的数据
    echo "#带宽的使用情况:#" >>123.txt
    while (( $i<5 ))
    do
    #原先的`ifconfig eth0|sed -n "7p"|awk '{print $2}'|cut -c7-`方式获取网卡的信息为空,已经注释掉
    #rx_before=`ifconfig eth0|sed -n "7p"|awk '{print $2}'|cut -c7-`
    #tx_before=`ifconfig eth0|sed -n "7p"|awk '{print $6}'|cut -c7-`
    rx_before=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $2}')
    tx_before=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $10}')
    sleep 2
    #rx_after=`ifconfig eth0|sed -n "7p"|awk '{print $2}'|cut -c7-`
    #tx_after=`ifconfig eth0|sed -n "7p"|awk '{print $6}'|cut -c7-`
    rx_after=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $2}')
    tx_after=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $10}')
    
    rx_result=$[(rx_after-rx_before)/1024/1024/2*8]
    tx_result=$[(tx_after-tx_before)/1024/1024/2*8]
    echo  "$time Now_In_Speed: $rx_result Mbps Now_OUt_Speed: $tx_result Mbps" >>123.txt
    
    let "i++"
    done
    
    rx_result=$(cat 123.txt|grep "$time"|awk '{In+=$4}END{print In}')
    tx_result=$(cat 123.txt|grep "$time"|awk '{Out+=$7}END{print Out}')
    In_Speed=$(echo "scale=2;$rx_result/5"|bc)
    Out_Speed=$(echo "scale=2;$tx_result/5"|bc)
    #echo "#带宽的5次的平均值是:#" >>123.txt
    echo  "$time In_Speed_average: $In_Speed Mbps Out_Speed_average: $Out_Speed Mbps" >>123.txt
    
    #CPU使用情况
    which sar > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
      total=`vmstat 1 5|awk '{x+=$13;y+=$14}END{print x+y}'`
      average=$(echo "scale=2;$total/5"|bc)
    fi
    echo "#CPU使用率:#" >>123.txt
    echo "Total CPU  is already use: $average%" >>123.txt
    #磁盘使用情况(注意:需要用sed先进行格式化才能进行累加处理)
    disk_used=$(df -m | sed '1d;/ /!N;s/
    //;s/ +/ /;' | awk '{used+=$3} END{print used}')
    disk_totalSpace=$(df -m | sed '1d;/ /!N;s/
    //;s/ +/ /;' | awk '{totalSpace+=$2} END{print totalSpace}')
    disk_all=$(echo "scale=4;$disk_used/$disk_totalSpace" | bc)
    disk_percent1=$(echo $disk_all | cut -c 2-3)
    disk_percent2=$(echo $disk_all | cut -c 4-5)
    disk_warning=`df -m | sed '1d;/ /!N;s/
    //;s/ +/ /;' | awk '{if ($5>85) print $5 $6;} '`
    echo "#磁盘利用率#" >>123.txt
    echo "hard disk has used: $disk_percent1.$disk_percent2%" >>123.txt
    echo -e "		#磁盘存在目录使用率超过85%报警#" >>123.txt
    echo -e "		over used: $disk_warning" >>123.txt
    
    
    #内存使用情况
    memery_used=$(free -m | awk 'NR==2' | awk '{print $3}')
    buffer_used=$(free -m | awk 'NR==2' | awk '{print $6}')
    cache_used=$(free -m | awk 'NR==2' | awk '{print $7}')
    free=$(free -m | awk 'NR==2' | awk '{printf $4}')
    memery_all=$(free -m | awk 'NR==2' | awk '{print $2}')
    used_all=$[memery_all-(free+buffer_used+cache_used)]
    echo "$used_all $memery_all $free" >>123.txt
    memery_percent=$(echo "scale=4;$memery_used / $memery_all" | bc)
    memery_percent2=$(echo "scale=4; $used_all / $memery_all" | bc)
    percent_part1=$(echo $memery_percent | cut -c 2-3)
    percent_part2=$(echo $memery_percent | cut -c 4-5) 
    percent_part11=$(echo $memery_percent2 | cut -c 2-3)
    percent_part22=$(echo $memery_percent2 | cut -c 4-5)
    echo "#内存使用率#" >> 123.txt
    echo "system memery is already use: $percent_part1.$percent_part2%" >>123.txt
    echo "actual memery is already use: $percent_part11.$percent_part22%" >>123.txt
    
    echo  "结束本次统计:$day $minute" >> 123.txt
    echo  "*************************************************************************" >> 123.txt
    echo -e "
    
    
    
    " >> 123.txt

    第二步:创建shell脚本中用到的文件123.txt    

    touch 123.txt

    第三步:给check.sh和123.txt授予所有权限  

    chmod 777 check.sh
    chmod 777 123.txt

    第四步:执行check.sh脚本   

    ./check.sh

    第五步:查看执行写入文件的情况

    出现这信息表名脚本成功运行实时统计情况。

    参考文章

    若是想使用定时任务,可使用crontab进行设置,请参见这篇文章

     

    学习本就是一个不断模仿、练习、再到最后面自己原创的过程。

    虽然可能从来不能写出超越网上通类型同主题博文,但为什么还是要写?
    于自己而言,博文主要是自己总结。假设自己有观众,毕竟讲是最好的学(见下图)。

    于读者而言,笔者能在这个过程get到知识点,那就是双赢了。
    当然由于笔者能力有限,或许文中存在描述不正确,欢迎指正、补充!
    感谢您的阅读。如果本文对您有用,那么请点赞鼓励。

     

     
  • 相关阅读:
    [转]大话企业级Android应用开发实战 用户界面
    [转]大话企业级Android应用开发实战 Activity
    logcat
    [转]大话企业级Android应用开发实战 2D图形
    戴尔R710服务器USC安装linux系统(转载)
    p4n 今天与朋友沟通支付云服务普及以及跨境电子商务的光辉前景
    atitit.研发管理标准化流程总结java开发环境与项目部署环境的搭建工具包总结
    atitit.产品console 日志的aticonsole 方案处理总结
    atitit. 研发管理如何根据自己的特挑选 产业、行业、职业、岗位与自己发展的关系
    Atitit.404错误解决标准流程and url汉字中文路径404错误resin4 resin chinese char path 404 err解决
  • 原文地址:https://www.cnblogs.com/zishengY/p/6819606.html
Copyright © 2011-2022 走看看