Advertisement
vartik

uptime.bat

Mar 13th, 2024 (edited)
1,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. rem Version: 1.2
  3. rem Author: Branislav Vartik
  4.  
  5. set loadsamples=60
  6. set loaddivisor=16
  7.  
  8. rem https://stackoverflow.com/questions/6359820/how-to-set-commands-output-as-a-variable-in-a-batch-file
  9. ::::: ---- defining the assign macro ---- ::::::::
  10. setlocal DisableDelayedExpansion
  11. (set LF=^
  12. %=EMPTY=%
  13. )
  14. set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  15.  
  16. ::set argv=Empty
  17. set assign=for /L %%n in (1 1 2) do ( %\n%
  18.    if %%n==2 (%\n%
  19.       setlocal enableDelayedExpansion%\n%
  20.       for /F "tokens=1,2 delims=," %%A in ("!argv!") do (%\n%
  21.          for /f "tokens=* delims=" %%# in ('%%~A') do endlocal^&set "%%~B=%%#" %\n%
  22.       ) %\n%
  23.    ) %\n%
  24. ) ^& set argv=,
  25.  
  26. ::::: -------- ::::::::
  27.  
  28. setlocal enableextensions enabledelayedexpansion
  29.  
  30. rem NOW
  31. set now=%time:~0,8%
  32. set now=%now: =0%
  33.  
  34. rem USERS
  35. %assign% qwinsta ^| findstr /r /c:"^...................[^ ]" ^| find /v /c " ID ",users
  36. set userstring=%users% user
  37. if %users% leq 9 set userstring= %userstring%
  38. if not %users% == 1 set userstring=%userstring%s
  39.  
  40. rem UPTIME
  41. %assign% wmic path Win32_PerfFormattedData_PerfOS_System Get SystemUptime ^| findstr /r /c:"[0-9]",uptimes
  42. set /a uptimes=%uptimes%, uptimed=uptimes / 86400, uptimes %%= 86400, uptimeh=uptimes / 3600, uptimes %%= 3600, uptimem=uptimes / 60, uptimes=uptimes %% 60
  43. if %uptimeh% leq 9 set uptimeh= %uptimeh%
  44. if %uptimem% leq 9 set uptimem=0%uptimem%
  45. set uptimestring=%uptimeh%:%uptimem%
  46. if %uptimed% == 1 set uptimestring=%uptimed% day, %uptimestring%
  47. if %uptimed% gtr 1 set uptimestring=%uptimed% days, %uptimestring%
  48.  
  49. rem LOAD AVERAGE
  50. set sum=
  51. for /f "tokens=2 delims=," %%n in ('typeperf -sc %loadsamples% "\Processor(_Total)\%% Processor Time" ^| findstr /r /c:"\""[0-9]"') do (
  52.     set n=%%n
  53.     set n=!n:"=!
  54.     set /a sum+=n
  55. )
  56. set /a load=sum*100/loadsamples/loaddivisor
  57. set load=00%load%
  58. set load=%load:~-3,1%.%load:~-2%
  59.  
  60. set msg= %now% up %uptimestring%, %userstring%,  load average: %load%, %load%, %load%
  61. echo %msg%
  62.  
  63. rem If parameter is given, send to syslog server
  64. if not [%1] == [] (
  65.     %~dp0logger.exe >nul -l %1 -p user.info -t logger "%msg%"
  66.     rem https://www.adiscon.com/adiscon-tools/an-unix-like-logger-for-windows/
  67. )
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement