故障转移群集警报自动化

时间:2017-09-25 15:17:07

标签: arrays powershell cluster-computing hashtable

在具有50个群集的高可用性环境(两个DC,主要和备用DC)中。每个群集中有5到6个节点。我想确保每个集群中的所有节点都是"在线" (州)并在"小学" (OwnerNode)。否则必须注明任何显示的节点。

我使用数组在foreach循环中存储所需的信息。问题是,编译需要很长时间。我想早点完成。

$Clusternodes= * some 50 elements *
$Standbynodes= * some 50 elements *

foreach ($cluster in $Clusternodes) {
    $NotOnline += Get-ClusterGroup -Cluster $Cluster |
                  where {$_.State -ne "Online"} |
                  Select-Object Name,OwnerNode,State
    foreach ($node in $Standbynodes) {
        $Standbys += Get-ClusterGroup -Cluster $Cluster |
                     where {$_.OwnerNode -eq "$node"} |
                     Select-Object Name,OwnerNode,State
    }
}

修改

Get-clustergroup -Cluster $Cluster在每个群集中返回5到6个条目。输出包含三列Name,OwnerNode,State。

我根据数据的状态(无论是否正在运行)和所有者节点(无论是主要还是次要DC)存储数组中的每个条目。因此,我想要四个数组。虽然前者很容易,但后者并不容易。因为它本身是另外两个阵列,每个阵列有50个奇数元素。因此,我使用哈希表,如下所述。但是,当我尝试下面的代码时,它总是返回一个空数组。

$Clusternodes = * some 50 elements *
$Standbynodes = * some 50 elements *
$Primarynodes = * some 50 elements *
$pr = @{}
$sb = @{}
$Standbynodes | ForEach-Object { $sb[$_] = $true }
$Primarynodes | ForEach-Object { $pr[$_] = $true }

$RunninginPrimary    = @()
$NotRunninginPrimary = @()
$RunninginStandby    = @()
$NotRunninginStandby = @()

foreach ($cluster in $Clusternodes) {
    $c = Get-ClusterGroup -Cluster $Cluster

    $NotRunninginStandby += $c | Where-Object {
        ($_.State -ne "Online") -and ($sb.ContainsKey($_.OwnerNode))
    } | Select-Object Name,OwnerNode,State

    $NotRunninginPrimary += $c | Where-Object {
        ($_.State -ne "Online") -and ($pr.ContainsKey($_.OwnerNode))
    } | Select-Object Name,OwnerNode,State

    $RunninginStandby += $c | Where-Object {
        ($_.State -eq "Online") -and ($sb.ContainsKey($_.OwnerNode))
    } | Select-Object Name,OwnerNode,State

    $RunninginPrimary += $c | Where-Object {
        ($_.State -eq "Online") -and ($pr.ContainsKey($_.OwnerNode))
    } | Select-Object Name,OwnerNode,State
}

2 个答案:

答案 0 :(得分:0)

您多次查询每个群集。要加快代码查询每个集群一次,将结果存储在变量中,并在循环的其余部分中使用该变量。您可能还希望使用哈希表查找替换迭代$Standbynodes的嵌套循环。

$Clusternodes = ...
$Standbynodes = ...

$sb = @{}
$Standbynodes | ForEach-Object { $sb[$_] = $true }

$NotOnline = @()
$Standbys  = @()

foreach ($cluster in $Clusternodes) {
    $c = Get-ClusterGroup -Cluster $Cluster

    $NotOnline += $c | Where-Object { $_.State -ne "Online" } |
                  Select-Object Name,OwnerNode,State
    $Standbys  += $c | Where-Object { $sb.ContainsKey($_.OwnerNode) } |
                  Select-Object Name,OwnerNode,State
}

答案 1 :(得分:0)

尝试使用工作流程。

workflow clusterCheck {

     $Clusternodes= * some 50 elements *
     $Standbynodes= * some 50 elements *

    foreach -parallel ($cluster in $Clusternodes) {
        $NotOnline += Get-ClusterGroup -Cluster $Cluster |
                  where {$_.State -ne "Online"} |
                  Select-Object Name,OwnerNode,State
        foreach -parallel ($node in $Standbynodes) {
                  $Standbys += Get-ClusterGroup -Cluster $Cluster |
                  where {$_.OwnerNode -eq "$node"} |
                  Select-Object Name,OwnerNode,State
        }
    }
}

来源: - Scripting guy