Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The shell script should now query the formatted output of the process name, cup memory utilization, startup time, online status, etc.

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The purpose is to query the formatted output such as the name of the process, cup memory utilization, startup time, online status, etc.

The result of the script implementation:

[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 configuration file storage path used by the script: / app/shell/process.cfg

The content of process.cfg

[GROUP_LIST]

WEB

DB

HADOOP

YUM

[WEB]

Nginx

Httpd

[DB]

Mysql

Oracle

Postgresql

[HADOOP]

Datanode

Namenode

Journalnode

[YUM]

Resourcemanager

Nodemanager

Content of the script:

#! / bin/bash

#

# Func:Get Porcess Status IN process.cfg

# Define Variables

HOME_DIR= "/ app/shell/"

CONFIG_FILE= "process.cfg"

This_pid=$$

#

# get_all_goup: get the members of the group

# this function does not need to enter parameters and returns all the group information 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 does not need to enter any parameters and returns all process information in the configuration file 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 a parameter, which is the process name, and returns a list of process PID, which may be one or more

#

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

# this function receives a parameter. The PID; return value of the parameter process is a list of running information of a process, which contains CPU occupancy memory

#

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

# this function takes a parameter, which is the name of the group; 0 or 1 is returned to indicate that the group is in the configuration file, and 1 means that the group is not in

#

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

# this function receives a parameter, which is the group name, and the return value is a list of all process names in the corresponding group

#

Function is_process_in_config

{

For pn in get_all_process;do

If [$pn = = $1]; then

Return

Fi

Done

}

# get_all_process_by_group

# this function takes a parameter that lists all the process names in the group as the name of the group is passed to the 0 returned by is_group_in_config

#

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

# this function takes a parameter, the parameter name is the process name; call get_all_group to get the group, get_all_process_by_group ${gn}; determine whether it is in this group; if so, go to 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]; then

Echo "${gn}"

Fi

Done

Done

}

# format_print

# this function takes a parameter; the parameter is the process name, which displays the process's 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"}'

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 ~] #

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report