ubl007
作者ubl007·2023-09-16 16:36
私有云高级交付(DTA方向)·百度智能云

一个Linux主机巡检脚本

字数 5146阅读 646评论 0赞 3

功能:

对Linux主机的物理资源消耗进行监测和统计,对运行在本机上的服务进程进行监测和统计,列出本机上的账户有效期

使用场景:

在没有 Promethes、zabbix、夜莺等监控工具的条件下,通过 crontab 排程应用本程序完成对主机上的关键信息的采集和输出,一遍分析当前主机的物理资源与业务程序的运行状态。


!/bin/bash

echo "################################################### README INFO ##############################################################"

echo "### Purpose: Carrying Out A Routine Inspection & Monitoring the Program Running-Status and the Consumed Hardware Resource ###"

echo "### Made By: PomanTeng ###"

echo "### E-mail: denggongmengbo@gmail.com ###"

echo "### WeChat: 1807479153 ###"

echo "### Version Identification Number:V2.1.0 ###"

echo "### Procedure Identification Number:20230916 ###"

echo "##############################################################################################################################"

Check the current OS user

[[ $(id -u) -gt 0 ]] && echo "请用root用户执行此脚本!" && exit 1

source /etc/profile

source ~/.bash_profile

echo " "

echo -e "现在时刻 :\\n \\n $(date)"

echo " "

echo " "

echo "当前OS及其依托的基础设备类型 :"

echo " "

cat /etc/release | awk '/release/' | uniq

echo " "

hostnamectl | awk '/Chassis|Virtualization/'

echo " "

Defining the Manufacturer-Variables

manufacturer=$(dmidecode -s system-manufacturer)

productName=$(dmidecode -s system-product-name)

productSN=$(dmidecode -s system-serial-number)

biosVendor=$(dmidecode -s bios-vendor)

biosVersion=$(dmidecode -s bios-version)

cpuSpecification=$(dmidecode -s processor-version | uniq)

Defining Variable "sysversion"

sysversion=$(cat /etc/release | grep "release" | uniq)

Defining the Metric-Variables

cpuTop50=$(ps -aux | head -n 1 | awk -F " " -v OFS=' ' '{print $2,$3,$11}' ; ps -aux | sort -k3nr | head -n 50 | awk -F " " -v OFS=' ' '{print $2,$3,$11}')

memTop50=$(ps -aux | head -n 1 | awk -F " " -v OFS=' ' '{print $2,$4,$11}' ; ps -aux | sort -k4nr | head -n 50 | awk -F " " -v OFS=' ' '{print $2,$4,$11}')

cpuConsumedTotal=ps -aux | head -n 1 | awk -F " " '{print $3}' ; ps -aux | awk '{sum+=$3}END{print sum}'

rootUsed=$(df | awk '$NF=="/" {var=$3}END{print var/1024/1024" GB"}')

rootRemain=$(df | awk '$NF=="/" {var=$4}END{print var/1024/1024" GB"}')

rootPercent=$(df | awk '$NF=="/" {print $5}')

dataUsed=$(df | awk '$NF=="/app" {var=$3}END{print var/1024/1024" GB"}')

dataRemain=$(df | awk '$NF=="/app" {var=$4}END{print var/1024/1024" GB"}')

dataPercent=$(df | awk '$NF=="/app" {print $5}')

bondRX=$(ifconfig | grep -A 8 "bond1" | grep "RX packets")

bondTX=$(ifconfig | grep -A 8 "bond1" | grep "TX packets")

sysLoad=$(top | head -n 5)

vmstatLoad=$(vmstat -wt)

memUsed=$(free -h | awk -F " " '{print $3}' | awk -F " " 'NR==2')

memRemain=$(free -h | awk -F " " '{print $7}' | awk -F " " 'NR==2')

swapUsed=$(free -h | awk -F " " '{print $3}' | awk -F " " 'NR==3')

swapRemain=$(free -h | awk -F " " '{print $4}' | awk -F " " 'NR==3')

localIP=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\\\\/(.*)/, "\\\\\\\\1", "g", $2)}' | awk 'NR==1')

listeningPortProgram=$(netstat -tlunp | awk -F " " 'NR == 1 {next} {print $0}')

portList=$(netstat -tlunp -e | awk -F " " 'NR==1 {next} {print $0}' | awk -F " " '{print $4}' | awk -F ":" '{print $NF}')

netstat -tlunp -e | awk -F " " 'NR==1 {next} {print $0}' | awk -F " " '{print $4}' | awk -F ":" '{print $NF}' | awk -F " " 'NR==1 {next} {print $0}' > ./portList.txt

Defining the Function "get_Port-Program" to get the port and the corresponding program

get_Port_Program(){

cat ./portList.txt | while read lines

do

for var in "$lines"

do

echo "&&&&& Separator BEGIN &&&&&"

echo -e "Current Port is : " $var

echo -e "PID and Program is : " pwdx $(lsof -i:$var | awk '/LISTEN?/' | awk -F " " '{print $2}' | uniq)

echo "&&&&& Separator END &&&&&"

done

done

}

Defining the Function "excute_Main" to get the Routine Inspection-Result

excute_Main(){

cat << EOF

设备制造商:

${manufacturer}

设备供货型号:

${productName}

设备供货序列号:

${productSN}

BIOS制造商:

${biosVendor}

BIOS版本号:

${biosVersion}

板载CPU规格:

${cpuSpecification}

操作系统版本:

${sysversion}

当前主机的 Local IP:

${localIP}

CPU消耗总计:

${cpuConsumedTotal}

CPU消耗前50的程序:

${cpuTop50}

内存消耗前50的程序:

${memTop50}

内存消耗使用量:

${memUsed}

内存消耗剩余量:

${memRemain}

交换空间消耗使用量:

${swapUsed}

交换空间消耗剩余量:

${swapRemain}

根分区的使用量:

${rootUsed}

根分区的剩余量:

${rootRemain}

根分区的使用百分比:

${rootPercent}

数据分区的使用量:

${dataUsed}

数据分区的剩余量:

${dataRemain}

数据分区的使用百分比:

${dataPercent}

Bond接收的数据量:

${bondRX}

Bond发送的数据量:

${bondTX}

操作系统负载:

${sysLoad}

&&&&& Separator &&&&&

${vmstatLoad}

正在监听的服务:

${listeningPortProgram}

EOF

}

Execute Functions

excute_Main

rm -rf ./portList.txt

echo " 本机运行的业务程序 "

echo " "

echo -e "ps -ef | grep "tiance" | grep -v "grep" | awk '{ for(i=1; i&lt;=7; i++){ $i="" }; print NR $0 }'"

echo " "

echo " 本机业务程序的端口状态 "

echo " "

echo -e "$(ss -tlunp | grep "tiance" | grep -v "grep" | awk -F " " '{print NR $5,$7}')"

echo " "

echo " 系统用户失效日期 "

echo " "

cat /etc/passwd | awk -F ":" '{print $1}' | while read line; do echo "$line" && chage -l $line | grep "Password expires" --color=always ;done

echo " "

exit

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

3

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广