The purpose is to check out the process name, cup memory usage, boot time online status formatted output
脚本实现的结果:
[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
The script uses the configuration file storage path: /app/shell/process.cfg
content of process.cfg
[GROUP_LIST]
WEB
DB
HADOOP
YUM[WEB]
nginx
httpd[DB]
mysql
oracle
postgresql[HADOOP]
datanode
namenode
journalnode[YUM]
resourcemanager
nodemanager
Script content:
! # / Bin / bash
#
#Func: the Get Status Porcess the IN process.cfg
The Variables #define
HOME_DIR = "/ App / the shell /"
CONFIG_FILE = "process.cfg"
this_pid = $$
#
#get_all_goup: members of the group to obtain
# function without the input parameters and returns the configuration information of all the groups in the configuration file process.cfg , such as WEB, DB, etc.
#
function get_all_group
{
G_LIST = sed -n "/\[GROUP_LIST\]/,/\[.*\]/p" ${HOME_DIR}/${CONFIG_FILE}|egrep -v "(^$|\[.*\])"
echo "$ {G_LIST}"
}
#get_all_process
# this function without any input parameters, and returns the configuration file information of all processes 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
# this function takes one argument, which is the process name, the process returns a PID list, or may be a plurality of
#
function get_process_pid_by_name
{
IF [$ # -ne. 1 ]; the then
return. 1
the else
= PIDS ps -ef | grep $1 | egrep -v grep|grep -v $0 | awk '{print $2}'
echo $ PIDS} {
Fi
}
#get_process_info_by_pid
# receiving a parameter which is a function, the process of the PID parameter; returned lists a process operational information, comprising memory usage CPU
#
function get_process_info_by_pid
{
IF [ ps -ef | awk -v pid=$1 '$2==pid{print }' | wc -l
-eq. 1]; the then
pro_status = "the RUNNING"
the 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_cpu pro_status} {pro_mem}} $"} $ {pro_start_time ""
}
#is_group_in_config
# this function receives a parameter parameter for the name of the group; return is 0 or 1; 0 representatives of the group in the configuration file, a representative is not
#
function is_group_in_config
{
for GN in get_all_group
; do
IF [ "$ {GN}" == "$ 1"]; the then
return
fi
DONE
echo "Not in Group $ IS process.cfg. 1"
return. 1
}
#is_process_in_config
# This function receives a parameter, the parameter for the group name; all the processes the return value is the name of the name list corresponding to the group
#
function is_process_in_config
{
for PN in get_all_process
; do
IF [ PN. 1 == $ $]; the then
return
Fi
DONE
}
#get_all_process_by_group
# this function receives a parameter, the parameter is passed to the group name list 0 is_group_in_config return names of all processes in the group
#
function get_all_process_by_group
{
is_group_in_config $. 1
IF [$? 0 -eq]; the then
P_LIST = sed -n "/\[$1\]/,/\[.*\]/p" ${HOME_DIR}/${CONFIG_FILE} | egrep -v "(^$|^#|\[.*\])"
echo $ {} P_LIST
the else
echo "Not in the GroupName $ IS process.cfg. 1"
Fi
}
#get_group_by_process_name
# This function receives a parameter, the parameter name is the process name; call get_all_group been set, get_all_process_by_group $ {gn}; determining whether in the group; if the group name
#
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]; the then
echo "$ {} GN"
Fi
DONE
DONE
}
#format_print
# this function receives a parameter; parameter is the name of the process, display process ProcessName --------- " "GroupName -------", "Status -----", "PID ----", "the CPU ----", "the MEMORY ----", "STARTIME ---
#
format_print function
{
PS -ef | grep $. 1 | grep -v grep | grep -v $ this_pid &> / dev / null
IF; the then [0 -eq $?]
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 ~]#