如何对ARM下的VM批量加Diag
Diag其实就是VM的一个Extension,
主要作用是在Portal上展示VM相关的信息指标,例如:CPU, IO, Net等相关信息。
主要是RM架构下VM的一个重要,直观的组件,
创建完VM默认虚拟机是不呈现这些信息,需要配置存储账号后才会展现,
那么,如果我们在页面上对每个虚拟机都这么操作就会执行重复的工作,那么工作就会没什么意义,
powershell就会干这些事,那么我们浅谈下如何执行这些工作。
首先,需要在powershell的活动目录放下一个 diagnostics_publicconfig.xml的文件,
这个XML文件里就包括我们需要开启的指标信息,详细我们可以自定义选择。
1 <?xml version="1.0" encoding="utf-8"?> 2 <PublicConfig xmlns="http://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration"> 3 <WadCfg> 4 <DiagnosticMonitorConfiguration overallQuotaInMB="4096"> 5 <DiagnosticInfrastructureLogs scheduledTransferLogLevelFilter="Error"/> 6 <PerformanceCounters scheduledTransferPeriod="PT1M"> 7 <PerformanceCounterConfiguration counterSpecifier="\Processor(_Total)\% Processor Time" sampleRate="PT15S" unit="Percent"> 8 <annotation displayName="CPU utilization" locale="en-us"/> 9 </PerformanceCounterConfiguration> 10 <PerformanceCounterConfiguration counterSpecifier="\Processor(_Total)\% Privileged Time" sampleRate="PT15S" unit="Percent"> 11 <annotation displayName="CPU privileged time" locale="en-us"/> 12 </PerformanceCounterConfiguration> 13 <PerformanceCounterConfiguration counterSpecifier="\Processor(_Total)\% User Time" sampleRate="PT15S" unit="Percent"> 14 <annotation displayName="CPU user time" locale="en-us"/> 15 </PerformanceCounterConfiguration> 16 <PerformanceCounterConfiguration counterSpecifier="\Processor Information(_Total)\Processor Frequency" sampleRate="PT15S" unit="Count"> 17 <annotation displayName="CPU frequency" locale="en-us"/> 18 </PerformanceCounterConfiguration> 19 <PerformanceCounterConfiguration counterSpecifier="\System\Processes" sampleRate="PT15S" unit="Count"> 20 <annotation displayName="Processes" locale="en-us"/> 21 </PerformanceCounterConfiguration> 22 <PerformanceCounterConfiguration counterSpecifier="\Process(_Total)\Thread Count" sampleRate="PT15S" unit="Count"> 23 <annotation displayName="Threads" locale="en-us"/> 24 </PerformanceCounterConfiguration> 25 <PerformanceCounterConfiguration counterSpecifier="\Process(_Total)\Handle Count" sampleRate="PT15S" unit="Count"> 26 <annotation displayName="Handles" locale="en-us"/> 27 </PerformanceCounterConfiguration> 28 <PerformanceCounterConfiguration counterSpecifier="\Memory\% Committed Bytes In Use" sampleRate="PT15S" unit="Percent"> 29 <annotation displayName="Memory usage" locale="en-us"/> 30 </PerformanceCounterConfiguration> 31 <PerformanceCounterConfiguration counterSpecifier="\Memory\Available Bytes" sampleRate="PT15S" unit="Bytes"> 32 <annotation displayName="Memory available" locale="en-us"/> 33 </PerformanceCounterConfiguration> 34 <PerformanceCounterConfiguration counterSpecifier="\Memory\Committed Bytes" sampleRate="PT15S" unit="Bytes"> 35 <annotation displayName="Memory committed" locale="en-us"/> 36 </PerformanceCounterConfiguration> 37 <PerformanceCounterConfiguration counterSpecifier="\Memory\Commit Limit" sampleRate="PT15S" unit="Bytes"> 38 <annotation displayName="Memory commit limit" locale="en-us"/> 39 </PerformanceCounterConfiguration> 40 <PerformanceCounterConfiguration counterSpecifier="\Memory\Pool Paged Bytes" sampleRate="PT15S" unit="Bytes"> 41 <annotation displayName="Memory paged pool" locale="en-us"/> 42 </PerformanceCounterConfiguration> 43 <PerformanceCounterConfiguration counterSpecifier="\Memory\Pool Nonpaged Bytes" sampleRate="PT15S" unit="Bytes"> 44 <annotation displayName="Memory non-paged pool" locale="en-us"/> 45 </PerformanceCounterConfiguration> 46 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\% Disk Time" sampleRate="PT15S" unit="Percent"> 47 <annotation displayName="Disk active time" locale="en-us"/> 48 </PerformanceCounterConfiguration> 49 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\% Disk Read Time" sampleRate="PT15S" unit="Percent"> 50 <annotation displayName="Disk active read time" locale="en-us"/> 51 </PerformanceCounterConfiguration> 52 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\% Disk Write Time" sampleRate="PT15S" unit="Percent"> 53 <annotation displayName="Disk active write time" locale="en-us"/> 54 </PerformanceCounterConfiguration> 55 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\Disk Transfers/sec" sampleRate="PT15S" unit="CountPerSecond"> 56 <annotation displayName="Disk operations" locale="en-us"/> 57 </PerformanceCounterConfiguration> 58 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\Disk Reads/sec" sampleRate="PT15S" unit="CountPerSecond"> 59 <annotation displayName="Disk read operations" locale="en-us"/> 60 </PerformanceCounterConfiguration> 61 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\Disk Writes/sec" sampleRate="PT15S" unit="CountPerSecond"> 62 <annotation displayName="Disk write operations" locale="en-us"/> 63 </PerformanceCounterConfiguration> 64 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\Disk Bytes/sec" sampleRate="PT15S" unit="BytesPerSecond"> 65 <annotation displayName="Disk speed" locale="en-us"/> 66 </PerformanceCounterConfiguration> 67 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\Disk Read Bytes/sec" sampleRate="PT15S" unit="BytesPerSecond"> 68 <annotation displayName="Disk read speed" locale="en-us"/> 69 </PerformanceCounterConfiguration> 70 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\Disk Write Bytes/sec" sampleRate="PT15S" unit="BytesPerSecond"> 71 <annotation displayName="Disk write speed" locale="en-us"/> 72 </PerformanceCounterConfiguration> 73 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\Avg. Disk Queue Length" sampleRate="PT15S" unit="Count"> 74 <annotation displayName="Disk average queue length" locale="en-us"/> 75 </PerformanceCounterConfiguration> 76 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\Avg. Disk Read Queue Length" sampleRate="PT15S" unit="Count"> 77 <annotation displayName="Disk average read queue length" locale="en-us"/> 78 </PerformanceCounterConfiguration> 79 <PerformanceCounterConfiguration counterSpecifier="\PhysicalDisk(_Total)\Avg. Disk Write Queue Length" sampleRate="PT15S" unit="Count"> 80 <annotation displayName="Disk average write queue length" locale="en-us"/> 81 </PerformanceCounterConfiguration> 82 <PerformanceCounterConfiguration counterSpecifier="\LogicalDisk(_Total)\% Free Space" sampleRate="PT15S" unit="Percent"> 83 <annotation displayName="Disk free space (percentage)" locale="en-us"/> 84 </PerformanceCounterConfiguration> 85 <PerformanceCounterConfiguration counterSpecifier="\LogicalDisk(_Total)\Free Megabytes" sampleRate="PT15S" unit="Count"> 86 <annotation displayName="Disk free space (MB)" locale="en-us"/> 87 </PerformanceCounterConfiguration> 88 </PerformanceCounters> 89 <Metrics resourceId="(Update with resource ID for the VM)" > 90 <MetricAggregation scheduledTransferPeriod="PT1H"/> 91 <MetricAggregation scheduledTransferPeriod="PT1M"/> 92 </Metrics> 93 <WindowsEventLog scheduledTransferPeriod="PT1M"> 94 <DataSource name="Application!*[System[(Level = 1 or Level = 2)]]"/> 95 <DataSource name="Security!*[System[(Level = 1 or Level = 2)]"/> 96 <DataSource name="System!*[System[(Level = 1 or Level = 2)]]"/> 97 </WindowsEventLog> 98 </DiagnosticMonitorConfiguration> 99 </WadCfg> 100 <StorageAccount>(Update with diagnostics storage account name)</StorageAccount> 101 </PublicConfig>
编辑完之后,那么我们在powershell执行以下代码就可以了。
Note:如果是单台虚拟机请指定存储账号和去掉foreach的循环。
具体可以改下代码的逻辑。
1 $VMs = Get-AzureRmVM 2 foreach ($vm in $VMs){ 3 $rg = $vm.ResourceGroupName #资源组名称 4 $vmName = $vm.Name #虚拟机名称 5 $storageName = $rg.ToLower() + "diag" #存储帐号名称 6 if($vmName -like "CNBJCAP00**" ){ 7 $vm = Get-AzureRmVM -ResourceGroupName $rg -Name $vmName 8 $storage = Get-AzureRmStorageAccount -ResourceGroupName $rg -Name $storageName 9 $storagekey = (Get-AzureRmStorageAccountKey -ResourceGroupName $rg -Name $storageName).Value[0] 10 $vmName+"这台虚拟机在名单中正在加监控请稍等" 11 Set-AzureRmVMDiagnosticsExtension -ResourceGroupName $rg ` 12 -VMName $vmName ` 13 -StorageAccountName $storageName ` 14 -StorageAccountKey $storagekey ` 15 -DiagnosticsConfigurationPath "diagnostics_publicconfig.xml" 16 continue; 17 } 18 else{ 19 $vmName+"这台虚拟机不符合要求将被剔除"; 20 } 21 }