目的是查询出进程的名字,cup内存占用率,启动时间在线状态等格式化输出
脚本实现的结果:
[root@zabbix ~]# sh /app/shell/app_status.sh
ProcessName---------GroupName-------Status-----PID----CPU----MEMORY----StarTime---
nginx WEB STOPED NULL NULL NULL NULL
httpd WEB STOPED NULL NULL NULL NULL
mysql DB RUNNING 3019 0.2 24.1 Wed Sep 11 12:02:21 2019
oracle DB STOPED NULL NULL NULL NULL
[root@zabbix ~]# sh /app/shell/app_status.sh nginx
ProcessName---------GroupName-------Status-----PID----CPU----MEMORY----StarTime---
nginx WEB RUNNING 4262 0.0 0.1 Wed Sep 11 12:18:19 2019
nginx WEB RUNNING 4263 0.0 0.1 Wed Sep 11 12:18:19 2019
脚本使用的配置文件存放路径:/app/shell/process.cfg
process.cfg的内容
[GROUP_LIST]
WEB
DB
HADOOP
YUM[WEB]
nginx
httpd[DB]
mysql
oracle
postgresql[HADOOP]
datanode
namenode
journalnode[YUM]
resourcemanager
nodemanager
脚本内容:
#!/bin/bash
#
#Func:Get Porcess Status IN process.cfg
#Define Variables
HOME_DIR="/app/shell/"
CONFIG_FILE="process.cfg"
this_pid=$$
#
#get_all_goup:得到组的成员
#该函数无需输入参数,返回配置配置文件 process.cfg中所有的组信息,如WEB ,DB等
#
function get_all_group
{
G_LIST=sed -n "/\[GROUP_LIST\]/,/\[.*\]/p" ${HOME_DIR}/${CONFIG_FILE}|egrep -v "(^$|\[.*\])"
echo "${G_LIST}"
}
#get_all_process
#该函数无需输入任何参数,返回配置文件process.cfg中所有进程信息
#
function get_all_process
{
for g in get_all_group
do
P_LIST=sed -n "/\[${g}\]/,/\[.*\]/p" ${HOME_DIR}/${CONFIG_FILE} |egrep -v "(^$|\[.*\])"
echo "${P_LIST}"
done
}
#get_process_pid_by_name
#该函数接收一个参数,参数是进程名,返回的是一个进程PID的列表,可能是一个或是多个
#
function get_process_pid_by_name
{
if [ $# -ne 1 ];then
return 1
else
pids=ps -ef | grep $1 | egrep -v grep|grep -v $0 | awk '{print $2}'
echo ${pids}
fi
}
#get_process_info_by_pid
#该函数是接收一个参数,参数进程PID;返回值是一进程运行信息的列表,其包含CPU 占用率 内存
#
function get_process_info_by_pid
{
if [ ps -ef | awk -v pid=$1 '$2==pid{print }' | wc -l
-eq 1 ];then
pro_status="RUNNING"
else
pro_status="STOPED"
fi
pro_cpu=ps aux | awk -v pid=$1 '$2==pid{print $3}'
pro_mem=ps aux | awk -v pid=$1 '$2==pid{print $4}'
pro_start_time=ps -p $1 -o lstart | grep -v STARTED
#echo "${pro_status} ${pro_cpu} ${pro_mem} "${pro_start_time}" "
}
#is_group_in_config
#该函数接收一个参数,参数为组的名称;返回是0或1;0代表该组在配置文件中,1代表不在
#
function is_group_in_config
{
for gn in get_all_group
;do
if [ "${gn}" == "$1" ];then
return
fi
done
echo "Group $1 is not in process.cfg"
return 1
}
#is_process_in_config
#该函数接收一个参数,参数为组名;返回值是对应组内所有进程名名称列表
#
function is_process_in_config
{
for pn in get_all_process
;do
if [ $pn == $1 ];then
return
fi
done
}
#get_all_process_by_group
#该函数接收一个参数,参数为组的名称传给is_group_in_config返回的0列出该组内所有进程名
#
function get_all_process_by_group
{
is_group_in_config $1
if [ $? -eq 0 ];then
p_list=sed -n "/\[$1\]/,/\[.*\]/p" ${HOME_DIR}/${CONFIG_FILE} | egrep -v "(^$|^#|\[.*\])"
echo ${p_list}
else
echo "GroupName $1 is not in process.cfg"
fi
}
#get_group_by_process_name
#该函数接收一个参数,参数名是进程名;调用get_all_group得到组,get_all_process_by_group ${gn};判断是否在这个组内;若在到组名称
#
function get_group_by_process_name
{
for gn in get_all_group
;do
for pn in get_all_process_by_group ${gn}
;do
if [ $pn == $1 ];then
echo "${gn}"
fi
done
done
}
#format_print
#该函数接收一个参数;参数是进程名,显示进程的ProcessName---------","GroupName-------","Status-----","PID----","CPU----","MEMORY----","StarTime---
#
function format_print
{
ps -ef | grep $1 | grep -v grep | grep -v $this_pid &> /dev/null
if [ $? -eq 0 ];then
pids=get_process_pid_by_name $1
for pid in $pids;do
get_process_info_by_pid $pid
awk -v name=$1 \
-v g_name=$2 \
-v status=$pro_status \
-v pid=$pid \
-v cpu=$pro_cpu \
-v mem=$pro_mem \
-v start_time="${pro_start_time}" \
'BEGIN{printf "%-20s%-16s%-11s%-7s%-7s%-10s%-20s\n",name,g_name,status,pid,cpu,mem,start_time}'
done
else
awk -v name=$1 -v g_name=$2 'BEGIN{printf "%-20s%-16s%-11s%-7s%-7s%-10s%-20s\n",name,name,"STOPED","NULL","NULL","NULL","NULL"}'
fi
}
##
##
if [ ! -e ${HOME_DIR}/${CONFIG_FILE} ];then
echo "${CONFIG_FILE} is not exist..please check.."
exit 1
fi
##
##
awk 'BEGIN{printf "%-20s%-16s%-10s%-6s%-7s%-10s%-20s\n","ProcessName---------","GroupName-------","Status-----","PID----","CPU----","MEMORY----","StarTime---"}'
##
##
if [ $# -gt 0 ];then
if [ "$1" == "-g" ];then
shift
for gn in $@;do
is_group_in_config ${gn} || continue
for pn in get_all_process_by_group $gn
;do
is_process_in_config $pn && format_print $pn $gn
done
done
else
for pn in $@;do
gn=get_group_by_process_name $pn
is_process_in_config $pn && format_print $pn $gn
done
fi
else
for pn in get_all_process
;do
gn=get_group_by_process_name $pn
is_process_in_config $pn && format_print $pn $gn
done
fi
[root@zabbix ~]#