背景信息
最近在重新看一些关于 Windows 性能的书籍, 对于我这样一个原来使用 SCOM 监控的懒人来说, Zabbix 上自带的 Windows OS template 的模板实在是不够用, 因为之前 SCOM 监控包 (微软出的, 把所有工作都做了, 我只需要按需启用规则和告警即可).
默认的 Zabbix 性能数据只有 Avg Latency, 平均的数据也不准, 想看下磁盘的 Latency 以及 IOPS 要自己动手, 看了下 zabbix 中 Windows performance Counter 的语法, 我略有退缩了. 全是用数字表示的性能计数器的 CounterCategory 以及 CounterName.
自带磁盘相关的统计只有传输速度, 以及平均延迟.
如果要监控其他的性能计数器, 它们的名称是什么, 作用是什么? 能否有个清单可以快速搜索?
操刀解决
好在 powershell 书写比较顺手, 写了下面一个函数, 整合了 zabbix 的性能计数器的语法.
- function Get-PerfCounterDesc{
- [cmdletbinding()]
- param(
- [switch]$show
- )
- $Categories = [System.Diagnostics.PerformanceCounterCategory]::GetCategories()
- $SingleInstanceCategories = $Categories | Where-Object {
- $_.CategoryType -eq "SingleInstance"
- }
- $MultiInstanceCategories = $Categories| Where-Object {
- $_.CategoryType -eq "MultiInstance"
- }
- $SingleInstanceCounters = $SingleInstanceCategories | ForEach-Object {
- (new-object System.Diagnostics.PerformanceCounterCategory($_.CategoryName)).GetCounters()
- }
- $MultiInstanceCounters = $MultiInstanceCategories | ForEach-Object {
- $category=new-object System.Diagnostics.PerformanceCounterCategory($_.CategoryName)
- if($category.InstanceExists('_Total')){
- $category.GetCounters('_Total')
- }elseif($category.InstanceExists('Total')){
- $category.GetCounters('Total')
- }else{
- $instanceNames=$category.GetInstanceNames()
- if($instanceNames.count -gt 0){
- $category.GetCounters($instanceNames[0])
- }
- }
- }
- $AllCounters = $MultiInstanceCounters + $SingleInstanceCounters
- $key="HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009"
- $counters=Get-ItemPropertyValue -Path $key -Name "counter"
- $Dict=@{
- }
- for ($i=0;$i -lt $counters.count;$i=$i+2){
- if($counters[$i+1] -and -not $Dict.ContainsKey($counters[$i+1])){
- $Dict.add($counters[$i+1],$counters[$i])
- }
- }
- Write-Debug $dict.keys.count
- $result=$AllCounters | Sort-Object Categoryname,Countername|
- Select-Object CategoryName,
- Countername,
- @{
- n="zabbixPerfCounter";e={
- 'perf_counter["\{0}({{#ReplaceThis}})\{1}"]' -f $dict[$_.CategoryName],$dict[$_.Countername]
- }
- },
- @{
- n="categoryNum";e={
- $Dict[$_.CategoryName]
- }
- },
- @{
- n="CounterNum";e={
- $Dict[$_.Countername]
- }
- },
- CategoryHelp,
- CounterHelp
- if($show){
- $result|Out-GridView
- }else{
- $result
- }
- }
怎么用呢? 把上面函数直接加到个人的 powershell 配置文件, 也就是在 powershell 控制台 notepad $profile , 把内容粘贴进去, 然后保存, 然后设置 set-executionpolicy remotesigned 以让自定义非签名的脚本可以运行.
新起一个 powershell , 直接敲 Get-PerfCounterDesc -show, 可以对结果进行各种过滤. 其中 zabbixPerfCounter 列, 就是生成的 zabbix 上使用的 key, 其中的 {#replaceThis} 请替换成计数器的实例名称. 比如_total.
然后我在 zabbix 模板中加入了下面的计数器来显示磁盘 IOPS 以及 Latency
item prototype
items
还需要对应更改 Grafana
## 附带资源及参考
来源: http://blog.51cto.com/yoke88/2440451