Microsoft.SystemCenter.VirtualMachineManager.2016.UserRoleUsageUnitMonitorType (UnitMonitorType)

Element properties:

RunAsDefault
AccessibilityPublic
Support Monitor RecalculateFalse

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource System.Scheduler Default
Probe ProbeAction Microsoft.SystemCenter.VirtualMachineManager.2016.PowerShellProbeActionModuleType Default
CDDummy ConditionDetection System.ExpressionFilter Default
CDOnCritical ConditionDetection System.ExpressionFilter Default
CDOnHealthy ConditionDetection System.ExpressionFilter Default
CDOnWarning ConditionDetection System.ExpressionFilter Default

Overrideable Parameters:

IDParameterTypeSelector
IntervalSecondsint$Config/IntervalSeconds$
TimeoutSecondsint$Config/TimeoutSeconds$
WarningThresholddouble$Config/WarningThreshold$
CriticalThresholddouble$Config/CriticalThreshold$

Source Code:

<UnitMonitorType ID="Microsoft.SystemCenter.VirtualMachineManager.2016.UserRoleUsageUnitMonitorType" Accessibility="Public">
<MonitorTypeStates>
<MonitorTypeState ID="UserRoleHealthy" NoDetection="false"/>
<MonitorTypeState ID="UserRoleWarning" NoDetection="false"/>
<MonitorTypeState ID="UserRoleCritical" NoDetection="false"/>
</MonitorTypeStates>
<Configuration>
<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="TimeoutSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WarningThreshold" type="xsd:double"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CriticalThreshold" type="xsd:double"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UserRoleId" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="VMMServer" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="WarningThreshold" Selector="$Config/WarningThreshold$" ParameterType="double"/>
<OverrideableParameter ID="CriticalThreshold" Selector="$Config/CriticalThreshold$" ParameterType="double"/>
</OverrideableParameters>
<MonitorImplementation>
<MemberModules>
<DataSource ID="DS" TypeID="System!System.Scheduler">
<Scheduler>
<SimpleReccuringSchedule>
<Interval>$Config/IntervalSeconds$</Interval>
</SimpleReccuringSchedule>
<ExcludeDates/>
</Scheduler>
</DataSource>
<ProbeAction ID="Probe" TypeID="Microsoft.SystemCenter.VirtualMachineManager.2016.PowerShellProbeActionModuleType">
<Parameters>
<Parameter>
<Name>VMMServer</Name>
<Value>$Config/VMMServer$</Value>
</Parameter>
<Parameter>
<Name>domain</Name>
<Value>$RunAs[Name="VMM2016Discovery!Microsoft.SystemCenter.VirtualMachineManager.2016.VMMServerConnectionRunAsProfile"]/Domain$</Value>
</Parameter>
<Parameter>
<Name>username</Name>
<Value>$RunAs[Name="VMM2016Discovery!Microsoft.SystemCenter.VirtualMachineManager.2016.VMMServerConnectionRunAsProfile"]/UserName$</Value>
</Parameter>
<Parameter>
<Name>pass</Name>
<Value>$RunAs[Name="VMM2016Discovery!Microsoft.SystemCenter.VirtualMachineManager.2016.VMMServerConnectionRunAsProfile"]/Password$</Value>
</Parameter>
</Parameters>
<ScriptBody><Script>param($VMMServer, $domain, $username, $pass)

function Cleanup
{
$module = get-module -name "virtualmachinemanagercore"

if ($module -ne $null)
{
remove-module -moduleInfo $module
}
if ($session -ne $null)
{
Remove-PSSession $session;
}
}

# UserRoleQuota.ps1 - calculates the user role usage ratio to its quota across clouds in VMM

#error codes
$NewPSSessionFailureCode = 25933;
$CredentialNullCode = 25934;
$GetVMMFailureCode = 25935;
$GetUserRoleFailureCode = 25936;
$ScriptSuccessCode = 25940;


$oAPI = New-Object -comObject 'MOM.ScriptAPI'
$pBag = $oAPI.CreatePropertyBag()

$evt = New-Object System.Diagnostics.Eventlog("Application");
$evt.Source = "Microsoft.SystemCenter.VirtualMachineManager.2016.Monitor.UserRoleQuotaUsageMonitor";
$errevent = [System.Diagnostics.EventLogEntryType]::Error;
$infoevent = [System.Diagnostics.EventLogEntryType]::Information;

$UserRolePropertyCount = 6;

$error.Clear();

$cred = $null;
if ($domain -AND $username -AND $pass)
{
#Append domain to get domain\username
$domainUser = $domain + "\" + $username;

#Create Cred object
$securePass = ConvertTo-SecureString -AsPlainText $pass -force
$cred = New-Object System.Management.Automation.PSCredential $domainUser, $securePass;

}

