Saturday, March 16, 2024

.::: Script check cpu usage & memory usage & Total Memory Server SQL Server include using sqlcmd to monitoring NMS :::.

 1. create store procedure cpu monitoring sql server for monitoring

CREATE PROCEDURE sp_total_usage_cpu_SQLProcessUtilization

AS
BEGIN
DECLARE @ts_now bigint = (SELECT cpu_ticks/(cpu_ticks/ms_ticks)FROM sys.dm_os_sys_info);

SELECT TOP(1)
               100 - SystemIdle - SQLProcessUtilization + SQLProcessUtilization AS [Total Usage CPU SQL Utilization]
           
FROM (
      SELECT record.value('(./Record/@id)[1]', 'int') AS record_id,
            record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int')
            AS [SystemIdle],
            record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]',
            'int')
            AS [SQLProcessUtilization], [timestamp]
      FROM (
            SELECT [timestamp], CONVERT(xml, record) AS [record]
            FROM sys.dm_os_ring_buffers
            WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
            AND record LIKE '%<SystemHealth>%') AS x
      ) AS y
ORDER BY record_id DESC;
END;
GO
 
 2. create store procedure memory monitoring sql server for monitoring

CREATE PROCEDURE sp_total_usage_memory_SQLProcessUtilization

AS
BEGIN
SELECT  CAST((physical_memory_in_use_kb / 1024) AS VARCHAR(10)) as  [Total Usage Memory SQL Utilization mb] FROM sys.dm_os_process_memory

END;
GO
 
 2.1 create store procedure memory (%) monitoring sql server for monitoring

CREATE PROCEDURE sp_total_usage_memory_SQL_percent

AS
BEGIN

DECLARE @total_memory_mb FLOAT, @available_memory_mb FLOAT, @memory_usage_percentagesql FLOAT,@memory_usage_mbsql FLOAT;

-- Mengambil total memori fisik yang tersedia pada SQL Server dalam megabyte
SELECT @total_memory_mb = physical_memory_kb / 1024.0
FROM sys.dm_os_sys_info;

-- Mengambil jumlah memori SQL Server
SELECT @memory_usage_mbsql=cast(physical_memory_in_use_kb / 1024.0 AS NUMERIC(10,2))
FROM sys.dm_os_process_memory

-- Menghitung persentase penggunaan memori SQL Server
SET @memory_usage_percentagesql = (@memory_usage_mbsql / @total_memory_mb) * 100;

-- Menampilkan hasil dengan pembulatan
---## SELECT @@servername as ServerName,ROUND(@memory_usage_percentagesql, 0) AS 'Memory Usage(%) SQL',ROUND(@memory_usage_mbsql, 0) AS 'Memory Usage(MB) SQL',ROUND(@total_memory_mb, 0) AS 'Memory Total(MB) Server';

SELECT ROUND(@memory_usage_percentagesql, 0) as  [Total Usage Memory SQL Utilization(%)] ;

END;
GO

3. exec store procedure

exec sp_total_usage_cpu_SQLProcessUtilization
exec sp_total_usage_memory_SQLProcessUtilization
exec teguhth..sp_total_usage_memory_SQL_percent
 
 

3.2 Sample prtg CPU Usage SQL
 



3.3 Sample prtg Memory Usage SQL
 



4. for manual checkimg fo cpu usage sql server


DECLARE @ts_now bigint = (SELECT cpu_ticks/(cpu_ticks/ms_ticks)FROM sys.dm_os_sys_info);

SELECT TOP(1) SQLProcessUtilization AS [SQL Server Process CPU Utilization],
               SystemIdle AS [System Idle Process],
               100 - SystemIdle - SQLProcessUtilization AS [Other Process CPU Utilization],
               100 - SystemIdle - SQLProcessUtilization + SQLProcessUtilization AS [Total Usage CPU SQL Utilization],
               DATEADD(ms, -1 * (@ts_now - [timestamp]), GETDATE()) AS [Event Time] , @@SERVERNAME as ServerName,@@servicename as ServiceName
FROM (
      SELECT record.value('(./Record/@id)[1]', 'int') AS record_id,
            record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int')
            AS [SystemIdle],
            record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]',
            'int')
            AS [SQLProcessUtilization], [timestamp]
      FROM (
            SELECT [timestamp], CONVERT(xml, record) AS [record]
            FROM sys.dm_os_ring_buffers
            WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
            AND record LIKE '%<SystemHealth>%') AS x
      ) AS y
ORDER BY record_id DESC;

 

5. for manual checking fo memory usage sql server

