Sha256: 6130afbfdbe25eaf701672a503aa61b4fe5e9cb1fb34e1e50100ef2a72200e8e

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require 'net/http'
require 'jsonpath'

module Handler
  class << self
    def http_state config
      key = config["key"]
      value = begin
        res = Net::HTTP.get_response(URI(config["check"]))
        res.code == '200' ? 1 : 0
      rescue
        0
      end
      return key => value
    end

    def http_metric config
      begin
        res = Net::HTTP.get_response(URI(config["check"]))
        parse(res.body, config)
      rescue Exception => e
        puts e
        nil
      end
    end

    def shell_state config
      key = config["key"]
      command = config["check"]
      value = system(command) ? 1 : 0
      return key => value
    end

    def shell_metric config
      key = config["key"]
      command = config["check"]

      value = begin
        parse(`#{command}`, config)

      rescue
        nil
      end

      return key => value
    end

    private
    def parse(data, config)
      parser = config["parser"] || "text"
      self.send("parse_#{parser}", data, config)
    end

    def parse_text(data, config)
      key = config["key"]
      return key => data
    end

    def parse_json(data, config)
      raise "json parser: Invalid json recieved from target service" if (!valid_json?(data))
      key = resolve_json_path(data, config["key"])
      value = resolve_json_path(data, config["jsonvalue"])
      raise "json parser: key count should equal value count" if (key.length != value.length)
      key.zip(value).to_h
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chaoite-0.1.4 lib/chaoite/handler.rb