邮件报警服务器,Powershell脚本监控服务器并邮件告警

【摘要】

实时掌握服务器运行状态对系统管理员来说非常重要,服务器出现问题是第一时间知道可以保证及时处理,将意外损失降到最小。利用powershell可简单实现监控功能,并在服务器掉线或关键服务停止时发出邮件通知管理员,使管理员可以及时处理故障。

【正文】

一脚本组成

87235bef77548885c4633a10ce8759d6.png

如图,除powershell脚本文件外,需要创建3个txt文件,分别为Servers.txt、Services.txt、Urls.txt。三个文件都是以csv格式导入,第一行内容需要保留,添加内容由第二行开始

Servers.txt添加服务器名用于监控服务器在线情况

71dab27743b66e78983eeedfa911ca44.png

Services.txt添加服务器名及服务名用于监控关键服务运行情况

265a6c7e3c4153febb36dfb541ccb5ff.png

Urls.txt添加网站用于监控网页是否能正常打开

64f5de299a4ac159bfd59fd3945422e7.png

二脚本内容

$Urls = import-csv Urls.txt

$Servers = import-csv Servers.txt

$Services = import-csv Services.txt

$mailcontent = $null

$mailFrom = "XXX@canway.net"

$mailTo = "XXX@canway.net"

$Credential = Get-Credential -Credential $mailFrom

While($true)

{

Foreach ($server in $Servers)

{

$hostname = $server.server

if (!(test-connection $hostname -count 2 -quiet))

{$date = Get-Date

$mailcontent = $mailcontent + "$hostname is offline $date `n"

Write-Host "$hostname is offline $date" -ForegroundColor Red}

}

Foreach ($service in $Services)

{

$Server = $Service.server

$ServiceName = $Service.service

If (!(test-connection $Server -count 2 -quiet))

{$date = Get-Date

$mailcontent = $mailcontent + "$Server is offline $date `n"

Write-Host "$Server is offline $date" -ForegroundColor Red}

Else

{$Servicestate = (Get-WmiObject -Class win32_service -ComputerName $Server|Where-Object{$_.Name -eq $ServiceName}).State

If($Servicestate -ne "Running")

{$date = Get-Date

$mailcontent = $mailcontent + "$ServiceName in $Server is not running! $date `n"

Write-Host "$ServiceName in $Server is not running! $date" -ForegroundColor Red}

}

}

Foreach($url in $Urls)

{

$Website = $url.website

Invoke-WebRequest $Website

If(!$?)

{$date = Get-Date

$mailcontent = $mailcontent + "$Website 无法连接 $date `n"

Write-Host "$Website 无法连接 $date" -ForegroundColor Red}

}

If($mailcontent -ne $null)

{

Send-MailMessage -Body $mailcontent -SmtpServer smtp.partner.outlook.cn -From $mailFrom -To $mailTo -Subject "Warning" -UseSsl -Credential $Credential -Port 587

}

$mailcontent = $null

sleep 3600

}

三脚本使用

用1.2所列出的脚本内容创建powershell脚本,与Servers.txt、Services.txt、Urls.txt放在同一路径下,用有权限的账户运行

当有任意一项检查不通过时,会发邮件通知管理员。如图,修改以下位置可以自定义收件人和发件人。

ffd99d84d7908f90e6014c4ddd70c663.png

运行脚本后会让你填入邮箱密码,之后脚本就会每个小时检查一次,检查不通过时发邮件到设定的收件人,脚本检查间隔可自定义,如图,初始设置为3600s

beb4841414a24f75f86b708e3e0681b6.png

3ca0d12ec5f0a5bf7aae3e82ec032463.png

收到邮件如图

e15be8e49308f8c1e0833477991e04b4.png

选择发件人邮箱后需要在下面的命令里修改Smtp服务器和端口号

Send-MailMessage-Body$mailcontent-SmtpServersmtp.partner.outlook.cn-From$mailFrom-To$mailTo-Subject"Warning"-UseSsl-Credential$Credential-Port587

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是通过存储中的OVF模板创建虚拟机的PowerShell脚本: ```powershell # 定义变量 $ovfFilePath = "C:\path\to\ovf\template.ovf" $vmName = "New Virtual Machine" $datastoreName = "Datastore Name" $clusterName = "Cluster Name" # 连接到vCenter Server Connect-VIServer vCenterServerName -User username -Password password # 获取集群和数据存储 $cluster = Get-Cluster -Name $clusterName $datastore = Get-Datastore -Name $datastoreName -Location $cluster # 部署虚拟机 $ovfConfig = New-Object VMware.Vim.OvfManager $ovfParams = $ovfConfig.ReadOvfConfiguration($ovfFilePath) $ovfParams.NetworkMapping[0].Network = Get-VirtualPortGroup -Name "Virtual Switch Name" $ovfParams.DeploymentOption = $ovfParams.DeploymentOption[0] $ovfConfig.DeployOVF($ovfFilePath, $cluster, $datastore, $vmName, $ovfParams) # 断开与vCenter Server的连接 Disconnect-VIServer vCenterServerName -Confirm:$false ``` 在上面的脚本中,您需要将以下变量替换为适当的值: - `$ovfFilePath`:OVF模板文件的路径 - `$vmName`:要创建的虚拟机的名称 - `$datastoreName`:虚拟机应存储在其中的数据存储的名称 - `$clusterName`:包含数据存储的集群的名称 - `vCenterServerName`:vCenter Server的名称 - `username`:vCenter Server的用户名 - `password`:vCenter Server的密码 - `Virtual Switch Name`:虚拟交换机的名称,它应该与OVF模板中定义的网络名称匹配。 请注意,您需要安装VMware PowerCLI才能运行此脚本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值