class Checkson::UI

Simple user interface for checkson

Public Class Methods

new() click to toggle source
# File lib/checkson/ui.rb, line 10
def initialize
  @jsonout = []

  set_default_options
  parse_options
  start_checks
end

Private Instance Methods

die(msg) click to toggle source
# File lib/checkson/ui.rb, line 111
def die(msg)
  warn(msg)
  exit(1)
end
jsonadd(sym, check) click to toggle source
# File lib/checkson/ui.rb, line 101
def jsonadd(sym, check)
  data = { description: check.description, status: sym.status }
  @jsonout.append(data)
end
output_json() click to toggle source
# File lib/checkson/ui.rb, line 84
def output_json
  puts @jsonout.to_json
end
output_plain(sym, check) click to toggle source
# File lib/checkson/ui.rb, line 88
def output_plain(sym, check)
  status_column = winsize[1] / 3 * 2
  print check.description
  case sym.status
  when :ok
    puts "\033[#{status_column}G\033[1;32mOK\033[0m"
  when :failed
    puts "\033[#{status_column}G\033[1;31mFAILED\033[0m"
    check.help.each { |message| puts "--> #{message}" } unless check.help.empty?
    sym.messages.each { |message| puts "   * #{message}" }
  end
end
parse_options() click to toggle source
# File lib/checkson/ui.rb, line 55
def parse_options
  OptionParser.new do |opts|
    opts.banner = 'Usage: checkson [options]'
    opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
      @opts[:verbose] = v
    end
    opts.on('-j', '--[no-]jsonexport', 'do output as json') do |j|
      @opts[:jsonexport] = j
    end
    opts.on('-d', '--datasource [TYPE]', %i[file api], 'Set datasource type', '(file, api)') do |d|
      @opts[:datasource] = d
    end
    opts.on('-f', '--file [FILE]', 'Config file to use') do |file|
      if File.exist?(file)
        @opts[:config_file] = file
      else
        die("Config file #{file} does not exist")
      end
    end
    opts.on('-e', '--endpoint [ADDR]', 'Config api to use') do |ep|
      if ep =~ URI::DEFAULT_PARSER.make_regexp
        @opts[:endpoint] = ep
      else
        die('URL of endpoint not valid')
      end
    end
  end.parse!
end
set_default_options() click to toggle source
# File lib/checkson/ui.rb, line 45
def set_default_options
  @opts = {
    datasource: :file,
    verbose: false,
    jsonexport: false,
    config_file: '/etc/checkson.rb',
    endpoint: 'https://checkson.fsrv.services:8080'
  }
end
start_checks() click to toggle source
# File lib/checkson/ui.rb, line 20
def start_checks
  if @opts[:datasource].eql? :api
    client = Checkson::APIClient.new(@opts[:endpoint], '/tmp/checkson.rb')
    client.writechecks
    @opts[:config_file] = client.outfile
  end
  @config = Checkson::Config.new(@opts[:config_file])

  @num_checks = @config.checks.length
  @num_ok = 0
  @config.checks.each do |check|
    c = check.klass.new(check.params)
    c.check
    @num_ok += 1 if c.status.eql? :ok
    output_plain c, check if @opts[:verbose]
    if @opts[:jsonexport]
      @opts[:verbose] = false
      jsonadd(c, check)
    end
  end
  @num_failed = (@num_checks - @num_ok)
  puts "-> #{@num_checks} checks performed: #{@num_ok} ok, #{@num_failed} failed" unless @opts[:jsonexport]
  output_json if @opts[:jsonexport]
end
winsize() click to toggle source
# File lib/checkson/ui.rb, line 106
def winsize
  require 'io/console'
  IO.console.winsize
end