Setting Specific Thresholds on Specific Drives

Welcome Forums General PowerShell Q&A Setting Specific Thresholds on Specific Drives

Viewing 0 reply threads
  • Author
    Posts
    • #6022
      Participant
      Topics: 1562
      Replies: 0
      Points: 1
      Rank: Member

      by w00tm3 at 2013-02-12 13:04:38

      Hello. I found a PowerShell script online that does a really nice report for drive drives from an input file. In the future I will make it query all servers using Get-QADComputer -sizelimit 0 -OSName &#39;*server*&#39; -LdapFilter &#39;&#40;!&#40;userAccountControl] but for now it uses an input txt file.  What I don't have a clue about how to do though is set specific thresholds or exclusions on specific drives.  For example, we have some servers that we know have a drive at 99% utilization but its okay because the data is static.  Another drive might be a SQL drive in which the DB is expanded or whatever and we want to exclude that specific drive on that specific server from monitoring.</p><p>I would like to have the script read from a text file to get these exclusions.  For example maybe the file could have something like the following:<br />Server1,D,0      (which would mean don't monitor drive D at all)<br />Server2,E,3      (which would mean only alert at 3% and not the standard set in the script)<br />Server3            (Don't get drive utilization for any drives on Server3)</p><p>Would anyone know how to do this?  Thanks!</p><p> # <br />Continue even if there are errors<br />$ErrorActionPreference = &quot;Continue&quot;;</p><p>#########################################################################################<br />#<br /># EMAIL PROPERTIES<br />#  - the $users that this report will be sent to.<br />#  - near the end of the script the smtpserver, From and Subject.</p><p># REPORT PROPERTIES<br />#  - you can edit the report path and report name of the html file that is the report. <br />#########################################################################################</p><p># Set your warning and critical thresholds<br />$percentWarning = 15;<br />$percentCritcal = 10;</p><p># EMAIL PROPERTIES<br /> # Set the recipients of the report.<br /> $users = &quot;[email protected]&quot;<br />   #$users = &quot;[email protected]&quot; # I use this for testing by using my email address.<br /> #$users = &quot;[email protected]&quot;, &quot;[email protected]&quot;, &quot;[email protected]&quot;;  # can be sent to individuals.</p><p># REPORT PROPERTIES<br /> # Path to the report<br /> $reportPath = &quot;C:\TempDriveSpace\&quot;;</p><p> # Report name<br /> $reportName = &quot;DiskSpaceRpt_$&#40;get-date -format ddMMyyyy&#41;.html&quot;;</p><p># Path and Report name together<br />$diskReport = $reportPath + $reportName</p><p>#Set colors for table cell backgrounds<br />$redColor = &quot;#FF0000&quot;<br />$orangeColor = &quot;#FBB917&quot;<br />$whiteColor = &quot;#FFFFFF&quot;</p><p># Count if any computers have low disk space.  Do not send report if less than 1.<br />$i = 0;</p><p># Get computer list to check disk space<br />$computers = Get-Content &quot;servers_c.txt&quot;;<br />$datetime = Get-Date -Format &quot;MM-dd-yyyy_HHmmss&quot;;</p><p># Remove the report if it has already been run today so it does not append to the existing report<br />If &#40;Test-Path $diskReport&#41;<br />    {<br />        Remove-Item $diskReport<br />    }</p><p># Cleanup old files..<br />$Daysback = &quot;-7&quot;<br />$CurrentDate = Get-Date;<br />$DateToDelete = $CurrentDate.AddDays&#40;$Daysback&#41;;<br />Get-ChildItem $reportPath | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item;</p><p># Create and write HTML Header of report<br />$titleDate = get-date -uformat &quot;%m-%d-%Y - %A&quot;<br />$header = &quot;<br /> &lt;html&gt;<br /> &lt;head&gt;<br /> &lt;meta http-equiv=&#39;Content-Type&#39; content=&#39;text/html; charset=iso-8859-1&#39;&gt;<br /> &lt;title&gt;DiskSpace Report&lt;/title&gt;<br /> &lt;STYLE TYPE=&#39;text/css&#39;&gt;<br /> &lt;!--<br /> td {<br /> font-family: Tahoma;<br /> font-size: 11px;<br /> border-top: 1px solid #999999;<br /> border-right: 1px solid #999999;<br /> border-bottom: 1px solid #999999;<br /> border-left: 1px solid #999999;<br /> padding-top: 0px;<br /> padding-right: 0px;<br /> padding-bottom: 0px;<br /> padding-left: 0px;<br /> }<br /> body {<br /> margin-left: 5px;<br /> margin-top: 5px;<br /> margin-right: 0px;<br /> margin-bottom: 10px;<br /> table {<br /> border: thin solid #000000;<br /> }<br /> --&gt;<br /> &lt;/style&gt;<br /> &lt;/head&gt;<br /> &lt;body&gt;<br /> &lt;table width=&#39;100%&#39;&gt;<br /> &lt;tr bgcolor=&#39;#CCCCCC&#39;&gt;<br /> &lt;td colspan=&#39;7&#39; height=&#39;25&#39; align=&#39;center&#39;&gt;<br /> &lt;font face=&#39;tahoma&#39; color=&#39;#003399&#39; size=&#39;4&#39;&gt;&lt;strong&gt;DiskSpace Report for $titledate&lt;/strong&gt;&lt;/font&gt;<br /> &lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;/table&gt;<br />&quot;<br /> Add-Content $diskReport $header</p><p># Create and write Table header for report<br /> $tableHeader = &quot;<br /> &lt;table width=&#39;100%&#39;&gt;&lt;tbody&gt;<br /> &lt;tr bgcolor=#CCCCCC&gt;<br />    &lt;td width=&#39;10%&#39; align=&#39;center&#39;&gt;Server&lt;/td&gt;<br /> &lt;td width=&#39;5%&#39; align=&#39;center&#39;&gt;Drive&lt;/td&gt;<br /> &lt;td width=&#39;15%&#39; align=&#39;center&#39;&gt;Drive Label&lt;/td&gt;<br /> &lt;td width=&#39;10%&#39; align=&#39;center&#39;&gt;Total Capacity&#40;GB&#41;&lt;/td&gt;<br /> &lt;td width=&#39;10%&#39; align=&#39;center&#39;&gt;Used Capacity&#40;GB&#41;&lt;/td&gt;<br /> &lt;td width=&#39;10%&#39; align=&#39;center&#39;&gt;Free Space&#40;GB&#41;&lt;/td&gt;<br /> &lt;td width=&#39;5%&#39; align=&#39;center&#39;&gt;Freespace %&lt;/td&gt;<br /> &lt;/tr&gt;<br />&quot;<br />Add-Content $diskReport $tableHeader</p><p># Start processing disk space reports against a list of servers<br />  foreach&#40;$computer in $computers&#41;<br /> { <br /> $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter &quot;DriveType = 3&quot;<br /> $computer = $computer.toupper&#40;&#41;<br /> foreach&#40;$disk in $disks&#41;<br /> {        <br /> $deviceID = $disk.DeviceID;<br />        $volName = $disk.VolumeName;<br /> [float]$size = $disk.Size;<br /> [float]$freespace = $disk.FreeSpace; <br /> $percentFree = [Math]::Round&#40;&#40;$freespace / $size&#41; * 100, 2&#41;;<br /> $sizeGB = [Math]::Round&#40;$size / 1073741824, 2&#41;;<br /> $freeSpaceGB = [Math]::Round&#40;$freespace / 1073741824, 2&#41;;<br />        $usedSpaceGB = $sizeGB - $freeSpaceGB;<br />        $color = $whiteColor;</p><p># Set background color to Orange if just a warning<br /> if&#40;$percentFree -lt $percentWarning&#41;      <br /> {<br />    $color = $orangeColor </p><p># Set background color to Orange if space is Critical<br />      if&#40;$percentFree -lt $percentCritcal&#41;<br />        {<br />        $color = $redColor<br />       }        </p><p> # Create table data rows <br />    $dataRow = &quot;<br /> &lt;tr&gt;<br />        &lt;td width=&#39;10%&#39;&gt;$computer&lt;/td&gt;<br /> &lt;td width=&#39;5%&#39; align=&#39;center&#39;&gt;$deviceID&lt;/td&gt;<br /> &lt;td width=&#39;15%&#39; &gt;$volName&lt;/td&gt;<br /> &lt;td width=&#39;10%&#39; align=&#39;center&#39;&gt;$sizeGB&lt;/td&gt;<br /> &lt;td width=&#39;10%&#39; align=&#39;center&#39;&gt;$usedSpaceGB&lt;/td&gt;<br /> &lt;td width=&#39;10%&#39; align=&#39;center&#39;&gt;$freeSpaceGB&lt;/td&gt;<br /> &lt;td width=&#39;5%&#39; bgcolor=`&#39;$color`&#39; align=&#39;center&#39;&gt;$percentFree&lt;/td&gt;<br /> &lt;/tr&gt;<br />&quot;<br />Add-Content $diskReport $dataRow;<br />Write-Host -ForegroundColor DarkYellow &quot;$computer $deviceID percentage free space = $percentFree&quot;;<br />    $i++ <br /> }<br /> }<br />}</p><p># Create table at end of report showing legend of colors for the critical and warning<br /> $tableDescription = &quot;<br /> &lt;/table&gt;&lt;br&gt;&lt;table width=&#39;20%&#39;&gt;<br /> &lt;tr bgcolor=&#39;White&#39;&gt;<br />    &lt;td width=&#39;10%&#39; align=&#39;center&#39; bgcolor=&#39;#FBB917&#39;&gt;Warning less than 15% free space&lt;/td&gt;<br /> &lt;td width=&#39;10%&#39; align=&#39;center&#39; bgcolor=&#39;#FF0000&#39;&gt;Critical less than 10% free space&lt;/td&gt;<br /> &lt;/tr&gt;<br />&quot;<br /> Add-Content $diskReport $tableDescription<br /> Add-Content $diskReport &quot;&lt;/body&gt;&lt;/html&gt;&quot;</p><p># Send Notification if alert $i is greater then 0<br />if &#40;$i -gt 0&#41;<br />{<br />    foreach &#40;$user in $users&#41;<br />{<br />        Write-Host &quot;Sending Email notification to $user&quot;</p><p> $smtpServer = &quot;mail.company.com&quot;<br /> $smtp = New-Object Net.Mail.SmtpClient&#40;$smtpServer&#41;<br /> $msg = New-Object Net.Mail.MailMessage<br /> $msg.To.Add&#40;$user&#41;<br />        $msg.From = &quot;[email protected]&quot;<br /> $msg.Subject = &quot;DiskSpace Report for $titledate&quot;<br />        $msg.IsBodyHTML = $true<br />        $msg.Body = get-content $diskReport<br /> $smtp.Send&#40;$msg&#41;<br />        $body = &quot;&quot;<br />    }<br />  }

      by AlexBrassington at 2013-02-13 04:44:49

      So what you want to do is to read in a csv file and then test to see if the disk has any special handling rules, then modify the behaviour as required.

      Try this. Note that i haven’t tested it, I need to grab lunch.
      #To import the CSV<br />#Path to the Special Handling rules csv file<br />$csvPath = &quot;&quot;<br />#get array of disks and special handling rules<br />$specialHandlingRules = Import-Csv $csvPath</p><p>#To be inserted in the for-each disk loop<br />   #Check for special handling instructions for this server and disk<br />      $specialRules = $specialHandlingRules | where {$_.Server -eq $computer -and $_.Disk -eq $disk.DeviceID}<br />  #set a flag for wether the disk should be ignored for warnings or not.<br />    $ignoreWarnings = $false<br />  #if there are any special rules for this, adjust prameters for it  <br />  if &#40;$specialRules.Count -gt 0&#41;<br />    {<br /># just in case you want multiple rules to be applicable to each disk.<br />        foreach &#40;$rule in $specialRules&#41;<br />        {<br />            switch &#40;$rule.Type&#41; <br />            { <br />                0 {<br />                #Set the $percentwarning to 0, supressing the warning.<br />$percentWarning = 0<br />                } <br />                2 {<br />                #Set flag to ignore all errors<br />                $ignoreWarnings = $false<br />                }    <br />            }<br />        }<br />    } </p><p>#to replace your current $i++ line<br />if &#40;!$ignoreWarnings&#41;<br />    {<br />    $i++     <br />     }

      It’s not the most efficient way to process things but it should work.

      You could make life a lot easier by using the ConvertTo-HTML commandlet, possibly mixing it in with Don’s creating HTML reports using powershell tricks. Also think about seperating the code out into functions. Long scripts like this are a lot easier to develop but a pain to maintain and a royal pain to debug. Putting in functions early makes testing and, over time, development a lot easier.

Viewing 0 reply threads
  • The topic ‘Setting Specific Thresholds on Specific Drives’ is closed to new replies.