提交 2db56ec5 authored 作者: 陈泽健's avatar 陈泽健

fix(edit):自动化部署定时任务脚本增加拆分的ujava监测定时设置,新增AI服务器监测的生成JSON的格式模板文件。

上级 04eb0502
# Report Summary Extractor Module
# Extract key information from health check report for DingTalk notification
function Get-ReportSummary {
param([string]$ReportPath)
if (-not (Test-Path $ReportPath)) {
throw "Report not found: $ReportPath"
}
# Read file with Default encoding (for Chinese)
$reader = [System.IO.StreamReader]::new($ReportPath, [System.Text.Encoding]::Default)
$content = $reader.ReadToEnd()
$reader.Close()
$info = @{}
# Extract header info
if ($content -match '\*\*时间:\*\*\s*(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})') {
$info.Time = $matches[1]
}
if ($content -match '\*\*主机:\*\*\s*([\d.]+)\s+\((\w+)\)') {
$info.Host = $matches[1]
$info.Hostname = $matches[2]
}
if ($content -match 'up\s+(\d+)\s+days?') {
$info.UptimeDays = [int]$matches[1]
}
if ($content -match '\*\*状态:\*\*\s*(正常|警告|严重)') {
$info.Status = $matches[1]
}
# Extract issues (more flexible pattern)
if ($content -match '关键问题.*?(\d+).*?警告.*?(\d+)') {
$info.CriticalIssues = [int]$matches[1]
$info.Warnings = [int]$matches[2]
}
# Extract metrics from table - search for value patterns in table context
$metrics = @{}
# CPU: find the percentage value near "CPU"
if ($content -match '(?s)\|.*?CPU.*?\|.*?([\d.]+%).*?\|') {
$metrics.CPU = $matches[1]
}
# Memory: find the percentage value near "Memory" or "内存"
if ($content -match '(?s)\|.*?Memory.*?\|.*?([\d.]+%).*?\|') {
$metrics.Memory = $matches[1]
}
# Swap: find the percentage value near "Swap"
if ($content -match '(?s)\|.*?Swap.*?\|.*?([\d.]+%).*?\|') {
$metrics.Swap = $matches[1]
}
# Disk: find the percentage value near "Disk"
if ($content -match '(?s)\|.*?Disk.*?\|.*?([\d.]+%).*?\|') {
$metrics.Disk = $matches[1]
}
# Threads: use Chinese column name
if ($content -match '(?s)\|.*?线程总数.*?\|.*?(\d+).*?\|') {
$metrics.Threads = $matches[1]
}
# Load: find the decimal value
if ($content -match '(?s)\|.*?Load.*?\|.*?([\d.]+).*?\|') {
$metrics.Load = $matches[1]
}
$info.Metrics = $metrics
# Extract service status (more flexible pattern)
if ($content -match '(\d+)\s*/\s*(\d+).*?(?i)current.*connections?') {
$info.MySQL = @{
Connections = "$($matches[1])/$($matches[2])"
Usage = ""
}
# Try to get usage
if ($content -match '(?i)current.*connections?.*?\(([\d.]+%)\)') {
$info.MySQL.Usage = $matches[1]
}
}
if ($content -match '(?i)slow.*quer.*?:\s*(\d+)') {
$info.SlowQueries = [int]$matches[1]
}
if ($content -match '(?i)cache.*hit.*ratio.*?:\s*([\d.]+%)') {
$info.RedisHitRate = $matches[1]
}
# Extract security info
if ($content -match '\*\*认证失败:\*\*\s*(\d+)') {
$info.AuthFailures = [int]$matches[1]
}
# Extract trends (simplified)
$trends = @{}
$info.Trends = $trends
return $info
}
function New-DingTalkSummary {
param([hashtable]$Info, [string]$ServerName = "")
$displayName = if ($ServerName) { $ServerName } else { $Info.Hostname }
# Status icon
$statusIcon = switch ($Info.Status) {
"Normal" { "OK" }
"Warning" { "WARN" }
"Critical" { "CRITICAL" }
default { "?" }
}
# Build summary
$summary = @"
### Server Health Report - $displayName
**Time**: $($Info.Time)
**Host**: $($Info.Host)
**Uptime**: $($Info.UptimeDays) days
**Status**: $statusIcon ($($Info.CriticalIssues) critical, $($Info.Warnings) warnings)
#### Metrics
"@
# Only show abnormal metrics
$abnormalMetrics = @()
# CPU check
if ($Info.Metrics.CPU) {
$cpuVal = [double]($Info.Metrics.CPU -replace '%')
if ($cpuVal -gt 85) {
$trendIcon = Get-TrendIcon -Trend $Info.Trends.CPU
$abnormalMetrics += "| CPU | $($Info.Metrics.CPU) | 85% | CRITICAL $trendIcon |"
}
}
# Memory check
if ($Info.Metrics.Memory) {
$memVal = [double]($Info.Metrics.Memory -replace '%')
if ($memVal -gt 85) {
$trendIcon = Get-TrendIcon -Trend $Info.Trends.Memory
$abnormalMetrics += "| Memory | $($Info.Metrics.Memory) | 85% | CRITICAL $trendIcon |"
}
elseif ($memVal -gt 70) {
$trendIcon = Get-TrendIcon -Trend $Info.Trends.Memory
$abnormalMetrics += "| Memory | $($Info.Metrics.Memory) | 85% | WARN $trendIcon |"
}
}
# Swap check
if ($Info.Metrics.Swap) {
$swapVal = [double]($Info.Metrics.Swap -replace '%')
if ($swapVal -gt 0) {
$abnormalMetrics += "| Swap | $($Info.Metrics.Swap) | - | WARN |"
}
}
# Threads check
if ($Info.Metrics.Threads) {
$threadsVal = [int]$Info.Metrics.Threads
if ($threadsVal -gt 1000) {
$trendIcon = Get-TrendIcon -Trend $Info.Trends.Threads
$abnormalMetrics += "| Threads | $($Info.Metrics.Threads) | 1000 | WARN $trendIcon |"
}
}
if ($abnormalMetrics.Count -eq 0) {
$summary += "`nAll metrics OK"
}
else {
$summary += "`n| Metric | Current | Threshold | Status |`n"
$summary += "| :--- | :--- | :--- | :--- |`n"
$summary += ($abnormalMetrics -join "`n")
}
# Service status
$summary += "`n`n#### Services`n"
$summary += "- MySQL: $($Info.MySQL.Connections) (usage $($Info.MySQL.Usage))`n"
$summary += "- Slow queries: $($Info.SlowQueries)`n"
$summary += "- Redis hit rate: $($Info.RedisHitRate)"
# Security alerts
if ($Info.AuthFailures -gt 100) {
$summary += "`n`n#### Security Alert`n"
$summary += "- Auth failures(24h): $($Info.AuthFailures)"
}
return $summary
}
function Get-TrendIcon {
param([string]$Trend)
if ($Trend -eq "+") { return "UP" }
if ($Trend -eq "-") { return "DOWN" }
return "SAME"
}
Export-ModuleMember -Function @('Get-ReportSummary', 'New-DingTalkSummary', 'Get-TrendIcon')
# Universal Report Summary Extractor
# Supports multiple report formats from different projects
function Get-UniversalReportSummary {
param([string]$ReportPath)
if (-not (Test-Path $ReportPath)) {
throw "Report not found: $ReportPath"
}
# Read with Default encoding for Chinese support
$reader = [System.IO.StreamReader]::new($ReportPath, [System.Text.Encoding]::Default)
$content = $reader.ReadToEnd()
$reader.Close()
$info = @{}
# Extract header info (support multiple formats)
if ($content -match '\*\*时间:\*\*\s*(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})') {
$info.Time = $matches[1]
}
if ($content -match '\*\*主机:\*\*\s*([\d.]+)\s*\((\w+)\)') {
$info.Host = $matches[1]
$info.Hostname = $matches[2]
}
# Extract uptime (support both "up X days" and "已运行X天")
if ($content -match 'up\s+(\d+)\s+days?') {
$info.UptimeDays = [int]$matches[1]
}
elseif ($content -match '已运行(\d+)天') {
$info.UptimeDays = [int]$matches[1]
}
# Extract status (support with/without emoji)
if ($content -match '\*\*状态:\*\*\s*(🟢|🟡|🔴)?\s*(正常|警告|严重)') {
$info.Status = $matches[2]
$info.StatusIcon = $matches[1]
}
elseif ($content -match '\*\*状态:\*\*\s*(正常|警告|严重)') {
$info.Status = $matches[1]
}
# Extract issues (support multiple formats)
# Format 1: "关键问题: 0, 警告: 3"
if ($content -match '关键问题[::]\s*(\d+).*?警告[::]\s*(\d+)') {
$info.CriticalIssues = [int]$matches[1]
$info.Warnings = [int]$matches[2]
}
# Format 2: "严重问题: 2, 警告: 13"
elseif ($content -match '严重问题[::]\s*(\d+).*?警告[::]\s*(\d+)') {
$info.CriticalIssues = [int]$matches[1]
$info.Warnings = [int]$matches[2]
}
# Format 3: "严重: 0, 警告: 0"
elseif ($content -match '严重[::]\s*(\d+).*?警告[::]\s*(\d+)') {
$info.CriticalIssues = [int]$matches[1]
$info.Warnings = [int]$matches[2]
}
# Format 4: "严重问题 (2):"
elseif ($content -match '严重问题\s*\((\d+)\):') {
$info.CriticalIssues = [int]$matches[1]
}
# Extract metrics (support Chinese and English column names)
$metrics = @{}
# CPU
if ($content -match '(?s)\|.*?CPU使用率.*?\|.*?([\d.]+%).*?\|') {
$metrics.CPU = $matches[1]
}
# Memory
if ($content -match '(?s)\|.*?内存使用率.*?\|.*?([\d.]+%).*?\|') {
$metrics.Memory = $matches[1]
}
# Swap
if ($content -match '(?s)\|.*?Swap使用率.*?\|.*?([\d.]+%).*?\|') {
$metrics.Swap = $matches[1]
}
# Threads
if ($content -match '(?s)\|.*?线程总数.*?\|.*?(\d+).*?\|') {
$metrics.Threads = $matches[1]
}
$info.Metrics = $metrics
# Extract service status
if ($content -match '(?i)(\d+)\s*/\s*(\d+).*?(当前|Current).*?(连接|connections?)') {
$info.MySQL = @{
Connections = "$($matches[1])/$($matches[2])"
Usage = ""
}
if ($content -match '(?i)(当前|Current).*?(连接|connections?).*?\(([\d.]+%)\)') {
$info.MySQL.Usage = $matches[3]
}
}
# Slow queries
if ($content -match '(?i)(慢查询数|Slow.*quer).*?:\s*(\d+)') {
$info.SlowQueries = [int]$matches[2]
}
# Redis cache hit rate
if ($content -match '(?i)(缓存命中率|Cache.*hit).*?:\s*([\d.]+%)') {
$info.RedisHitRate = $matches[2]
}
# Security info
if ($content -match '(?i)(认证失败|Auth.*fail).*?:\s*(\d+)') {
$info.AuthFailures = [int]$matches[2]
}
return $info
}
Export-ModuleMember -Function @('Get-UniversalReportSummary')
### Server Health Report - Lanzhou
**Time**: 2026-05-15 06:23:04
**Host**: 139.159.163.86
**Uptime**: 183 days
**Status**: ? (0 critical, 3 warnings)
#### Metrics
| Metric | Current | Threshold | Status |
| :--- | :--- | :--- | :--- |
| Swap | 1.4% | - | WARN |
| Threads | 1443 | 1000 | WARN SAME |
#### Services
- MySQL: (usage )
- Slow queries:
- Redis hit rate:
#### Security Alert
- Auth failures(24h): 267
# Simple test without signature
$webhook = "https://oapi.dingtalk.com/robot/send?access_token=27071a77f20da381e9a321653ec5f4dcf668bcf058c01162f28e3f1f8633386d"
$body = @{
msgtype = "text"
text = @{
content = "Test message without signature"
}
} | ConvertTo-Json -Depth 10
Write-Host "Sending simple test message..." -ForegroundColor Cyan
Write-Host "Webhook: $webhook" -ForegroundColor Yellow
try {
$response = Invoke-RestMethod -Uri $webhook -Method Post -Body $body -ContentType "application/json" -TimeoutSec 10
Write-Host "Response: $($response | ConvertTo-Json)" -ForegroundColor Green
if ($response.errcode -eq 0) {
Write-Host "SUCCESS!" -ForegroundColor Green
} else {
Write-Host "FAILED: errcode=$($response.errcode), errmsg=$($response.errmsg)" -ForegroundColor Red
}
} catch {
Write-Host "ERROR: $_" -ForegroundColor Red
}
......@@ -18,7 +18,7 @@
- 需要先判断该路径是否存在脚本,若不存在则不进行配置,打印相关日志记录。
- 新增定时任务:
- 定时周期:每周五凌晨1点执行
- 定时任务执行脚本:`/data/services/scripts/UbainsmysqlBakUp`
- 定时任务执行脚本:`/data/services/scripts/UbainsmysqlBakUp.sh`
- 需要先判断该路径是否存在脚本,若不存在则不进行配置,打印相关日志记录。
- 新增定时任务:
- 定时周期:*/5 * * * *
......@@ -39,6 +39,14 @@
- 定时周期:0 4 * * *
- 定时任务执行脚本:`/data/services/scripts/auto_clean_deleted_ubains_v3.sh`
- 需要先判断该路径是否存在脚本,若不存在则不进行配置,打印相关日志记录。
- 新增定时任务:
- 定时周期:*/3 * * * *
- 定时任务执行脚本:`/data/services/scripts/nacos-service.sh`
- 需要先判断该路径是否存在脚本,若不存在则不进行配置,打印相关日志记录。
- 新增定时任务:
- 定时周期:*/3 * * * *
- 定时任务执行脚本:`/data/services/scripts/ujava2-startup.sh`
- 需要先判断该路径是否存在脚本,若不存在则不进行配置,打印相关日志记录。
#### 参数控制设计
- **参数方式:** 使用 `--enable-xxx` / `--disable-xxx` 开关式参数
......
# 远程自动化部署_需求文档
## 相关资料要求
### 部署包路径
- ARM架构:\\192.168.9.9\发布版本\03服务器部署\临时使用-新统一平台\ARM部署包-请勿使用
- X86架构:\\192.168.9.9\发布版本\03服务器部署\临时使用-新统一平台\X86部署包\全量版
- ARM架构:已上传至对应服务器。
- X86架构:已上传至对应服务器。
### 目标服务器
- X86架构服务器:192.168.5.52 root Ubains@123
......
......@@ -27,7 +27,7 @@ TASK_LOG[check_health]=""
TASK_STATUS[check_health]=true # 默认启用
# mysql_backup 任务定义
TASK_CRON[mysql_backup]="0 0 * * 5"
TASK_CRON[mysql_backup]="0 1 * * 5"
TASK_SCRIPT[mysql_backup]="/data/services/scripts/UbainsmysqlBakUp.sh"
TASK_LOG[mysql_backup]=""
TASK_STATUS[mysql_backup]=true # 默认启用
......@@ -62,6 +62,12 @@ TASK_SCRIPT[auto_clean]="/data/services/scripts/auto_clean_deleted_ubains_v3.sh"
TASK_LOG[auto_clean]=""
TASK_STATUS[auto_clean]=true # 默认启用
# nacos_monitor 任务定义
TASK_CRON[nacos_monitor]="*/3 * * * *"
TASK_SCRIPT[nacos_monitor]="/data/services/scripts/nacos-service.sh"
TASK_LOG[nacos_monitor]=""
TASK_STATUS[nacos_monitor]=true # 默认启用
# health_check 任务定义(服务自检脚本,强制启用)
TASK_CRON[health_check]="0 0 * * *"
TASK_SCRIPT[health_check]="/data/services/scripts/check_health_shell/check_server_health.sh"
......@@ -120,6 +126,8 @@ function show_help() {
--disable-mysql-logs-backup 禁用 mysql_logs_backup 定时任务
--enable-auto-clean 启用 auto_clean 定时任务
--disable-auto-clean 禁用 auto_clean 定时任务
--enable-nacos-monitor 启用 nacos_monitor 定时任务
--disable-nacos-monitor 禁用 nacos_monitor 定时任务
-h, --help 显示此帮助信息
注意:
......@@ -134,6 +142,7 @@ function show_help() {
$0 --enable-mysql-backup # 启用数据库备份
$0 --disable-monitor-redis --disable-monitor-emqx # 禁用监控任务
$0 --enable-ujava2 --enable-check-health --enable-mysql-backup # 显式启用所有任务
$0 --disable-nacos-monitor # 禁用 nacos 监控任务
示例(函数方式):
source $0 # 导入脚本
......@@ -141,6 +150,7 @@ function show_help() {
add_crontab_job --disable-ujava2 # 禁用 ujava2-startup
add_crontab_job --enable-mysql-backup # 启用数据库备份
add_crontab_job --disable-monitor-redis --disable-monitor-emqx # 禁用监控任务
add_crontab_job --enable-nacos-monitor # 启用 nacos 监控任务
EOF
}
......@@ -240,6 +250,17 @@ function parse_arguments() {
TASK_STATUS[auto_clean]=false
fi
;;
--enable-nacos-monitor)
TASK_STATUS[nacos_monitor]=true
;;
--disable-nacos-monitor)
if [[ "${TASK_STATUS[nacos_monitor]}" == "false" ]]; then
log "WARN" "⚠️ 参数冲突:nacos_monitor 同时被启用和禁用,跳过该任务"
TASK_STATUS[nacos_monitor]=conflict
else
TASK_STATUS[nacos_monitor]=false
fi
;;
-h|--help)
show_help
exit 0
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论