Class: Op5util::Monitor

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/op5util/commit.rb,
lib/op5util/monitor.rb,
lib/op5util/add_host.rb,
lib/op5util/list_hosts.rb,
lib/op5util/status_host.rb,
lib/op5util/autocomplete.rb,
lib/op5util/add_hostgroups.rb,
lib/op5util/status_summary.rb,
lib/op5util/list_hostgroups.rb,
lib/op5util/method_template.rb,
lib/op5util/schedule_checks.rb,
lib/op5util/acknowledge_alarm.rb,
lib/op5util/schedule_downtime.rb

Overview

Foo

Instance Method Summary collapse

Constructor Details

#initialize(monitor, username, password) ⇒ Monitor

debug_output $stdout



18
19
20
21
22
23
24
25
26
27
# File 'lib/op5util/monitor.rb', line 18

def initialize(monitor, username, password)
  @monitor = monitor
  @auth = { username: username, password: password }
  @base_uri = "https://#{@monitor}/api/"
  url = 'status/status?format=json'
  response = self.class.get(@base_uri + url, basic_auth: @auth, verify: false)
  raise AuthenticationError if !response.nil? && !response.code.nil? && response.code == 401
  raise CommunicationError if response.nil? || response.code.nil? || response.code != 200
  self
end

Instance Method Details

#acknowledge_all_alarms(options) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
# File 'lib/op5util/acknowledge_alarm.rb', line 25

def acknowledge_all_alarms(options)
  response = self.class.get(@base_uri + 'config/host?format=json',
                            basic_auth: @auth, verify: false)
  raise ApiError unless response.code == 200
  JSON.parse!(response.body).map { |h| h['name'] }.each do |host|
    acknowledge_host_alarms(host, options)
  end
end

#acknowledge_host_alarms(host, options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/op5util/acknowledge_alarm.rb', line 6

def acknowledge_host_alarms(host, options)
  host_states = host_status(host)
  if host_states[:host] > 0
    puts 'Acknowledge host alarm for host ' + host
    ack_host_alarm(host, options)
  else
    puts 'No alarm for host ' + host + ', not acking host'
  end
  if host_states[:services].count > 0
    host_states[:services].each do |s|
      ack_host_service(host, s, options)
      puts "Service \"#{s}\" acknowledged" if options[:verbose]
    end
    puts 'All service alarms for host ' + host + ' acknowledged'
  else
    puts "No service alarms to acknowledge for host #{host}"
  end
end

#add_host(host, options) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
# File 'lib/op5util/add_host.rb', line 13

def add_host(host, options)
  body = build_add_host_request_body(host, options)
  response = self.class.post(@base_uri + 'config/host',
                             headers: { 'Content-Type' => 'application/json' },
                             body: body, basic_auth: @auth, verify: false)
  raise ApiError, "Response code: #{response.code}, Message: #{response.body}" if response.code != 201
  commit_op5_config unless options['no-commit'.to_sym]
end

#add_hostgroups(host, hostgroups, no_commit_config) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/op5util/add_hostgroups.rb', line 8

def add_hostgroups(host, hostgroups, no_commit_config)
  hostgroups.each do |group|
    members = get_hostgroup_members(group)
    if !members.grep(host).empty?
      puts "Host #{host} is already a member of hostgroup #{group}"
    else
      raise NoSuchHostError, host unless host_exist?(host)
      members << host
      update_hostgroup(group, members)
    end
  end
  commit_op5_config unless no_commit_config
end

#autocompleteObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/op5util/autocomplete.rb', line 6

def autocomplete
  s = '
# Add this to suitable shell startup file to get tab autocomplete for op5util.
# op5util autocomplete >> ~/.bashrc
# ZSH-users should uncomment the line below
# autoload -U +X bashcompinit && bashcompinit

