This repository has been archived by the owner on Jan 6, 2020. It is now read-only.
forked from cavaliercoder/sysinv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.cpp
183 lines (142 loc) · 5.33 KB
/
cluster.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "stdafx.h"
#include "sysinv.h"
#pragma comment(lib, "ClusAPI.lib")
#include <ClusApi.h>
PNODE EnumClusterGroups(HCLUSTER hCluster);
PNODE EnumClusterNodes(HCLUSTER hCluster);
PNODE GetClusterGroupDetail(HCLUSTER hCluster, LPWSTR groupName);
PNODE GetClusterResourceDetail(HCLUSTER hCluster, LPWSTR resourceName);
PNODE EnumClusterServices()
{
PNODE clusterNode = NULL;
PNODE node = NULL;
HCLUSTER hCluster;
CLUSTERVERSIONINFO clusterVersionInfo;
TCHAR buffer[MAX_PATH + 1];
DWORD bufferLen = MAX_PATH + 1;
DWORD res;
// Get handle to cluster
if (NULL == (hCluster = OpenCluster(NULL)))
return clusterNode;
// Get cluster info
clusterVersionInfo.dwVersionInfoSize = sizeof(CLUSTERVERSIONINFO);
if (0 != (res = GetClusterInformation(hCluster, (LPWSTR)&buffer, &bufferLen, &clusterVersionInfo)))
goto clean_cluster;
// Create cluster node
clusterNode = node_alloc(_T("Cluster"), 0);
node_att_set(clusterNode, _T("Name"), buffer, NAFLG_KEY);
// Parse version
_swprintf(buffer, _T("%u.%u.%u"), clusterVersionInfo.MajorVersion, clusterVersionInfo.MinorVersion, clusterVersionInfo.BuildNumber);
node_att_set(clusterNode, _T("Version"), buffer, 0);
// Other details
node_att_set(clusterNode, _T("Vendor"), clusterVersionInfo.szVendorId, 0);
node_att_set(clusterNode, _T("ServicePack"), clusterVersionInfo.szCSDVersion, 0);
_swprintf(buffer, _T("%d"), clusterVersionInfo.dwClusterHighestVersion);
node_att_set(clusterNode, _T("HighestVersion"), buffer, 0);
_swprintf(buffer, _T("%d"), clusterVersionInfo.dwClusterLowestVersion);
node_att_set(clusterNode, _T("LowestVersion"), buffer, 0);
// Get nodes
if (NULL != (node = EnumClusterNodes(hCluster)))
node_append_child(clusterNode, node);
// Get groups
if (NULL != (node = EnumClusterGroups(hCluster)))
node_append_child(clusterNode, node);
clean_cluster:
CloseCluster(hCluster);
return clusterNode;
}
PNODE EnumClusterNodes(HCLUSTER hCluster)
{
PNODE nodesNode = NULL;
PNODE nodeNode = NULL;
HCLUSENUM hEnumerator = 0;
HGROUP hNode = 0;
TCHAR buffer[MAX_PATH + 1];
DWORD bufferLen = MAX_PATH + 1;
DWORD res = 0;
DWORD resType = 0;
DWORD index = 0;
// Get node enumerator handle
if (NULL == (hEnumerator = ClusterOpenEnum(hCluster, CLUSTER_ENUM_NODE)))
return nodesNode;
nodesNode = node_alloc(_T("Nodes"), NFLG_TABLE);
while (ERROR_NO_MORE_ITEMS != (res = ClusterEnum(hEnumerator, index++, &resType, (LPWSTR)&buffer, &bufferLen))) {
nodeNode = node_alloc(_T("Node"), NFLG_TABLE_ROW);
node_att_set(nodeNode, _T("Name"), buffer, NAFLG_KEY);
node_append_child(nodesNode, nodeNode);
bufferLen = MAX_PATH + 1;
}
ClusterCloseEnum(hEnumerator);
return nodesNode;
}
PNODE EnumClusterGroups(HCLUSTER hCluster)
{
PNODE groupsNode = NULL;
PNODE groupNode = NULL;
HCLUSENUM hEnumerator = 0;
HGROUP hGroup = 0;
TCHAR buffer[MAX_PATH + 1];
DWORD bufferLen = MAX_PATH + 1;
DWORD res = 0;
DWORD resType = 0;
DWORD index = 0;
// Get group enumerator handle
if (NULL == (hEnumerator = ClusterOpenEnum(hCluster, CLUSTER_ENUM_GROUP)))
return groupsNode;
groupsNode = node_alloc(_T("Groups"), NFLG_TABLE);
while (ERROR_NO_MORE_ITEMS != (res = ClusterEnum(hEnumerator, index++, &resType, (LPWSTR)&buffer, &bufferLen))) {
if(NULL != (groupNode = GetClusterGroupDetail(hCluster, buffer)))
node_append_child(groupsNode, groupNode);
bufferLen = MAX_PATH + 1;
}
ClusterCloseEnum(hEnumerator);
return groupsNode;
}
PNODE GetClusterGroupDetail(HCLUSTER hCluster, LPWSTR groupName)
{
PNODE groupNode = NULL;
PNODE resourcesNode = NULL;
PNODE resourceNode = NULL;
HGROUP hGroup = 0;
HGROUPENUM hGroupEnum = 0;
TCHAR buffer[MAX_PATH + 1];
DWORD bufferLen = MAX_PATH + 1;
DWORD res = 0;
DWORD resType = 0;
DWORD index = 0;
groupNode = node_alloc(_T("Group"), NFLG_TABLE_ROW);
node_att_set(groupNode, _T("Name"), groupName, NAFLG_KEY);
// Get handle to cluster group
if (NULL != (hGroup = OpenClusterGroup(hCluster, groupName))) {
// Get resources
if (NULL != (hGroupEnum = ClusterGroupOpenEnum(hGroup, CLUSTER_GROUP_ENUM_CONTAINS))) {
resourcesNode = node_append_new(groupNode, _T("Resources"), NFLG_TABLE);
while (ERROR_NO_MORE_ITEMS != (res = ClusterGroupEnum(hGroupEnum, index++, &resType, (LPWSTR) &buffer, &bufferLen))) {
// Ger resource node
if (NULL != (resourceNode = GetClusterResourceDetail(hCluster, buffer)))
node_append_child(resourcesNode, resourceNode);
bufferLen = MAX_PATH + 1;
}
ClusterGroupCloseEnum(hGroupEnum);
}
CloseClusterGroup(hGroup);
}
return groupNode;
}
PNODE GetClusterResourceDetail(HCLUSTER hCluster, LPWSTR resourceName)
{
PNODE resourceNode = NULL;
HRESOURCE hResource = 0;
TCHAR buffer[MAX_PATH + 1];
DWORD bufferLen = MAX_PATH + 1;
resourceNode = node_alloc(_T("Resource"), NFLG_TABLE_ROW);
node_att_set(resourceNode, _T("Name"), resourceName, NAFLG_KEY);
// Get handle to resource
if (NULL != (hResource = OpenClusterResource(hCluster, resourceName))) {
// Get resource type
bufferLen = MAX_PATH + 1;
if (0 == ClusterResourceControl(hResource, NULL, CLUSCTL_RESOURCE_GET_RESOURCE_TYPE, NULL, 0, (LPVOID)&buffer, sizeof(buffer), &bufferLen))
node_att_set(resourceNode, _T("Type"), buffer, 0);
}
return resourceNode;
}