当前位置: 代码网 > it编程>前端脚本>Powershell > PowerShell 读取性能计数器二进制文件(.blg)记录并汇总计算

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

2024年05月15日 Powershell 我要评论
由于监控及报告需要,要统计性能计数器每天数值情况,确认数据库服务器的运行状况。若打开计数器填写,比较麻烦,现在统计用 powershell 来读取计数器的值。第一阶段:powershell 读取计数器

由于监控及报告需要,要统计性能计数器每天数值情况,确认数据库服务器的运行状况。若打开计数器填写,比较麻烦,现在统计用 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 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com