Sunday, 26 August 2012 17:28

Monitor your BizTalk environment using PowerShell - Disk Space Monitoring

Written by 
Rate this item
(6 votes)
Disk Space Storage Report Disk Space Storage Report

One of the principal needs for BizTalk Administrators is the ability to monitor the health of BizTalk environments and react promptly to possible problems, you can accomplished this by using certain tools such as:

  • BizTalk Administration Console: is a Microsoft Management Console (MMC) that you can use to manage and monitor BizTalk Server, and that you can use to deploy and manage your BizTalk Server applications.
  • BizTalk360: that provides a nice user friendly interface to monitoring and supporting your BizTalk environments
  • SCOM (System Center Operation Manager) that can provide comprehensive monitoring for Windows systems and with the additional of BizTalk Server 2010 Management Pack, SCOM can offer also monitoring capabilities for BizTalk artifacts and BizTalk-related platform components.
  • Many more…

However, unfortunately many times, some of these tools are not available for us but we still need to accomplish this task.

So how can PowerShell help us?

Windows PowerShell is a Windows command-line shell designed especially for system administrators. It includes an interactive prompt and a scripting environment that can be used independently or in combination. PowerShell can be used by BizTalk administrators to help them in automating tasks and monitor certain resources or operations.

In this post I will explain how you can be able to monitoring disk spaces in your environment (BizTalk/SQL/Other machines) using PowerShell.

This script allows you to set:

  • A range of machines you need to monitor
#########################################################
# List of computers to be monitored
#########################################################
param (
      $serverList =  "C:\\Machine.txt"
)
$computers = Get-Content $serverList
The Machine.txt is a simple text file with the list of all machine names you want to monitor:

servername1
servername2
servername3

  • Configure disk free space warning and critical level

#########################################################
# Configuration of alarmists
#########################################################
[decimal]$warningThresholdSpace = 20 # Percentage of free disk space - Warning (orange).
[decimal]$criticalThresholdSpace = 10 # Percentage of free disk space - critical (red)

  • And configure your email notification settings

#########################################################
# List of users who will receive the report
#########################################################
$mailto = "email1, email2" 

#########################################################
# SMTP properties
#########################################################
$emailFrom = "emailfrom"
$smtpServer = "mySMTPServer" #SMTP Server.
$smtpUsername = "myUsername"
$smtpPassword = "myPassword"

The script will monitor the disk space in all machines and you will receive an email with a list of all disk that are below the threshold set. If all the disks are above the threshold set is not sent any email.

