Sha256: 2ec78d4e1e781fc4585f22136c57a6281aaa88cfa8eb94f857415c4f878ea171

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module StatsLite
  module Default
    class << self
      def host
        {
          hostname: h.command("hostname", cache: true),
          ip: ip
        }
      end
      def cpu
        {
          model: h.command("lscpu | grep 'Model name' | cut -f 2 -d \":\" | awk '{$1=$1}1'", cache: true),
          cores: h.command("nproc", cache: true),
          usage: _cpu_usage
        }
      end

      def ip
        {
          public: h.command("curl -s ifconfig.me", cache: true)
        }
      end

      def ram
        result = -> do
          map = Vidibus::Sysinfo.memory.to_h
          map.map do |k, v|
            map[k] = "#{(v / 1024.to_f).round(2)}GB"
          end
          map
        end

        { usage: h.fetch(:ram, result, expires_in: 10) }
      end

      def _cpu_usage
        total = <<-CMD
(grep 'cpu ' /proc/stat;sleep 0.1;grep 'cpu ' /proc/stat)|awk -v RS="" '{print ""($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)"%"}'
        CMD
        {
          total: fmt_nr(h.command(total))
        }
      end

      def hdd
        result = -> do
          map =  Vidibus::Sysinfo.storage.to_h
          map.map do |k, v|
            map[k] = "#{v}GB"
          end
        end

        { usage: h.fetch(:hdd, result, expires_in: 60) }
      end

      def fmt_nr(number)
        "#{number.gsub("%", "").to_i}%"
      end

      def h
        StatsLite::Helper
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stats_lite-0.5.1 lib/stats_lite/default.rb
stats_lite-0.5.0 lib/stats_lite/default.rb
stats_lite-0.1.0 lib/stats_lite/default.rb