Posted in
Virtual Machine Manager,
Windows Powershell,
Windows Server |
No Comment | 2,941 views | 07/05/2014 23:04
In the first part of our Azure Monitoring series, I showed you how to collect basic monitoring data from your Azure VMs. That was real time statistics but it was output everything on PowerShell console via Write-Host. It doesn’t make any sense because it’s not practical.
Now in this part, we will output all data into XML file, so you can use it on a website or on your CRM. You can also use .NET or PHP to parse XML data and put statistics onto website.
Following script is sample codes of VirtualMetric: Azure Module. You can check it from: http://www.virtualmetric.com
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
| ################################
## VirtualMetric: Azure Module
################################
# Debug Path
$DebugPath = "C:\PowerShell\debug.txt"
# Debug Preference
$ErrorActionPreference = "SilentlyContinue";
# Import Azure PowerShell Module
Import-Module Azure -EA Stop
# Import Azure Settings
Import-AzurePublishSettingsFile "C:\PowerShell\Azure.Credentials.publishsettings" -EA Stop
# Azure VM Credentials
$Credentials = Get-Credential
while ($True)
{
# Execution Time Calculation
$PoSHStartTime = Get-Date
# Start Work
Write-Host "Starting job.."
# Append Stats XML Header
$GeneralStatsXML = "<?xml version=""1.0"" encoding=""utf-8""?>`n"
$GeneralStatsXML += "<Result>`n"
$GeneralStatsXML += " <Code>1</Code>`n"
$GeneralStatsXML += " <Message>Operation is completed</Message>`n"
# Append VM Reports XML Header
$VMReportsXML = "<?xml version=""1.0"" encoding=""utf-8""?>`n"
$VMReportsXML += "<Result>`n"
$VMReportsXML += " <Code>1</Code>`n"
$VMReportsXML += " <Message>Operation is completed</Message>`n"
# Get Azure Virtual Machines
$VMs = Get-AzureVM | Where ServiceName -eq "AzureWebFarm"
# Clear Values
$VMCount = 0;
$VMTotalvCPU = 0;
$VMTotalCPUUsage = 0;
$VMFreeCPU = 0;
$VMTotalMemory = 0;
$VMTotalMemoryUsage = 0;
$VMFreeMemory = 0;
$VMTotalDisk = 0;
$VMTotalDiskUsage = 0;
$VMFreeDisk = 0;
foreach ($VM in $VMs)
{
# Clear Variables
$VMDetails = $Null;
$AzureEndpoints = $Null;
$VMName = $Null;
$VMServiceName = $Null;
$VMIPAddress = $Null;
$VMAvailabilitySetName = $Null;
$VMInstanceSize = $Null;
$VMPowerState = $Null;
$VMDNSAddress = $Null;
$VMDNSName = $Null;
$VMWinRMCertThumbprint = $Null;
$CheckAzureVMCert = $Null;
$OSInformation = $Null;
$CPUInformation = $Null;
# Get Azure VM Details
$VMDetails = Get-AzureVM -ServiceName $VM.ServiceName -Name $VM.Name
# Get Azure VM Information
$VMName = $VMDetails.Name
$VMServiceName = $VMDetails.ServiceName
$VMIPAddress = $VMDetails.IPAddress
$VMAvailabilitySetName = $VMDetails.AvailabilitySetName
$VMInstanceSize = $VMDetails.InstanceSize
$VMPowerState = $VMDetails.PowerState
$VMDNSAddress = $VMDetails.DNSName
$VMWinRMCertThumbprint = $VMDetails.VM.DefaultWinRMCertificateThumbprint
# Get Azure VM DNS Name
$VMDNSName = $VMDNSAddress -match "(?<=\//)(.*?)(?=\/)"
$VMDNSName = $Matches[0]
# Parameter Validation
if (!$VMAvailabilitySetName) { $VMAvailabilitySetName = "Non-HA" }
Write-Host " "
Write-Host " "
Write-Host "VM Details:"
Write-Host " "
Write-Host "VM Name: $VMName"
Write-Host "VM ServiceName: $VMServiceName"
Write-Host "VM IP Address: $VMIPAddress"
Write-Host "VM Availability SetName: $VMAvailabilitySetName"
Write-Host "VM Instance Size: $VMInstanceSize"
Write-Host "VM Power State: $VMPowerState"
Write-Host "VM DNS Address: $VMDNSAddress"
Write-Host "VM DNS Name: $VMDNSName"
Write-Host "VM WinRM Certificate Thumbprint: $VMWinRMCertThumbprint"
# Get Azure VM Endpoints
$AzureEndpoints = $VMDetails | Get-AzureEndpoint
Write-Host " "
Write-Host " "
Write-Host "VM Endpoint Details:"
foreach ($AzureEndpoint in $AzureEndpoints)
{
# Clear Values
$AzureEndpointLBSetName = $Null;
$AzureEndpointProbePort = $Null;
$AzureEndpointProbeProtocol = $Null;
# Get Azure Endpoint Details
$AzureEndpointName = $AzureEndpoint.Name
$AzureEndpointLocalPort = $AzureEndpoint.LocalPort
$AzureEndpointLBSetName = $AzureEndpoint.LBSetName
$AzureEndpointVip = $AzureEndpoint.Vip
$AzureEndpointPort = $AzureEndpoint.Port
$AzureEndpointProtocol = $AzureEndpoint.Protocol
$AzureEndpointProbePort = $AzureEndpoint.ProbePort
$AzureEndpointProbeProtocol = $AzureEndpoint.ProbeProtocol
# Parameter Validation
if (!$AzureEndpointLBSetName) { $AzureEndpointLBSetName = "Non-HA" }
if (!$AzureEndpointProbePort) { $AzureEndpointProbePort = "Non-HA" }
if (!$AzureEndpointProbeProtocol) { $AzureEndpointProbeProtocol = "Non-HA" }
Write-Host " "
Write-Host "Azure Endpoint Name: $AzureEndpointName"
Write-Host "Azure Endpoint Local Port: $AzureEndpointLocalPort"
Write-Host "Azure Endpoint LB SetName: $AzureEndpointLBSetName"
Write-Host "Azure Endpoint Vip: $AzureEndpointVip"
Write-Host "Azure Endpoint Port: $AzureEndpointPort"
Write-Host "Azure Endpoint Protocol: $AzureEndpointProtocol"
Write-Host "Azure Endpoint Probe Port: $AzureEndpointProbePort"
Write-Host "Azure Endpoint Probe Protocol: $AzureEndpointProbeProtocol"
}
# Check Azure VM Certificate
$CheckAzureVMCert = Get-ChildItem -Path Cert:\LocalMachine\Root | Where Thumbprint -eq $VMWinRMCertThumbprint
if (!$CheckAzureVMCert)
{
# Get Azure VM Certificate
$AzureX509Cert = Get-AzureCertificate -ServiceName $VMServiceName -Thumbprint $VMWinRMCertThumbprint -ThumbprintAlgorithm sha1
# Save Certificate to Temp File
$CertTempFile = [IO.Path]::GetTempFileName()
$AzureX509Cert.Data | Out-File $CertTempFile
# Get Local Certificate File
$LocalCertFile = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $CertTempFile
# Import Local Certificate
$CertStore = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root", "LocalMachine"
$CertStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$CertStore.Add($LocalCertFile)
$CertStore.Close()
# Remove Temp File
Remove-Item $CertTempFile
}
if ($VMPowerState -eq "Started")
{
# Get Azure VM WinRM Uri
$AzureVMWinRMUri = Get-AzureWinRMUri -ServiceName $VMServiceName -Name $VMName
# Get Azure VM Credentials
if (!$Credentials) { $Credentials = Get-Credential }
# Get Azure VM Data
$OSInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_OperatingSystem"} -Credential $Credentials
$CPUInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_Processor"} -Credential $Credentials
$DiskInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_LogicalDisk" | Where DeviceID -eq "C:"} -Credential $Credentials
if ($OSInformation -and $CPUInformation -and $DiskInformation)
{
# Get Azure VM Resource Usage
$AzureVMCPUName = $CPUInformation[0].Name
$AzureVMCPUSocket = @($CPUInformation).Count
$AzureVMCPUCount = $CPUInformation[0].NumberOfCores * $AzureVMCPUSocket
$AzureVMCPULoad = $CPUInformation | Measure-Object -Property LoadPercentage -Sum
$AzureVMCPUUsage = [math]::round(($AzureVMCPULoad.Sum / $AzureVMCPULoad.Count), 0)
$AzureVMTotalMemory = ([math]::round(($OSInformation.TotalVisibleMemorySize / 1MB), 1))
$AzureVMFreeMemory = ([math]::round(($OSInformation.FreePhysicalMemory / 1MB), 1))
$AzureVMTotalMemoryUsage = [int]$AzureVMTotalMemory - [int]$AzureVMFreeMemory
$AzureVMTotalDisk = ([math]::round(($DiskInformation.Size / 1GB), 1))
$AzureVMFreeDisk = ([math]::round(($DiskInformation.FreeSpace / 1GB), 1))
$AzureVMTotalDiskUsage = [int]$AzureVMTotalDisk - [int]$AzureVMFreeDisk
$AzureVMDiskReadIO = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {[math]::round(((Get-Counter -Counter "\LogicalDisk(C:)\Disk Reads/Sec").CounterSamples[0].CookedValue), 0)} -Credential $Credentials
$AzureVMDiskWriteIO = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {[math]::round(((Get-Counter -Counter "\LogicalDisk(C:)\Disk Writes/Sec").CounterSamples[0].CookedValue), 0)} -Credential $Credentials
Write-Host " "
Write-Host "CPU Name: $AzureVMCPUName"
Write-Host "CPU Count: $AzureVMCPUCount"
Write-Host "CPU Usage: $AzureVMCPUUsage"
Write-Host "Total Memory: $AzureVMTotalMemory GB"
Write-Host "Free Memory: $AzureVMFreeMemory GB"
Write-Host "Total Disk: $AzureVMTotalDisk GB"
Write-Host "Free Disk: $AzureVMFreeDisk GB"
Write-Host "Disk Read IO: $AzureVMDiskReadIO"
Write-Host "Disk Write IO: $AzureVMDiskWriteIO"
# VM Reports
$VMReportsXML += " <OperationResult>`n"
$VMReportsXML += " <VMName>$VMName</VMName>`n"
$VMReportsXML += " <ProcessorCount>$AzureVMCPUCount</ProcessorCount>`n"
$VMReportsXML += " <MemoryLimit>$AzureVMTotalMemory</MemoryLimit>`n"
$VMReportsXML += " <TotalDisk>$AzureVMTotalDisk</TotalDisk>`n"
$VMReportsXML += " <VMReadIO>$AzureVMDiskReadIO</VMReadIO>`n"
$VMReportsXML += " <VMWriteIO>$AzureVMDiskWriteIO</VMWriteIO>`n"
$VMReportsXML += " <VMState>$VMPowerState</VMState>`n"
$VMReportsXML += " </OperationResult>`n"
}
else
{
# Get Azure VM Resource Usage
$AzureVMCPUCount = 0;
$AzureVMCPUUsage = 0;
$AzureVMTotalMemory = 0;
$AzureVMFreeMemory = 0;
$AzureVMTotalMemoryUsage = 0;
$AzureVMTotalDisk = 0;
$AzureVMFreeDisk = 0;
$AzureVMTotalDiskUsage = 0;
$AzureVMDiskReadIO = 0;
$AzureVMDiskWriteIO = 0;
# VM Reports
$VMReportsXML += " <OperationResult>`n"
$VMReportsXML += " <VMName>$VMName</VMName>`n"
$VMReportsXML += " <ProcessorCount>$AzureVMCPUCount</ProcessorCount>`n"
$VMReportsXML += " <MemoryLimit>$AzureVMTotalMemory</MemoryLimit>`n"
$VMReportsXML += " <TotalDisk>$AzureVMTotalDisk</TotalDisk>`n"
$VMReportsXML += " <VMReadIO>$AzureVMDiskReadIO</VMReadIO>`n"
$VMReportsXML += " <VMWriteIO>$AzureVMDiskWriteIO</VMWriteIO>`n"
$VMReportsXML += " <VMState>$VMPowerState</VMState>`n"
$VMReportsXML += " </OperationResult>`n"
}
}
else
{
# Get Azure VM Resource Usage
$AzureVMCPUCount = 0;
$AzureVMCPUUsage = 0;
$AzureVMTotalMemory = 0;
$AzureVMFreeMemory = 0;
$AzureVMTotalMemoryUsage = 0;
$AzureVMTotalDisk = 0;
$AzureVMFreeDisk = 0;
$AzureVMTotalDiskUsage = 0;
$AzureVMDiskReadIO = 0;
$AzureVMDiskWriteIO = 0;
# VM Reports
$VMReportsXML += " <OperationResult>`n"
$VMReportsXML += " <VMName>$VMName</VMName>`n"
$VMReportsXML += " <ProcessorCount>$AzureVMCPUCount</ProcessorCount>`n"
$VMReportsXML += " <MemoryLimit>$AzureVMTotalMemory</MemoryLimit>`n"
$VMReportsXML += " <TotalDisk>$AzureVMTotalDisk</TotalDisk>`n"
$VMReportsXML += " <VMReadIO>$AzureVMDiskReadIO</VMReadIO>`n"
$VMReportsXML += " <VMWriteIO>$AzureVMDiskWriteIO</VMWriteIO>`n"
$VMReportsXML += " <VMState>$VMPowerState</VMState>`n"
$VMReportsXML += " </OperationResult>`n"
}
# Calculations
$VMCount = [int]$VMCount + 1;
$VMTotalvCPU = [int]$VMTotalvCPU + [int]$AzureVMCPUCount;
$VMTotalCPUUsage = [int]$VMTotalCPUUsage + [int]$AzureVMCPUUsage;
$VMTotalMemory = [int]$VMTotalMemory + [int]$AzureVMTotalMemory
$VMTotalMemoryUsage = [int]$VMTotalMemoryUsage + [int]$AzureVMTotalMemoryUsage
$VMFreeMemory = [int]$VMFreeMemory + [int]$AzureVMFreeMemory
$VMTotalDisk = [int]$VMTotalDisk + [int]$AzureVMTotalDisk
$VMTotalDiskUsage = [int]$VMTotalDiskUsage + [int]$AzureVMTotalDiskUsage
$VMFreeDisk = [int]$VMFreeDisk + [int]$AzureVMFreeDisk
}
[int]$VMTotalCPUUsage = ([math]::round(($VMTotalCPUUsage / $VMCount), 0))
$FreeCPUPercent = 100 - [int]$VMTotalCPUUsage;
# General Stats
$GeneralStatsXML += " <OperationResult>`n"
$GeneralStatsXML += " <ServiceName>$VMServiceName</ServiceName>`n"
$GeneralStatsXML += " <VMCount>$VMCount</VMCount>`n"
$GeneralStatsXML += " <TotalvCPU>$VMTotalvCPU</TotalvCPU>`n"
$GeneralStatsXML += " <TotalCPUUsage>$VMTotalCPUUsage</TotalCPUUsage>`n"
$GeneralStatsXML += " <FreeCPUPercent>$FreeCPUPercent</FreeCPUPercent>`n"
$GeneralStatsXML += " <FreeMemory>$VMFreeMemory</FreeMemory>`n"
$GeneralStatsXML += " <TotalMemoryUsage>$VMTotalMemoryUsage</TotalMemoryUsage>`n"
$GeneralStatsXML += " <TotalMemory>$VMTotalMemory</TotalMemory>`n"
$GeneralStatsXML += " <FreeDiskSpace>$VMFreeDisk</FreeDiskSpace>`n"
$GeneralStatsXML += " <TotalDiskUsage>$VMTotalDiskUsage</TotalDiskUsage>`n"
$GeneralStatsXML += " <TotalDiskSpace>$VMTotalDisk</TotalDiskSpace>`n"
$GeneralStatsXML += " </OperationResult>`n"
# Append Stats XML Footer
$GeneralStatsXML += "</Result>`n"
# Append VM Reports XML Footer
$VMReportsXML += "</Result>`n"
# Remove old statistics
Remove-Item -Path "C:\inetpub\wwwroot\xml\vm\reports.xml" -EA SilentlyContinue
Remove-Item -Path "C:\inetpub\wwwroot\xml\statistics.xml" -EA SilentlyContinue
# Export new statistics
Add-Content -Value $VMReportsXML -Path "C:\inetpub\wwwroot\xml\vm\reports.xml"
Add-Content -Value $GeneralStatsXML -Path "C:\inetpub\wwwroot\xml\statistics.xml"
# Prepare for Next Job
Write-Host " "
Start-Sleep 60
} |
################################
## VirtualMetric: Azure Module
################################
# Debug Path
$DebugPath = "C:\PowerShell\debug.txt"
# Debug Preference
$ErrorActionPreference = "SilentlyContinue";
# Import Azure PowerShell Module
Import-Module Azure -EA Stop
# Import Azure Settings
Import-AzurePublishSettingsFile "C:\PowerShell\Azure.Credentials.publishsettings" -EA Stop
# Azure VM Credentials
$Credentials = Get-Credential
while ($True)
{
# Execution Time Calculation
$PoSHStartTime = Get-Date
# Start Work
Write-Host "Starting job.."
# Append Stats XML Header
$GeneralStatsXML = "<?xml version=""1.0"" encoding=""utf-8""?>`n"
$GeneralStatsXML += "<Result>`n"
$GeneralStatsXML += " <Code>1</Code>`n"
$GeneralStatsXML += " <Message>Operation is completed</Message>`n"
# Append VM Reports XML Header
$VMReportsXML = "<?xml version=""1.0"" encoding=""utf-8""?>`n"
$VMReportsXML += "<Result>`n"
$VMReportsXML += " <Code>1</Code>`n"
$VMReportsXML += " <Message>Operation is completed</Message>`n"
# Get Azure Virtual Machines
$VMs = Get-AzureVM | Where ServiceName -eq "AzureWebFarm"
# Clear Values
$VMCount = 0;
$VMTotalvCPU = 0;
$VMTotalCPUUsage = 0;
$VMFreeCPU = 0;
$VMTotalMemory = 0;
$VMTotalMemoryUsage = 0;
$VMFreeMemory = 0;
$VMTotalDisk = 0;
$VMTotalDiskUsage = 0;
$VMFreeDisk = 0;
foreach ($VM in $VMs)
{
# Clear Variables
$VMDetails = $Null;
$AzureEndpoints = $Null;
$VMName = $Null;
$VMServiceName = $Null;
$VMIPAddress = $Null;
$VMAvailabilitySetName = $Null;
$VMInstanceSize = $Null;
$VMPowerState = $Null;
$VMDNSAddress = $Null;
$VMDNSName = $Null;
$VMWinRMCertThumbprint = $Null;
$CheckAzureVMCert = $Null;
$OSInformation = $Null;
$CPUInformation = $Null;
# Get Azure VM Details
$VMDetails = Get-AzureVM -ServiceName $VM.ServiceName -Name $VM.Name
# Get Azure VM Information
$VMName = $VMDetails.Name
$VMServiceName = $VMDetails.ServiceName
$VMIPAddress = $VMDetails.IPAddress
$VMAvailabilitySetName = $VMDetails.AvailabilitySetName
$VMInstanceSize = $VMDetails.InstanceSize
$VMPowerState = $VMDetails.PowerState
$VMDNSAddress = $VMDetails.DNSName
$VMWinRMCertThumbprint = $VMDetails.VM.DefaultWinRMCertificateThumbprint
# Get Azure VM DNS Name
$VMDNSName = $VMDNSAddress -match "(?<=\//)(.*?)(?=\/)"
$VMDNSName = $Matches[0]
# Parameter Validation
if (!$VMAvailabilitySetName) { $VMAvailabilitySetName = "Non-HA" }
Write-Host " "
Write-Host " "
Write-Host "VM Details:"
Write-Host " "
Write-Host "VM Name: $VMName"
Write-Host "VM ServiceName: $VMServiceName"
Write-Host "VM IP Address: $VMIPAddress"
Write-Host "VM Availability SetName: $VMAvailabilitySetName"
Write-Host "VM Instance Size: $VMInstanceSize"
Write-Host "VM Power State: $VMPowerState"
Write-Host "VM DNS Address: $VMDNSAddress"
Write-Host "VM DNS Name: $VMDNSName"
Write-Host "VM WinRM Certificate Thumbprint: $VMWinRMCertThumbprint"
# Get Azure VM Endpoints
$AzureEndpoints = $VMDetails | Get-AzureEndpoint
Write-Host " "
Write-Host " "
Write-Host "VM Endpoint Details:"
foreach ($AzureEndpoint in $AzureEndpoints)
{
# Clear Values
$AzureEndpointLBSetName = $Null;
$AzureEndpointProbePort = $Null;
$AzureEndpointProbeProtocol = $Null;
# Get Azure Endpoint Details
$AzureEndpointName = $AzureEndpoint.Name
$AzureEndpointLocalPort = $AzureEndpoint.LocalPort
$AzureEndpointLBSetName = $AzureEndpoint.LBSetName
$AzureEndpointVip = $AzureEndpoint.Vip
$AzureEndpointPort = $AzureEndpoint.Port
$AzureEndpointProtocol = $AzureEndpoint.Protocol
$AzureEndpointProbePort = $AzureEndpoint.ProbePort
$AzureEndpointProbeProtocol = $AzureEndpoint.ProbeProtocol
# Parameter Validation
if (!$AzureEndpointLBSetName) { $AzureEndpointLBSetName = "Non-HA" }
if (!$AzureEndpointProbePort) { $AzureEndpointProbePort = "Non-HA" }
if (!$AzureEndpointProbeProtocol) { $AzureEndpointProbeProtocol = "Non-HA" }
Write-Host " "
Write-Host "Azure Endpoint Name: $AzureEndpointName"
Write-Host "Azure Endpoint Local Port: $AzureEndpointLocalPort"
Write-Host "Azure Endpoint LB SetName: $AzureEndpointLBSetName"
Write-Host "Azure Endpoint Vip: $AzureEndpointVip"
Write-Host "Azure Endpoint Port: $AzureEndpointPort"
Write-Host "Azure Endpoint Protocol: $AzureEndpointProtocol"
Write-Host "Azure Endpoint Probe Port: $AzureEndpointProbePort"
Write-Host "Azure Endpoint Probe Protocol: $AzureEndpointProbeProtocol"
}
# Check Azure VM Certificate
$CheckAzureVMCert = Get-ChildItem -Path Cert:\LocalMachine\Root | Where Thumbprint -eq $VMWinRMCertThumbprint
if (!$CheckAzureVMCert)
{
# Get Azure VM Certificate
$AzureX509Cert = Get-AzureCertificate -ServiceName $VMServiceName -Thumbprint $VMWinRMCertThumbprint -ThumbprintAlgorithm sha1
# Save Certificate to Temp File
$CertTempFile = [IO.Path]::GetTempFileName()
$AzureX509Cert.Data | Out-File $CertTempFile
# Get Local Certificate File
$LocalCertFile = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $CertTempFile
# Import Local Certificate
$CertStore = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root", "LocalMachine"
$CertStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$CertStore.Add($LocalCertFile)
$CertStore.Close()
# Remove Temp File
Remove-Item $CertTempFile
}
if ($VMPowerState -eq "Started")
{
# Get Azure VM WinRM Uri
$AzureVMWinRMUri = Get-AzureWinRMUri -ServiceName $VMServiceName -Name $VMName
# Get Azure VM Credentials
if (!$Credentials) { $Credentials = Get-Credential }
# Get Azure VM Data
$OSInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_OperatingSystem"} -Credential $Credentials
$CPUInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_Processor"} -Credential $Credentials
$DiskInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_LogicalDisk" | Where DeviceID -eq "C:"} -Credential $Credentials
if ($OSInformation -and $CPUInformation -and $DiskInformation)
{
# Get Azure VM Resource Usage
$AzureVMCPUName = $CPUInformation[0].Name
$AzureVMCPUSocket = @($CPUInformation).Count
$AzureVMCPUCount = $CPUInformation[0].NumberOfCores * $AzureVMCPUSocket
$AzureVMCPULoad = $CPUInformation | Measure-Object -Property LoadPercentage -Sum
$AzureVMCPUUsage = [math]::round(($AzureVMCPULoad.Sum / $AzureVMCPULoad.Count), 0)
$AzureVMTotalMemory = ([math]::round(($OSInformation.TotalVisibleMemorySize / 1MB), 1))
$AzureVMFreeMemory = ([math]::round(($OSInformation.FreePhysicalMemory / 1MB), 1))
$AzureVMTotalMemoryUsage = [int]$AzureVMTotalMemory - [int]$AzureVMFreeMemory
$AzureVMTotalDisk = ([math]::round(($DiskInformation.Size / 1GB), 1))
$AzureVMFreeDisk = ([math]::round(($DiskInformation.FreeSpace / 1GB), 1))
$AzureVMTotalDiskUsage = [int]$AzureVMTotalDisk - [int]$AzureVMFreeDisk
$AzureVMDiskReadIO = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {[math]::round(((Get-Counter -Counter "\LogicalDisk(C:)\Disk Reads/Sec").CounterSamples[0].CookedValue), 0)} -Credential $Credentials
$AzureVMDiskWriteIO = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {[math]::round(((Get-Counter -Counter "\LogicalDisk(C:)\Disk Writes/Sec").CounterSamples[0].CookedValue), 0)} -Credential $Credentials
Write-Host " "
Write-Host "CPU Name: $AzureVMCPUName"
Write-Host "CPU Count: $AzureVMCPUCount"
Write-Host "CPU Usage: $AzureVMCPUUsage"
Write-Host "Total Memory: $AzureVMTotalMemory GB"
Write-Host "Free Memory: $AzureVMFreeMemory GB"
Write-Host "Total Disk: $AzureVMTotalDisk GB"
Write-Host "Free Disk: $AzureVMFreeDisk GB"
Write-Host "Disk Read IO: $AzureVMDiskReadIO"
Write-Host "Disk Write IO: $AzureVMDiskWriteIO"
# VM Reports
$VMReportsXML += " <OperationResult>`n"
$VMReportsXML += " <VMName>$VMName</VMName>`n"
$VMReportsXML += " <ProcessorCount>$AzureVMCPUCount</ProcessorCount>`n"
$VMReportsXML += " <MemoryLimit>$AzureVMTotalMemory</MemoryLimit>`n"
$VMReportsXML += " <TotalDisk>$AzureVMTotalDisk</TotalDisk>`n"
$VMReportsXML += " <VMReadIO>$AzureVMDiskReadIO</VMReadIO>`n"
$VMReportsXML += " <VMWriteIO>$AzureVMDiskWriteIO</VMWriteIO>`n"
$VMReportsXML += " <VMState>$VMPowerState</VMState>`n"
$VMReportsXML += " </OperationResult>`n"
}
else
{
# Get Azure VM Resource Usage
$AzureVMCPUCount = 0;
$AzureVMCPUUsage = 0;
$AzureVMTotalMemory = 0;
$AzureVMFreeMemory = 0;
$AzureVMTotalMemoryUsage = 0;
$AzureVMTotalDisk = 0;
$AzureVMFreeDisk = 0;
$AzureVMTotalDiskUsage = 0;
$AzureVMDiskReadIO = 0;
$AzureVMDiskWriteIO = 0;
# VM Reports
$VMReportsXML += " <OperationResult>`n"
$VMReportsXML += " <VMName>$VMName</VMName>`n"
$VMReportsXML += " <ProcessorCount>$AzureVMCPUCount</ProcessorCount>`n"
$VMReportsXML += " <MemoryLimit>$AzureVMTotalMemory</MemoryLimit>`n"
$VMReportsXML += " <TotalDisk>$AzureVMTotalDisk</TotalDisk>`n"
$VMReportsXML += " <VMReadIO>$AzureVMDiskReadIO</VMReadIO>`n"
$VMReportsXML += " <VMWriteIO>$AzureVMDiskWriteIO</VMWriteIO>`n"
$VMReportsXML += " <VMState>$VMPowerState</VMState>`n"
$VMReportsXML += " </OperationResult>`n"
}
}
else
{
# Get Azure VM Resource Usage
$AzureVMCPUCount = 0;
$AzureVMCPUUsage = 0;
$AzureVMTotalMemory = 0;
$AzureVMFreeMemory = 0;
$AzureVMTotalMemoryUsage = 0;
$AzureVMTotalDisk = 0;
$AzureVMFreeDisk = 0;
$AzureVMTotalDiskUsage = 0;
$AzureVMDiskReadIO = 0;
$AzureVMDiskWriteIO = 0;
# VM Reports
$VMReportsXML += " <OperationResult>`n"
$VMReportsXML += " <VMName>$VMName</VMName>`n"
$VMReportsXML += " <ProcessorCount>$AzureVMCPUCount</ProcessorCount>`n"
$VMReportsXML += " <MemoryLimit>$AzureVMTotalMemory</MemoryLimit>`n"
$VMReportsXML += " <TotalDisk>$AzureVMTotalDisk</TotalDisk>`n"
$VMReportsXML += " <VMReadIO>$AzureVMDiskReadIO</VMReadIO>`n"
$VMReportsXML += " <VMWriteIO>$AzureVMDiskWriteIO</VMWriteIO>`n"
$VMReportsXML += " <VMState>$VMPowerState</VMState>`n"
$VMReportsXML += " </OperationResult>`n"
}
# Calculations
$VMCount = [int]$VMCount + 1;
$VMTotalvCPU = [int]$VMTotalvCPU + [int]$AzureVMCPUCount;
$VMTotalCPUUsage = [int]$VMTotalCPUUsage + [int]$AzureVMCPUUsage;
$VMTotalMemory = [int]$VMTotalMemory + [int]$AzureVMTotalMemory
$VMTotalMemoryUsage = [int]$VMTotalMemoryUsage + [int]$AzureVMTotalMemoryUsage
$VMFreeMemory = [int]$VMFreeMemory + [int]$AzureVMFreeMemory
$VMTotalDisk = [int]$VMTotalDisk + [int]$AzureVMTotalDisk
$VMTotalDiskUsage = [int]$VMTotalDiskUsage + [int]$AzureVMTotalDiskUsage
$VMFreeDisk = [int]$VMFreeDisk + [int]$AzureVMFreeDisk
}
[int]$VMTotalCPUUsage = ([math]::round(($VMTotalCPUUsage / $VMCount), 0))
$FreeCPUPercent = 100 - [int]$VMTotalCPUUsage;
# General Stats
$GeneralStatsXML += " <OperationResult>`n"
$GeneralStatsXML += " <ServiceName>$VMServiceName</ServiceName>`n"
$GeneralStatsXML += " <VMCount>$VMCount</VMCount>`n"
$GeneralStatsXML += " <TotalvCPU>$VMTotalvCPU</TotalvCPU>`n"
$GeneralStatsXML += " <TotalCPUUsage>$VMTotalCPUUsage</TotalCPUUsage>`n"
$GeneralStatsXML += " <FreeCPUPercent>$FreeCPUPercent</FreeCPUPercent>`n"
$GeneralStatsXML += " <FreeMemory>$VMFreeMemory</FreeMemory>`n"
$GeneralStatsXML += " <TotalMemoryUsage>$VMTotalMemoryUsage</TotalMemoryUsage>`n"
$GeneralStatsXML += " <TotalMemory>$VMTotalMemory</TotalMemory>`n"
$GeneralStatsXML += " <FreeDiskSpace>$VMFreeDisk</FreeDiskSpace>`n"
$GeneralStatsXML += " <TotalDiskUsage>$VMTotalDiskUsage</TotalDiskUsage>`n"
$GeneralStatsXML += " <TotalDiskSpace>$VMTotalDisk</TotalDiskSpace>`n"
$GeneralStatsXML += " </OperationResult>`n"
# Append Stats XML Footer
$GeneralStatsXML += "</Result>`n"
# Append VM Reports XML Footer
$VMReportsXML += "</Result>`n"
# Remove old statistics
Remove-Item -Path "C:\inetpub\wwwroot\xml\vm\reports.xml" -EA SilentlyContinue
Remove-Item -Path "C:\inetpub\wwwroot\xml\statistics.xml" -EA SilentlyContinue
# Export new statistics
Add-Content -Value $VMReportsXML -Path "C:\inetpub\wwwroot\xml\vm\reports.xml"
Add-Content -Value $GeneralStatsXML -Path "C:\inetpub\wwwroot\xml\statistics.xml"
# Prepare for Next Job
Write-Host " "
Start-Sleep 60
}
You will see xml files in the following directories. So you can make a website to parse that data.
This is demo page of VirtualMetric Azure Module:
You can download dashboard HTML files from here:
That is a dashboard in realtime.