Skip to content

Commit

Permalink
提交:系统进程运维监控脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
useryu1015 committed Apr 26, 2024
1 parent a0a06b2 commit be20ed4
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@
4.Test/test_FOCAS/new_fwlib/fwlib




*4.Test/test_OPCUA
92 changes: 92 additions & 0 deletions 5.DOS/echo_ppid
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/sh

write_file="${1:-/dev/tty}"
proc_name="${2:-ditoo}"

# write_file="

# 函数:限制字符串宽度
# 参数:
# $1: 要截断的字符串
# $2: 最大宽度
# 返回值:截断后的字符串
truncate_string() {
local string="$1"
local max_width="$2"

if [ ${#string} -gt $max_width ]; then
echo "${string:0:$max_width}"
else
echo "$string"
fi
}


# 函数:获取进程内存信息
# 参数:
# $1: 进程 PID
# 输出:进程的虚拟内存、物理内存和磁盘内存信息
get_process_memory_info() {
local pid="$1"
local cmd="$2"
local status_file="/proc/$pid/status"

# 检查进程是否存在
if [ ! -e "$status_file" ]; then
echo "Process with PID $pid does not exist."
return 1
fi

# 提取虚拟内存、物理内存和磁盘内存信息
local virtual_memory=$(awk '/VmSize/ {print $2 " " $3}' "$status_file")
local physical_memory=$(awk '/VmRSS/ {print $2 " " $3}' "$status_file")
local disk_memory=$(awk '/VmSwap/ {print $2 " " $3}' "$status_file")

# 输出信息
printf "%-10s %-16s %-16s %-16s %-40s\n" "$pid" "$virtual_memory" "$physical_memory" "$disk_memory" "$cmd" >> "${write_file}"

}

# 处理进程信息函数
process_info() {
local info="$1"
local pid=$(echo "$info" | awk '{print $1}')
local proc=$(echo "$info" | awk '{$1=""; print $0}')

# 在这里编写你的处理逻辑,例如发送信号给进程等
get_process_memory_info $pid "$proc"
}

# 循环处理进程信息函数
loop_process_info() {
local pattern="$1"
local interval="$2"

while true; do
echo "**[$pattern]进程状态:**" >> "${write_file}"
echo '```' >> "${write_file}"
printf "%-10s %-20s %-20s %-20s %-40s\n" "PID" "虚拟内存" "物理内存" "磁盘内存" "执行命令" >> "${write_file}"

# 使用 pgrep 命令获取匹配的进程信息
# local processes=$(pgrep -f "$pattern" -a)
# # 逐行处理进程信息 # fixme: arm执行异常
# while IFS= read -r line; do
# process_info "$line"
# done <<< "$processes"

pgrep -f "$pattern" -a | while IFS= read -r line; do
# 在这里处理每一行的输出结果
# echo "$line"
process_info "$line"
done

echo '```' >> "${write_file}"

return
# 休眠一段时间
sleep "$interval"
done
}

# 在这里调用循环处理进程信息函数,并传入进程匹配模式和循环间隔
loop_process_info "$proc_name" 5
35 changes: 35 additions & 0 deletions 5.DOS/echo_proc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

write_file="${1:-/dev/tty}"
proc_name="${2:-ditoo}"

pritf_log() {
local mes="$1"
local cmd="$2"

# echo CDM: $mes $cmd
# 添加信息到文件
echo "**$mes** " >> "${write_file}"
echo '```' >> "${write_file}"
eval "$cmd" >> "${write_file}"
echo "\`\`\`" >> "${write_file}"
echo >> "${write_file}"
}


# echo 当前时间: > ${write_file}; date >> ${write_file}; echo ------ >> ${write_file};
pritf_log "当前时间:" "date"
pritf_log "内存使用情况:" "free -h"
pritf_log "磁盘使用情况:" "df -h"
pritf_log "在线进程信息:" "pidof ${proc_name}"
pritf_log "在线进程信息:" "pgrep -a -f ${proc_name}"
pritf_log "网络连接状态:" "netstat -natp | grep ${proc_name}"

## x86:处理一些复杂的命令&调试信息
core=`uname -m`
if [ $core = "x86_64" ]; then
echo x86_64 architecture

pritf_log "进程资源占用:" "ps aux | egrep '${proc_name}|USER'"
fi

33 changes: 33 additions & 0 deletions 5.DOS/echo_unix
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/sh

# 定义日志文件路径
write_file=$1
if [ -z "$write_file" ]; then
write_file="/dev/tty"
fi

# 获取当前时间
time=$(date "+%Y-%m-%d %H:%M:%S")

# 监控CPU使用率
cpu_usage=$(top -b -n 1 | grep "Cpu(s)" | awk '{print $2}' | awk -F "." '{print $1}')
cpu_info="CPU使用率:$cpu_usage%"

# 监控内存使用率
mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}' | awk -F "." '{print $1}')
mem_info="内存使用率:$mem_usage%"

# 监控磁盘使用率
disk_usage=$(df -h | awk '$NF=="/"{printf "%s", $5}')
disk_info="磁盘使用率:$disk_usage"

# 监控网络连接数
# net_connections=$(netstat -an | grep 1883 | grep ESTABLISHED | wc -l)
net_connections=$(netstat -an | grep ESTABLISHED | wc -l)
net_info="网络连接数:$net_connections"

# 将监控结果输出到日志文件中
echo "**硬件资源**" >> "${write_file}"
echo '```' >> "${write_file}"
echo "$time $cpu_info $mem_info $disk_info $net_info" >> "$write_file"
echo '```' >> "${write_file}"
13 changes: 13 additions & 0 deletions 5.DOS/print_debug_info.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

write_file=$1
if [ -z "$write_file" ]; then
write_file="/dev/tty"
fi

current_path=$(dirname "$0")
# echo "当前文件的执行路径是: $current_path"

${current_path}/echo_proc $write_file
${current_path}/echo_unix $write_file
${current_path}/echo_ppid $write_file
File renamed without changes.
File renamed without changes.
File renamed without changes.
148 changes: 148 additions & 0 deletions 5.DOS/zdebug
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#!/bin/sh
## Readme ##
# * 用于调试xx

## 全局
OTHER_OPER=

## 杀死进程
dt4_killall_proc()
{
## 杀死看门狗
PGREP_RESULT=$(pgrep -f "reboot_server.sh")
if [ -n "$PGREP_RESULT" ]; then
kill $PGREP_RESULT
echo "All matching processes terminated."
fi

## 杀死主程序
PGREP_RESULT=$(pgrep -f "ditoo")
if [ -n "$PGREP_RESULT" ]; then
kill $PGREP_RESULT
# killall ditoo
echo "All matching processes terminated."
fi
}

## 测试主线程维护
dt4_wdogs()
{
while true
do
pgrep_ret=$(pgrep -f "ditoo main")
if [ -z "$pgrep_ret" ]; then
echo restart main
/mnt/nandflash/DT4/ditoo main >> /dev/null 2>&1 &
fi

pgrep_ret=$(pgrep -f "ditoo report")
if [ -z "$pgrep_ret" ]; then
echo restart report
/mnt/nandflash/DT4/ditoo report >> /dev/null 2>&1 &
fi

sleep 1
done
}

show_usage()
{
echo "Usage: $0 [-h] [-c core] [-o opt]"
echo "Options:"
echo " -h, --help Display help message"
# echo " -c core Set target platform architecture(e320, x100, x300, x86)"
echo " -o opt Set operation mode:"
echo " wdog: 运行看门狗"
echo " wdogs: 运行 main && report 看门狗"
echo " kill: 杀死所有进程"
echo " print: 打印调试信息"

exit 0
}

perform_operation() {
echo "-- run $OTHER_OPER"
case "$OTHER_OPER" in
wdog)
/etc/reboot_server.sh
;;
wdogs)
dt4_killall_proc
dt4_wdogs
;;
kill)
dt4_killall_proc
;;
print)
/mnt/nandflash/DT4/tools/print_debug_info.sh
;;
*)
echo "Unknown operation: $OTHER_OPER"
exit 1
;;
esac

