S2D Performance Metrics Data Source

Microsoft.Windows.Server.10.0.Storage.StorageSpacesDirect.Volume.Deduplication.MetricsDataSource (DataSourceModuleType)

Performance metrics data source.

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
Scheduler DataSource System.Scheduler Default
Script ProbeAction Microsoft.Windows.PowerShellPropertyBagProbe Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
IntervalSecondsint$Config/IntervalSeconds$Interval (sec)
SyncTimestring$Config/SyncTime$Sync Time
TimeoutSecondsint$Config/TimeoutSeconds$Timeout (sec)
DebugModebool$Config/DebugMode$Enable debug outputEnable debug output

Source Code:

<DataSourceModuleType ID="Microsoft.Windows.Server.10.0.Storage.StorageSpacesDirect.Volume.Deduplication.MetricsDataSource" Accessibility="Internal">
<Configuration>
<IncludeSchemaTypes>
<SchemaType>Windows!Microsoft.Windows.PowerShellSchema</SchemaType>
<SchemaType>System!System.ExpressionEvaluatorSchema</SchemaType>
</IncludeSchemaTypes>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="StorageClassName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="StorageObjectUniqueID" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" maxOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IntervalSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SyncTime" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="0" name="DebugMode" type="xsd:boolean"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="SyncTime" Selector="$Config/SyncTime$" ParameterType="string"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="DebugMode" Selector="$Config/DebugMode$" ParameterType="bool"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="Scheduler" TypeID="System!System.Scheduler">
<Scheduler>
<SimpleReccuringSchedule>
<Interval>$Config/IntervalSeconds$</Interval>
<SyncTime>$Config/SyncTime$</SyncTime>
</SimpleReccuringSchedule>
<ExcludeDates/>
</Scheduler>
</DataSource>
<ProbeAction ID="Script" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagProbe">
<ScriptName>GetPerformanceMetricsScript.ps1</ScriptName>
<ScriptBody><Script>

param([string] $storageClassName, [string] $targetInstanceUniqueID,$DebugMode)



#general variables
#-----------------
$errorActionPreference = 'Stop'

$errEvent = [System.Diagnostics.EventLogEntryType]::Error;
$infoEvent = [System.Diagnostics.EventLogEntryType]::Information;
$GenericInformationEvent = 8671;
$GenericFailureEvent = 8672;
$DiscoveryGenericInformationEvent = 8861;
$DiscoveryGenericFailureEvent = 8862;
$script:traceMsg = "";
$ObjectNotFoundCode = -2146233087
$msft_namespace = "root\microsoft\windows\storage"
$cluster_namespace = "root\mscluster"

# Guid length for Virtual Disk
$guidLength = 38
$PadLength = 1 #Object Id ended with double quote


if ($false -eq [string]::IsNullOrEmpty($DebugMode))
{
$DebugMode = [string]::Equals("true",$DebugMode,[System.StringComparison]::OrdinalIgnoreCase)
}
else
{
$DebugMode = $false
}

$momAPI = New-Object -comObject 'MOM.ScriptAPI'

#Common Functions
Function Get-Guid([string]$sguid)
{
if ([string]::IsNullOrEmpty($sguid))
{
return $null
}

$guid = [guid]::NewGuid()
if ($false -eq [guid]::TryParse($sguid,[ref] $guid))
{
return $null;
}

return $guid
}

Function GetDiskIdFilterFromVdObjectId([string]$ObjectId)
{
if ($true -eq [string]::IsNullOrEmpty($ObjectId))
{
return $null
}

$Length = $ObjectId.Length
#check guid length - 38 in {guid} format
$rGuidLength = $guidLength + $PadLength
if ($Length -lt $rGuidLength) {return $null;}

$sguid = $ObjectId.Substring($Length - $rGuidLength,$guidLength)

$guid = Get-Guid -sguid $sguid

if ($null -eq $guid) {return $null;}

return "DiskId='\\\\?\\Disk{$guid}'"
}

Function GetVolumesFromVd($vd)
{
$errorActionPreference = "Stop"

$Filter = GetDiskIdFilterFromVdObjectId -ObjectId $vd.ObjectId
if ($null -eq $Filter ) {return $null;}

$partitions = Get-CimInstance -ClassName "MSFT_Partition" -Namespace $msft_namespace -Filter $Filter | Where-Object {$_.AccessPaths -ne $null}
$volumes = @{}

foreach ($partition in $partitions)
{
$Volume = Get-CimAssociatedInstance -Association "MSFT_PartitionToVolume" -InputObject $partition -Namespace $msft_namespace
if ($null -ne $Volume -and "CSVFS" -eq $Volume.FileSystem )
{
$volumes[$Volume] = 1;
}
}

return $volumes
}

