Lo más de la historia: el script de inspección de servidor más simple

Tabla de contenido

          Prefacio

          Código fuente del script

          llevado a cabo

          para resumir

 

Prefacio

         En nuestro trabajo diario de operación y mantenimiento, es indispensable mantener el servidor, y para verificar el servidor regularmente, necesitamos verificarlo desde múltiples aspectos. Si la empresa tiene menos servidores, podemos iniciar sesión en los servidores uno por uno para verificar los servidores ¿Qué pasa cuando hay más servidores? En este momento, necesitamos automatizar la inspección, escribir algunos scripts o seleccionar herramientas. Las siguientes son algunas inspecciones simples que se usan comúnmente.

Código fuente del script

[root@aliyun scripts]# vim 巡检.sh 
echo "系统巡检脚本:Version `date +%F`"

echo -e "\033[33m*******************************************************系统检查 *******************************************************\033[0m"
echo "系统:`uname -a | awk '{print $NF}'`"
echo "发行版本:`cat /etc/redhat-release`"
echo "内核:`uname -r`"
echo "主机名:`hostname`"
echo "SELinux:`/usr/sbin/sestatus | grep 'SELinux status:' | awk '{print $3}'`"
echo "语言/编码:`echo $LANG`"
echo "当前时间:`date +%F_%T`"
echo "最后启动:`who -b | awk '{print $3,$4}'`"
echo "运行时间:`uptime | awk '{print $3}' | sed 's/,//g'`"

echo -e "\033[33m*******************************************************CPU检查 *******************************************************\033[0m"
echo "物理CPU个数: `cat /proc/cpuinfo | grep "physical id" | awk '{print $4}' | sort | uniq | wc -l`"
echo "逻辑CPU个数: `cat /proc/cpuinfo | grep "processor" | awk '{print $3}' | sort | uniq | wc -l`"
echo "每CPU核心数: `cat /proc/cpuinfo | grep "cores" | awk '{print $4}'`"
echo "CPU型号: `cat /proc/cpuinfo | grep "model name" | awk -F":" '{print $2}'`"
echo "CPU架构: `uname -m`"
echo -e "\033[33m*******************************************************内存检查 *******************************************************\033[0m"
echo "总共内存:`free -mh | awk "NR==2"| awk '{print $2}'`"
echo "使用内存:`free -mh | awk "NR==2"| awk '{print $3}'` "
echo "剩余内存: `free -mh | awk "NR==2"| awk '{print $4}'`"
echo -e "\033[33m*******************************************************硬盘检查 *******************************************************\033[0m"
echo "总共磁盘大小:`df -hT | awk "NR==2"|awk '{print $3}'`"


echo -e "\033[33m*******************************************************网络检查 *******************************************************\033[0m"
echo `ip a | grep eno | awk "NR==2" | awk '{print $NF,":",$2}'`
echo "网关:`ip route | awk 'NR==1'| awk '{print $3}'`"
echo "DNS: `cat /etc/resolv.conf | grep "nameserver" | awk '{print $2}'`"

ping -c 4 www.baidu.com > /dev/null
if [ $? -eq 0 ];then
    echo "网络连接:正常"
else
    echo "网络连接:失败"
fi
echo -e "\033[33m*******************************************************安全检查 *******************************************************\033[0m"
echo "登陆用户信息:`last | grep "still logged in" | awk '{print $1}'| sort | uniq`"
md5sum -c --quiet /etc/passwd > /dev/null 2&>1
if [ $? -eq 0 ];then
    echo "文件未被串改"
else
    echo "文件被串改"
fi

llevado a cabo

[root@aliyun scripts]# sh 巡检.sh 
系统巡检脚本:Version 2020-10-17
*******************************************************系统检查 *******************************************************
系统:GNU/Linux
发行版本:CentOS Linux release 7.7.1908 (Core)
内核:3.10.0-1062.18.1.el7.x86_64
主机名:aliyun
SELinux:disabled
语言/编码:en_US.UTF-8
当前时间:2020-10-17_10:21:24
最后启动:2020-09-14 20:00
运行时间:32
*******************************************************CPU检查 *******************************************************
物理CPU个数: 1
逻辑CPU个数: 2
每CPU核心数: 1
1
CPU型号:  Intel(R) Xeon(R) Platinum 8269CY CPU @ 2.50GHz
 Intel(R) Xeon(R) Platinum 8269CY CPU @ 2.50GHz
CPU架构: x86_64
*******************************************************内存检查 *******************************************************
总共内存:3.7G
使用内存:739M 
剩余内存: 155M
*******************************************************硬盘检查 *******************************************************
总共磁盘大小:1.9G
*******************************************************网络检查 *******************************************************

网关:172.28.191.253
DNS: 100.100.2.136
100.100.2.138
网络连接:正常
*******************************************************安全检查 *******************************************************
登陆用户信息:root
文件被串改

para resumir

          Aquí uso mi servidor Alibaba Cloud para simular la inspección. Si hay varios servidores, puede usar Ansible para distribuir la inspección y usar el módulo de secuencia de comandos para la distribución por lotes.

Supongo que te gusta

Origin blog.csdn.net/yeyslspi59/article/details/109128906
Recomendado
Clasificación