$EVENT_ID_SUCCESS = 1000 #Use IDs in the range 1 - 1000
$EVENT_ID_SCRIPTERROR = 999 #Then you can use eventcreate.exe to test the MP
#Global vars
#$mblnLogSuccessEvent #As String(ToBoolean) (passed in as parameter of script)
#$mstrIdentifier #As String (passed in as parameter of script)
#*********************************************************************************************
# PROCEDURE: Main
# DESCRIPTION: Reads the script parameters and creates the cluster disk monitoring data.
# PARAMETERS: void
#*********************************************************************************************
Function Main()
{
$ErrorActionPreference = 'SilentlyContinue'
$error.Clear()
# Targeted at Microsoft.Windows.Cluster.VirtualServer (based on Microsoft.Windows.Server.Computer)
$objMomScriptAPI = New-Object -ComObject Mom.ScriptAPI -ErrorAction SilentlyContinue
$Message = "-- Cluster Disks Monitoring Script -- An error occurred while running the script."
LogScriptEvent -LogType 0 -EventId $EVENT_ID_SCRIPTERROR -EventLevel $EVENT_TYPE_WARNING -objMomScriptAPI $objMomScriptAPI -Message $Message
Create-EmptyData -objMomScriptAPI $objMomScriptAPI
}
}
Function Create-EmptyData ($objMomScriptAPI)
{
if ($null -eq $objMomScriptAPI)
{
return
}
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
#Create an Empty typed property bag of the performance data type
$objTypedPropertyBag = $objMomScriptAPI.CreateTypedPropertyBag($PERFORMANCE_DATA_TYPE)
#Add the property bag to the script#s XML output
$objTypedPropertyBag
$error.Clear()
}
Function Set-PerfDataFromList($list, $objMomScriptAPI)
{
if ($null -eq $objMomScriptAPI)
{
return
}
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
foreach($data in $list)
{
#Add the values for the perf counter#s object name, counter name, instance name and the value)
if($data.$PERFORMANCE_OBJECT_NAME -eq $null)
{
continue
}
$objTypedPropertyBag.AddValue($PERFORMANCE_OBJECT_NAME, $data.$PERFORMANCE_OBJECT_NAME)
$objTypedPropertyBag.AddValue($PERFORMANCE_COUNTER_NAME, $data.$PERFORMANCE_COUNTER_NAME)
$objTypedPropertyBag.AddValue($PERFORMANCE_INSTANCE_NAME, $data.$PERFORMANCE_INSTANCE_NAME)
if(($data.$PERFORMANCE_VALUE_NAME -eq $null) -or ($data.$PERFORMANCE_VALUE_NAME -eq "")){
$objTypedPropertyBag.AddValue($PERFORMANCE_VALUE_NAME, 0)
}else{
$objTypedPropertyBag.AddValue($PERFORMANCE_VALUE_NAME, $data.$PERFORMANCE_VALUE_NAME)
}
#Necessary information for the property bag data source filter condition
#to enable Agent cook down
$objTypedPropertyBag.AddValue($INSTANCE_NAME, $data.$INSTANCE_NAME)
$objTypedPropertyBag
}
}
Function Get-WmiQueryFilter
{
$strOSVersion = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").CurrentVersion
#Adding OS Validation to use propert WMI query to get correct instances
#ResourceClass and IsClusterSharedVolume properties are not available in Windows 2008 OS, only 2008 R2 and beyond.
$strWMIQuery = [string]::Empty
if ($strOSVersion -ne "6.0")
{
$strWMIQuery = " ResourceClass = 1 and IsClusterSharedVolume = FALSE"
}
return $strWMIQuery
}
Function Get-ClusterDiskPerfDataWmi($objClusterResources,$list,$useCim)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
if ($null -eq $list)
{
$error.Clear()
return $null
}
foreach ($objClusterDisk in $objClusterDisks)
{
#Get the associated disk partition
$objClusterDiskPartitions = WMIGetAssociatorInstanceNoAbort -sAssociatorName $WMI_CLUSTER_DISKPARTITION_TO_DISK_ASSOCIATOR_CLASS -sWmiAssociatorName "MSCluster_DiskPartition" -oWmiInstance $objClusterDisk -useCim $useCim
foreach ($objClusterDiskPartition in $objClusterDiskPartitions)
{
CreateMonitoringData -objClusterDisk $objClusterDisk -objClusterDiskPartition $objClusterDiskPartition -objClusterResource $objClusterResource -list $list
}
}
}
$error.Clear()
}
#****************************************************************************************************************
# FUNCTION: MonitorClusterDisks
# DESCRIPTION: Creates monitoring data for all instances of the class #Microsoft.Windows.Server.ClusterDisksMonitoring.ClusterDisk# using WMI.
# PARAMETERS: IN String strTargetComputer: principal name of the targeted #Microsoft.Windows.Cluster.VirtualServer# instance.
# IN String strClusterName: the cluster containing the cluster shared volume
# RETURNS: ArrayList of Performance Data
#****************************************************************************************************************
Function MonitorClusterDisks ($useCIM)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
$list = New-Object System.Collections.ArrayList($null)
if ($error.Count -ne 0)
{
$error.Clear()
return $null
}
#****************************************************************************************************************
# FUNCTION: CreateMonitoringData
# DESCRIPTION: Create monitoring data for a cluster shared volume.
#
# PARAMETERS: IN Object objClusterResource: the cluster disks as SWbemObject of type MSCluster_Disk.
# IN Object objClusterDiskPartition: the associated disk partition as SWbemObject of type MSCluster_DiskPartition.
# IN Object objClusterResource: the associated cluster resource as SWbemObject of type MSCluster_Resource.
# RETURNS: Array List of Performance data
#****************************************************************************************************************
Function CreateMonitoringData($objClusterDisk, $objClusterDiskPartition, $objClusterResource,$list)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
if ($null -eq $list)
{
$error.Clear()
return
}
#Create performance data
$strPartitionName = $objClusterResource.$WMI_NAME_PROPERTY_NAME + "_" + $objClusterDiskPartition.$WMI_PATH_PROPERTY_NAME
#***************************************************************************************
# PROCEDURE: CreatePerformanceTypedPropertyBag
# DESCRIPTION: Generates a performance typed property bag and adds it to the script#s XML output.
# PARAMETERS: IN String strObjectName: name of the performance object
# IN String strCounterName: name of the performance counter
# IN String strInstanceName: name of the performance counter in instance
# IN Variant varValue: value of the performance counter
# IN String strCSVInstanceName: Friendly volume name of the cluster shared volume
# IN Object objMomScriptAPI: initialised MOM.ScriptAPI object
#***************************************************************************************
Function CreatePerformanceTypedPropertyBag($strObjectName, $strCounterName, $strInstanceName, $varValue, $strCSVInstanceName)
{
Function Get-ComputerName([string]$strTargetComputer)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
$strTargetComputerShortName = $strTargetComputer
Function Throw-ScriptError([string]$Message)
{
throw $Message
exit
}
Function Get-CSVResource([string]$VolumeId,[hashtable]$VolumeToResource)
{
if ($null -eq $VolumeToResource -or [string]::IsNullOrEmpty($VolumeId))
{
return $null
}
$CsvResource = $VolumeToResource[$VolumeId]
return $CsvResource
}
Function Get-CSVPartition([string]$VolumeId,[hashtable]$VolumeToPartition)
{
if ($null -eq $VolumeToPartition -or [string]::IsNullOrEmpty($VolumeId))
{
return $null
}
$CsvPartition = $VolumeToPartition[$VolumeId]
return $CsvPartition
}
Function Get-ResourceNameToVolumeTable([bool]$useCim,[hashtable]$ResourceToVolume)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
if ($null -eq $ResourceToVolume)
{
return $false
}
Function Get-VolumeToPartitionTable([bool]$useCim,[hashtable]$VolumeToPartition)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
if ($null -eq $VolumeToPartition)
{
return $false
}
foreach ($Partition in $Partitions)
{
$VolumeToPartition[$Partition.Path] = $Partition
}
return $true
}
Function Get-ResourceNameToResourceTable([bool]$useCim,[hashtable]$NameToResource,[string]$Filter)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()