Class: WavefrontCli::Event

Inherits:
Base
  • Object
show all
Includes:
Wavefront::Mixins
Defined in:
lib/wavefront-cli/event.rb

Overview

CLI coverage for the v2 ‘event’ API.

Instance Attribute Summary collapse

Attributes inherited from Base

#klass, #klass_word, #options, #wf

Instance Method Summary collapse

Methods inherited from Base

#check_status, #dispatch, #display, #do_delete, #do_describe, #do_import, #do_tag_add, #do_tag_clear, #do_tag_delete, #do_tag_set, #do_tags, #do_undelete, #format_var, #handle_error, #handle_response, #import_to_create, #initialize, #load_display_class, #load_file, #mk_creds, #mk_opts, #run, #validate_id, #validate_opts, #validate_tags, #validator_exception, #validator_method

Constructor Details

This class inherits a constructor from WavefrontCli::Base

Instance Attribute Details

#state_dirObject (readonly)

Returns the value of attribute state_dir.



12
13
14
# File 'lib/wavefront-cli/event.rb', line 12

def state_dir
  @state_dir
end

Instance Method Details

#do_closeObject

The user doesn’t have to give us an event ID. If no event name is given, we’ll pop the last event off the stack. If an event name is given and it doesn’t look like a full WF event name, we’ll look for something on the stack. If it does look like a real event, we’ll make and API call straight away.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wavefront-cli/event.rb', line 74

def do_close
  ev_file = nil

  ev = if options[:'<id>'] == false
         pop_event
       elsif options[:'<id>'] =~ /^\d{13}:.+/
         ev_file = state_dir + options[:'<id>']
         options[:'<id>']
       else
         pop_event(options[:'<id>'])
       end

  abort "No locally stored event matches '#{options[:'<id>']}'." unless ev

  res = wf.close(ev)
  ev_file.unlink if ev_file && ev_file.exist? && res.status.code == 200
  res
end

#do_createObject



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
# File 'lib/wavefront-cli/event.rb', line 37

def do_create
  options[:start] = Time.now unless options[:start]

  t_start = parse_time(options[:start], true)
  id = [t_start, options[:'<event>']].join(':')

  body = { name:        options[:'<event>'],
           startTime:   t_start,
           id:          id,
           annotations: {} }

  body[:annotations][:details] = options[:desc] if options[:desc]
  body[:annotations][:severity] = options[:severity] if options[:severity]
  body[:annotations][:type] = options[:type] if options[:type]
  body[:hosts] = options[:host] if options[:host]

  if options[:instant]
    body[:endTime] = t_start + 1
  elsif options[:end]
    body[:endTime] = parse_time(options[:end], true)
  end

  resp = wf.create(body)

  unless options[:nostate] || options[:end] || options[:instant]
    create_state_file(id, options[:host])
  end

  resp
end

#do_listObject



25
26
27
28
29
30
# File 'lib/wavefront-cli/event.rb', line 25

def do_list
  options[:start] = Time.now - 600 unless options[:start]
  options[:end] = Time.now unless options[:end]
  wf.list(options[:start], options[:end], options[:limit] || 100,
          options[:offset] || nil)
end

#do_showObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/wavefront-cli/event.rb', line 93

def do_show
  begin
    events = state_dir.children
  rescue Errno::ENOENT
    raise 'There is no event state directory on this host.'
  end

  if events.size.zero?
    puts 'No open events.'
  else
    events.sort.reverse.each { |e| puts e.basename }
  end

  exit
end

#do_updateObject



32
33
34
35
# File 'lib/wavefront-cli/event.rb', line 32

def do_update
  k, v = options[:'<key=value>'].split('=')
  wf.update(options[:'<id>'], k => v)
end

#post_initialize(_options) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/wavefront-cli/event.rb', line 15

def post_initialize(_options)
  begin
    @state_dir = EVENT_STATE_DIR + Etc.getlogin
  rescue
    @state_dir = EVENT_STATE_DIR + 'notty'
  end

  create_state_dir
end