shell编程系列26--大型脚本工具开发实战

时间:2021-12-22 04:04:13
shell编程系列26--大型脚本工具开发实战

大型脚本工具开发实战

拆分脚本功能,抽象函数

、function get_all_group    返回进程组列表字符串

、function get_all_process    返回进程名列表字符串"nginx httpd mysql datanode"

、function get_process_info    返回进程详细信息列表字符串,详细信息包括:运行状态、PID、CPU、MEM、启动时间

    注:该函数可以接收一个参数,参数为进程名称

、function get_all_process_by_group    返回进程组内的所有进程名称列表字符串

    例子:DB组--> "mysql postgresql oracle"

    功能函数1代码:

    function get_all_group
{
if [ ! -e $HOME_DIR/$CONFIG_FILE ];then
echo "$CONFIG_file is not exists..Please check"
exit
else
G_LIST=$(sed -n '/\[GROUP_LIST]/,/\[.*\]/p' /root/lesson/9.1/process.cfg | egrep -v "(^$|\[.*\])")
echo "$G_LIST"
fi
} [root@localhost 9.1]# sed -n '/\[GROUP_LIST]/,/\[.*\]/p' process.cfg | grep -v "^$" | grep -v "\[.*\]"
WEB
DB
HADOOP
YARN [root@localhost 9.1]# sed -n '/\[GROUP_LIST]/,/\[.*\]/p' process.cfg | egrep -v "(^$|\[.*\])"
WEB
DB
HADOOP
YARN 功能函数2代码:
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
} 功能函数3代码:
# ps -aux 第三列是cpu使用率,第四列是内存使用率
[root@localhost ~]# ps -aux | grep httpd
root 0.0 0.2 ? Ss : : /usr/sbin/httpd -DFOREGROUND
apache 0.0 0.1 ? S : : /usr/sbin/httpd -DFOREGROUND
apache 0.0 0.1 ? S : : /usr/sbin/httpd -DFOREGROUND
apache 0.0 0.1 ? S : : /usr/sbin/httpd -DFOREGROUND
apache 0.0 0.1 ? S : : /usr/sbin/httpd -DFOREGROUND
apache 0.0 0.1 ? S : : /usr/sbin/httpd -DFOREGROUND function get_process_info # 进程本身的pid
this_pid=$$
function get_process_pid_by_name # 获取进程的pid
{
if [ $# -ne ];then
return
else
#pids=`ps -ef | grep $ | grep -v grep | grep -v $this_pid |grep -v $| awk '{print $2}'`
pids=`ps -ef | grep $ | grep -v grep | grep -v $| awk '{print $2}'`
echo $pids
fi
} function get_process_info_by_pid # 获取进行信息
{
# awk 中引入一个变量
if [ `ps -ef | awk -v pid=$ '$2==pid{print}' | wc -l` -eq ];then
pro_status="RUNNING"
else
pro_status="STOPED"
fi
pro_cpu=`ps aux | awk -v pid=$ '$2==pid{print $3}'`
pro_mem=`ps aux | awk -v pid=$ '$2==pid{print $4}'`
pro_start_time=`ps -p $ -o lstart | grep -v STARTED`
} 功能函数4代码实现:
function is_group_in_config
{
# 如果遍历不存在就返回1
for gn in `get_all_group`;do
if [ "$gn" == "$1" ];then
return
fi
done
return
} function get_all_process_by_group
{
is_group_in_config
if [ $? -eq ];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
} 程序主流程设计: ./app_status.sh执行有三种情况: 、无参数 列出配置文件中所有进程的运行信息
、-g GroupName 列出GroupName组内的所有进程
、process_name1 列出指定进程的运行信息 function get_all_group
说明:该函数无需输入任何参数:返回配置文件 process.cfg中所有的组信息,例如web,db等 function get_all_process 说明:该函数无需输入任何参数:返回配置文件 process.cfg中所有的进程信息 function get_process_pid_by_name 说明:该函数接收一个参数,参数为进程名称:返回值是一个PID的列表,可能有一个PID,也可能有多个 function get_process_info_by_pid 说明:该函数接收一个参数,参数为进程PID;返回值是一个进程运行信息的列表,列表包含运行状态:CPU占用率、内存占用率、进程运行时间 function is_group_in_config 说明 :该函数接收一个参数,参数为组的名称;返回值是0或1,0代表该组在配置文件中,1代表该组不在配置文件中 function get_all_process_by_group 说明:该函数接收一个参数,参数为组名称:返回值是对应组内的所有进程名称列表 function get_group_by_process_name 说明:该函数接收一个参数,参数是一个进程名称:返回值是一个组名 function format_print 说明:该函数接收两个参数,第一个参数为process_name,第二个参数为组名称
返回值,是针对每一个进程PID的运行信息 function is_process_in_config 说明:该函数接收一个参数,参数为进程名称;返回值是0或1,0代表该进程在配置文件中,1代表进程不在配置文件中 配置文件:
[root@localhost 9.1]# cat /root/lesson/9.1/process.cfg
[GROUP_LIST]
WEB
DB
HADOOP
YARN [WEB]
nginx
httpd [DB]
mysql
postgresql
oracle [HADOOP]
datanode
namenode
journalnode [YARN]
resourcemanager
nodemanager # 程序脚本
[root@localhost 9.1]# cat /data/shell/app_status.sh
#!/bin/bash
#
# Func: Get Process Status In process.cfg # Define Variables
HOME_DIR="/root/lesson/9.1"
CONFIG_FILE="process.cfg"
# 进程本身的pid
this_pid=$$ # 获取所有的组
function get_all_group
{
G_LIST=$(sed -n '/\[GROUP_LIST]/,/\[.*\]/p' $HOME_DIR/$CONFIG_FILE | egrep -v "(^$|\[.*\])")
echo $G_LIST
} #for g in `get_all_group`;do
# echo $g
#done # 获取所有进程
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
} # 通过pid获取进程名称
function get_process_pid_by_name
{
if [ $# -ne ];then
return
else
#pids=`ps -ef | grep $ | grep -v grep | grep -v $this_pid |grep -v $| awk '{print $2}'`
pids=`ps -ef | grep $ | grep -v grep | grep -v $| awk '{print $2}'`
echo $pids
fi
} # 通过pid 获取进程信息
function get_process_info_by_pid
{
# awk 中引入一个变量
if [ `ps -ef | awk -v pid=$ '$2==pid{print}' | wc -l` -eq ];then
pro_status="RUNNING"
else
pro_status="STOPED"
fi
pro_cpu=`ps aux | awk -v pid=$ '$2==pid{print $3}'`
pro_mem=`ps aux | awk -v pid=$ '$2==pid{print $4}'`
pro_start_time=`ps -p $ -o lstart | grep -v STARTED` #echo "pro_status=$pro_status"
#echo "pro_cpu=$pro_cpu"
#echo "pro_mem=$pro_mem"
#echo "pro_start_time=$pro_start_time"
} # 判断输入的组是否在配置文件中
function is_group_in_config
{
# 如果遍历不存在就返回1
for gn in `get_all_group`;do
if [ "$gn" == "$1" ];then
return
fi
done
echo "Group $1 is not in process.cfg"
return
} # 判断进程是否在配置中
function is_process_in_config
{
for pn in `get_all_process`;do
if [ $pn == $ ];then
return
fi
done
echo "Process $1 is not in process.cfg"
return
} # 通过组名获取组下所有的进程
function get_all_process_by_group
{
# 判断输入的组名是否在group中
is_group_in_config $
if [ $? -eq ];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
} # 通过进程名称获取所在的组
function get_group_by_process_name
{
for gn in `get_all_group`;do
for pn in `get_all_process_by_group $gn`;do
#echo "pn=$pn"
if [ $pn == $ ];then
echo "$gn"
fi
done
done
} #if [ ! -e $HOME_DIR/$CONFIG_FILE ];then
# echo "$CONFIG_FILE is not exists..please check.."
#fi #is_group_in_config $ && echo "exists" || echo "not exists" #get_all_process_by_group $ function format_print
{
ps -ef | grep $ | grep -v grep | grep -v $this_pid &> /dev/null
if [ $? -eq ];then
pids=`get_process_pid_by_name $`
#echo "pids=$pids"
for pid in $pids;do
get_process_info_by_pid $pid awk -v p_name=$ -v g_name=$ -v p_status=$pro_status -v p_pid=$pid -v p_cpu=$pro_cpu -v p_mem=$pro_mem -v p_start_time="$pro_start_time" 'BEGIN{printf "%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",p_name,g_name,p_status,p_pid,p_cpu,p_mem,p_start_time}'
# -v 定义的参数需要用双引号引起来"",否则会报错 awk: fatal: cannot open file `' for reading (No such file or directory),主要是因为 p_start_time 中间有空格,需要""双引号引起来
#awk -v p_name="$1" -v g_name="$2" -v p_status="$pro_status" -v p_pid="$pid" -v p_cpu="$pro_cpu" -v p_mem="$pro_mem" -v p_start_time="$pro_start_time" 'BEGIN{printf "%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",p_name,g_name,p_pid,p_status,p_cpu,p_mem,p_start_time}'
done
else
awk -v p_name=$ -v g_name=$ 'BEGIN{printf "%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",p_name,g_name,"NULL","Stopped","NULL","NULL","NULL"}'
fi
} # 打印头信息
awk 'BEGIN{printf "%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n","ProcessName---","GroupName---","Status---","Pid---","CPU---","MEMORY---","StartTime---"}' if [ $# -gt ];then
# 传递的是组
if [ "$1" == "-g" ];then
shift
# 遍历传递参数的组
for gn in $@;do
# 组名是否在配置文件中, 执行不成功才会 continue 跳过
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`
#echo "gn=$gn, pn=$pn"
is_process_in_config $pn
if [ $? -eq ];then
#echo "format print"
format_print $pn $gn
fi
done
fi #format_print nginx WEB #group_name=`get_group_by_process_name $`
#echo $group_name
[root@localhost 9.1]# # 常用使用方法 [root@localhost shell]# ./app_status.sh
ProcessName--- GroupName--- Status--- Pid--- CPU--- MEMORY--- StartTime---
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
httpd WEB RUNNING 0.0 0.2 Sat Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
mysql DB RUNNING 0.0 0.0 Sat Jun ::
mysql DB RUNNING 0.0 4.8 Sat Jun ::
postgresql DB NULL Stopped NULL NULL NULL
oracle DB NULL Stopped NULL NULL NULL
datanode HADOOP NULL Stopped NULL NULL NULL
namenode HADOOP NULL Stopped NULL NULL NULL
journalnode HADOOP NULL Stopped NULL NULL NULL
resourcemanager YARN NULL Stopped NULL NULL NULL
nodemanager YARN NULL Stopped NULL NULL NULL
[root@localhost shell]# ./app_status.sh -g WEB
ProcessName--- GroupName--- Status--- Pid--- CPU--- MEMORY--- StartTime---
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
httpd WEB RUNNING 0.0 0.2 Sat Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
[root@localhost shell]# ./app_status.sh -g WEB DB
ProcessName--- GroupName--- Status--- Pid--- CPU--- MEMORY--- StartTime---
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
httpd WEB RUNNING 0.0 0.2 Sat Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
mysql DB RUNNING 0.0 0.0 Sat Jun ::
mysql DB RUNNING 0.0 4.8 Sat Jun ::
postgresql DB NULL Stopped NULL NULL NULL
oracle DB NULL Stopped NULL NULL NULL
[root@localhost shell]# ./app_status.sh nginx httpd
ProcessName--- GroupName--- Status--- Pid--- CPU--- MEMORY--- StartTime---
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
nginx WEB RUNNING 0.0 0.1 Sat Jun ::
httpd WEB RUNNING 0.0 0.2 Sat Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
httpd WEB RUNNING 0.0 0.1 Sun Jun ::
[root@localhost shell]#