新闻动态

PowerShell 读取性能计数器二进制文件(.blg)记录并汇总计算

发布日期:2022-01-24 08:02 | 文章来源:源码之家

由于监控及报告需要,要统计性能计数器每天数值情况,确认数据库服务器的运行状况。若打开计数器填写,比较麻烦,现在统计用 powershell 来读取计数器的值。

第一阶段:Powershell 读取计数器文件并统计其中一个计数器的值

$startDate = (Get-Date).AddDays(-1).Date 
$endDate = (Get-Date).Date 
$perfPath = "D:\DataFiles\PERFMON\MSSQL_PERFMON_08240904.blg" 
 
#读取文件中的计数器名称 
$counterList = Import-Counter -Path $perfPath 
$countersNameList = $counterList[0].countersamples | % {$_.path} 
 
#筛选指定计数器和时间重新导入PS 
$counter = $countersNameList -like '*Processor Time*' 
$counterData = Import-Counter -Path $perfPath -Counter $counter | Where-Object -FilterScript {($_.Timestamp -ge $startDate) -and ($_.Timestamp -lt $endDate)}  
 
#计算日期范围内的数值统计 
$counterInfo = $counterData | Foreach-Object {$_.CounterSamples} | Measure-Object -property CookedValue -Average -Maximum 
 
#哈希表存储结果数据 
$resultTable=@{} 
$resultTable."CPU 利用率——平均" = $counterInfo.Average 
$resultTable."CPU 利用率——最大" = $counterInfo.Maximum 
 
$resultTable 

第二阶段:批量统计文件中的所有计数器并导出到文件中

$startDate = (Get-Date).AddDays(-1).Date  
$endDate = (Get-Date).Date  
$perfPath = "D:\360Downloads\*.blg" 
 
#哈希表存储结果数据  
$resultTable=@{} 
 
#导入指定时间的所有计数器信息 
$counterData = Import-Counter -Path $perfPath | Where-Object -FilterScript {($_.Timestamp -ge $startDate) -and ($_.Timestamp -lt $endDate)} 
 
#所有的计数器名字 
$countersNameList = $counterData[0].countersamples | % {$_.Path} 
 
#遍历每个计数器,将计算结果存储到哈希表中 
foreach($counterName in $countersNameList)  
{  
#$counterName = "\\hzc\system\threads" 
$counterDataOne = $counterData | Foreach-Object {$_.CounterSamples} | Where {$_.Path -like $counterName}  
$counterInfo = $counterDataOne | Measure-Object CookedValue -Average -Minimum -Maximum 
$resultTable.$($counterName+" :平均值") = $counterInfo.Average 
$resultTable.$($counterName+" :最小值") = $counterInfo.Minimum 
$resultTable.$($counterName+" :最大值") = $counterInfo.Maximum 
} 
 
#$resultTable.GetEnumerator() | sort Name | Format-Table -Auto 
#几种方法导出到文件 
$resultTable.GetEnumerator() | sort Name | Format-Table -Auto | Out-File "D:\360Downloads\PerfmonCounter.txt" 
$resultTable.GetEnumerator() | sort Name | Export-Csv -Path "D:\360Downloads\PerfmonCounter.txt" -Encoding "unicode" -Force 
$resultTable.GetEnumerator() | sort Name | Format-List | Export-Csv -Path "D:\360Downloads\PerfmonCounter.xlsx" -Encoding "unicode" -Force 

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

相关文章

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部