if ($cred -eq $null)
{
$evt.WriteEntry("Credentials are null for user: " + $domainUser, $errevent, $CredentialNullCode);
Cleanup
return;
}


$error.Clear();
$session = New-PSSession -ComputerName $VMMServer -Authentication Default -Credential $cred;
if ($error.Count -ne 0)
{
$evt.WriteEntry($error, $errevent, $NewPSSessionFailureCode);
if ($session -ne $null)
{
Remove-PSSession $session;
}
return;
}


$error.Clear();
$vmm = Invoke-Command -Session $session -ArgumentList $VMMServer -ScriptBlock {$module = get-module -name "virtualmachinemanagercore"; if ($module -eq $null) {Import-Module "virtualmachinemanagercore"; } $vmm = Get-SCVMMServer -ComputerName $args[0]; $vmm};
if ($vmm -eq $null)
{
$evt.WriteEntry("VMM Server was null" + $error, $errevent, $GetVMMFailureCode);
Cleanup
return;
}

$userRoleUsageList = Invoke-Command -Session $session -ScriptBlock {

Try
{
#calculate userrole quota usage vs capacity percentage
function GetPercent
{
param($usage, $capacity)
$percent = 0;
if ($capacity -ne 0)
{
$percent = [Math]::Round(($usage * 100) / $capacity);
}

$percent;
}


$allClouds = @(Get-SCCloud -VMMServer $vmm);
$allQuota = @(Get-SCUserRoleQuota -QuotaPerUser $false -VMMServer $vmm);
$allQuotaMap = @{}
foreach($quota in $allQuota)
{
$userRoleMap= $allQuotaMap[$quota.CloudID.ToString()];
if($userRoleMap -eq $null)
{
$userRoleMap =@{}
}
$userRoleMap[$quota.RoleID.ToString()] = $quota;
$allQuotaMap[$quota.CloudID.ToString()] = $userRoleMap;
}
$quotaPerUserRoleMap = @{}
foreach($cloud in $allClouds)
{
$tempUsageMap = @{}
$usageList = @(Get-SCCloudUsage -ReturnPerUserRole -Cloud $cloud -VMMServer $vmm);
foreach($usage in $usageList )
{
if ($usage.RoleID -ne $null)
{
$tempUsageMap[$usage.RoleID.ToString()] = $usage;
}
}
$quotaPerUserRoleMap[$cloud.ID.ToString()]= $tempUsageMap;
}
$allUserRoles = @(Get-SCUserRole -VMMServer $vmm)
foreach ($u in $allUserRoles)
{
$cpuCapacitySum = 0; $cpuUsageSum = 0;
$customQuotaCapacitySum = 0; $customQuotaUsageSum = 0;
$memCapacitySum = 0; $memUsageSum = 0;
$storageCapacitySum = 0; $storageUsageSum = 0;
$vmCountCapacitySum = 0; $vmCountUsageSum = 0;
$cpuUsagePercent = 0; $memUsagePercent = 0;
$storageUsagePercent = 0; $customQuotaUsagePercent = 0;
$vmCountUsagePercent = 0;
$userRoleId = $u.ID.ToString();
foreach ($c in $allClouds)
{
if ($allQuotaMap[$c.ID.ToString()] -ne $null -and $allQuotaMap[$c.ID.ToString()].Count -gt 0)
{
$quota = $allQuotaMap[$c.ID.ToString()][$userRoleId];
if ($quota -ne $null)
{
if ($quotaPerUserRoleMap[$c.ID.ToString()] -ne $null -and $quotaPerUserRoleMap[$c.ID.ToString()].Count -gt 0)
{
$usage = $quotaPerUserRoleMap[$c.ID.ToString()][$userRoleId];

# if user role quota uses max available, then that cloud usage/quota does not apply
if(!$quota.UseCPUCountDefault)
{
$cpuUsageSum += $usage.CPUUsageCount;
$cpuCapacitySum +=$quota.CPUCount
}

if(!$quota.UseCustomQuotaCountDefault)
{
$customQuotaUsageSum += $usage.CustomQuotaUsageCount;
$customQuotaCapacitySum +=$quota.CustomQuotaCount
}

if(!$quota.UseMemoryDefault)
{
$memUsageSum += $usage.MemoryUsageMB;
$memCapacitySum +=$quota.MemoryMB
}

if(!$quota.UseStorageGBDefault)
{
$storageUsageSum += $usage.StorageUsageGB;
$storageCapacitySum +=$quota.StorageGB
}

if(!$quota.UseVMCountDefault)
{
$vmCountUsageSum += $usage.VMUsageCount;
$vmCountCapacitySum +=$quota.VMCount
}
}
}
}
}

$cpuUsagePercent = GetPercent -usage $cpuUsageSum -capacity $cpuCapacitySum;
$customQuotaUsagePercent = GetPercent -usage $customQuotaUsageSum -capacity $customQuotaCapacitySum;
$memUsagePercent = GetPercent -usage $memUsageSum -capacity $memCapacitySum;
$storageUsagePercent = GetPercent -usage $storageUsageSum -capacity $storageCapacitySum;
$vmCountUsagePercent = GetPercent -usage $vmCountUsageSum -capacity $vmCountCapacitySum;

# output properties with quota percentages for every user role
$userRoleId
$cpuUsagePercent
$customQuotaUsagePercent
$memUsagePercent
$storageUsagePercent
$vmCountUsagePercent
}
}
Finally
{
if ($vmm -ne $null)
{
$vmm.Disconnect();
}
}
}
#end remote script


