您的位置:首页 > 运维架构 > Linux

分享一个自己写的比较牛的linux服务器自动监控程序

2016-11-09 17:01 381 查看
#!/bin/bash
#version0.3 使用top执行后的文件分析,对系统的影响更小
#程序目的:监控系统的cpu、内存、存储、网络的健康状态
#编写者:Lion Lan 13071039067
#编写时间:2016/11/9

#执行下环境变量
source /home/`whoami`/.bash_profile

#脚本部署的目录
dir=/home/lion/monitor

#获取到的top执行后的文件
topfile=$dir/top.txt

#最后输出记录的文件
logfile=$dir/monitorlog.txt

#主机名
serverName=`hostname`
#当前的日期
currDate=`date +%F`
#当前的时间
currTime=`date +%R`

top -bn 1 > $topfile

declare taskTotal
declare taskRunning
declare taskSleeping
declare taskStopped
declare taskZombie

declare cpuUs
declare cpuSy
declare cpuNi
declare cpuId
declare cpuWa
declare cpuHi
declare cpuSi
declare cpuSt

declare memTotal
declare memUsed
declare memFree
declare memBuffers
declare swapTotal
declare swapUsed
declare swapFree
declare swapCached

#获取到top文件头的所有的参数

#进程的有关参数,获取到的字符串是数字(字符串类型)
taskTotal=`cat $topfile| grep -i tasks | head -1 | awk -F" " '{print $2}'`
taskRunning=`cat $topfile | grep -i tasks| head -1|awk -F" " '{print $4}'`
taskSleeping=`cat $topfile | grep -i tasks| head -1|awk -F" " '{print $6}'`
taskStopped=`cat $topfile | grep -i tasks| head -1|awk -F" " '{print $8}'`
taskZombie=`cat $topfile | grep -i tasks| head -1|awk -F" " '{print $10}'`

#注意:有关cpu的参数获取到的值都带有%
cpuUs=`cat $topfile | grep -i cpu| head -1|awk -F" " '{print $2}'|awk -F"us" '{print $1}'`
cpuSy=`cat $topfile | grep -i cpu| head -1|awk -F" " '{print $3}'|awk -F"sy" '{print $1}'`
cpuNi=`cat $topfile | grep -i cpu| head -1|awk -F" " '{print $4}'|awk -F"ni" '{print $1}'`
cpuId=`cat $topfile | grep -i cpu| head -1|awk -F" " '{print $5}'|awk -F"id" '{print $1}'`
cpuWa=`cat $topfile | grep -i cpu| head -1|awk -F" " '{print $6}'|awk -F"wa" '{print $1}'`
cpuHi=`cat $topfile | grep -i cpu| head -1|awk -F" " '{print $7}'|awk -F"hi" '{print $1}'`
cpuSi=`cat $topfile | grep -i cpu| head -1|awk -F" " '{print $8}'|awk -F"si" '{print $1}'`
cpuSt=`cat $topfile | grep -i cpu| head -1|awk -F" " '{print $9}'|awk -F"st" '{print $1}'`

#注意:有关memory的参数获取到的值都带有k
memTotal=`cat $topfile |grep -i mem| head -1|awk -F" " '{print $2}'`
memUsed=`cat $topfile |grep -i mem| head -1|awk -F" " '{print $4}'`
memFree=`cat $topfile |grep -i mem| head -1|awk -F" " '{print $6}'`
memBuffers=`cat $topfile |grep -i mem| head -1|awk -F" " '{print $8}'`

#注意:有关swap的参数获取到的值都带有k
swapTotal=`cat $topfile| grep -i swap|head -1|awk -F" " '{print $2}'`
swapUsed=`cat $topfile| grep -i swap|head -1|awk -F" " '{print $4}'`
swapFree=`cat $topfile| grep -i swap|head -1|awk -F" " '{print $6}'`
swapCached=`cat $topfile| grep -i swap|head -1|awk -F" " '{print $8}'`

#内存的使用率
declare currentMemRate
function getMemoryRate()
{
#系统的全部内存(K)
totalMemory=`echo $memTotal| sed 's/k//g'`

#当前已经使用的内存(K)
currentMemory=`echo $memUsed| sed 's/k//g'`

#系统当前的内存使用率
currentMemRate=`echo "scale=4;$currentMemory/$totalMemory" | bc | awk '{printf "%.4f",$0}'`
}

getMemoryRate

recordJson="{\"hostname\":\"$serverName\",
\"date\":\"$currDate\",
\"time\":\"$currTime\",
\"taskTotal\":\"$taskTotal\",
\"taskRunning\":\"$taskTotal\",
\"taskSleeping\":\"$taskSleeping\",
\"taskStopped\":\"$taskStopped\",
\"taskZombie\":\"$taskZombie\",
\"cpuUs\":\"$cpuUs\",
\"cpuSy\":\"$cpuSy\",
\"cpuNi\":\"$cpuNi\",
\"cpuId\":\"$cpuId\",
\"cpuWa\":\"$cpuWa\",
\"cpuHi\":\"$cpuHi\",
\"cpuSi\":\"$cpuSi\",
\"cpuSt\":\"$cpuSt\",
\"memTotal\":\"$memTotal\",
\"memUsed\":\"$memUsed\",
\"memFree\":\"$memFree\",
\"memBuffers\":\"$memBuffers\",
\"swapTotal\":\"$swapTotal\",
\"swapUsed\":\"$swapUsed\",
\"swapFree\":\"$swapFree\",
\"swapCached\":\"$swapCached\",
\"currentMemRate\":\"$currentMemRate\"
}"

echo $recordJson | sed 's/[ ]//g' >> $logfile


最后得到的是json的字符串,后续更新。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