Class: AlertTweeter::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/alert_tweeter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *a, &b) ⇒ Object (private)



126
127
128
129
130
131
132
133
# File 'lib/alert_tweeter.rb', line 126

def method_missing(name, *a, &b)
  namesym = name.to_sym
  if val = opts[namesym]
    return val
  else
    super
  end
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



22
23
24
# File 'lib/alert_tweeter.rb', line 22

def opts
  @opts
end

Class Method Details

.run!(*a, &b) ⇒ Object



24
25
26
# File 'lib/alert_tweeter.rb', line 24

def self.run!(*a, &b)
  new.run!(*a, &b)
end

Instance Method Details

#configObject



28
29
30
# File 'lib/alert_tweeter.rb', line 28

def config
  @config ||= YAML.load_file(opts[:config])
end

#configure_client!(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/alert_tweeter.rb', line 36

def configure_client!(path)
  require 'yaml'

  hash = config.fetch('auth')

  Twitter.configure do |c|
    %w[consumer_key consumer_secret oauth_token oauth_token_secret].each do |k|
      c.__send__(:"#{k}=", hash.fetch(k))
    end
  end
end

#host_notify!Object



66
67
68
69
70
71
72
# File 'lib/alert_tweeter.rb', line 66

def host_notify!
  send_notification <<-EOS.strip_heredoc[0...140]
    #{notification_type} #{host_name} #{event_state}
    #{timestamp}
    #{host_output}
  EOS
end

#notify_usersObject



32
33
34
# File 'lib/alert_tweeter.rb', line 32

def notify_users
  @config.fetch('notify_users')
end

#run!(*args) ⇒ 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/alert_tweeter.rb', line 74

def run!(*args)
  args << '-h' if args.empty?

  @opts = Slop.parse!(args, :strict => true, :help => true) do
    banner <<-EOS.strip_heredoc
      Usage: #{File.basename($0)} [opts] 

      Will direct-message the configured users about service problems. Command-line options
      are provided that more-or-less match their nagios macro counterparts.
    EOS

    on :f, :config,                 'path to the config file to use', true, :default => DEFAULT_CONFIG_PATH

    on :n, :notification_type,      'string describing the type of notification being sent', true

    on :s, :event_state,            'string indicating the state of event-generating object', true, :default => 'UNKNOWN'
    on :y, :event_state_type,       'HARD or SOFT', true

    on :H, :host_name,              'host name where the alert is triggered', true, :required => true
    on :a, :host_address,           'IP address of the host', true
    on :A, :host_attempt,           'number of the current re-check', true, :as => :integer
    on :O, :host_output,            'output from host check', true
    on :U, :host_duration_sec,      'number of seconds the host has spent in the current state', true, :as => :integer

    on :d, :service_desc,           'description of the service', true
    on :a, :service_attempt,        'number of the current re-check', true, :as => :integer
    on :u, :service_duration_sec,   'number of seconds the service has been in the current state', true, :as => :integer
    on :o, :service_output,         'first line of text output from the last service check', true

    on :t, :timet,                  'seconds since unix epoch', true, :as => :integer, :default => Time.now.to_i
  end

  if opts.help?
    $stderr.puts opts.help
    exit 1
  end

  unless opts[:service_output] or opts[:host_output]
    $stderr.puts "ERROR: You must specify either a --service_output or --host_output option"
    exit 1
  end

  configure_client!(opts[:config])

  if opts[:service_output]
    service_notify!
  else
    host_notify!
  end
end

#send_notification(message) ⇒ Object



52
53
54
55
56
# File 'lib/alert_tweeter.rb', line 52

def send_notification(message)
  notify_users.each do |user|
    Twitter.direct_message_create(user, message)
  end
end

#service_notify!Object



58
59
60
61
62
63
64
# File 'lib/alert_tweeter.rb', line 58

def service_notify!
  send_notification <<-EOS.strip_heredoc[0...140]
    #{notification_type} #{host_name}/#{service_desc} #{event_state}
    #{timestamp}
    #{service_output}
  EOS
end

#timestampObject



48
49
50
# File 'lib/alert_tweeter.rb', line 48

def timestamp
  Time.at(timet.to_i).strftime('%Y-%m-%d %H:%M:%S')
end