if ($error.Count -ne 0)
{
$evt.WriteEntry($error, $errevent, $GetUserRoleFailureCode);
Cleanup
return;
}
else
{
if ($userRoleUsageList -ne $null -and $userRoleUsageList.Count -ne $null)
{
$count = $userRoleUsageList.Count - $userRoleUsageList.Count % $UserRolePropertyCount;
for ( $i = 0; $i -lt $count; $i += $UserRolePropertyCount)
{
if ($userRoleUsageList[$i] -ne "")
{
$pBag.AddValue($userRoleUsageList[$i] + "-CPU", $userRoleUsageList[$i+1])
$pBag.AddValue($userRoleUsageList[$i] + "-CustomQuota", $userRoleUsageList[$i+2])
$pBag.AddValue($userRoleUsageList[$i] + "-Memory", $userRoleUsageList[$i+3])
$pBag.AddValue($userRoleUsageList[$i] + "-Storage", $userRoleUsageList[$i+4])
$pBag.AddValue($userRoleUsageList[$i] + "-VMCount", $userRoleUsageList[$i+5])
}
}
$evt.WriteEntry("Successfully created Property bag for userroles", $infoevent, $ScriptSuccessCode);
$pBag
}
}
Cleanup

</Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</ProbeAction>
<!-- Inserting a dummy CD in between native and managed modules to avoid memory leaks in healthservice -->
<ConditionDetection ID="CDDummy" TypeID="System!System.ExpressionFilter">
<Expression>
<SimpleExpression>
<ValueExpression>
<Value Type="Double">1</Value>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="Double">1</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</ConditionDetection>
<ConditionDetection ID="CDOnHealthy" TypeID="System!System.ExpressionFilter">
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="Double">Property[@Name='$Config/UserRoleId$']</XPathQuery>
</ValueExpression>
<Operator>Less</Operator>
<ValueExpression>
<Value Type="Double">$Config/WarningThreshold$</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</ConditionDetection>
<ConditionDetection ID="CDOnWarning" TypeID="System!System.ExpressionFilter">
<Expression>
<And>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="Double">Property[@Name='$Config/UserRoleId$']</XPathQuery>
</ValueExpression>
<Operator>GreaterEqual</Operator>
<ValueExpression>
<Value Type="Double">$Config/WarningThreshold$</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="Double">Property[@Name='$Config/UserRoleId$']</XPathQuery>
</ValueExpression>
<Operator>Less</Operator>
<ValueExpression>
<Value Type="Double">$Config/CriticalThreshold$</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</And>
</Expression>
</ConditionDetection>
<ConditionDetection ID="CDOnCritical" TypeID="System!System.ExpressionFilter">
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="Double">Property[@Name='$Config/UserRoleId$']</XPathQuery>
</ValueExpression>
<Operator>GreaterEqual</Operator>
<ValueExpression>
<Value Type="Double">$Config/CriticalThreshold$</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</ConditionDetection>
</MemberModules>
<RegularDetections>
<RegularDetection MonitorTypeStateID="UserRoleHealthy">
<Node ID="CDOnHealthy">
<Node ID="CDDummy">
<Node ID="Probe">
<Node ID="DS"/>
</Node>
</Node>
</Node>
</RegularDetection>
<RegularDetection MonitorTypeStateID="UserRoleWarning">
<Node ID="CDOnWarning">
<Node ID="CDDummy">
<Node ID="Probe">
<Node ID="DS"/>
</Node>
</Node>
</Node>
</RegularDetection>
<RegularDetection MonitorTypeStateID="UserRoleCritical">
<Node ID="CDOnCritical">
<Node ID="CDDummy">
<Node ID="Probe">
<Node ID="DS"/>
</Node>
</Node>
</Node>
</RegularDetection>
</RegularDetections>
</MonitorImplementation>
</UnitMonitorType>