SELECT @@SERVERNAME as ServerName,@@servicename as ServiceName,cast(physical_memory_in_use_kb / 1024.0 AS NUMERIC(10,2)) as Memory_Usage_MB,
cast(physical_memory_in_use_kb / 1048576.0  AS NUMERIC(10,2)) as Memory_Usage_GB FROM sys.dm_os_process_memory

 
5.2 for manual checking memory usage sql server in % n mb

DECLARE @total_memory_mb FLOAT, @available_memory_mb FLOAT, @memory_usage_percentagesql FLOAT,@memory_usage_mbsql FLOAT;

-- Mengambil total memori fisik yang tersedia pada SQL Server dalam megabyte
SELECT @total_memory_mb = physical_memory_kb / 1024.0
FROM sys.dm_os_sys_info;

-- Mengambil jumlah memori SQL Server
SELECT @memory_usage_mbsql=cast(physical_memory_in_use_kb / 1024.0 AS NUMERIC(10,2))
FROM sys.dm_os_process_memory

-- Menghitung persentase penggunaan memori SQL Server
SET @memory_usage_percentagesql = (@memory_usage_mbsql / @total_memory_mb) * 100;

-- Menampilkan hasil dengan pembulatan
SELECT @@servername as ServerName,ROUND(@memory_usage_percentagesql, 0) AS 'Memory Usage(%) SQL',ROUND(@memory_usage_mbsql, 0) AS 'Memory Usage(MB) SQL',ROUND(@total_memory_mb, 0) AS 'Memory Total(MB) Server';
 

6. checking total memory server from sql server


SELECT @@SERVERNAME as ServerName,@@servicename as ServiceName,
  cast(total_physical_memory_kb / 1024.0 AS NUMERIC(10,2)) AS total_physical_memory_mb,
  cast(available_physical_memory_kb / 1024.0 AS NUMERIC(10,2)) AS available_physical_memory_mb,
  cast(total_page_file_kb / 1024.0 AS NUMERIC(10,2)) AS total_page_file_mb,
  cast(available_page_file_kb / 1024.0 AS NUMERIC(10,2)) AS available_page_file_mb
FROM sys.dm_os_sys_memory
 

 
 7. run from sqlcmd

sqlcmd -E -S mssql -d teguhth -Q "select getdate()as start;exec sp_total_usage_cpu_SQLProcessUtilization;select getdate() as Finish"
sqlcmd -E -S mssql -d teguhth -Q "select getdate()as start;exec sp_total_usage_memory_SQLProcessUtilization;select getdate() as Finish"
 

8. create bat script to special monitoring using remove some character

8.1 create bat script sp_total_usage_cpu_SQLProcessUtilization.bat


@echo off
setlocal enabledelayedexpansion

REM Run sqlcmd command and store output in a temporary file
sqlcmd -E -S mssql -d teguhth -Q "exec sp_total_usage_cpu_SQLProcessUtilization" > temp_output.txt

REM Filter out unwanted lines using findstr
type temp_output.txt | findstr /v /c:"Total Usage CPU SQL Utilization" /c:"-------" /c:"(1 rows affected)"

REM Delete the temporary file
del temp_output.txt

endlocal


8.2 create bat script sp_total_usage_memory_SQLProcessUtilization.bat

@echo off
setlocal enabledelayedexpansion

REM Run sqlcmd command and store output in a temporary file
sqlcmd -E -S mssql -d teguhth -Q "exec sp_total_usage_memory_SQLProcessUtilization" > temp_output.txt

REM Filter out unwanted lines using findstr
type temp_output.txt | findstr /v /c:"Total Usage Memory SQL Utilization mb" /c:"-------" /c:"(1 rows affected)"

REM Delete the temporary file
del temp_output.txt

endlocal
 


9. other it using loop bath script

@echo off
setlocal

rem Set your SQL Server connection details
set "serverName=localhost"
set "username=sa"
set "password=P@ssw0rd"
set "databaseName=teguhth"

rem Set the interval for checking (in seconds)
set "interval=5"

:loop
cls

rem Get CPU and memory usage using Tasklist
tasklist /fi "imagename eq sqlservr.exe" /fo table /v | find "SQL Server"

rem Get CPU usage using SQLCMD
sqlcmd -S %serverName% -U %username% -P %password% -d %databaseName% -Q "exec teguhth..sp_total_usage_cpu_SQLProcessUtilization"

rem Get Memory usage using SQLCMD
sqlcmd -S %serverName% -U %username% -P %password% -d %databaseName% -Q "exec sp_total_usage_memory_SQLProcessUtilization" -h-1 -W -s "|"

rem Wait for the specified interval
timeout /nobreak /t %interval% >nul

goto loop

 
 


No comments:

Post a Comment

Popular Posts