Nginx巡检脚本
nginx_check.sh #健康检查的主脚本
start.sh #运行检查脚本并将结果输出到日志文件
result.log #脚本执行后的示例输出结果
脚本内nginx的安装目录如下,如需修改请自行修改脚本第 15 行
#nginx_home=/home/nginx/nginx
可以通过查询环境变量关于nginx的路径来修改
nginx_check.sh
#!/bin/bash ############################################################## # File Name: nginx_check.sh # Version: V1.0 # Author: houtuo # Mail: tuo.hou@rklink.cn # Created Time : 2019-04-16 10:10:30 # Description: nginx health check ############################################################## # 加载操作系统环境 source /etc/profile # 统一使用英文的UTF-8字符集 export LANG=en_US.UTF-8 date_time=`date +%F` nginx_home=/home/nginx/nginx function get_system_status(){ echo "" echo -e "*******************************系统检查*****************************************" if [ -e /etc/sysconfig/i18n ];then default_LANG="$(grep "LANG=" /etc/sysconfig/i18n | grep -v "^#" | awk -F '"' '{print $2}')" else default_LANG=$LANG fi if [ -f /etc/redhat-release ];then Release=$(cat /etc/redhat-release 2>/dev/null) fi if [ -f /etc/SuSE-release ];then Release=$(cat /etc/SuSE-release |head -3|sed 's/,/\n/g'|awk '{{printf"%s ",$0}}' 2>/dev/null) fi Kernel=$(uname -r) OS=$(uname -o) Hostname=$(uname -n) if [ -f /etc/redhat-release ];then SELinux=$(/usr/sbin/sestatus | grep "SELinux status: " | awk '{print $3}') else SELinux="" fi LastReboot=$(who -b | awk '{print $3,$4}') uptime=$(uptime | sed 's/.*up \([^,]*\), .*/\1/') echo " 系统:$OS" echo " 发行版本:$Release" echo " 内核:$Kernel" echo " 主机名:$Hostname" echo " SELinux:$SELinux" echo "语言/编码:$default_LANG" echo " 当前时间:$(date +'%F %T')" echo " 最后启动:$LastReboot" echo " 运行时间:$uptime" } function get_cpu_status(){ echo "" echo -e "*******************************************************CPU检查*******************************************" Physical_CPUs=$(grep "physical id" /proc/cpuinfo| sort | uniq | wc -l) Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l) CPU_Kernels=$(grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}') CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq) CPU_Arch=$(uname -m) echo "物理CPU个数:$Physical_CPUs" echo "逻辑CPU个数:$Virt_CPUs" echo "每CPU核心数:$CPU_Kernels" echo "CPU型号:$CPU_Type" echo "CPU架构:$CPU_Arch" echo "" /usr/bin/vmstat 1 5 } function get_mem_status(){ echo "" echo -e "*******************************************************Memory检查***************************************" /usr/bin/free -m } function get_proc_status(){ echo "" echo -e "*******************************************************进程检查********************************************" if [ $(ps -ef | grep defunct | grep -v grep | wc -l) -ge 1 ];then echo "" echo "僵尸进程"; echo "--------" ps -ef | head -n1 ps -ef | grep defunct | grep -v grep fi echo "" echo "内存占用TOP10" echo "-------------" echo -e "PID %MEM RSS COMMAND $(ps aux | awk '{print $2, $4, $6, $11}' | sort -k3rn | head -n 10 )"| column -t echo "" echo "CPU占用TOP10" echo "------------" top b -n1 | head -17 | tail -11 } function get_disk_status(){ echo "" echo -e "*******************************************************Disk检查********************************************" /bin/df -hiP | sed 's/Mounted on/Mounted/'> ./inode /bin/df -hTP | sed 's/Mounted on/Mounted/'> ./disk /usr/bin/join ./disk ./inode | awk '{print $1,$2,"|",$3,$4,$5,$6,"|",$8,$9,$10,$11,"|",$12}'| column -t [ -f ./disk ] && rm -f ./disk [ -f ./inode ] && rm -f ./inode } function get_ulimit(){ echo "" echo -e "*******************************************************Ulimit检查******************************************" ulimit -a } function get_listener_status(){ echo "" echo -e "*******************************************************监听检查********************************************" TCPListen=$(ss -ntul | column -t) echo "$TCPListen" } function get_kernel_conf(){ echo "" echo -e "*******************************************************Kernel检查*****************************************" for a in vm.swappiness fs.file-max net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_probes net.ipv4.ip_local_port_range do echo "$a = `sysctl -n $a`" done } function get_nginx_version(){ echo "" echo -e "*******************************************************Nginx Version检查*********************************" $nginx_home/sbin/nginx -V 2>&1 } function get_nginx_conf(){ echo "" echo -e "*******************************************************Nginx Config检查**********************************" cat $nginx_home/conf/nginx.conf|grep -Ev "#|^$" \cp -ar $nginx_home/conf ./conf_`hostname` } function main(){ get_system_status get_cpu_status get_mem_status get_proc_status get_disk_status get_listener_status get_ulimit get_kernel_conf get_nginx_version get_nginx_conf } # 调用main函数 main
start.sh
#!/bin/bash ############################################################## # File Name: start.sh # Version: V1.0 # Author: houtuo # Mail: tuo.hou@rklink.cn # Created Time : 2019-04-16 10:10:30 # Description: nginx health check ############################################################## # 加载操作系统环境 source /etc/profile # 统一使用英文的UTF-8字符集 export LANG=en_US.UTF-8 sh nginx_check.sh > result.log
result.log(示例)
*******************************************************系统检查 ****************************************************** 系统:GNU/Linux 发行版本:Red Hat Enterprise Linux release 8.4 (Ootpa) 内核:4.18.0-305.el8.x86_64 主机名:fileserver SELinux:disabled 语言/编码:en_US.UTF-8 当前时间:2022-05-14 14:58:21 最后启动:2022-05-14 14:44 运行时间:14 min *******************************************************CPU检查****************************************************** 物理CPU个数:4 逻辑CPU个数:4 每CPU核心数:1 CPU型号:Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz CPU架构:x86_64 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 5663356 3240 314820 0 0 69 18 126 155 1 2 97 0 0 0 0 0 5663332 3240 314832 0 0 0 29 289 408 0 0 99 0 0 0 0 0 5663332 3240 314832 0 0 0 34 307 438 0 0 100 0 0 0 0 0 5663332 3240 314832 0 0 0 0 258 403 0 0 100 0 0 0 0 0 5663332 3240 314832 0 0 0 0 263 429 0 0 100 0 0 *******************************************************Memory检查*************************************************** total used free shared buff/cache available Mem: 7741 1900 5530 52 310 5549 Swap: 8191 0 8191 *******************************************************进程检查******************************************************* 僵尸进程 -------- UID PID PPID C STIME TTY TIME CMD awx 1653 1494 0 14:44 ? 00:00:01 [python3] <defunct> 内存占用TOP10 ------------- PID %MEM RSS COMMAND 1506 1.6 132756 python3 1494 1.6 131092 python3 1495 1.6 129816 python3 2642 1.5 122864 python3 1656 1.5 122168 python3 1654 1.5 122060 python3 1655 1.5 122020 python3 1648 1.5 120188 python3 1548 1.5 119312 /var/lib/awx/venv/awx/bin/uwsgi 1551 1.5 119312 /var/lib/awx/venv/awx/bin/uwsgi CPU占用TOP10 ------------ PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3280 nginx 20 0 65548 4940 4116 R 6.2 0.1 0:00.01 top 1 root 20 0 183680 11256 8716 S 0.0 0.1 0:02.68 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd 3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp 4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp 6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H-events_highpri 9 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq 10 root 20 0 0 0 0 S 0.0 0.0 0:00.03 ksoftirqd/0 11 root 20 0 0 0 0 I 0.0 0.0 0:00.55 rcu_sched 12 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 *******************************************************Disk检查******************************************************* Filesystem Type | Size Used Avail Use% | Inodes IUsed IFree IUse% | Mounted devtmpfs devtmpfs | 3.8G 0 3.8G 0% | 964K 398 963K 1% | /dev tmpfs tmpfs | 3.8G 196K 3.8G 1% | 968K 44 968K 1% | /dev/shm tmpfs tmpfs | 3.8G 196K 3.8G 1% | 968K 696 968K 1% | /run tmpfs tmpfs | 3.8G 196K 3.8G 1% | 968K 17 968K 1% | /sys/fs/cgroup tmpfs tmpfs | 3.8G 9.0M 3.8G 1% | 968K 44 968K 1% | /dev/shm tmpfs tmpfs | 3.8G 9.0M 3.8G 1% | 968K 696 968K 1% | /run tmpfs tmpfs | 3.8G 9.0M 3.8G 1% | 968K 17 968K 1% | /sys/fs/cgroup tmpfs tmpfs | 3.8G 0 3.8G 0% | 968K 44 968K 1% | /dev/shm tmpfs tmpfs | 3.8G 0 3.8G 0% | 968K 696 968K 1% | /run tmpfs tmpfs | 3.8G 0 3.8G 0% | 968K 17 968K 1% | /sys/fs/cgroup /dev/mapper/vgroot-lvroot xfs | 138G 5.4G 133G 4% | 69M 184K 69M 1% | / /dev/sda1 xfs | 4.0G 216M 3.8G 6% | 2.0M 309 2.0M 1% | /boot /dev/mapper/vgdata-lvdata xfs | 20G 11G 9.5G 53% | 10M 8.0K 10M 1% | /home/nginx tmpfs tmpfs | 775M 0 775M 0% | 968K 6 968K 1% | /run/user/0 *******************************************************监听检查******************************************************* Netid State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess udp UNCONN 0 0 127.0.0.1:323 0.0.0.0:* udp UNCONN 0 0 [::1]:323 [::]:* tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:* tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* tcp LISTEN 0 128 0.0.0.0:5432 0.0.0.0:* tcp LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* tcp LISTEN 0 128 0.0.0.0:443 0.0.0.0:* tcp LISTEN 0 128 [::]:80 [::]:* tcp LISTEN 0 128 [::]:22 [::]:* tcp LISTEN 0 128 [::]:5432 [::]:* tcp LISTEN 0 128 [::1]:6010 [::]:* tcp LISTEN 0 128 [::]:443 [::]:* *******************************************************Ulimit检查***************************************************** core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 30817 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 65535 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 65535 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited *******************************************************Kernel检查**************************************************** vm.swappiness = 10 fs.file-max = 2000000 net.ipv4.tcp_keepalive_time = 300 net.ipv4.tcp_keepalive_probes = 9 net.ipv4.ip_local_port_range = 1024 65535 *******************************************************Nginx Version检查******************************************** nginx version: nginx/1.20.2 built by gcc 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) built with OpenSSL 1.1.1g FIPS 21 Apr 2020 TLS SNI support enabled configure arguments: --prefix=/home/nginx/nginx-1.20.2 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module *******************************************************Nginx Config检查********************************************* worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
这里在一台装了nginx主机上测试结果

© 版权声明
文章版权归作者所有,未经允许请勿转载。