前言
本篇文章基于链接: shell 脚本监听服务器,钉钉机器人推送的基础上修改完善而来,其中还有很多不足的地方,欢迎各位进行指正
一、监控内容
原本的脚本仅仅对磁盘。内存、CPU、服务、数据库进行了监控,原本的脚本并未对参数等属性进行抽离,也因此对多台服务器的复用性较低,我基于对方的代码进行了补充和完善。
其中新增加了对docker和k8s的监控
二、使用步骤
1.创建钉钉机器人
钉钉机器人需要基于群组实现,也就是说,必须要新创建一个群聊,才能创建机器人
1.1创建群聊
找到钉钉的右上角点击+号,选择人员后直接创建即可
1.2创建机器人
先打开刚创建好的群聊
打开之后拉到最下面会发现有个机器人的选项
点击之后可以添加机器人
添加自定义机器人即可
这里有三种安全设置,必须选择其中一种,最简单的方案为选择自定义关键词,可以选择多个
选择自己发送的消息内的关键词即可,例如我的关键词添加为内存 磁盘 CPU这三个关键词,当然也可以添加其他的,一般是没人乱给你发消息的。
勾选我已阅读并同意,然后完成即可,创建完成后可以看到多出来一个webhook
这个就是一个钉钉的地址,后面拼接了一个唯一token
到这一步,钉钉的准备工作就完成了
2.脚本
# 可配置属性值
# 内存占用
$memThreshold = 90
# cpu 占用
$cpuThreshold = 90
# 磁盘占用
$diskThreshold = 90
# 钉钉通知 URL 和 token
$url = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx"
# 循环时间秒数
$circulationTime = 3600
# 需要检测的指定地址的能否联通
$informAddress = @()
# 需要检测的本地端口号是否存在
$informPort = @(3306,6369)
# 需要通知的用户
$informUser = @("17561424136")
# 当前系统的 Ip 地址
$ip = "192.168.xxx.xxx"
# 标题
$title = "windows server服务器监控"
# 当前日期
$time = Get-Date -Format "yyyy-MM-dd"
# 当前时间
$times = Get-Date -Format "HH:mm:ss"
# 处理方法
while ($true) {
# 文本展示的提示
$textInformUser = ""
# at 展示的提示
$atInformUser = "["
$firstElement = $true
# 处理两种类型的数据
foreach ($value in $informUser) {
$textInformUser += "@$value "
if (-not $firstElement) {
$atInformUser += ","
}
$atInformUser += "$value"
$firstElement = $false
}
$atInformUser += "]"
# 错误标识
$sendAlert = $false
# 确认指定的地址是否能够使用
$addressText = ""
foreach ($value in $informAddress) {
try {
$response = Invoke-WebRequest -Uri $value -TimeoutSec 10 -ErrorAction Stop
if ($response.StatusCode -ne 200) {
$sendAlert = $true
$addressText += "**地址$value 状态**: <font color=`"#FF0000`">异常</font>`n`n"
}
}
catch {
$sendAlert = $true
$addressText += "**地址$value 状态**: <font color=`"#FF0000`">异常</font>`n`n"
}
}
# 确认当前地址的端口号是否正常运行
$portText = ""
foreach ($value in $informPort) {
try {
$ports = (Get-NetTCPConnection | Where-Object { $_.LocalPort -eq $value } | Measure-Object).Count -gt 0
if (-not $ports) {
$sendAlert = $true
$portText += "**端口$value 状态**: <font color=`"#FF0000`">异常</font>`n`n"
}
}
catch {
Write-Host "检测端口$value 时出错"
}
}
# 校验 cup,内存,磁盘的占用比
try {
$diskInfo = Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object { $_.DeviceID -eq "C:" }
$lsblk = (($diskInfo.Size - $diskInfo.FreeSpace) / $diskInfo.Size * 100)
$memAvailable = (Get-Counter "\Memory\Available MBytes").CounterSamples.CookedValue
$memCommitted = (Get-Counter "\Memory\Committed Bytes").CounterSamples.CookedValue
$mem = ($memAvailable / ($memCommitted / 1MB)) * 100
$cpuUsage = (Get-Counter "\Processor(_Total)\% Processor Time").CounterSamples.CookedValue
}
catch {
Write-Host "获取资源使用率时出错"
}
# 判断是否需要发送预警
if ($mem -gt $memThreshold -or $cpuUsage -gt $cpuThreshold -or $lsblk -gt $diskThreshold) {
$sendAlert = $true
}
# 构建钉钉消息体
$textBody = @{
"msgtype" = "markdown"
"markdown" = @{
"title" = $title
"text" = "`n`n**监控项**: <font color=`"#0000FF`">$title</font>`n`n**报警时间**: <font color=`"#0000FF`">$time $times</font>`n`n**监控 ip**: <font color=`"#0000FF`">$ip</font>`n`n**报警项**: <font color=`"#FF0000`">服务运行情况</font>`n`n**磁盘空间使用率**: <font color=`"#FF0000`">$lsblk%</font>`n`n**内存使用率**: <font color=`"#FF0000`">$mem%</font>`n`n**CPU 使用率**: <font color=`"#FF0000`">$cpuUsage%</font>`n`n$addressText $portText $textInformUser `n`n> 脚本定时监控."
}
"at" = @{
"isAtAll" = $false
"atMobiles" = $atInformUser
}
} | ConvertTo-Json -Depth 4
# 如果需要发送预警,则构建并发送钉钉消息
if ($sendAlert) {
try {
Invoke-RestMethod -Uri $url -Method Post -ContentType "application/json; charset=utf-8" -Body $textBody
}
catch {
Write-Host "发送钉钉消息失败"
}
}
# 等待 1 小时
Start-Sleep -Seconds $circulationTime
}
总结
具体的实现