USE [DBA]
GO
/****** Object: StoredProcedure
[dbo].[CPU_Memory_Monitor] Script Date: 08/04/2017 11:37:23
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Procedure [dbo].[CPU_Memory_Monitor]
As begin
/****** Object: Table
[dbo].[CPUMemoryMonitor] Script Date: 16/06/2016 19:14:01
******/
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CPUMemoryMonitor]')
AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[CPUMemoryMonitor](
[Memory] [float] NULL,
[CPU] [float] NULL,
[Date] datetime
) ON [PRIMARY]
END
/****** Object: Table
[dbo].[CPUMonitoronly] Script Date: 16/06/2016 19:13:15
******/
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CPUMonitoronly]')
AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[CPUMonitoronly](
[CPU] [float] NULL,
[Date] Datetime
) ON [PRIMARY]
END
DECLARE @memory_usage FLOAT
,
@cpu_usage FLOAT
SET @cpu_usage = ( SELECT TOP ( 1 )
[CPU] / 100.0 AS
[CPU_usage]
FROM ( SELECT
record.value('(./Record/@id)[1]', 'int') AS record_id
, record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS [CPU]
FROM ( SELECT
[timestamp]
, CONVERT(XML, record) AS [record]
FROM sys.dm_os_ring_buffers WITH ( NOLOCK )
WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
AND record LIKE
N'%%'
) AS x
) AS y
ORDER BY
record_id DESC
)
declare @physicalMemoryInUseKB bigint
declare @totalSystemMemoryBytes bigint
select @physicalMemoryInUseKB =
physical_memory_in_use_kb from sys.dm_os_process_memory
select @totalSystemMemoryBytes =
physical_memory_in_bytes from sys.dm_os_sys_info
select @memory_usage =convert(float,
@physicalMemoryInUseKB) * 1024
/ convert(float, @totalSystemMemoryBytes)
insert into
CPUMemoryMonitor
SELECT @memory_usage [memory_usage]
, @cpu_usage [cpu_usage],getdate() as [date]
DECLARE @CPU_BUSY int, @IDLE int
SELECT @CPU_BUSY = @@CPU_BUSY, @IDLE = @@IDLE WAITFOR DELAY '000:00:01'
insert into CPUMonitoronly
SELECT (@@CPU_BUSY -
@CPU_BUSY)/((@@IDLE
- @IDLE + @@CPU_BUSY -
@CPU_BUSY) *1.00) *100 AS 'CPU Utilization by
sqlsrvr.exe' ,getdate() as [date]
End
GO
USE [msdb]
GO
/****** Object: Job
[CPU_Memory_Monitor] Script Date: 08/04/2017 11:35:05 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory
[[Uncategorized (Local)]]] Script Date: 08/04/2017 11:35:05
******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE
name=N'[Uncategorized
(Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <>
0) GOTO
QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode =
msdb.dbo.sp_add_job @job_name=N'CPU_Memory_Monitor',
@enabled=0,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'No
description available.',
@category_name=N'[Uncategorized
(Local)]',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <>
0) GOTO
QuitWithRollback
/****** Object: Step
[CPU_Memory_Monitor] Script Date: 08/04/2017 11:35:06 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'CPU_Memory_Monitor',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0,
@subsystem=N'TSQL',
@command=N'Exec
CPU_Memory_Monitor',
@database_name=N'DBA',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <>
0) GOTO
QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id
= @jobId,
@start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <>
0) GOTO
QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'CPU_Monitor',
@enabled=1,
@freq_type=8,
@freq_interval=127,
@freq_subday_type=8,
@freq_subday_interval=1,
@freq_relative_interval=0,
@freq_recurrence_factor=1,
@active_start_date=20160616,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959,
@schedule_uid=N'9dad2c22-373b-45c9-a24d-22e7bd014c11'
IF (@@ERROR <> 0 OR @ReturnCode <>
0) GOTO
QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id
= @jobId,
@server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <>
0) GOTO
QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO
No comments:
Post a Comment