Giter Club home page Giter Club logo

gmsv_serverstat's Introduction

๐Ÿ“Š gmsv_serverstat

Simple serverside binary module which can expose information about system resource usage to Lua.

Installation

Download the relevant module for your server's operating system and platform/Gmod branch from the releases section.

Drop the module into garrysmod/lua/bin/ in your server's files. If the bin folder doesn't exist, create it.

If you're not sure on what operating system/platform your server is running, run this in your server's console:

lua_run print((system.IsWindows()and"Windows"or system.IsLinux()and"Linux"or"Unsupported").." "..(jit.arch=="x64"and"x86-64"or"x86"))

Usage

To load the module, simply require it:

require("serverstat")

Blocking Functions

Some of these functions may block the main thread whilst acquiring information about the system & process. Make sure to call these functions sparingly.

Some functions will only block when they are called for the first time.

Some functions may also return default values (such as 0) when called for the first time.

-- Gets SRCDS' CPU usage
-- [float] 0..1
serverstat.ProcessCPUUsage()

-- Gets SRCDS' memory usage in MiB
-- [float] MiB
serverstat.ProcessMemoryUsage()

-- Gets the system's total CPU usage
-- [float] 0..1
serverstat.SystemCPUUsage()

-- Gets the system's current memory usage in MiB
-- [float] MiB
serverstat.SystemMemoryUsage()

-- Gets the system's total memory installed in MiB
-- [float] MiB
serverstat.SystemTotalMemory()

-- Gets the system's available memory in MiB
-- [float] MiB
serverstat.SystemAvailableMemory()

-- Gets the system's number of physical CPUs (cores)
-- [integer]
serverstat.PhysicalCPUs()

-- Gets the system's number of logical CPUs
-- Roughly equates to physical cores (CPUs) ร— threads per core
-- [integer]
serverstat.LogicalCPUs()

-- Fetches all resource usage information about the system and process as a table.
-- The table is keyed by the above function names and their respective return data as the value.
-- [table]
serverstat.All()

-- Fetches all SYSTEM resource usage information about the system and process as a table.
-- The table is keyed by the above function names and their respective return data as the value.
-- [table]
serverstat.AllSystem()

-- Fetches all SRCDS resource usage information about the system and process as a table.
-- The table is keyed by the above function names and their respective return data as the value.
-- [table]
serverstat.AllProcess()

Asynchronous Functions

Each blocking function has an asynchronous equivalent in the serverstat.async table which takes a single function callback argument.

Using the asynchronous functions will acquire the requested information on a separate thread.

The thread goes to sleep when unused and uses no system resources until needed again.

serverstat.async.ProcessCPUUsage(function(data) ... end)
serverstat.async.ProcessMemoryUsage(function(data) ... end)
serverstat.async.SystemCPUUsage(function(data) ... end)
serverstat.async.SystemMemoryUsage(function(data) ... end)
serverstat.async.SystemTotalMemory(function(data) ... end)
serverstat.async.SystemAvailableMemory(function(data) ... end)
serverstat.async.PhysicalCPUs(function(data) ... end)
serverstat.async.LogicalCPUs(function(data) ... end)
serverstat.async.All(function(data) ... end)
serverstat.async.AllSystem(function(data) ... end)
serverstat.async.AllProcess(function(data) ... end)

Realtime Functions

Additionally, serverstat provides a "realtime" data API.

This is to discourage multiple script authors from making what is essentially the same thing; an autorefreshing timer for the data this module provides.

These functions will return data that is updated roughly every 250ms. They are synchronous; updating the data is automatically done in a timer the module creates for you.

This timer will only be created if you start using these functions. It will persist until the server shuts down.

The realtime data API is deliberately missing functions such as LogicalCPUs and PhysicalCPUs, because these are constant values.

-- You don't need to call these functions; they are provided for convenience if you want to control the realtime updater timer yourself.
serverstat.realtime.Start()
serverstat.realtime.Stop()
serverstat.realtime.SetInterval(seconds)

serverstat.realtime.ProcessCPUUsage()
serverstat.realtime.ProcessMemoryUsage()

