Method: Zabbix::Monitor#acknowledge

Defined in:
lib/zmonitor.rb

#acknowledge(pattern = '') ⇒ Object

Raises:

  • (StandardError)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/zmonitor.rb', line 119

def acknowledge(pattern = '')
  puts 'Retrieving list of active unacknowledged triggers that match: '.bold.blue + '%s'.green % pattern, ''
  filtered = []
  eventlist = self.get_events()
  eventlist.each do |e|
    if e[:hostname] =~ /#{pattern}/ or e[:description] =~ /#{pattern}/
      event = @api.event.get_last_by_trigger(e[:id])
      e[:eventid] = event['eventid'].to_i
      e[:acknowledged] = event['acknowledged'].to_i
      filtered << e if e[:acknowledged] == 0
    end
  end
  abort("No alerts found, so aborting".yellow) if filtered.length == 0
  filtered.each.with_index do |a,i|
    message = '%s - %s (%s)'.color_by_severity(a[:severity]) % [ a[:fuzzytime], a[:description], a[:hostname] ]
    puts "%4d >".bold % (i+1) + message
  end

  puts '', '       Selection - enter "all", or a set of numbers listed above separated by spaces.'
  print ' Sel > '.bold
  input = STDIN.gets.chomp()

  no_ack_msg = "Not acknowledging anything."
  raise StandardError.new("No input. #{no_ack_msg}".green) if input == ''
  to_ack = (1..filtered.length).to_a if input =~ /^\s*all\s*$/ # only string we'll accept
  raise StandardError.new("Invalid input. #{no_ack_msg}".red) if to_ack.nil? and (input =~ /^([0-9 ]+)$/).nil?
  to_ack = input.split.map(&:to_i).sort if to_ack.nil? # Split our input into a sorted array of integers
  # Let's first check if a value greater than possible was given, to help prevent typos acknowledging the wrong thing
  to_ack.each { |i| raise StandardError.new("You entered a value greater than %d! Please double check. #{no_ack_msg}".yellow % filtered.length) if i > filtered.length }

  puts  '', '       Message   - enter an acknowledgement message below, or leave blank for the default.'
  print ' Msg > '.bold
  message = STDIN.gets.chomp()
  puts

  # Finally! Acknowledge EVERYTHING
  to_ack.each do |a|
    puts 'Acknowledging: '.green + '%s (%s)' % [ filtered[a-1][:description], filtered[a-1][:hostname] ]
    if message == ''
      @api.whoami = @api.user.get_fullname()
      @api.event.acknowledge(filtered[a-1][:eventid])
    else
      @api.event.acknowledge(filtered[a-1][:eventid], message)
    end
  end
end