Function AddTraceMessage
{
param($message)

$errorActionPreference = 'SilentlyContinue'
$timeStamp = (get-date -format "HH:mm:ss:fff");
$script:traceMsg = $script:traceMsg + "`n[" + $timeStamp + "] " + $message;
}

Function Log-FinalDebugData([int]$FailureEvent,[int]$InformationEvent,$exception,[string]$SCRIPT_NAME,[string]$traceMsg,[bool]$DebugMode = $false,$message = [string]::Empty)
{
$ErrorActionPreference = "SilentlyContinue"

if ($null -ne $exception)
{
if ([string]::Empty -eq $message) {$message = "Error occured during script execution:"}
$momAPI.LogScriptEvent($SCRIPT_NAME, $FailureEvent, 1, "$message $($exception.Message)");
}

if ($script:traceMsg -ne $null -and $true -eq $DebugMode)
{
$momAPI.LogScriptEvent($SCRIPT_NAME,$InformationEvent, 0, $traceMsg);
}
}

Function Check-CimError($Exception)
{
$errorActionPreference = 'SilentlyContinue'
$result = $false

if ($null -eq $Exception)
{
return $result
}

$error.Clear()
$type = $Exception.GetType()

if (0 -ne $error.Count)
{
$error.Clear()
return $result
}

if ("Microsoft.PowerShell.Cmdletization.Cim.CimJobException" -eq $type.FullName -and $ObjectNotFoundCode -eq $Exception.HResult)
{
$result = $true
}

return $result
}

Function Get-FaultException($exception,$IsObjectExist)
{
if ($false -eq $IsObjectExist)
{
$IsError = Check-CimError -Exception $exception
if ($true -eq $IsError)
{
$exception = $null
}

}

return $exception

}

Function Load-Module ([string] $ModuleName)
{
if ([string]::IsNullOrEmpty($ModuleName) )
{
return $false
}

$ErrorActionPreference="SilentlyContinue"
$error.Clear()

$retval = $false
$Tmodule = Get-Module -Name $ModuleName

########Check for powershell 1.0
if ($error.Count -ne 0)
{
$Exception = $error[0].Exception

if ($null -ne $Exception)
{
$type = $Exception.GetType()
if ([System.Management.Automation.CommandNotFoundException] -eq $type)
{
$error.Clear()
return $retval
}
}

$error.Clear()
}

if ($null -eq $Tmodule)
{
$Tmodule = Get-Module -Name $ModuleName -ListAvailable
if ($error.Count -ne 0){ return $retval;}
if ($null -eq $Tmodule) {return $retval;}

Import-Module $ModuleName
if ($error.Count -eq 0){ $retval = $true;}

}
else
{
$retval = $true
}

return $retval

}

AddTraceMessage -message "Start on Node: $env:COMPUTERNAME"
$IsStorageModuleReady = Load-Module -ModuleName "Storage"

if ($false -eq $IsStorageModuleReady)
{
if ($error.Count -ne 0)
{
$message = "Cannot initializa Storage module. Error: " + $error[0].ToString()

}
else
{
$message = "Cannot load Storage module."
}

AddTraceMessage -message $message

}

#Load cimcmdlets
$IsCimReady = Load-Module -ModuleName "CimCmdLets"





#param([string] $storageClassName, [string] $targetInstanceUniqueID,$DebugMode)



$scriptName = "GetVolumeDeduplicationMetricsScript.ps1"
$msft_namespace = "root\microsoft\windows\storage"
#-----------------

#Log Errors
function LogErrors($exception)
{
$messageToLog = ""
if ($null -ne $exception)
{
$messageToLog = "Error occured during script execution. Instance UniqueID: $targetInstanceUniqueID . `n "

}

Log-FinalDebugData -FailureEvent $GenericFailureEvent -InformationEvent $GenericInformationEvent -exception $exception -SCRIPT_NAME $scriptName -traceMsg $script:traceMsg -DebugMode $DebugMode -message $messageToLog


}

#add propertyBag Deduplication
function AddBagDeduplicationData([HashTable]$valuenameDictionary)
{
$ErrorActionPreference = "Stop"

if ($null -eq $valuenameDictionary -or 0 -eq $valuenameDictionary.Count)
{
return
}


$propertyBag = $momAPI.CreatePropertyBag()
$propertyBag.AddValue("SourceUniqueId", $targetInstanceUniqueID);
$propertyBag.AddValue("FreeSpace", $valuenameDictionary["FreeSpace"]);
$propertyBag.AddValue("SavedSpace", $valuenameDictionary["SavedSpace"]);
$propertyBag.AddValue("OptimizedFilesSize", $valuenameDictionary["OptimizedFilesSize"]);
$propertyBag.AddValue("InPolicyFilesSize", $valuenameDictionary["InPolicyFilesSize"]);


$propertyBag

}




