Send CPU load and RAM usage from BAT file in Windows

This is an example of sending the CPU load and RAM usage from a Windows machine using a BAT file. It retrieves the CPU load and RAM usage using the built-in Windows command 'wmic' and sends it to Gazer Cloud.

BAT File Code


@echo off
setlocal enabledelayedexpansion
rem === Set your API_KEY below ==========================
set API_KEY=StK4qZU7V2QfhR7XtZAD2k
rem =====================================================

for /f "usebackq tokens=* delims=" %%A in (`curl -s https://gazer.cloud/view_url_by_api_key/%API_KEY%`) do set VIEW_URL=%%A
echo =====================================================
echo View URL:
echo %VIEW_URL%
echo =====================================================
echo Press Ctrl+C to stop sending data.
echo.

:loop

for /f "tokens=* delims=" %%A in ('wmic cpu get loadpercentage ^| findstr /r "^[0-9]"') do (
    set CPU_LOAD_RAW=%%A
)

set CPU_LOAD=%CPU_LOAD_RAW: =%
echo CPU Load: !CPU_LOAD!%%


for /f "tokens=1,2 delims==" %%A in ('wmic OS get FreePhysicalMemory^,TotalVisibleMemorySize /Value ^| findstr "="') do (
    if "%%A"=="FreePhysicalMemory" set FREE_MEM_KB=%%B
    if "%%A"=="TotalVisibleMemorySize" set TOTAL_MEM_KB=%%B
)

set /a FREE_MEM_MB=%FREE_MEM_KB% / 1024
set /a TOTAL_MEM_MB=%TOTAL_MEM_KB% / 1024
set /a USED_MEM_MB=%TOTAL_MEM_MB% - %FREE_MEM_MB%

set /a MEM_PERCENT=(%USED_MEM_MB% * 100) / %TOTAL_MEM_MB%

echo RAM: %USED_MEM_MB% MB / %TOTAL_MEM_MB% MB (%MEM_PERCENT%%%)

set VALUE_CPU=!CPU_LOAD!
set VALUE_CPU_Name="CPU%%20Load"
set VALUE_CPU_UOM=%%25

set VALUE_MEM=!USED_MEM_MB!
set VALUE_MEM_Name="MEM%%20Load"
set VALUE_MEM_UOM="MB"

set VALUE_MEM_Percent=!MEM_PERCENT!
set VALUE_MEM_Percent_Name="MEM%%20Load%%20(%%25)"
set VALUE_MEM_Percent_UOM=%%25

echo Sending value: !VALUE_CPU! !VALUE_MEM! !VALUE_MEM_Percents!

set CMD=curl -s -X POST "https://gazer.cloud/set/%API_KEY%"
set CMD=!CMD! -d "/=!VALUE_CPU!"
set CMD=!CMD! -d "/_name=!VALUE_CPU_Name!"
set CMD=!CMD! -d "/_uom=!VALUE_CPU_UOM!"
set CMD=!CMD! -d "/Mem=!VALUE_MEM!"
set CMD=!CMD! -d "/Mem/_name=!VALUE_MEM_Name!"
set CMD=!CMD! -d "/Mem/_uom=!VALUE_MEM_UOM!"
set CMD=!CMD! -d "/MemPercent=!VALUE_MEM_Percent!"
set CMD=!CMD! -d "/MemPercent/_name=!VALUE_MEM_Percent_Name!"
set CMD=!CMD! -d "/MemPercent/_uom=!VALUE_MEM_Percent_UOM!"
!CMD!
	
timeout /t 1 >nul
goto loop
        

Instructions

  1. Copy the above BAT file code into a text editor and save it with a .bat extension, e.g., send_cpu_mem_load.bat.
  2. Replace the placeholder API key (StK4qZU7V2QfhR7XtZAD2k) with your actual Gazer Cloud API key.
  3. Open a Command Prompt window and navigate to the directory where you saved the BAT file.
  4. Run the BAT file by typing its name (e.g., send_cpu_mem_load.bat) and pressing Enter.
  5. The script will start sending CPU load and RAM usage data to Gazer Cloud every second. You can stop it by pressing Ctrl+C.
Copyright © 2020-2025 Gazer.Cloud Authors. All rights reserved.