Class: Wavefront::Cli::Alerts

Inherits:
Wavefront::Cli show all
Defined in:
lib/wavefront/cli/alerts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Wavefront::Cli

#initialize, #load_profile

Constructor Details

This class inherits a constructor from Wavefront::Cli

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



24
25
26
# File 'lib/wavefront/cli/alerts.rb', line 24

def arguments
  @arguments
end

#optionsObject

Returns the value of attribute options.



24
25
26
# File 'lib/wavefront/cli/alerts.rb', line 24

def options
  @options
end

Instance Method Details

#human_line(k, v) ⇒ Object



106
107
108
# File 'lib/wavefront/cli/alerts.rb', line 106

def human_line(k, v)
  '%-22s%s' % [k, v]
end

#human_line_additionalInformation(k, v) ⇒ Object



139
140
141
# File 'lib/wavefront/cli/alerts.rb', line 139

def human_line_additionalInformation(k, v)
  human_line(k, indent_wrap(v))
end

#human_line_alertStates(k, v) ⇒ Object



135
136
137
# File 'lib/wavefront/cli/alerts.rb', line 135

def human_line_alertStates(k, v)
  human_line(k, v.join(','))
end

#human_line_created(k, v) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/wavefront/cli/alerts.rb', line 110

def human_line_created(k, v)
  #
  # The 'created' and 'updated' timestamps are in epoch
  # milliseconds
  #
  human_line(k, Time.at(v / 1000))
end

#human_line_hostsUsed(k, v) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/wavefront/cli/alerts.rb', line 122

def human_line_hostsUsed(k, v)
  #
  # Put each host on its own line, indented.
  #
  return k unless v
  v.sort!
  [human_line(k, v.shift)] + v.map {|el| human_line('', el)}
end

#human_line_metricsUsed(k, v) ⇒ Object



131
132
133
# File 'lib/wavefront/cli/alerts.rb', line 131

def human_line_metricsUsed(k, v)
  human_line_hostsUsed(k, v)
end

#human_line_updated(k, v) ⇒ Object



118
119
120
# File 'lib/wavefront/cli/alerts.rb', line 118

def human_line_updated(k, v)
  human_line_created(k, v)
end

#humanize(alerts) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/wavefront/cli/alerts.rb', line 74

def humanize(alerts)
  #
  # Selectively display alert information in an easily
  # human-readable format. I have chosen not to display certain
  # fields which I don't think are useful in this context. I also
  # wish to put the fields in order. Here are the fields I want, in
  # the order I want them.
  #
  row_order = %w(name created severity condition displayExpression
                 minutes resolveAfterMinutes updated alertStates
                 metricsUsed hostsUsed additionalInformation)

  # build up an array of lines then turn it into a string and
  # return it
  #
  # Most things get printed with the human_line() method, but some
  # data needs special handling. To do that, just add a method
  # called human_line_key() where key is something in row_order,
  # and it will be found.
  #
  x = alerts.map do |alert|
    row_order.map do |key|
      lm = "human_line_#{key}"
      if self.respond_to?(lm)
        self.method(lm.to_sym).call(key, alert[key])
      else
        human_line(key, alert[key])
      end
    end
  end
end

#indent_wrap(line, cols = 78, offset = 22) ⇒ Object



143
144
145
146
147
148
# File 'lib/wavefront/cli/alerts.rb', line 143

def indent_wrap(line, cols=78, offset=22)
  #
  # hanging indent long lines to fit in an 80-column terminal
  #
  line.gsub(/(.{1,#{cols - offset}})(\s+|\Z)/, "\\1\n#{' ' * offset}")
end

#runObject



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wavefront/cli/alerts.rb', line 26

def run
  alerts = Wavefront::Alerting.new(@options[:token])
  queries = alerts.public_methods(false).sort
  queries.delete(:token)

  raise 'Missing query.' if arguments.empty?
  query = arguments[0].to_sym

  unless queries.include?(query)
    raise 'State must be one of: ' + queries.each {|q| q.to_s}.join(', ')
  end

  unless Wavefront::Client::ALERT_FORMATS.include?(
                                          @options[:format].to_sym)
    raise 'Output format must be one of: ' +
          Wavefront::Client::ALERT_FORMATS.join(', ')
  end

  # This isn't especially nice, but if require to
  # avoiding breaking the Alerting interface :(
  options = Hash.new
  options[:host] = @options[:endpoint]

  if @options[:shared]
    options[:shared_tags] = @options[:shared].delete(' ').split(',')
  end

  if @options[:private]
    options[:private_tags] = @options[:private].delete(' ').split(',')
  end

  result = alerts.send(query, options)

  case @options[:format].to_sym
  when :ruby
    pp result
  when :json
    puts JSON.pretty_generate(JSON.parse(result))
  when :human
    puts humanize(JSON.parse(result))
  else
    puts "Invalid output format, See --help for more detail."
    exit 1
  end

  exit 0
end