Sha256: 90ed151320256ada1302b7aab344cf41813ea128ad8d81d22b02ef773b537130

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module RailsServerMonitor
  class ServerSetup
    attr_reader :server

    def initialize
      @server = RailsServerMonitor::Server.find_or_initialize_by(hostname: hostname)
    end

    def call
      server.tap do
        next update_server if server.updated_at.blank?

        needs_update = (Time.zone.now - server.updated_at) >= config.update_server_interval
        update_server if needs_update
      end
    end

    private
      def hostname
        @hostname ||= config.hostname.call.squish
      end

      def update_server
        server.assign_attributes({
          last_seen_at: Time.zone.now,
          system_os: system_os,
          system_os_language: system_os_language,
          system_os_timezone: system_os_timezone,
          system_kernel_version: system_kernel_version,
          system_cpu_name: system_cpu_name,
          system_cpu_cores: system_cpu_cores,
          system_cpu_frequency: system_cpu_frequency,
          system_ram_available_in_mb: system_ram_available_in_mb,
          system_hdd_available_in_gb: system_hdd_available_in_gb
        })

        server.save!
      end

      def system_os
        File.read("/etc/lsb-release") if File.exist?("/etc/lsb-release")
      end

      def system_os_language
        ENV["LANG"]
      end

      def system_os_timezone
        File.read("/etc/timezone")
      end

      def system_kernel_version
        `uname -r`.squish
      end

      def system_cpu_name
        Sys::CPU.model
      end

      def system_cpu_cores
        Sys::CPU.num_cpu
      end

      def system_cpu_frequency
        Sys::CPU.freq
      end

      def system_ram_available_in_mb
        ram_helper[:total]
      end

      def system_hdd_available_in_gb
        storage_helper[:total]
      end

      def config
        RailsServerMonitor.config
      end

      def storage_helper
        @store ||= Vidibus::Sysinfo.storage.to_h
      end

      def ram_helper
        @ram_helper ||= Vidibus::Sysinfo.memory.to_h
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails-server-monitor-0.2.1 app/services/rails_server_monitor/server_setup.rb
rails-server-monitor-0.2.0 app/services/rails_server_monitor/server_setup.rb
rails-server-monitor-0.1.10 app/services/rails_server_monitor/server_setup.rb
rails-server-monitor-0.1.9 app/services/rails_server_monitor/server_setup.rb