_op5util()
{
local cur prev opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
op5command="${COMP_WORDS[1]}"
opts="acknowledge add add_hostgroups downtime help hostgroups schedule status"

case "${op5command}" in
    acknowledge)
        COMPREPLY=( $(compgen -W "-c --comment=Work_in_Progress -p --persistent --no-persistent -v --verbose --no-verbose $(op5util hosts)" -- ${cur}) )
        ;;
    add)
        COMPREPLY=( $(compgen -W "-a --alias=\" -c --contactgroups=\" -g --hostgroups=\" -i --ipaddr=\" HOST_TO_ADD  $(op5util hostgroups)" -- ${cur}) )
        ;;
    add_hostgroups)
        COMPREPLY=( $(compgen -W "-g --hostgroup=  $(op5util hosts)" -- ${cur}) )
        ;;
    autocomplete)
        COMPREPLY=( $(compgen -W "ENTER" -- ${cur}) )
        ;;
    downtime)
        COMPREPLY=( $(compgen -W "-t --time=n_hour_duration_of_downtime -w --wait=n_hours_before_downtime_start -c --comment=  $(op5util hosts)" -- ${cur}) )
        ;;
    hostgroups)
        COMPREPLY=( $(compgen -W "-l --long  $(op5util hostgroups)" -- ${cur}) )
        ;;
    schedule)
        COMPREPLY=( $(compgen -W "-v --verbose  $(op5util hosts)" -- ${cur}) )
        ;;
    status)
        COMPREPLY=( $(compgen -W "-l --long -s --short  $(op5util hosts)" -- ${cur}) )
        ;;
    *)
        ;;
esac
if [ -z $COMPREPLY ]; then
    COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
fi
return 0
}
complete -F _op5util op5util
'
  puts s
end

#commit_op5_configObject



6
7
8
9
10
11
12
# File 'lib/op5util/commit.rb', line 6

def commit_op5_config
  if pending_changes.empty?
    puts 'No changes to commit'
  else
    do_commit
  end
end

#list_hostgroups(hostgroup, options) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/op5util/list_hostgroups.rb', line 7

def list_hostgroups(hostgroup, options)
  response = self.class.get(@base_uri + 'config/hostgroup?format=json',
                            basic_auth: @auth, verify: false)
  raise ApiError unless response.code == 200
  if hostgroup.nil?
    hostgroups = JSON.parse!(response.body).map { |h| h['name'] }
  else
    hostgroups = JSON.parse!(response.body).map { |h| h['name'] }.select { |hg| hg == hostgroup }
    raise NoSuchHostgroupError if hostgroups.empty?
  end
  if options[:long]
    list_hostgroups_with_services(hostgroups)
  else
    hostgroups.each { |h| puts h }
  end
end

#list_hosts(host, options) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
# File 'lib/op5util/list_hosts.rb', line 6

def list_hosts(host, options)
  response = self.class.get(@base_uri + 'config/host?format=json',
                            basic_auth: @auth, verify: false)
  raise ApiError unless response.code == 200
  JSON.parse!(response.body).map { |h| h['name'] }.select { |h| host.nil? ? true : h == host }.each do |h|
    puts h
    print_detailed_host_info(h) if options[:long]
  end
end

#method_templateObject

Raises:



6
7
8
9
10
11
# File 'lib/op5util/method_template.rb', line 6

def method_template
  response = self.class.get(@base_uri + 'some/path?format=json',
                            basic_auth: @auth, verify: false)
  raise ApiError unless response.code == 200
  puts 'Do something'
end

#schedule_checks(host, options) ⇒ Object



6
7
8
9
# File 'lib/op5util/schedule_checks.rb', line 6

def schedule_checks(host, options)
  schedule_host_check(host)
  schedule_service_checks_for_host(host, options)
end

#schedule_downtime(host_string, options) ⇒ Object



7
8
9
10
11
# File 'lib/op5util/schedule_downtime.rb', line 7

def schedule_downtime(host_string, options)
  host_string.split(',').each do |host|
    schedule_downtime_for_host(host, options)
  end
end

#status_host(host, options) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/op5util/status_host.rb', line 8

def status_host(host, options)
  full_status = JSON.parse!(get_host_status(host))
  if options[:short] || (!options[:short] && !options[:long])
    print_short_status(full_status)
  else
    print_full_status(full_status)
  end
end

#status_summary(options) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/op5util/status_summary.rb', line 7

def status_summary(options)
  if options[:long]
    print_hosts_summary
  else
    print_summary
  end
end