#########################################################
# Monitoring Process
#########################################################
[System.Array]$results = foreach ($cmp in $computers) { 
 Get-WMIObject  -ComputerName $cmp Win32_LogicalDisk |
where{($_.DriveType -eq 3) -and (($_.freespace/$_.size*100) -lt $warningThresholdSpace) }|
select @{n='Server Name' ;e={"{0:n0}" -f ($cmp)}},
@{n='Volume Name' ;e={"{0:n0}" -f ($_.volumename)}},
@{n='Driver' ;e={"{0:n0}" -f ($_.name)}},
@{n='Capacity (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},
@{n='Free Space (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}},
@{n='Percentage Free';e={"{0:n2}%" -f ($_.freespace/$_.size*100)}}
}

We can format the result of the email so that it is presented in a nice way:

#########################################################
# Formating result
#########################################################
$tableStart="<table style='boder:0px 0px 0px 0px;'><tr><th>Server Name</th><th>Volume Name</th><th>Driver</th>
<th>Capacity (Gb)</th><th>Free Space (Gb)</th><th>Percentage Free</th></tr>"

$allLines=""
for($i=0;$i -lt $results.Length;$i++){
     #get das variáveis
     $servers=($results[$i] | select -ExpandProperty "Server Name"  )
     $volumes=($results[$i] | select -ExpandProperty "Volume Name" )
     $drives=($results[$i] | select -ExpandProperty "Driver" )
     $capac=($results[$i] | select -ExpandProperty "Capacity (Gb)" )
     $freeSpace=($results[$i] | select -ExpandProperty "Free Space (Gb)" )
     $percentage=($results[$i] | select -ExpandProperty "Percentage Free" )
     
     #alterna cores das linhas
     if(($i % 2) -ne 0){
         $beginning="<tr style='background-color:white;'>"
     }else{
         $beginning="<tr style='background-color:rgb(245,245,245);'>"
     }
     #controi o body
     $bodyEl ="<td> " + $servers+ " </td>" 
     $bodyEl+="<td> " + $volumes + " </td>"
     $bodyEl+="<td style='text-align:center;'> " + $drives + " </td>"
     $bodyEl+="<td style='text-align:center;'> " + $capac + " </td>"
     $bodyEl+="<td style='text-align:center;'> " + $freeSpace + " </td>"
     $fr=[System.Double]::Parse($freeSpace)
     $cap=[System.Double]::Parse($capac)
     if((($fr/$cap)*100) -lt [System.Int32]::Parse($criticalThresholdSpace)){
         $bodyEl+= "<td style='color:red;font-weight:bold;text-align:center;'>"+$percentage +"</td>"
     }
     else{
         $bodyEl+="<td style='color:orange;text-align:center;'>"+$percentage +"</td>"
     }    
     $end="</tr>"
     $allLines+=$beginning+$bodyEl+$end
}
$tableBody=$allLines
$tableEnd="</table>"
$tableHtml=$tableStart+$tableBody+$tableEnd

# HTML Format for Output 
$HTMLmessage = @"
<font color=""black"" face=""Arial"" size=""3"">
<h1 style='font-family:arial;'><b>Disk Space Storage Report</b></h1>
<p style='font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;'>This report was generated because the drive(s) listed below have less than $warningThresholdSpace % free space. Drives above this threshold will not be listed.</p>
<br><br>
<style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
ol{margin:0;}
table{width:80%;}
thead{}
thead th{font-size:120%;text-align:left;}
th{border-bottom:2px solid rgb(79,129,189);border-top:2px solid rgb(79,129,189);padding-bottom:10px;padding-top:10px;}
tr{padding:10px 10px 10px 10px;border:none;}
#middle{background-color:#900;}
</style>
<body BGCOLOR=""white"">
$tableHtml
</body>
"@

or present it in a simple way:

#########################################################
# Formating result
#########################################################
$tableFragment = $results | ConvertTo-HTML -fragment

# HTML Format for Output 
$HTMLmessage = @"
<font color=""black"" face=""Arial"" size=""3"">
<h1 style='font-family:arial;'><b>Disk Space Storage Report</b></h1>
<p style='font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;'>This report was generated because the drive(s) listed below have less than $warningThresholdSpace % free space. Drives above this threshold will not be listed.</p>
<br><br>
<style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
ol{margin:0;}
table{width:80%;}
thead{}
thead th{font-size:120%;text-align:left;}
th{border-bottom:2px solid rgb(79,129,189);border-top:2px solid rgb(79,129,189);padding-bottom:10px;padding-top:10px;}
tr{padding:10px 10px 10px 10px;border:none;}
#middle{background-color:#900;}
</style>
<body BGCOLOR=""white"">
$tableFragment
</body>
"@

The script can be found and download on Microsoft TechNet Gallery: http://gallery.technet.microsoft.com/disk-spaces-in-your-df94e274

Note: This type of script must be viewed as a complement to the tools mentioned above or used in the absence of them.

Read 7290 times Last modified on Sunday, 26 August 2012 17:57
Sandro Pereira

Sandro Pereira lives in Portugal and works as a BizTalk consultant at DevScope. In the last few years has been working implementing integration scenarios and Cloud Provisioning at a major telecommunications service provider in Portugal. His main focus is on Integration Technologies where is been using .NET, BizTalk and SOAP/XML/XSLT since 2002.

He is an active blogger, member of BizTalk Brazil Community, member and moderator of MSDN BizTalk Server Forums, Code Gallery contributor and was awarded Most Valuable Professional (MVP) for BizTalk Server by Microsoft since 2011. He has certifications for BizTalk Server 2006 and BizTalk Server 2010.

twitterlinkedin

Website: sandroaspbiztalkblog.wordpress.com/

5 comments

  • Comment Link Saravana Kumar Monday, 27 August 2012 03:59 posted by Saravana Kumar

    Great article Sandro explaining the power of PowerShell. One thing I can't see in the article is where do you set the timing for monitoring. Does it monitor every minute, hour, etc.

    As you rightly pointed out both at the beginning and at the end it's important to have the right tools for monitoring, as it will take way lot of management challenges like easy configuration, maintenance, better notification channels, robustness etc.

  • Comment Link Sandro Pereira Monday, 27 August 2012 08:55 posted by Sandro Pereira

    Well that’s the problem… You have to build your own ”monitoring center”, for example you can create a windows task scheduler to run the script and where you set the timing for monitoring: every day, hour,… you can do it in the local BizTalk machine or by another machine in the domain.

  • Comment Link Vasanth Wednesday, 14 November 2012 16:34 posted by Vasanth

    Great Article Sandro..

    In light of this, We have hosted a BizTalk Server with SQL Services in the same machine. Now the SQL Services is consuming 70% of memory.
    I see most of my jobs are running except DTAPurge and Archive and Monitor BizTalk Server.

    Do you know, how to bring the SQL Services memory footprint down?

    Thanks

  • Comment Link Sandro Pereira Thursday, 31 January 2013 13:54 posted by Sandro Pereira

    Hi Vasanth,

    By default, SQL Server will use as much memory as it requires. On busy systems, SQL Server can force other applications to compromise their response time and scalability. You may consider limiting SQL Server's memory consumption by using a fixed memory size or setting a maximum memory size.

    To set a fixed amount of memory:
    - In Object Explorer, right-click a server and select Properties.
    - Click the Memory node.
    - Under Server Memory Options, enter the amount that you want for Minimum server memory and Maximum server memory.

  • Comment Link Sandro Pereira Thursday, 31 January 2013 14:00 posted by Sandro Pereira

    Another important thing "DTA Purge and Archive" and "Monitor BzTalk Server" job must be Enable! and therefore running.

    These two jobs are important to the health of your environment!

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.