DEV Community

sebyx07
sebyx07

Posted on

Get server statistics using 🚀StatsLite:

StatsLite

Here is the link to the tool

Simple way to get server information as json. Easy to extend with a config.rb file.
Embeddable in Rails

Standalone usage

Requirements ruby, min version 2

sudo apt-get install -y ruby build-essential ruby-dev
Enter fullscreen mode Exit fullscreen mode

Usage standalone, you might need to run some commands as sudo.

gem install stats_lite --no-ri --no-rdoc
stats-lite
curl http://localhost:9111
Enter fullscreen mode Exit fullscreen mode

Add to startup, crontab

crontab -e
# then add
@reboot /usr/local/bin/stats-lite
Enter fullscreen mode Exit fullscreen mode

demo

create a config.rb then stats-lite

# example config.rb
# basic usage

StatsLite.configure do |s|
  s.password "1234" # password protection
  s.port 9111 # listening port
end
Enter fullscreen mode Exit fullscreen mode
# now it's protected by passowrd
curl http://localhost:9111?password=1234 
Enter fullscreen mode Exit fullscreen mode

Rails usage

gem "stats_lite"

# routes.rb
mount StatsLite::App => "/server-stats" 
Enter fullscreen mode Exit fullscreen mode

Advanced configuration


StatsLite.configure do |s, h|
  s.password ENV["STATS_LITE_PASS"] # defaults to nil, unprotected
  s.port ENV["STATS_LITE_PORT"] # defaults to 9111

  s.data -> (data) do # add more data
    data[:ruby_current_time] = Time.now # simple value
    data[:linux_time] = h.command("date") # bash command, supports {cache: true, expires_in: 60} 

    data[:slow_command] = h.fetch :slow_command, -> {
      sleep 1
      "slow command"
    }, expires_in: 5 # cached ruby value
  end

  s.app do |sinatra| # extend the app, add multiple routes, which are protected by the password
    sinatra.get("/another_route") do
      content_type :json

      { cpus: h.command("nproc", { cache: true }) }.to_json
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Top comments (0)