Class: Diplomat::Check

Inherits:
RestClient show all
Defined in:
lib/diplomat/check.rb

Overview

Methods for interacting with the Consul check API endpoint

Instance Method Summary collapse

Methods inherited from RestClient

access_method?, #concat_url, #configuration, #initialize, method_missing, respond_to?, respond_to_missing?, #use_named_parameter

Constructor Details

This class inherits a constructor from Diplomat::RestClient

Instance Method Details

#checks(options = {}) ⇒ OpenStruct

Get registered checks

Returns:

  • (OpenStruct)

    all data associated with the service



11
12
13
14
# File 'lib/diplomat/check.rb', line 11

def checks(options = {})
  ret = send_get_request(@conn, ['/v1/agent/checks'], options)
  JSON.parse(ret.body)
end

#deregister(check_id, options = {}) ⇒ Integer

Deregister a check

Parameters:

  • check_id (String)

    the unique id of the check

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Integer)

    Status code



64
65
66
67
# File 'lib/diplomat/check.rb', line 64

def deregister(check_id, options = {})
  ret = send_put_request(@conn, ["/v1/agent/check/deregister/#{check_id}"], options, nil)
  ret.status == 200
end

#fail(check_id, output = nil, options = {}) ⇒ Integer

Fail a check

Parameters:

  • check_id (String)

    the unique id of the check

  • output (String) (defaults to: nil)

    human-readable message will be passed through to the check’s Output field

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Integer)

    Status code



107
108
109
# File 'lib/diplomat/check.rb', line 107

def fail(check_id, output = nil, options = {})
  update_ttl(check_id, 'critical', output, options)
end

#pass(check_id, output = nil, options = {}) ⇒ Integer

Pass a check

Parameters:

  • check_id (String)

    the unique id of the check

  • output (String) (defaults to: nil)

    human-readable message will be passed through to the check’s Output field

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Integer)

    Status code



89
90
91
# File 'lib/diplomat/check.rb', line 89

def pass(check_id, output = nil, options = {})
  update_ttl(check_id, 'passing', output, options)
end

#register_script(check_id, name, notes, args, interval, options = {}) ⇒ Integer

Register a check rubocop:disable Metrics/ParameterLists

Parameters:

  • check_id (String)

    the unique id of the check

  • name (String)

    the name

  • notes (String)

    notes about the check

  • args (String[])

    command to be run for check

  • interval (String)

    frequency (with units) of the check execution

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Integer)

    Status code



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/diplomat/check.rb', line 25

def register_script(check_id, name, notes, args, interval, options = {})
  unless args.is_a?(Array)
    raise(Diplomat::DeprecatedArgument, 'Script usage is deprecated, replace by an array of args')
  end

  definition = JSON.generate(
    'ID' => check_id,
    'Name' => name,
    'Notes' => notes,
    'Args' => args,
    'Interval' => interval
  )
  ret = send_put_request(@conn, ['/v1/agent/check/register'], options, definition)
  ret.status == 200
end

#register_ttl(check_id, name, notes, ttl, options = {}) ⇒ Boolean

Register a TTL check

Parameters:

  • check_id (String)

    the unique id of the check

  • name (String)

    the name

  • notes (String)

    notes about the check

  • ttl (String)

    time (with units) to mark a check down

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Boolean)

    Success



49
50
51
52
53
54
55
56
57
58
# File 'lib/diplomat/check.rb', line 49

def register_ttl(check_id, name, notes, ttl, options = {})
  definition = JSON.generate(
    'ID' => check_id,
    'Name' => name,
    'Notes' => notes,
    'TTL' => ttl
  )
  ret = send_put_request(@conn, ['/v1/agent/check/register'], options, definition)
  ret.status == 200
end

#update_ttl(check_id, status, output = nil, options = {}) ⇒ Integer

Update a TTL check

Parameters:

  • check_id (String)

    the unique id of the check

  • status (String)

    status of the check. Valid values are “passing”, “warning”, and “critical”

  • output (String) (defaults to: nil)

    human-readable message will be passed through to the check’s Output field

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Integer)

    Status code



75
76
77
78
79
80
81
82
# File 'lib/diplomat/check.rb', line 75

def update_ttl(check_id, status, output = nil, options = {})
  definition = JSON.generate(
    'Status' => status,
    'Output' => output
  )
  ret = send_put_request(@conn, ["/v1/agent/check/update/#{check_id}"], options, definition)
  ret.status == 200
end

#warn(check_id, output = nil, options = {}) ⇒ Integer

Warn a check

Parameters:

  • check_id (String)

    the unique id of the check

  • output (String) (defaults to: nil)

    human-readable message will be passed through to the check’s Output field

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Integer)

    Status code



98
99
100
# File 'lib/diplomat/check.rb', line 98

def warn(check_id, output = nil, options = {})
  update_ttl(check_id, 'warning', output, options)
end