#Create Storage Metrics property bag data

$healthreport = @()
$storageObject = $null
$IsStorageObjectExist = $false
$IsStorageSubSystem = $false

$IsDeduplicationReady = Load-Module -ModuleName "Deduplication"

if ($false -eq $IsDeduplicationReady)
{
if ($error.Count -ne 0)
{
$message = "Cannot initializa Storage module. Error: " + $error[0].ToString()

}
else
{
$message = "Cannot load Storage module."
}

AddTraceMessage -message $message
exit
}

Try
{
$error.Clear();
AddTraceMessage -message "Metrics Script was run"
AddTraceMessage -message $storageClassName
AddTraceMessage -message $targetInstanceUniqueID

if ([string]::IsNullOrEmpty($targetInstanceUniqueID))
{
$message = "Instance Id is empty exiting."
AddTraceMessage -message $message
LogErrors -exception $null

exit
}

if ($false -eq $IsStorageModuleReady)
{
$message = "Cannot initialize storage Powershell cmdlets. Exiting.."
AddTraceMessage -message $message

LogErrors -exception $null
exit
}

if($storageClassName -eq "MSFT_Volume")
{
$message = "Trying to get volume with Id: $targetInstanceUniqueID"
AddTraceMessage -message $message
$storageObject = Get-Volume -UniqueID $targetInstanceUniqueID

$IsStorageObjectExist = $true
$message = "Finish getting storage object data."
AddTraceMessage -message $message

}




if ($null -eq $storageObject)
{
$message = "Storage Object with Id: $targetInstanceUniqueID is empty. Exiting.."
AddTraceMessage -message $message
LogErrors -exception $null

exit
}

if ($storageObject.OperationalStatus -ne "OK" -and $storageObject.OperationalStatus -ne "Online")
{
$message = "Storage Object with Id: $targetInstanceUniqueID is in $($storageObject.OperationalStatus) state. Exiting.."
AddTraceMessage -message $message
LogErrors -exception $null

exit

}

$message = "Finish processing Health Report for storage object with Id: $targetInstanceUniqueID"

AddTraceMessage -message $message



$valuenameDictionary = @{}
$recordsList = @()

$DedupStatusDetails = Get-DedupVolume -VolumeId $targetInstanceUniqueID | Get-DedupStatus
$recordsList += $healthreport.Records | select

if($DedupStatusDetails -eq $null -or $DedupStatusDetails.Count -eq 0)
{
$message = "Metrics records for Storage Object with UniqueID: $targetInstanceUniqueID, were not found in health report."
AddTraceMessage -message $message
LogErrors -exception $null

exit

}


if ($DedupStatusDetails -ne $null)
{

$FreeSpace = $DedupStatusDetails.FreeSpace / 1TB
$SavedSpace = $DedupStatusDetails.SavedSpace / 1TB
$OptimizedFilesSize = $DedupStatusDetails.OptimizedFilesSize / 1TB
$InPolicyFilesSize = $DedupStatusDetails.InPolicyFilesSize / 1TB
$valuenameDictionary.Add("FreeSpace", $FreeSpace);
$valuenameDictionary.Add("SavedSpace", $SavedSpace);
$valuenameDictionary.Add("OptimizedFilesSize", $OptimizedFilesSize);
$valuenameDictionary.Add("InPolicyFilesSize", $InPolicyFilesSize);

}

AddBagDeduplicationData -valuenameDictionary $valuenameDictionary

}
Catch
{
#If storage object doesn't exist SCOM may have lag in discovery so ignore this case
$exception = Get-FaultException -exception $_.Exception -IsObjectExist $IsStorageObjectExist
AddTraceMessage -message $exception.Message
}
Finally
{
LogErrors -exception $exception

}</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>storageClassName</Name>
<Value>$Config/StorageClassName$</Value>
</Parameter>
<Parameter>
<Name>targetInstanceUniqueID</Name>
<Value>$Config/StorageObjectUniqueID$</Value>
</Parameter>
<Parameter>
<Name>DebugMode</Name>
<Value>$Config/DebugMode$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<StrictErrorHandling>true</StrictErrorHandling>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="Script">
<Node ID="Scheduler"/>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>