Module: Zabbirc::Irc::PluginMethods

Extended by:
ActiveSupport::Concern, Help::ClassMethods
Includes:
Help
Included in:
Plugin
Defined in:
lib/zabbirc/irc/plugin_methods.rb

Constant Summary

Constants included from Help

Help::FEATURES

Instance Method Summary collapse

Methods included from Help::ClassMethods

help_features, register_help

Methods included from Help

#help_features, #zabbirc_help, #zabbirc_help_detail

Instance Method Details

#acknowledge_event(m, eventid, message) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zabbirc/irc/plugin_methods.rb', line 22

def acknowledge_event m, eventid, message
  op = authenticate m
  return unless op
  event = find_event m, eventid
  return unless event

  if event.acknowledge "#{op.nick}: #{message}"
    m.reply "#{op.nick}: Event `#{event.label}` acknowledged with message: #{message}"
  else
    m.reply "#{op.nick}: Could not acknowledge event `#{event.label}`"
  end
rescue Zabbix::NotConnected => e
  rescue_not_connected m, e
end

#get_login(obj) ⇒ Object



187
188
189
190
191
192
193
194
195
196
# File 'lib/zabbirc/irc/plugin_methods.rb', line 187

def  obj
  case obj
  when Cinch::Message
    obj.user.user.sub("~","")
  when Cinch::User
    obj.user.user.sub("~","")
  when String
    obj
  end
end

#get_op(obj) ⇒ Object Also known as: authenticate

Authentication and helpers



180
181
182
183
# File 'lib/zabbirc/irc/plugin_methods.rb', line 180

def get_op obj
   =  obj
  ops.get 
end

#host_latest(m, host, limit) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/zabbirc/irc/plugin_methods.rb', line 59

def host_latest m, host, limit
  limit ||= 8
  op = authenticate m
  return unless op
  host = find_host m, host
  return unless host

  msg = ["#{op.nick}: Host: #{host.name}"]
  events = Zabbix::Event.get(hostids: host.id, limit: limit, selectHosts: :extend, selectRelatedObject: :extend, sortfield: :clock, sortorder: "DESC")
  if events.empty?
    msg[0] << " - no events found"
  else
    msg[0] << " - showing last #{events.size} events"
    events.each do |event|
      msg << "#{op.nick}: !latest: #{event.label}"
    end
  end
  m.reply msg.join("\n")
rescue Zabbix::NotConnected => e
  rescue_not_connected m, e
end

#host_status(m, host) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zabbirc/irc/plugin_methods.rb', line 37

def host_status m, host
  op = authenticate m
  return unless op
  host = find_host m, host
  return unless host

  triggers = Zabbix::Trigger.get(hostids: host.id, filter: {value: 1}, selectHosts: :extend)
  triggers = triggers.sort{|x,y| x.priority <=> y.priority }
  msg = ["#{op.nick}: Host: #{host.name}"]
  if triggers.empty?
    msg[0] << " - status: OK"
  else
    msg[0] << " - status: #{triggers.size} problems"
    triggers.each do |trigger|
      msg << "#{op.nick}: status: #{trigger.label}"
    end
  end
  m.reply msg.join("\n")
rescue Zabbix::NotConnected => e
  rescue_not_connected m, e
end

#list_events(m) ⇒ Object

Events



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/zabbirc/irc/plugin_methods.rb', line 158

def list_events m
  op = authenticate m
  return unless op
  events = Zabbix::Event.recent
  msg = if events.any?
          events.collect do |e|
            "#{op.nick}: #{e.label}"
          end.join("\n")
        else
          "#{op.nick}: No last events"
        end
  m.reply msg
rescue Zabbix::NotConnected => e
  rescue_not_connected m, e
end

#opsObject



174
175
176
# File 'lib/zabbirc/irc/plugin_methods.rb', line 174

def ops
  @ops ||= bot.zabbirc_service.ops
end

#set_events_priority(m, op, value) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/zabbirc/irc/plugin_methods.rb', line 125

def set_events_priority m, op, value
  if value.nil?
    m.reply "#{op.nick}: events_priority allowed values: #{Priority::PRIORITIES.values.collect{|v| "`#{v}`"}.join(', ')} or numeric #{Priority::PRIORITIES.keys.join(", ")} "
    return
  end
  begin
    value = value.to_i if value =~ /^\d+$/
    priority = Priority.new value
  rescue ArgumentError
    m.reply "#{op.nick}: uknown value `#{value}`. Allowed values: #{Priority::PRIORITIES.values.collect{|v| "`#{v}`"}.join(', ')} or numeric #{Priority::PRIORITIES.keys.join(", ")} "
    return
  end
  op.setting.set :events_priority, priority.code
  m.reply "#{op.nick}: setting `events_priority` has been set to `#{op.setting.get :events_priority}`"
end

#set_notify(m, op, value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/zabbirc/irc/plugin_methods.rb', line 108

def set_notify m, op, value
  if value.nil?
    m.reply "#{op.nick}: notify allowed values: true, on, 1, false, off, 0"
    return
  end
  case value
  when "true", "on", "1"
    op.setting.set :notify, true
  when "false", "off", "0"
    op.setting.set :notify, false
  else
    m.reply "#{op.nick}: uknown value `#{value}`. Allowed values: true, on, 1, false, off, 0"
    return
  end
  m.reply "#{op.nick}: setting `notify` has been set to `#{op.setting.get :notify}`"
end

#set_primary_channel(m, op, value) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/zabbirc/irc/plugin_methods.rb', line 141

def set_primary_channel m, op, value
  channel_names = op.channels.collect(&:name)
  if value.nil?
    m.reply "#{op.nick}: notify allowed values: #{channel_names.join(", ")}"
    return
  end
  case value
  when *channel_names
    op.setting.set :primary_channel, value
  else
    m.reply "#{op.nick}: uknown value `#{value}`. Allowed values: #{channel_names.join(", ")}"
    return
  end
  m.reply "#{op.nick}: setting `primary_channel` has been set to `#{op.setting.get :primary_channel}`"
end

#set_setting(m, key, value) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/zabbirc/irc/plugin_methods.rb', line 93

def set_setting m, key, value
  op = authenticate m
  return unless op
  case key
  when "notify"
    set_notify m, op, value
  when "events_priority"
    set_events_priority m, op, value
  when "primary_channel"
    set_primary_channel m, op, value
  else
    m.reply "#{op.nick}: unknown setting `#{key}`"
  end
end

#show_settings(m) ⇒ Object

Settings



87
88
89
90
91
# File 'lib/zabbirc/irc/plugin_methods.rb', line 87

def show_settings m
  op = authenticate m
  return unless op
  m.reply "#{op.nick}: #{op.setting}"
end

#sync_ops(m, u = nil) ⇒ Object



81
82
83
84
# File 'lib/zabbirc/irc/plugin_methods.rb', line 81

def sync_ops m, u=nil
  return if u and u.nick == bot.nick
  bot.zabbirc_service.ops_service.iterate
end

#zabbirc_status(m) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/zabbirc/irc/plugin_methods.rb', line 8

def zabbirc_status m
  ops_msg = ops.find_all{|o| o.nick.present? }.collect{|o| "#{o.nick} as #{o.login}"}
  msg = []
  if Zabbix::Connection.test_connection
    msg << "#{m.user.nick}: Zabbix API connection successfull"
  else
    msg << "#{m.user.nick}: Zabbix API connection FAILED !!!"
  end
  msg << "#{m.user.nick}: Identified ops: #{ops_msg.join(", ")}"
  m.reply msg.join("\n")
rescue Zabbix::NotConnected => e
  rescue_not_connected m, e
end