shell脚本监控内嵌linux系统业务、CPU、内存等

定制板卡中bash无法使用,使用标准sh完成对系统及业务监控

#! /bin/sh

#Process_Name="/guard /aps /sa /zyguard"
Process_Name="/mp /mcu"
#获取主机IP地址
ip=`ifconfig eth0 |grep "inet addr"| cut -f 2 -d ":"|cut -f 1 -d " "`
gstr="/guard"
space=" "
First_Process=""

#获取初始业务进程号
for i in $Process_Name
do
	if [[ $i == $gstr ]]
	then
		Bf_Process=`ps |grep $i|grep -v grep|grep -v ‘/bin/bash‘|awk ‘{print $1}‘`
		First_Process=$First_Process$i":"$Bf_Process$space
	else
		Bf_Process=`ps |grep $i|grep -v grep|awk ‘{print $1}‘`
		First_Process=$First_Process$i":"$Bf_Process$space
	fi
done

while true
do
	For_num=1
	sleep 3
	Second_Process=""
	#第二次获取业务进程号
	for i in $Process_Name
	do
		if [[ $i == $gstr ]]
		then
			Bs_Process=`ps |grep $i|grep -v grep|grep -v ‘/bin/bash‘|awk ‘{print $1}‘`
			Second_Process=$Second_Process$i":"$Bs_Process$space
		else
			Bs_Process=`ps |grep $i|grep -v grep|awk ‘{print $1}‘`
			Second_Process=$Second_Process$i":"$Bs_Process$space
		fi
	done
	echo "First_Process:"$First_Process
	echo "Second_Process:"$Second_Process
	#查看所有业务进程有无变化,有变化输出到日志文件
	for j in $Process_Name
	do
		echo $j
		One_f_Process=`echo $First_Process|awk -v t="${For_num}" ‘{print $t}‘`
		One_s_Process=`echo $Second_Process|awk -v t="${For_num}" ‘{print $t}‘`
		For_num=$((For_num+1))
		echo "One_f_Process:"$One_f_Process
		echo "One_s_Process:"$One_s_Process
		if [[ "$One_f_Process" != "$One_s_Process" ]]
		then
			Time_Now=`date`
			echo $Time_Now
			echo $j
			echo $One_s_Process
			echo $One_f_Process
			echo -e $Time_Now"\t"$j"\tCollapse\tThe new process:"$One_s_Process"\t\tThe old process:"$One_f_Process >> /usr/tmp/$ip‘_collapse‘.txt
		fi
	done
	#获取业务进程号
	First_Process=""
	for i in $Process_Name
	do
		if [[ $i == $gstr ]]
		then
			Bf_Process=`ps |grep $i|grep -v grep|grep -v ‘/bin/bash‘|awk ‘{print $1}‘`
			First_Process=$First_Process$i":"$Bf_Process$space
		else
			Bf_Process=`ps |grep $i|grep -v grep|awk ‘{print $1}‘`
			First_Process=$First_Process$i":"$Bf_Process$space
		fi
	done

	#系统内存使用率监控
	Memory_Use_Rate=`free | grep Mem | awk ‘{printf"%d",$3/$2*100}‘`
	if [ 80 -le $Memory_Use_Rate ]
	then
		Time_Now=`date`
		echo -e $Time_Now"\tSystm Memory\tMemory_Use_Rate\tAt present the use value:"$Memory_Use_Rate"%\tThreshold:80%" >> /usr/tmp/$ip‘_alarm‘.txt
	fi

	#系统CPU剩余率监控
	Cpu_Residual_Rate=`mpstat|grep all|awk ‘{printf"%d",$11}‘`
	echo $Cpu_Residual_Rate
	if [ $Cpu_Residual_Rate -le 20 ]
	then
		Time_Now=`date`
		echo $Time_Now
		echo -e $Time_Now"\tSystm Cpu\tCpu_Residual_Rate(%idle)\tAt present the use value:"$Cpu_Residual_Rate"%\tThreshold:20%" >> /usr/tmp/$ip‘_alarm‘.txt
	fi	

	#业务内存使用率监控
	System_Memory=`free | grep Mem | awk ‘{printf"%d",$2/1024}‘`
	for k in $Process_Name
	do
		if [[ $k == $gstr ]]
		then
			Process_Memory_Use=`ps |grep $k|grep -v grep|grep -v ‘/bin/bash‘|awk ‘{print $3}‘`
			var_length=${#Process_Memory_Use}
			var_Position=`expr $var_length - 1`
			m=${Process_Memory_Use:$var_Position:1}
			if [[ $m == "m" ]]
			then
				Pro_Mem_Use=${Process_Memory_Use:0:$var_Position}
			else
				Pro_Mem_Use=`expr $Process_Memory_Use / 1024`
			fi
			System_Memory_Threshold=`free | grep Mem | awk ‘{printf"%d",$2/1024*0.8}‘`
			if [[ $Pro_Mem_Use  -ge $System_Memory_Threshold ]]
			then
				Time_Now=`date`
				echo -e $Time_Now"\t"$k"\tProcess_Memory_Use\tAt present the use value:"$Pro_Mem_Use"Mb\tThreshold:"$System_Memory_Threshold"Mb" >> /usr/tmp/$ip‘_alarm‘.txt
			fi
		else
			Process_Memory_Use=`ps |grep $k|grep -v grep|awk ‘{print $3}‘`
			var_length=${#Process_Memory_Use}
			var_Position=`expr $var_length - 1`
			m=${Process_Memory_Use:$var_Position:1}
			if [[ $m == "m" ]]
			then
				Pro_Mem_Use=${Process_Memory_Use:0:$var_Position}
			else
				Pro_Mem_Use=`expr $Process_Memory_Use / 1024`
			fi
			System_Memory_Threshold=`free | grep Mem | awk ‘{printf"%d",$2/1024*0.8}‘`
			if [[ $Pro_Mem_Use  -ge $System_Memory_Threshold ]]
			then
				Time_Now=`date`
				echo -e $Time_Now"\t"$k"\tProcess_Memory_Use\tAt present the use value:"$Pro_Mem_Use"Mb\tThreshold:"$System_Memory_Threshold"Mb" >> /usr/tmp/$ip‘_alarm‘.txt
			fi
		fi
	done
done
时间: 2024-08-30 04:37:09

shell脚本监控内嵌linux系统业务、CPU、内存等的相关文章

用shell脚本监控linux系统 自动发送邮件

此脚本可以做一个定时的检测,超出设定的值,即往邮箱发送警告 脚本用到bc,sendmail,163邮箱, yum install bc #!/bin/bash #System Monitoring Script while [ 1 ] do #本机需开启postfix或sendmail服务. #报警邮件地址设置 [email protected] [email protected] #设置脚本运行间隔时间.单位(秒). RUNTIME=900 #内存使用率监控设置,单位 (%) MEMTHRE=

linux shell脚本监控进程是否存在

用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货:    #!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ]thenecho "start process....."elseecho "runing....."fi #####processString 表示进程特征字符串,能够查询到唯一进程的特征字符串0表示存在的$? -ne 0 不存在,$? -eq 0 存在 定时执行:

Linux下shell脚本监控Tomcat的状态并实现自动启动

最近公司需要在Linux下监控tomcat的服务,一旦tomcat服务存在异常或者宕机,重启tomcat保证服务的正常运行,由于Linux下有Shell脚本可以实现此效果,下面是Linux下shell脚本监控Tomcat的状态并实现自动启动的步骤. 1.编写Shell脚本monitor.sh #!/bin/sh # func:自动监控tomcat脚本并且执行重启操作# author:EagleHao# date:2018-04-08# DEFINE # 获取tomcat进程ID(其中[grep

使用shell脚本监控少量服务器并发送微信告警信息

01. 概括 1.0 脚本更新地址Git更新地址:shell_monitor_script.sh文章原文地址:使用shell脚本监控少量服务器并发送微信告警信息 1.1 前提:平台系统:linux系统下接收信息:微信企业号 1.2 脚本说明需要修改微信接口脚本对应参数的修改:包括IP,告警阈值等 1.3 脚本使用该脚本监控包括Disk.CPU.MEM.LOAD等主机资源,以及docker服务和docker容器.将该脚本附件假定放于:/home/mai/.check_host.sh.那么在定时任务

【转载】用shell脚本监控进程是否存在 不存在则启动的实例

用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货: #!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ]thenecho "start process....."elseecho "runing....."fi#####processString 表示进程特征字符串,能够查询到唯一进程的特征字符串0表示存在的$? -ne 0 不存在,$? -eq 0 存在 ---------

shell 脚本实战笔记(4)--linux磁盘分区重新挂载

背景: Hadoop的HDFS文件系统的挂载, 默认指定的文件目录是/mnt/disk{N}. 当运维人员, 不小心把磁盘挂载于其他目录, 比如/mnt/data, /mnt/disk01, /mnt/diska时, HDFS会选择根分区, 当往HDFS里灌数据时, 导致的结果往往是根分区被快速的消耗尽, 而其他分区未见使用. 在CDH版本中, HDFS的配置如下所示: dfs.datanode.data.dir => /mnt/disk{N}/dfs/dn dfs.datanode.data.

shell 脚本实战笔记(9)--linux自动批量添加用户

前言: 添加linux用户帐号,这个相对简单, 在面对集群, 许多机器的时候, 我们该如何去做和实现? 这篇短文, 简单讲解一些思路, 尽可能地涉及周边的一些知识点. 不光是运维人员会面临这个问题, 对一个基于linux平台的集群服务或软件(比如hadoop集群), 有时也会涉及到这块. 应用场景: 是以centos 6.4作为演示的系统, 其他的系统有类同, 也有差异, 且以实战演练, 一步步的讲述下流程. *) 实战演练 查阅useradd的使用和参数选项useradd --help -d,

用shell脚本监控进程是否存在 不存在则启动的实例附带if判断详细条件

#!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ]thenecho "start process....."elseecho "runing....."fi#####processString 表示进程特征字符串,能够查询到唯一进程的特征字符串0表示存在的$? -ne 0 不存在,$? -eq 0 存在 --------------------------------------------

shell脚本监控进程是否存在,不存在则启动实例

用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货: #!/bin/sh ps -fe|grep processString |grep -v grep if [ $? -ne 0 ] then echo "start process....." else echo "runing....." fi processString 表示进程特征字符串,能够查询到唯一进程的特征字符串 0表示存在的 $? -ne 0 不存在,$? -eq 0 存在 定时执行