search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Azure VM Monitoring – Part 1
Posted in Virtual Machine Manager, Windows Powershell, Windows Server | 1 Comment | 3,319 views | 07/05/2014 22:55

This is the first part of our Azure Monitoring script. It’s just a concept to show you how to monitor your Azure VM via PowerShell. Azure Powershell is not able to give performance data of virtual machines, so we will use remote PowerShell to connect Azure VMs.

My VMs are Windows Server 2012 R2, so Remote PowerShell is enabled by default on that servers. But if you are using earlier version of Windows, you should enable Remote PowerShell.

I simply used “Get-Credential” to get authenticated on Azure VMs but you can also store your password in a file as a plain text and use it to authenticate.

My Azure Subscriptions settings are in “Azure.Credentials.publishsettings” file. You can download it from Azure 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
################################
## 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.."
 
	# Get Azure Virtual Machines
	$VMs = Get-AzureVM | Where ServiceName -eq "AzureWebFarm"
 
	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"} -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))
				$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"
			}
		}
	}
 
	# Prepare for Next Job
	Write-Host " "
	Start-Sleep 60
}

As you see, after monitoring VMs, I simply output it with Write-Host. But next time, we will use XML to format it.


Comments (1)

Manikandan

May 3rd, 2015
05:49:06

I started the using this script but shows started job after that no repsonse.

Can you please help me to execute



Leave a Reply