脚本说明
下面是我经常使用的服务器集群自检测脚本,用于每次重启自动检测每个节点服务的启动情况。
脚本实现的功能:
每次服务器启动,自动检测每个节点的服务启动情况,如果服务没启动,脚本会自动启动该服务。如果启动失败,会邮件告知管理员。
能大大减轻管理员巡检的工作量。
以下是脚本:
#!/bin/bash
# 脚本名称:server_check.sh
# 脚本功能:自动检查服务器集群中各服务器的服务运行情况,检测到自启动失败的服务时尝试重新启动,并记录日志。如果日志中存在启动失败的服务,会发送邮件通知管理员。
# 作者:北国大人 from CSDN
# 创建时间:2023年10月12日
# 最后修改时间:2025年3月8日
# 使用方法:在登录服务器上运行此脚本,确保登录服务器已设置为SSH免密登录
# 参数说明:无
# 依赖项:postfix、systemd
# 日志记录:日志文件以当前时间命名,存储在 /var/log/ 目录下
# 返回值:无
# 注意事项:确保脚本中的服务器列表和需要检查的服务列表根据实际情况进行配置
# 定义服务器列表,将“”中的服务器名修改成你自己的,必须设置免密登录。
SERVERS=("hpc01" "hpc02" "hpc03" "hpc04" "hpc05" "hpc06" "hpc07" "hpc08" "hpc09" "hpc10" \
"hpc11" "hpc12" "hpc13" "hpc14" "hpc15" "hpc16" "hpc17" "hpc18" "hpc19" "hpc20")
# 定义需要检查的服务列表
SERVICES=("zabbix-agent" "ntpd" "lsfd")
# 定义日志文件路径前缀
LOG_FILE_PREFIX="/var/log/server_check/server_check_"
# 获取当前时间戳
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
# 生成日志文件名
LOG_FILE="${LOG_FILE_PREFIX}${TIMESTAMP}.log"
# 定义管理员邮箱(默认你已经部署了postfix等邮箱系统)(如果没有部署,可看我主页)
ADMIN_EMAIL="admin@email.capitel.com"
# 定义检查服务的函数
check_service() {
local server=$1
local service=$2
local status
# 检查服务状态
status=$(ssh "$server" "systemctl is-active $service")
if [ "$status" = "active" ]; then
echo "Service $service on $server is running."
return 0
else
echo "Service $service on $server is not running."
return 1
fi
}
# 定义重启服务的函数
restart_service() {
local server=$1
local service=$2
echo "Attempting to restart service $service on $server..."
if ssh "$server" "systemctl restart $service"; then
echo "Service $service on $server restarted successfully."
return 0
else
echo "Failed to restart service $service on $server."
return 1
fi
}
# 定义检查服务器的函数
check_server() {
local server=$1
local failed_services=()
echo "Checking server $server..."
for service in "${SERVICES[@]}"; do
if ! check_service "$server" "$service"; then
if ! restart_service "$server" "$service"; then
failed_services+=("$service")
fi
fi
done
if [ ${#failed_services[@]} -eq 0 ]; then
echo "All services on $server are running."
else
echo "Services ${failed_services[*]} on $server failed to restart."
fi
}
# 定义发送邮件通知的函数
send_email_notification() {
local log_file=$1
local admin_email=$2
# 检查日志文件中是否有服务启动失败的记录
if grep -q "failed to restart" "$log_file"; then
# 构造邮件内容
local subject="Server Check Notification - $(date +%Y%m%d_%H%M%S)"
local body="Dear Administrator,
The server check has completed. Some services failed to restart. Please review the log file for details.
Log File: $log_file
Regards,
Server Check Script"
# 发送邮件
echo "$body" | mail -s "$subject" "$admin_email"
echo "Email notification sent to $admin_email."
else
echo "No service failures detected. No email notification sent."
fi
}
# 主函数
main() {
echo "Starting server check at $(date)" | tee -a "$LOG_FILE"
for server in "${SERVERS[@]}"; do
echo "Checking server $server..." | tee -a "$LOG_FILE"
check_server "$server" | tee -a "$LOG_FILE"
done
echo "Server check completed at $(date)" | tee -a "$LOG_FILE"
# 检查日志文件并发送邮件通知
send_email_notification "$LOG_FILE" "$ADMIN_EMAIL"
}
# 执行主函数
main
脚本说明
模块化设计:
- check_service:检查单个服务是否运行。
- restart_service:尝试重启失败的服务。
- check_server:检查单个服务器上的所有服务。
- main:主函数,遍历所有服务器并调用检查函数。
可扩展性:
- 可以通过修改 SERVICES 数组轻松添加新的服务。
- 可以通过修改 SERVERS 数组轻松添加新的服务器。
日志记录:
- 脚本会将检查结果记录到 /var/log/server_check/server_check_****.log 文件中,方便后续分析。
错误处理:
- 如果服务检查失败,会尝试重启服务。
- 启动失败会邮件告知
邮件发送逻辑:
- 使用 grep 命令检查日志文件中是否有 “failed to restart” 的记录。
- 如果有失败记录,构造邮件内容并使用 mail 命令发送邮件。
- 邮件主题包含当前时间戳,邮件正文包含简单的通知信息和日志文件路径。
注意事项
1、必须设置免密登录
2、需部署postfix邮箱系统
创建启动服务(也可以直接运行脚步,我就是)
创建一个 systemd 服务文件,用于开机自启(这个是从我的笔记里贴过来的,没尝试)
创建一个服务文件 /etc/systemd/system/server_check.service,内容如下:
[Unit]
Description=Server Check Script
After=network.target
[Service]
Type=oneshot
ExecStart=/opt/server_check.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
创建一个 systemd 定时器文件
创建一个定时器文件 /etc/systemd/system/server_check.timer,内容如下:
[Unit]
Description=Run Server Check Script at Boot
[Timer]
Unit=server_check.service
OnBootSec=2min
[Install]
WantedBy=timers.target
说明:
OnBootSec=2min 表示在系统启动后2分钟触发服务。
Type=oneshot 表示服务只运行一次。
RemainAfterExit=yes 表示服务运行完成后保持状态为 “active”。
使用方法
1、确保你的系统已安装并配置好 postfix,并且 mail 命令可用。如果以管理员身份运行,邮件会以root的身份发送。
使用前,可以使用 echo “Test email body” | mail -s “Test Subject” admin@email.capitel.com 进行测试
2、启动服务
sudo systemctl daemon-reload
sudo systemctl enable server_check.timer
systemctl status server_check.timer