serverstat.realtime.SystemCPUUsage()
serverstat.realtime.SystemMemoryUsage()
serverstat.realtime.SystemAvailableMemory()

-- These functions return a reference to the table that is shared
-- with other addons. If you are going to be mutating this table,
-- please use the Copy functions below instead.
serverstat.realtime.All() -- { System = serverstat.realtime.AllSystem(), Process = serverstat.realtime.AllProcess() }
serverstat.realtime.AllSystem() -- { CPUUsage = [float], MemoryUsage = [float] MIB, AvailableMemory = [float] MiB }
serverstat.realtime.AllProcess() -- { CPUUsage = [float], MemoryUsage = [float] MIB }

-- These functions return a copy of the table that is shared
-- with other addons. If you aren't going to be mutating the table,
-- you should just use the above functions instead.
serverstat.realtime.AllCopy()
serverstat.realtime.AllSystemCopy()
serverstat.realtime.AllProcessCopy()

gmsv_serverstat's People

Contributors

williamvenner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gmsv_serverstat's Issues

Couldn't load module library! (/lib/i386-linux-gnu/libc.so.6: version `GLIBC_2.28' not found

lua_run require("serverstat")

require("serverstat")...

[ERROR] lua_run:1: Couldn't load module library! (/lib/i386-linux-gnu/libc.so.6: version `GLIBC_2.28' not found (required by /root/gmod/garrysmod/lua/bin/gmsv_serverstat_linux.dll))

  1. require - [C]:-1
  2. unknown - lua_run:1

Ubuntu 18.04

print((system.IsWindows()and"Windows"or system.IsLinux()and"Linux"or"Unsupported").." "..(jit.arch=="x64"and"x86-64"or"x86"))...
Linux x86

`ProcessCPUUsage` field values doesn't mean anything

Recently, I switched from the old version of this module to the new one and I noticed that the ProcessCPUUsage field was giving completely ununderstandable values and didn't seem to make sense compared to the others.

For example, here is the result I can get (it happens to me all the time and is not exceptional):

> PrintTable(serverstat.All())...
LogicalCPUs = 8
PhysicalCPUs = 8
ProcessCPUUsage = 40300 -- ???
ProcessMemoryUsage = 572.24609375
SystemAvailableMemory = 19969.983398438
SystemCPUUsage = 41.920177459717
SystemMemoryUsage = 49319.202148438
SystemTotalMemory = 70362.319335938

The same thing happens with the asynchronous function (but sometimes it returns 0 without any reason).

> serverstat.async.All(function(data) PrintTable(data) end)...
LogicalCPUs = 8
PhysicalCPUs = 8
ProcessCPUUsage = 0 -- ???
ProcessMemoryUsage = 573.2109375
SystemAvailableMemory = 20000.5234375
SystemCPUUsage = 100 -- Why not
SystemMemoryUsage = 49288.662109375
SystemTotalMemory = 70362.319335938

For information, my server is on a dedicated machine hosted by a game provider (so I don't have control of the machine to check the stats). AFAIK, the server is running under Debian 10 64-bit.

] version
Protocol version 24
Exe version 2021.06.09 (garrysmod)
Exe build: 14:56:57 Jul 12 2021 (8283) (4000)
GMod version 2021.07.12, branch: unknown
Linux 32bit Dedicated Server

And here is the script I use to get the information from the module.

-- From : https://github.com/WilliamVenner/gmsv_serverstat/
if not file.Exists("bin/gmsv_serverstat_*.dll", "LUA") then return end

require("serverstat")

concommand.Add("server-stats", function(ply)

    local data = serverstat.All()
    local phrase = "CPU Usage : %s (%s/%s)\nMemory Usage : %s (%s)"

    phrase = string.format(phrase,

        math.Round(data.ProcessCPUUsage, 2) .. "%",

        data.LogicalCPUs, data.PhysicalCPUs,

        string.NiceSize(data.ProcessMemoryUsage * 10 ^ 6),

        string.NiceSize(data.SystemTotalMemory * 10 ^ 6)

    )

    Raven.Server.SendChatMessage(ply, color_red, phrase)

end)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.