Class: Op5util::Monitor
- Inherits:
-
Object
- Object
- Op5util::Monitor
- 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
- #acknowledge_all_alarms(options) ⇒ Object
- #acknowledge_host_alarms(host, options) ⇒ Object
- #add_host(host, options) ⇒ Object
- #add_hostgroups(host, hostgroups, no_commit_config) ⇒ Object
- #autocomplete ⇒ Object
- #commit_op5_config ⇒ Object
-
#initialize(monitor, username, password) ⇒ Monitor
constructor
debug_output $stdout.
- #list_hostgroups(hostgroup, options) ⇒ Object
- #list_hosts(host, options) ⇒ Object
- #method_template ⇒ Object
- #schedule_checks(host, options) ⇒ Object
- #schedule_downtime(host_string, options) ⇒ Object
- #status_host(host, options) ⇒ Object
- #status_summary(options) ⇒ Object
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
25 26 27 28 29 30 31 32 |
# File 'lib/op5util/acknowledge_alarm.rb', line 25 def acknowledge_all_alarms() 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, ) 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, ) host_states = host_status(host) if host_states[:host] > 0 puts 'Acknowledge host alarm for host ' + host ack_host_alarm(host, ) 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, ) puts "Service \"#{s}\" acknowledged" if [: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
13 14 15 16 17 18 19 20 |
# File 'lib/op5util/add_host.rb', line 13 def add_host(host, ) body = build_add_host_request_body(host, ) 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 ['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 |
#autocomplete ⇒ Object
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_config ⇒ Object
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
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, ) 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 [:long] list_hostgroups_with_services(hostgroups) else hostgroups.each { |h| puts h } end end |
#list_hosts(host, options) ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/op5util/list_hosts.rb', line 6 def list_hosts(host, ) 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 [:long] end end |
#method_template ⇒ Object
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, ) schedule_host_check(host) schedule_service_checks_for_host(host, ) 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, ) host_string.split(',').each do |host| schedule_downtime_for_host(host, ) 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, ) full_status = JSON.parse!(get_host_status(host)) if [:short] || (![:short] && ![: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() if [:long] print_hosts_summary else print_summary end end |