-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyperv.cs
177 lines (151 loc) · 5.97 KB
/
hyperv.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using NewRelic.Platform.Sdk;
using NewRelic.Platform.Sdk.Utils;
namespace Org.BeyondComputing.NewRelic.HyperV
{
class hyperv
{
public struct NewRelicMetric
{
public string Description;
public float Metric;
}
public static ManagementObjectCollection GetVMDetails(string Server)
{
// Connect to the Hyper-V namespace
ManagementScope manScope = new ManagementScope($@"\\{Server}\root\virtualization\v2");
// Grab all VM info from msvm_ComputerSystem
// Exclude host from metrics - just want VM's
ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE Caption = \"Virtual Machine\"");
ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj);
ManagementObjectCollection vmCollection = vmSearcher.Get();
// Return Collection
return vmCollection;
}
public static ManagementObject GetVSMS(string Server)
{
ManagementScope scope = new ManagementScope($@"\\{Server}\root\virtualization\v2");
scope.Connect();
ManagementPath wmiPath = new ManagementPath("Msvm_VirtualSystemManagementService");
ManagementClass serviceClass = new ManagementClass(scope, wmiPath, null);
ManagementObjectCollection services = serviceClass.GetInstances();
ManagementObject serviceObject = null;
foreach (ManagementObject service in services)
{
serviceObject = service;
}
return serviceObject;
}
public static ManagementObject GetVirtualSystemSettingData(string Server, string Name)
{
// Connect to the Hyper-V namespace
ManagementScope manScope = new ManagementScope($@"\\{Server}\root\virtualization\v2");
// Grab settings for the specified VM
ObjectQuery queryObj = new ObjectQuery($"SELECT * FROM Msvm_VirtualSystemSettingData WHERE VirtualSystemIdentifier = \"{Name}\"");
ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj);
ManagementObjectCollection vmCollection = vmSearcher.Get();
ManagementObject mo = vmCollection.OfType<ManagementObject>().FirstOrDefault();
return mo;
}
public static NewRelicMetric GetHealthState(ManagementObject VM)
{
NewRelicMetric Metric = new NewRelicMetric();
int HealthCode = Convert.ToInt32(VM["HealthState"]);
if(HealthCode == 5)
{
Metric.Metric = 0;
Metric.Description = "Healthy";
}
else if(HealthCode == 20)
{
Metric.Metric = 1;
Metric.Description = "Major Failure";
}
else
{
Metric.Metric = 2;
Metric.Description = "Critical Failure";
}
return Metric;
}
public static NewRelicMetric GetReplicationHealth(ManagementObject VM)
{
NewRelicMetric Metric = new NewRelicMetric();
int HealthCode = Convert.ToInt32(VM["ReplicationHealth"]);
if (HealthCode == 0)
{
Metric.Metric = 0;
Metric.Description = "Not applicable";
}
else if (HealthCode == 1)
{
Metric.Metric = 0;
Metric.Description = "Ok";
}
else if (HealthCode == 2)
{
Metric.Metric = 1;
Metric.Description = "Warning";
}
else
{
Metric.Metric = 2;
Metric.Description = "Critical";
}
return Metric;
}
public static NewRelicMetric GetReplicationMode(ManagementObject VM)
{
NewRelicMetric Metric = new NewRelicMetric();
int HealthCode = Convert.ToInt32(VM["ReplicationMode"]);
if (HealthCode == 0)
{
Metric.Metric = 0;
Metric.Description = "None";
}
else if (HealthCode == 1)
{
Metric.Metric = 1;
Metric.Description = "Primary";
}
else if (HealthCode == 2)
{
Metric.Metric = 2;
Metric.Description = "Recovery";
}
else if (HealthCode == 3)
{
Metric.Metric = 3;
Metric.Description = "Replica";
}
else if (HealthCode == 4)
{
Metric.Metric = 4;
Metric.Description = "Extended replica";
}
return Metric;
}
public static UInt64 GetHostMemoryCapacityBytes(string Server)
{
// Connect to the namespace
ManagementScope manScope = new ManagementScope($@"\\{Server}\root\CIMV2");
// Grab the memory information from the machine
ObjectQuery queryObj = new ObjectQuery("SELECT * FROM win32_PhysicalMemory");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(manScope, queryObj);
ManagementObjectCollection memoryCollection = searcher.Get();
// Calculate Total Capacity
UInt64 hostCapacity = 0;
foreach(ManagementObject memory in memoryCollection)
{
hostCapacity += (UInt64)memory["Capacity"];
}
// Return Memory Capacity
return hostCapacity;
}
}
}