1. 程式人生 > >利用PowerShell監控Win-Server效能

利用PowerShell監控Win-Server效能

USE [TestDB] GO /*系統性能監控:CPU、記憶體、disk*/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc M_cpumem --手動獲取資料庫伺服器IP @ip nvarchar(20) AS BEGIN /*獲取CPU、記憶體使用情況資訊*/ --作業Get-CPU_Mem,執行powershell指令碼 DECLARE @cm nvarchar(100) SET @cm=( SELECT log FROM msdb.dbo.sysjobstepslogs)
--cpu使用率 DECLARE @cpu int SET @cpu=CAST((SELECT SUBSTRING(@cm,5,4)) AS numeric(2,0)) --記憶體使用率 DECLARE @mem int SET @mem=CAST((SELECT SUBSTRING(@cm,22,4)) AS numeric(2,0)) /*CPU過載報警監控*/ --CPU使用率大於80%說明系統出現高耗 IF (@cpu>80) BEGIN DECLARE @str_subject_cpu
nvarchar(100),@str_html_cpu nvarchar(max); SET @str_subject_cpu='伺服器:'+@ip+' CPU高耗報警' --CPU或排程器當前分配的工作情況 SET @str_html_cpu='Dear All: <br>&nbsp;&nbsp;系統CPU>80%,請參閱系統排程情況,進行效能優化處理!<br> <table border=1 >'+ '
<tr> <td style="width: 80px"><p align="center" >Scheduler_ID</p></td> <td style="width: 80px"><p align="center" >CPU_ID</p></td> <td style="width: 150px"><p align="center" >狀態</p></td> <td style="width: 80px"><p align="center" >Is_Idle</p></td> <td style="width: 90px"><p align="center" >當前任務數</p></td> <td style="width: 80px"><p align="center" >等待排程執行緒數</p></td> <td style="width: 90px"><p align="center" >當前執行緒數</p></td> <td style="width: 90px"><p align="center" >活動執行緒數</p></td> <td style="width: 90px"><p align="center" >掛起任務數</p></td> </tr>'+ CAST(( SELECT td=scheduler_id,'', td=cpu_id,'', td=status,'', td=is_idle,'', td=current_tasks_count,'', td=runnable_tasks_count,'', td=current_workers_count,'', td=active_workers_count,'', td=work_queue_count,'' FROM master.sys.dm_os_schedulers WHERE scheduler_id<255 FOR xml path('tr'),type ) AS nvarchar(max))+ N'</table>'; BEGIN EXEC msdb.dbo.sp_send_dbmail @profile_name='Email', @recipients='[email protected]', @subject=@str_subject_cpu, @body=@str_html_cpu, @body_format='HTML' END END /*記憶體過載報警監控*/ --記憶體使用率大於60%說明系統出現高耗 IF (@mem>60) BEGIN DECLARE @str_subject_mem nvarchar(100),@str_html_mem nvarchar(max); SET @str_subject_mem='伺服器:'+@ip+' 記憶體高耗報警' --系統性能計數器 SET @str_html_mem='Dear All: <br>&nbsp;&nbsp;系統Memory>60%,請參閱系統性能監控情況,進行效能優化處理!<br> <table border=1 >'+ '<tr> <td style="width: 150px"><p align="center" >Object_Name</p></td> <td style="width: 150px"><p align="center" >Counter_Name</p></td> <td style="width: 100px"><p align="center" >Value</p></td> </tr>'+ CAST(( SELECT td=object_name,'', td=counter_name,'', td=cntr_value,'' FROM sys.dm_os_performance_counters WHERE object_name in ('SQLServer:Buffer Manager','SQLServer:Memory Manager') AND counter_name in ('Buffer cache hit ratio','Checkpoint pages/sec','Lazy writes/sec','Page lIFe expectancy','Memory Grants PENDing','Target Server Memory (KB)','Total Server Memory (KB)') FOR xml path('tr'),type ) AS nvarchar(max))+ N'</table>'; BEGIN EXEC msdb.dbo.sp_send_dbmail @profile_name='Email', @recipients='[email protected]', @subject=@str_subject_mem, @body=@str_html_mem, @body_format='HTML' END END /*磁碟使用報警監控*/ --磁碟可使用率小於15%時進行郵件報警 CREATE TABLE temp_disk_space(Drive_Name nvarchar(50),Total_Space_GB NUMERIC(18,2),Free_Space_GB NUMERIC(18,2)) INSERT INTO temp_disk_space SELECT DISTINCT vs.volume_mount_point, CAST(vs.total_bytes / 1024.0 / 1024 / 1024 AS NUMERIC(18,2)), CAST(vs.available_bytes / 1024.0 / 1024 / 1024 AS NUMERIC(18,2)) FROM sys.master_files AS f CROSS APPLY sys.dm_os_volume_stats(f.database_id,f.file_id) AS vs SELECT Drive_Name,(Free_Space_GB * 100)/Total_Space_GB AS [% Free_Space] INTO #temp_space_monitor FROM temp_disk_space ORDER BY Drive_Name IF exists(SELECT 1 FROM #temp_space_monitor WHERE [% Free_Space] < 15) BEGIN DECLARE @str_subject_disk nvarchar(100); SET @str_subject_disk='伺服器:'+@ip+' 磁碟空間不足' EXEC msdb.dbo.sp_send_dbmail @profile_name='Email', @recipients='[email protected]', @subject=@str_subject_disk, @body = 'Dear ALL: 請及時清理磁碟,磁碟空間使用情況見附件!', @query = 'SELECT * FROM TestDB.dbo.temp_disk_space', --使用全稱 @attach_query_result_as_file = 1, @query_attachment_filename = 'disk.txt' END DROP table TestDB.dbo.temp_disk_space, #temp_space_monitor END