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
Instance Method Summary collapse
- #acknowledge_event(m, eventid, message) ⇒ Object
- #get_login(obj) ⇒ Object
-
#get_op(obj) ⇒ Object
(also: #authenticate)
Authentication and helpers.
- #host_latest(m, host, limit) ⇒ Object
- #host_status(m, host) ⇒ Object
-
#list_events(m) ⇒ Object
Events.
- #ops ⇒ Object
- #set_events_priority(m, op, value) ⇒ Object
- #set_notify(m, op, value) ⇒ Object
- #set_primary_channel(m, op, value) ⇒ Object
- #set_setting(m, key, value) ⇒ Object
-
#show_settings(m) ⇒ Object
Settings.
- #sync_ops(m, u = nil) ⇒ Object
- #zabbirc_status(m) ⇒ Object
Methods included from Help::ClassMethods
Methods included from Help
#help_features, #zabbirc_help, #zabbirc_help_detail
Instance Method Details
#acknowledge_event(m, eventid, message) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 20 def acknowledge_event m, eventid, 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 end |
#get_login(obj) ⇒ Object
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 177 def get_login 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
170 171 172 173 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 170 def get_op obj login = get_login obj ops.get login end |
#host_latest(m, host, limit) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 53 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") end |
#host_status(m, host) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 33 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") end |
#list_events(m) ⇒ Object
Events
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 150 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 end |
#ops ⇒ Object
164 165 166 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 164 def ops @ops ||= bot.zabbirc_service.ops end |
#set_events_priority(m, op, value) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 117 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` was set to `#{op.setting.get :events_priority}`" end |
#set_notify(m, op, value) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 100 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` was set to `#{op.setting.get :notify}`" end |
#set_primary_channel(m, op, value) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 133 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` was set to `#{op.setting.get :primary_channel}`" end |
#set_setting(m, key, value) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 85 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
79 80 81 82 83 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 79 def show_settings m op = authenticate m return unless op m.reply "#{op.nick}: #{op.setting}" end |
#sync_ops(m, u = nil) ⇒ Object
73 74 75 76 |
# File 'lib/zabbirc/irc/plugin_methods.rb', line 73 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 |
# 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") end |