exit 0
}

process_options()
{
# 解析命令行参数
while getopts ":hc:o:-:" opt; do
case $opt in
h)
show_usage
;;
c)
# 处理 -c 选项的参数
CORE_APP="$OPTARG"
;;
o)
# 处理 -o 选项的参数
OTHER_OPER="$OPTARG"
perform_operation
;;
-)
case "${OPTARG}" in
help)
# 处理 --help 选项
show_usage
;;
*)
# 其他选项
;;
esac
;;
\?)
# 处理无效选项
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
# 处理缺少参数的选项
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
}

## main ##
RUNDIR=$(pwd)

process_options "$@"

# 配置makefile依赖
if [ "$CORE_APP" == "e320" ];then
COMPILER_PREFIX=arm-openwrt-linux-gnueabi-
# CLOUD_DIR="e320"
elif [ "$CORE_APP" == "x100" ];then
COMPILER_PREFIX=arm-openwrt-linux-muslgnueabi-
elif [ "$CORE_APP" == "x300" ];then
COMPILER_PREFIX=arm-openwrt-linux-muslgnueabi-
elif [ "$CORE_APP" == "x86" ];then
COMPILER_PREFIX=""
fi


echo -e "\033[0;32m--debug done\033[0m"

0 comments on commit be20ed4

Please sign in to comment.