zabbirc 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0442b2c7c53bf6dafea3ea822c5782d94b7ba20f
4
- data.tar.gz: c04aa4f8923eba8a611e44fd3192d0f63c287045
3
+ metadata.gz: c14c5732f6e3e5d9209fb9b788cd2f533142eecd
4
+ data.tar.gz: d868a4bca00497bf01f8e808eb7d8a51165a9f70
5
5
  SHA512:
6
- metadata.gz: a77d8bab630b1fea6945384de58ed5e884fcc8689de3304a73ff8cdfff57358f14bc439a319333690fbf8228e28ddfa694921dcee152866bc94142d3c91e8c96
7
- data.tar.gz: 170bd4acf9dd5acf23ac20bab8c63391416328d2458ea2860acdbd081d078580af681f27ab84b4181feb5bf2d101f28117f7a496ad51c4f7a32b39b16c6885d0
6
+ metadata.gz: ac71f44f93f205561568d147b9467227180c3a79b928f850d861577221f6f73c3cdb78073e6c6b8f1c2a80b0df5f795e060e9c720f9db8a358088b1a81cb6db4
7
+ data.tar.gz: a7b4006245ccc9004f0b28c21dbb09a7e4755f68c5acc3a7f699e6f8fcefa990a9a4a0d852d9b3cc861eb1c29c5c0b416597bd9c67c403b45d5e0f863ca748f4
data/config/config.rb CHANGED
@@ -7,4 +7,6 @@ Zabbirc.configure do |config|
7
7
  # config.irc_channels = ["#libra-zabbix"]
8
8
  config.irc_server = "irc.freenode.org"
9
9
  config.irc_channels = ["#zabbirc-test", "#zabbirc-test-2"]
10
+
11
+ config.notify_about_events_from_last = 25.minutes
10
12
  end
@@ -21,8 +21,8 @@ module Zabbirc
21
21
  match /(settings set)\s*$/, method: :zabbirc_help_detail
22
22
 
23
23
  # Events
24
- register_help "events", "Show events from last #{Zabbirc.config.notify_about_events_from_last.to_i / 60} minutes. Usage: !events"
25
- match "events", method: :list_events
24
+ register_help "events", "Show events from last #{Zabbirc.config.notify_about_events_from_last.to_i / 60} minutes filtered by <priority> and <host>. Usage: !events [<priority [<host>]]"
25
+ match /events(?: ([a-zA-Z0-9\-]+)(?: ([a-zA-Z0-9\-]+))?)?\s*$/, method: :list_events
26
26
 
27
27
  # Host
28
28
  register_help "status", "Show status of host. Usage: !status <hostname>"
@@ -94,8 +94,8 @@ module Zabbirc
94
94
  op = authenticate m
95
95
  return unless op
96
96
  case key
97
- when "notify"
98
- set_notify m, op, value
97
+ when "notify", "notify_recoveries"
98
+ set_boolean m, op, key, value
99
99
  when "events_priority"
100
100
  set_events_priority m, op, value
101
101
  when "primary_channel"
@@ -105,21 +105,22 @@ module Zabbirc
105
105
  end
106
106
  end
107
107
 
108
- def set_notify m, op, value
108
+ def set_boolean m, op, key, value
109
109
  if value.nil?
110
- m.reply "#{op.nick}: notify allowed values: true, on, 1, false, off, 0"
110
+ m.reply "#{op.nick}: #{key} allowed values: true, on, 1, false, off, 0"
111
111
  return
112
112
  end
113
+
113
114
  case value
114
115
  when "true", "on", "1"
115
- op.setting.set :notify, true
116
+ op.setting.set key, true
116
117
  when "false", "off", "0"
117
- op.setting.set :notify, false
118
+ op.setting.set key, false
118
119
  else
119
120
  m.reply "#{op.nick}: uknown value `#{value}`. Allowed values: true, on, 1, false, off, 0"
120
121
  return
121
122
  end
122
- m.reply "#{op.nick}: setting `notify` has been set to `#{op.setting.get :notify}`"
123
+ m.reply "#{op.nick}: setting `#{key}` has been set to `#{op.setting.get key}`"
123
124
  end
124
125
 
125
126
  def set_events_priority m, op, value
@@ -155,16 +156,22 @@ module Zabbirc
155
156
  end
156
157
 
157
158
  ### Events
158
- def list_events m
159
+ def list_events m, priority=nil, host=nil
159
160
  op = authenticate m
160
161
  return unless op
162
+ priority = parse_priority(m, priority || 0)
163
+ return unless priority
164
+
161
165
  events = Zabbix::Event.recent
166
+ events = events.select{|e| e.priority >= priority }
167
+ events = events.select{|e| e.any_host_matches? /#{host}/ } if host
162
168
  msg = if events.any?
163
169
  events.collect do |e|
164
170
  "#{op.nick}: #{e.label}"
165
171
  end.join("\n")
166
172
  else
167
- "#{op.nick}: No last events"
173
+ host_filter = host ? " and host `#{host}`" : ""
174
+ "#{op.nick}: No last events for priority `#{priority}`#{host_filter}"
168
175
  end
169
176
  m.reply msg
170
177
  rescue Zabbix::NotConnected => e
@@ -228,6 +235,14 @@ module Zabbirc
228
235
  end
229
236
  end
230
237
 
238
+ def parse_priority m, priority
239
+ op = get_op m
240
+ Priority.new(priority)
241
+ rescue ArgumentError => e
242
+ m.reply("#{op.nick}: #{e}")
243
+ nil
244
+ end
245
+
231
246
  def rescue_not_connected m, e
232
247
  op = get_op m
233
248
  return unless op
data/lib/zabbirc/op.rb CHANGED
@@ -65,6 +65,7 @@ module Zabbirc
65
65
  def interested_in? event
66
66
  return false unless setting.get :notify
67
67
  return false if @notified_events.key? event.id
68
+ return false if event.value == :ok and not setting.get :notify_recoveries
68
69
  event.priority >= interesting_priority
69
70
  end
70
71
 
@@ -86,7 +86,7 @@ module Zabbirc
86
86
  end
87
87
 
88
88
  def pre_start_script
89
- @ops_service.iterate
89
+ @ops_service.sync_ops
90
90
  @ops.load_settings
91
91
  rescue => e
92
92
  Zabbirc.logger.error "Exception `#{e}` while running pre_start_script"
@@ -3,6 +3,18 @@ module Zabbirc
3
3
  class Ops < Base
4
4
  def iterate
5
5
  sync_ops
6
+ synchronize do
7
+ @service.ops.dump_settings
8
+ end
9
+ end
10
+
11
+ def sync_ops
12
+ synchronize do
13
+ sync_zabbix
14
+ @cinch_bot.channels.each do |channel|
15
+ sync_irc channel
16
+ end
17
+ end
6
18
  rescue Zabbix::NotConnected => e
7
19
  if Zabbix::Connection.up?
8
20
  @service.ops.interested.notify e.to_s
@@ -20,16 +32,6 @@ module Zabbirc
20
32
  channel.users.keys.find { |irc_user| irc_user.user.sub("~","") == login }
21
33
  end
22
34
 
23
- def sync_ops
24
- synchronize do
25
- sync_zabbix
26
- @cinch_bot.channels.each do |channel|
27
- sync_irc channel
28
- end
29
- end
30
- # @service.ops.dump_settings
31
- end
32
-
33
35
  def sync_irc channel
34
36
  logins = channel_logins channel
35
37
 
@@ -2,6 +2,7 @@ module Zabbirc
2
2
  class Setting
3
3
  DEFAULTS = ActiveSupport::HashWithIndifferentAccess.new({
4
4
  notify: true,
5
+ notify_recoveries: true,
5
6
  primary_channel: nil,
6
7
  events_priority: Zabbirc.config.default_events_priority
7
8
  })
@@ -14,8 +15,7 @@ module Zabbirc
14
15
  stored_options = ActiveSupport::HashWithIndifferentAccess.new stored_options
15
16
  unknown_keys = stored_options.keys - DEFAULTS.keys
16
17
  stored_options.delete_if{|k,_v| unknown_keys.include? k }
17
- stored_options.merge DEFAULTS.deep_dup
18
- @options = stored_options
18
+ @options = DEFAULTS.deep_dup.merge(stored_options)
19
19
  end
20
20
 
21
21
  def set name, value
@@ -79,6 +79,10 @@ module Zabbirc
79
79
  gsub("%state", "#{state}")
80
80
  end
81
81
 
82
+ def any_host_matches? regexp
83
+ hosts.any?{|h| h.name =~ regexp }
84
+ end
85
+
82
86
  private
83
87
 
84
88
  def determine_related_object
@@ -28,7 +28,7 @@ module Zabbirc
28
28
 
29
29
  def initialize attrs
30
30
  @attrs = ActiveSupport::HashWithIndifferentAccess.new attrs
31
- raise ArgumentError, "attribute `#{self.class.model_name}id` not found, probably not an Event" unless @attrs.key? :"#{self.class.model_name}id"
31
+ raise ArgumentError, "attribute `#{self.class.model_name}id` not found, probably not a #{self.class.model_name}" unless @attrs.key? :"#{self.class.model_name}id"
32
32
  end
33
33
 
34
34
  def id
data/lib/zabbirc.rb CHANGED
@@ -25,7 +25,4 @@ require_dir "zabbirc/irc/*.rb"
25
25
  require 'zabbirc/zabbix/resource/base'
26
26
  require_dir "zabbirc/zabbix/*.rb"
27
27
  require 'zabbirc/services/base'
28
- require_dir "zabbirc/services/*.rb"
29
-
30
- # TODO Error message when cannot connect to Zabbix
31
- # TODO Dump op specific settings in loop, so it can be loaded on the fatal server failure
28
+ require_dir "zabbirc/services/*.rb"
data/spec/bot_spec.rb CHANGED
@@ -125,27 +125,51 @@ describe Zabbirc::Irc::PluginMethods do
125
125
  let(:recent_events) { [] }
126
126
 
127
127
  it "should report no last events" do
128
- expect(mock_message).to receive(:reply).with("#{mock_nick}: No last events")
128
+ expect(mock_message).to receive(:reply).with("#{mock_nick}: No last events for priority `#{Zabbirc::Priority.new(0)}`")
129
129
  bot.list_events mock_message
130
130
  end
131
131
  end
132
132
 
133
- context "no last events" do
134
- let(:event1) { double "Event1", label: "Event 1 label" }
135
- let(:event2) { double "Event2", label: "Event 2 label" }
136
- let(:recent_events) { [event1, event2] }
133
+ context "some last events" do
134
+ let(:event1_information) { double "Event1", label: "Event 1 label", priority: Zabbirc::Priority.new(:information) }
135
+ let(:event2_information) { double "Event2", label: "Event 2 label", priority: Zabbirc::Priority.new(:information) }
136
+ let(:event3_high) { double "Event3", label: "Event 3 label", priority: Zabbirc::Priority.new(:high) }
137
+ let(:recent_events) { [event1_information, event2_information, event3_high] }
137
138
  let(:expected_msg) { recent_events.collect{|e| "#{mock_nick}: #{e.label}"}.join("\n") }
138
139
 
139
- it "should report no last events" do
140
+ before do
141
+ recent_events.each do |e|
142
+ allow(e).to receive(:any_host_matches?).and_return(false)
143
+ end
144
+ end
145
+
146
+ it "should report all last events" do
140
147
  expect(mock_message).to receive(:reply).with(expected_msg)
141
148
  bot.list_events mock_message
142
149
  end
150
+
151
+ context "with high priority filtered" do
152
+ let(:expected_msg) { [event3_high].collect{|e| "#{mock_nick}: #{e.label}"}.join("\n") }
153
+ it "should report high priority last events" do
154
+ expect(mock_message).to receive(:reply).with(expected_msg)
155
+ bot.list_events mock_message, "high"
156
+ end
157
+ end
158
+
159
+ context "with host filtered" do
160
+ let(:expected_msg) { [event1_information].collect{|e| "#{mock_nick}: #{e.label}"}.join("\n") }
161
+ it "should report host matched last events" do
162
+ allow(event1_information).to receive(:any_host_matches?).and_return(true )
163
+ expect(mock_message).to receive(:reply).with(expected_msg)
164
+ bot.list_events mock_message, "information", "host1"
165
+ end
166
+ end
143
167
  end
144
168
  end
145
169
 
146
170
  describe "#show_settings" do
147
171
  let(:mock_user_settings) { {primary_channel: "#channel-1", events_priority: "high", notify: false } }
148
- let(:expected_msg) { "#{mock_nick}: notify: false, primary_channel: #channel-1, events_priority: high" }
172
+ let(:expected_msg) { "#{mock_nick}: notify: false, notify_recoveries: true, primary_channel: #channel-1, events_priority: high" }
149
173
  it "should show settings" do
150
174
  expect(mock_message).to receive(:reply).with(expected_msg)
151
175
  bot.show_settings mock_message
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabbirc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Filip Zachar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-10 00:00:00.000000000 Z
11
+ date: 2014-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -145,7 +145,6 @@ files:
145
145
  - tmp/module_test.rb
146
146
  - tmp/playground.rb
147
147
  - tmp/thread_test.rb
148
- - zabbirc_config.rb
149
148
  homepage: https://github.com/tulak/zabbirc
150
149
  licenses:
151
150
  - MIT
data/zabbirc_config.rb DELETED
@@ -1,14 +0,0 @@
1
- Zabbirc.configure do |config|
2
- ### Zabbix server configuration
3
- config.zabbix_api_url = "https://your.zabbix-server.com/zabbix/api_jsonrpc.php"
4
- config.zabbix_login = "zabbirc"
5
- config.zabbix_password = "zabbircpass"
6
-
7
- ### IRC configurations
8
- # config.irc_server = "irc.freenode.org"
9
- # config.irc_channels = ["#zabbirc-test", "#zabbirc-test-2"]
10
-
11
- ### Zabbirc configurations
12
- # config.events_check_interval = 10.seconds
13
- # config.notify_about_events_from_last = 5.minutes
14
- end