AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 102745
Accepted
rmwetmore
rmwetmore
Asked: 2010-01-15 08:48:07 +0800 CST2010-01-15 08:48:07 +0800 CST 2010-01-15 08:48:07 +0800 CST

在 Windows Server 2008 上设置磁盘空间不足警报

  • 772

我想知道当任何逻辑磁盘分区空间不足时,是否有一种简单的方法可以在 Windows Server 2008 上触发电子邮件警报。由于 DB 日志文件,我有 2 个 SQL 服务器接近耗尽磁盘空间。

谢谢,瑞安

windows-server-2008 sql-server hard-drive disk-space-utilization alerts
  • 7 7 个回答
  • 90260 Views

7 个回答

  • Voted
  1. Best Answer
    Mark Pettibone
    2010-10-16T07:01:57+08:002010-10-16T07:01:57+08:00

    让 Windows Server 2008 发送磁盘空间不足电子邮件警报的一种简单方法是使用任务计划程序和系统日志。如果可用空间低于 HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\ DiskSpaceThreshold中指定的百分比,系统日志中会记录一个事件,该事件可以触发任务发送电子邮件消息。

    1. 打开任务计划程序并创建一个新任务。
    2. 输入任务名称,选择“无论用户是否登录都运行”,并选中“不存储密码”。
    3. 在触发器选项卡上添加一个新触发器。
    4. 在“开始任务”框中选择“在事件上”。
    5. 将日志设置为“系统”,将源设置为“srv”,将事件 ID 设置为“2013”​​。
    6. 在“操作”选项卡上添加新操作。
    7. 将操作设置为“发送电子邮件”并正确填写其余设置。
    8. 要配置系统日志中记录磁盘空间不足事件的时间,请打开注册表编辑器,导航到 HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters 并添加一个名为“DiskSpaceThreshold”的 DWORD 值,将其设置为所需的百分比。当条目不存在时,默认值为 10。
    • 39
  2. David Mackintosh
    2010-01-15T08:51:38+08:002010-01-15T08:51:38+08:00

    我通过 snmp 向我的(单独的)nagios 实例添加了磁盘空间监控。

    • 1
  3. sky100
    2010-01-19T08:26:35+08:002010-01-19T08:26:35+08:00

    为什么不每天运行一个 powershell 脚本作为计划任务呢?如果脚本发现磁盘的可用空间低于 10%,它会向您发送电子邮件或通知。

    这是检查磁盘可用空间的示例代码:

    获取内容 ForEach-Object { $ ; Get-WMIObject –计算机名 $ Win32_LogicalDisk -filter "DriveType=3" | ForEach-Object { $ .DeviceID; $ .FreeSpace/1GB } }

    • 1
  4. Frank Heimes
    2017-06-28T23:29:18+08:002017-06-28T23:29:18+08:00

    由于 PowerShell 语法不正确,这两个示例都不起作用。以下代码列出了当前主机的卷大小(使用 PowerShell 5.0):

    Get-WmiObject win32_logicalDisk -filter "DriveType=3" | %{ $_.DeviceID; $_.FreeSpace/1GB }
    

    以下代码列出了server.txt中列出的主机的卷大小:

    Get-Content server.txt | %{ Get-WMIObject –computername $_ Win32_LogicalDisk -filter "DriveType=3" | %{ $_.DeviceID; $_.FreeSpace/1GB } }
    

    边注

    请注意,外部占位符$_枚举服务器地址,而内部占位符$_枚举设备。这是 PowerShell 新手经常遇到的问题。如果您想在内循环中使用服务器地址,则必须将其分配给外循环中的新变量。

    这里使用的论坛软件有缺陷。在发布预览中,即使没有转义为代码,它也能$_正确显示。$_但是最后一篇文章删除了下划线,从而使 PowerShell 示例不正确。

    • 1
  5. Sameeksha Kumari
    2018-05-25T22:40:12+08:002018-05-25T22:40:12+08:00

    您可以使用此脚本通过电子邮件服务器发送电子邮件。只需将 smtp 服务器名称替换为您的服务器名称即可。如果在同一台机器上,则使用“localhost”(smtp 服务器必须正常运行)。该脚本也可以在这里找到:https ://gallery.technet.microsoft.com/scriptcenter/Disk-Space-Report-Reports-98e64d65

    脚本保存在本地驱动器后,可以使用powershell轻松运行并进行测试。一旦脚本似乎工作正常,则可以使用 Windows 任务调度程序根据要求将其安排为每天或每小时运行一次。本文介绍如何使用任务计划程序运行脚本。 https://www.metalogix.com/help/Content%20Matrix%20Console/SharePoint%20Edition/002_HowTo/004_SharePointActions/012_SchedulingPowerShell.htm

    ############################################################################# 
    #                                                                                                                                                     # 
    #  Check disk space and send an HTML report as the body of an email.                                                   # 
    #  Reports only disks on computers that have low disk space.                                                                 # 
    #  Author: Mike Carmody                                                                                                                   # 
    #  Some ideas extracted from Thiyagu's Exchange DiskspaceHTMLReport module.                                  # 
    #  Date: 8/10/2011                                                          # 
    #  I have not added any error checking into this script yet.                # 
    #                                                                           # 
    #                                                                           # 
    ############################################################################# 
    # Continue even if there are errors 
    $ErrorActionPreference = "Continue"; 
    
    ######################################################################################### 
    # Items to change to make it work for you. 
    # 
    # EMAIL PROPERTIES 
    #  - the $users that this report will be sent to. 
    #  - near the end of the script the smtpserver, From and Subject. 
    
    # REPORT PROPERTIES 
    #  - you can edit the report path and report name of the html file that is the report.  
    ######################################################################################### 
    
    # Set your warning and critical thresholds 
    $percentWarning = 15; 
    $percentCritcal = 10; 
    
    # EMAIL PROPERTIES 
     # Set the recipients of the report. 
      $users = "[email protected]" 
        #$users = "[email protected]" # I use this for testing by uing my email address. 
      #$users = "[email protected]", "[email protected]", "[email protected]";  # can be sent to individuals. 
    
    
    # REPORT PROPERTIES 
     # Path to the report 
      $reportPath = "D:\Jobs\DiskSpaceQuery\Reports\"; 
    
     # Report name 
      $reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html"; 
    
    # Path and Report name together 
    $diskReport = $reportPath + $reportName 
    
    #Set colors for table cell backgrounds 
    $redColor = "#FF0000" 
    $orangeColor = "#FBB917" 
    $whiteColor = "#FFFFFF" 
    
    # Count if any computers have low disk space.  Do not send report if less than 1. 
    $i = 0; 
    
    # Get computer list to check disk space 
    $computers = Get-Content "servers_c.txt"; 
    $datetime = Get-Date -Format "MM-dd-yyyy_HHmmss"; 
    
    # Remove the report if it has already been run today so it does not append to the existing report 
    If (Test-Path $diskReport) 
        { 
            Remove-Item $diskReport 
        } 
    
    # Cleanup old files.. 
    $Daysback = "-7" 
    $CurrentDate = Get-Date; 
    $DateToDelete = $CurrentDate.AddDays($Daysback); 
    Get-ChildItem $reportPath | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item; 
    
    # Create and write HTML Header of report 
    $titleDate = get-date -uformat "%m-%d-%Y - %A" 
    $header = " 
      <html> 
      <head> 
      <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'> 
      <title>DiskSpace Report</title> 
      <STYLE TYPE='text/css'> 
      <!-- 
      td { 
       font-family: Tahoma; 
       font-size: 11px; 
       border-top: 1px solid #999999; 
       border-right: 1px solid #999999; 
       border-bottom: 1px solid #999999; 
       border-left: 1px solid #999999; 
       padding-top: 0px; 
       padding-right: 0px; 
       padding-bottom: 0px; 
       padding-left: 0px; 
      } 
      body { 
       margin-left: 5px; 
       margin-top: 5px; 
       margin-right: 0px; 
       margin-bottom: 10px; 
       table { 
       border: thin solid #000000; 
      } 
      --> 
      </style> 
      </head> 
      <body> 
      <table width='100%'> 
      <tr bgcolor='#CCCCCC'> 
      <td colspan='7' height='25' align='center'> 
      <font face='tahoma' color='#003399' size='4'><strong>AEM Environment DiskSpace Report for $titledate</strong></font> 
      </td> 
      </tr> 
      </table> 
    " 
     Add-Content $diskReport $header 
    
    # Create and write Table header for report 
     $tableHeader = " 
     <table width='100%'><tbody> 
     <tr bgcolor=#CCCCCC> 
        <td width='10%' align='center'>Server</td> 
     <td width='5%' align='center'>Drive</td> 
     <td width='15%' align='center'>Drive Label</td> 
     <td width='10%' align='center'>Total Capacity(GB)</td> 
     <td width='10%' align='center'>Used Capacity(GB)</td> 
     <td width='10%' align='center'>Free Space(GB)</td> 
     <td width='5%' align='center'>Freespace %</td> 
     </tr> 
    " 
    Add-Content $diskReport $tableHeader 
    
    # Start processing disk space reports against a list of servers 
      foreach($computer in $computers) 
     {  
     $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3" 
     $computer = $computer.toupper() 
      foreach($disk in $disks) 
     {         
      $deviceID = $disk.DeviceID; 
            $volName = $disk.VolumeName; 
      [float]$size = $disk.Size; 
      [float]$freespace = $disk.FreeSpace;  
      $percentFree = [Math]::Round(($freespace / $size) * 100, 2); 
      $sizeGB = [Math]::Round($size / 1073741824, 2); 
      $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2); 
            $usedSpaceGB = $sizeGB - $freeSpaceGB; 
            $color = $whiteColor; 
    
    # Set background color to Orange if just a warning 
     if($percentFree -lt $percentWarning)       
      { 
        $color = $orangeColor  
    
    # Set background color to Orange if space is Critical 
          if($percentFree -lt $percentCritcal) 
            { 
            $color = $redColor 
           }         
    
     # Create table data rows  
        $dataRow = " 
      <tr> 
            <td width='10%'>$computer</td> 
      <td width='5%' align='center'>$deviceID</td> 
      <td width='15%' >$volName</td> 
      <td width='10%' align='center'>$sizeGB</td> 
      <td width='10%' align='center'>$usedSpaceGB</td> 
      <td width='10%' align='center'>$freeSpaceGB</td> 
      <td width='5%' bgcolor=`'$color`' align='center'>$percentFree</td> 
      </tr> 
    " 
    Add-Content $diskReport $dataRow; 
    Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree"; 
        $i++   
      } 
     } 
    } 
    
    # Create table at end of report showing legend of colors for the critical and warning 
     $tableDescription = " 
     </table><br><table width='20%'> 
     <tr bgcolor='White'> 
        <td width='10%' align='center' bgcolor='#FBB917'>Warning less than 15% free space</td> 
     <td width='10%' align='center' bgcolor='#FF0000'>Critical less than 10% free space</td> 
     </tr> 
    " 
      Add-Content $diskReport $tableDescription 
     Add-Content $diskReport "</body></html>" 
    
    # Send Notification if alert $i is greater then 0 
    if ($i -gt 0) 
    { 
        foreach ($user in $users) 
    { 
            Write-Host "Sending Email notification to $user" 
    
      $smtpServer = "MySMTPServer" 
      $smtp = New-Object Net.Mail.SmtpClient($smtpServer) 
      $msg = New-Object Net.Mail.MailMessage 
      $msg.To.Add($user) 
            $msg.From = "[email protected]" 
      $msg.Subject = "Environment DiskSpace Report for $titledate" 
            $msg.IsBodyHTML = $true 
            $msg.Body = get-content $diskReport 
      $smtp.Send($msg) 
            $body = "" 
        } 
      } 
    
    • 0
  6. Luis Guzman
    2016-11-24T16:47:15+08:002016-11-24T16:47:15+08:00

    我已经修复了脚本。只需创建一个名为 server.txt 的文本文件并包含 IP 地址或服务器名称,然后您就可以执行以下脚本

    获取内容 server.txt | foreach-object{Get-WmiObject -ComputerName 192.168.22.208 win32_logicalDisk -filter "DriveType=3"|ForEach-Object{$ .DeviceID; $ .FreeSpace/1GB}}

    问候,路易斯。

    • -1
  7. Tilak Pawar
    2017-09-06T06:00:09+08:002017-09-06T06:00:09+08:00

    获取内容 server.txt | foreach-object{Get-WmiObject -ComputerName xx.xx.xx.xx win32_logicalDisk -filter "DriveType=3" | forEach-Object{$ .DeviceID; $ .FreeSpace/1GB}}

    • -1

相关问题

  • 是否可以在单个 W2008 服务器上安装 Exchange Server?

  • Vanilla Powershell 是否足以成为 Windows 和 DB 服务器管理员的语言?

  • 为什么添加新驱动器后我的磁盘驱动器访问速度如此之慢?

  • 在 SQL Server 中,何时应将 PRIMARY Data FileGroup 拆分为辅助数据文件?

  • 如何从 SQL Server 2008 中的备份中排除索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve