Solved

problem with several loops on script

Posted on 2013-06-04
2
239 Views
Last Modified: 2013-06-30
In a txt file $MONITORING_ENV, there are the following lines :
k:\msql\:DISK:5%:8%
I:\backup:DISK:20%:40%

Hello

I try to generate alerts for disks in $MONITORING_ENV with the following script, but I think there is a loop problem.
$LIST_SPEC_DISK= @( Select-String $MONITORING_ENV -Pattern "^(.+):DISK:" |? {$_ -notmatch "^([A-Z]:\\.+):DISK:OFF"} | ForEach-Object { $_.matches } | ForEach-Object { $_.groups[1].value } | Select -Unique -ErrorAction silentlycontinue)
$LIST_SPEC_DISK = Get-WmiObject Win32_Volume -Filter "Label!='OSDisk'" | Where-Object { $LIST_SPEC_DISK -contains $_.Name} -ErrorAction silentlycontinue	  

ForEach($DISK in $LIST_SPEC_DISK) {

  [double] $free_sz=$DISK.FreeSpace
  $FREE_SZ=[int]($free_sz/(1024*1024))


  $MOUNT_POINT=$DISK.NAME
  
  $SPEC_DISK = @()
  get-content $MONITORING_ENV| ? { $_ -like '*:DISK:*%' } | % {
  $val = $_ -split ':' 
  $SPEC_DISK += New-Object PsObject -Property @{
  DISK       = $val[0]+':'+$val[1]
  warning  = $val[3]
  critical = $val[4]
  }
  }
  
  foreach ($SPEC_DISK.DISK in $SPEC_DISK ) {
  
  if ($SPEC_DISK.WARNING -match "%") {

   
  $_SPEC_WARN_THRES = $SPEC_DISK.WARNING.REPLACE("%","")
	

  $_SPEC_PANIC_THRES = $SPEC_DISK.CRITICAL.REPLACE("%","")
	

  $_UNIT = "%"

  }
    
  elseif ($SPEC_DISK.CRITICAL -match "M") {
   
  $_SPEC_WARN_THRES = $SPEC_DISK.WARNING.REPLACE("M","")

  $_SPEC_PANIC_THRES = $SPEC_DISK.CRITICAL.REPLACE("M","")

  $_UNIT = "M"
	
   }
    $SPEC_MOUNT_POINT=$SPEC_DISK.DISK

  }
  
if ($FREE_SZ -gt 0) {
$FREE_PCT=[int](($FREE_SZ * 100 / $TOTAL_SZ))
}
  

if  ($_UNIT = "%" -and $SPEC_MOUNT_POINT -eq $MOUNT_POINT -and $FREE_PCT -lt $_SPEC_PANIC_THRES) { 
	
$DISK_STATUS="DBMS[DISASTER]:: Freespace in $MOUNT_POINT $FREE_PCT % < PANIC level $_SPEC_PANIC_THRES %"

 }
}

Open in new window


How can I resolve ?

Thanks
0
Comment
Question by:bibi92
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39221304
Few observations..

In line 22 you mentioned..
foreach ($SPEC_DISK.DISK in $SPEC_DISK )
 I would change it to foreach ($SPEC_D in $SPEC_DISK )
and the Var's inside the foreach loop need to be changed accordingly..
For example..
foreach ($SPEC_D in $SPEC_DISK) {
  
  if ($SPEC_D.WARNING -match "%") {

   
  $_SPEC_WARN_THRES = $SPEC_D.WARNING.REPLACE("%","")
	

  $_SPEC_PANIC_THRES = $SPEC_D.CRITICAL.REPLACE("%","")
	

  $_UNIT = "%"

  }
    
  elseif ($SPEC_D.CRITICAL -match "M") {
   
  $_SPEC_WARN_THRES = $SPEC_D.WARNING.REPLACE("M","")

  $_SPEC_PANIC_THRES = $SPEC_D.CRITICAL.REPLACE("M","")

  $_UNIT = "M"
	
  }
    $SPEC_MOUNT_POINT=$SPEC_D.DISK

  }

Open in new window


In line 37...

$SPEC_DISK.CRITICAL -match "M"
As per your input sheet and my understanding the value of the var is always like "number with %" (For example 5% or 20%), so it will never match "M"

I think you should replace "M" with "%"

In line 55..
if  ($_UNIT = "%" -and $SPEC_MOUNT_POINT -eq $MOUNT_POINT -and $FREE_PCT -lt $_SPEC_PANIC_THRES) {

change $_UNIT = "%" to $_UNIT -eq "%"
0
 

Author Closing Comment

by:bibi92
ID: 39288503
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question