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

#!/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的字符串,后续更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值