linux服务器性能(网卡流量、CPU、内存、磁盘使用率)监控
转载请注明:http://www.cnblogs.com/zishengY/p/6819606.html
广义的网站的监控涵盖所有的非业务行为的数据采集与管理,包括数据分析师和产品设计师使用的网站用户行为日志、业务运行数据,以及供运维工程师和开发工程师使用的性能统计数据等。
本文主要是通过shell脚本来收集服务器性能指标,如系统load、内存占用、磁盘IO、CPU占用,并将其写入一个文件中,及时判断应用情况,防患于未然 。
实现步骤如下:
第一步:编写shell脚本
vim check.sh
添加下面脚本之后保存
#!/bin/bash
#这个脚本使用来统计CPU、磁盘、内存使用率、带宽的
total=
system=
user=
i= #带宽使用情况
time=`date "+%Y-%m-%d %k:%M"`
day=`date "+%Y-%m-%d"`
minute=`date "+%k:%M"`
echo "*************************************************************************" >> .txt
echo "统计开始时间:$day $minute" >> .txt #循环五次,避免看到的是偶然的数据
echo "#带宽的使用情况:#" >>.txt
while (( $i< ))
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
#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)///*]
tx_result=$[(tx_after-tx_before)///*]
echo "$time Now_In_Speed: $rx_result Mbps Now_OUt_Speed: $tx_result Mbps" >>.txt let "i++"
done rx_result=$(cat .txt|grep "$time"|awk '{In+=$4}END{print In}')
tx_result=$(cat .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次的平均值是:#" >>.txt
echo "$time In_Speed_average: $In_Speed Mbps Out_Speed_average: $Out_Speed Mbps" >>.txt #CPU使用情况
which sar > /dev/null >&
if [ $? -ne ]
then
total=`vmstat |awk '{x+=$13;y+=$14}END{print x+y}'`
average=$(echo "scale=2;$total/5"|bc)
fi
echo "#CPU使用率:#" >>.txt
echo "Total CPU is already use: $average%" >>.txt
#磁盘使用情况(注意:需要用sed先进行格式化才能进行累加处理)
disk_used=$(df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{used+=$3} END{print used}')
disk_totalSpace=$(df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{totalSpace+=$2} END{print totalSpace}')
disk_all=$(echo "scale=4;$disk_used/$disk_totalSpace" | bc)
disk_percent1=$(echo $disk_all | cut -c -)
disk_percent2=$(echo $disk_all | cut -c -)
disk_warning=`df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{if ($5>85) print $5 $6;} '`
echo "#磁盘利用率#" >>.txt
echo "hard disk has used: $disk_percent1.$disk_percent2%" >>.txt
echo -e "\t\t#磁盘存在目录使用率超过85%报警#" >>.txt
echo -e "\t\tover used: $disk_warning" >>.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" >>.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 -)
percent_part2=$(echo $memery_percent | cut -c -)
percent_part11=$(echo $memery_percent2 | cut -c -)
percent_part22=$(echo $memery_percent2 | cut -c -)
echo "#内存使用率#" >> .txt
echo "system memery is already use: $percent_part1.$percent_part2%" >>.txt
echo "actual memery is already use: $percent_part11.$percent_part22%" >>.txt echo "结束本次统计:$day $minute" >> .txt
echo "*************************************************************************" >> .txt
echo -e "\n\n\n\n" >> .txt
第二步:创建shell脚本中用到的文件123.txt
touch .txt
第三步:给check.sh和123.txt授予所有权限
chmod check.sh
chmod .txt

第四步:执行check.sh脚本
./check.sh

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

出现这信息表名脚本成功运行实时统计情况。
参考文章
若是想使用定时任务,可使用crontab进行设置,请参见这篇文章
学习本就是一个不断模仿、练习、再到最后面自己原创的过程。
虽然可能从来不能写出超越网上通类型同主题博文,但为什么还是要写?
于自己而言,博文主要是自己总结。假设自己有观众,毕竟讲是最好的学(见下图)。于读者而言,笔者能在这个过程get到知识点,那就是双赢了。
当然由于笔者能力有限,或许文中存在描述不正确,欢迎指正、补充!
感谢您的阅读。如果本文对您有用,那么请点赞鼓励。
linux服务器性能(网卡流量、CPU、内存、磁盘使用率)监控的更多相关文章
- 用 Python 脚本实现对 Linux 服务器的网卡流量监控
*这篇文章网上已经有相关代码,为了加深印象,我做了相关批注,希望对朋友们有帮助 工作原理:基于/proc文件系统 Linux 系统为管理员提供了非常好的方法,使其可以在系统运行时更改内核,而不需要重新 ...
- linux - 服务器性能评估
影响Linux服务器性能的因素 cpu 内存 磁盘IO 网络IO 系统性能评估标准 影响性能因素 好 坏 糟糕 CPU user% + sys%< 70% user% + sys%= 85% u ...
- 【转】linux服务器性能查看
转载自https://blog.csdn.net/achenyuan/article/details/78974729 1.1 cpu性能查看 1.查看物理cpu个数: cat /proc/cpuin ...
- Linux服务器性能查看分析调优
一 linux服务器性能查看 1.1 cpu性能查看 1.查看物理cpu个数: cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc ...
- Linux服务器性能分析与调优
一 linux服务器性能查看 1.1 cpu性能查看 1.查看物理cpu个数: cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc ...
- linux服务器性能查看
1.1 cpu性能查看 1.查看物理cpu个数: cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc -l 2.查看每个物理cpu ...
- Linux 性能监控之CPU&内存&I/O监控Shell脚本2
Linux 性能监控之CPU&内存&I/O监控Shell脚本2 by:授客 QQ:1033553122 思路: 捕获数据->停止捕获数据->提取数据 备注:一些命令的输 ...
- Linux 性能监控之CPU&内存&I/O监控Shell脚本1
Linux 性能监控之CPU&内存&I/O监控Shell脚本1 by:授客 QQ:1033553122 #!/bin/bash # 获取要监控的本地服务器IP地址 IP=`if ...
- Linux服务器性能评估与优化(一)
网络内容总结(感谢原创) 1.前言简介 一.影响Linux服务器性能的因素 1. 操作系统级 性能调优是找出系统瓶颈并消除这些瓶颈的过程. 很多系统管理员认为性能调优仅仅是调整一下 ...
- Linux服务器性能评估与优化
一.影响务器性能因素 影响企业生产环境Linux服务器性能的因素有很多,一般分为两大类,分别为操作系统层级和应用程序级别.如下为各级别影响性能的具体项及性能评估的标准: (1)操作系统级别 内存: C ...
随机推荐
- 关于jstl的问题:The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed
Current sofeware:java Eclipse ee 4.5.2 + Tomcat 6.0 Question: 在tomcat中部署好了我的项目,然后发布后没有报错.但是当在浏览器打开的时 ...
- WebService客户端添加SOAPHeader信息
通过JAXBContext创建Marshaller对头信息进行解析为dom,获取WSBindingProvider,使用Headers.creat()创建soap的Header元素: 另外就是:将us ...
- 设计模式的征途—1.单例(Singleton)模式
单例模式属于创建型模式的一种,创建型模式是一类最常用的设计模式,在软件开发中应用非常广泛.创建型模式将对象的创建和使用分离,在使用对象时无需关心对象的创建细节,从而降低系统的耦合度,让设计方案更易于修 ...
- dede织梦数据表字段解释
提示:常用字段,可以在dede后台->系统->SQL命令行工具,执行sql语句来批量修改 dede_addonarticle 附加文章表 aid int(11) 文章编号 ...
- iOS开发寻找最近公共view
新技能 #pragma mark --寻找最近公共view + (NSArray *)superViews:(UIView *)view{ if (view==nil) { return @[]; } ...
- PHPCMSV9上线方法及文件权限设置
上线步骤: a.替换代码和数据库文件内的域名b.修改cache/configs/database.php中的数据库密码c.修改cache/configs/system.php文件中的网站路径变量 'w ...
- nginx 配置禁用ip地址访问
做过面向公网WEB运维的苦逼们肯定见识过各种恶意扫描.拉取.注入等图谋不轨行为吧?对于直接对外的WEB服务器,我们可以直接通过 iptables . Nginx 的deny指令或者是程序来ban掉这些 ...
- Java中一个方法只被一个线程调用一次
1.想在运行时抛出异常,终止方法的运行 private final Set<Long> THREADS = new HashSet<>(); public void someM ...
- 【Spring】详解Spring中Bean的加载
之前写过bean的解析,这篇来讲讲bean的加载,加载要比bean的解析复杂些,该文之前在小编原文中有发表过,要看原文的可以直接点击原文查看,从之前的例子开始,Spring中加载一个bean的方式: ...
- windows server 定期备份数据库脚本
将以下文件保存为.bat脚本,在计划任务中添加定时任务运行此脚本即可.脚本中的备份目录,数据库目录和压缩文件目录请自行修改. @echo off rem 当前路径切换到备份数据库目录 cd D:\wa ...
