zabbirc 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/zabbirc +0 -2
- data/bin/zabbirc-install +34 -1
- data/config/config.rb +4 -2
- data/lib/zabbirc/irc/help.rb +40 -0
- data/lib/zabbirc/irc/plugin.rb +23 -6
- data/lib/zabbirc/irc/plugin_methods.rb +29 -45
- data/lib/zabbirc/op.rb +36 -21
- data/lib/zabbirc/op_list.rb +42 -3
- data/lib/zabbirc/service.rb +17 -0
- data/lib/zabbirc/services/ops.rb +32 -10
- data/lib/zabbirc/setting.rb +14 -2
- data/lib/zabbirc/zabbix/resource/finders.rb +2 -2
- data/lib/zabbirc.rb +7 -1
- data/spec/bot_spec.rb +2 -2
- data/spec/support/mock_bot.rb +2 -1
- data/tmp/module_test.rb +54 -0
- data/tmp/thread_test.rb +63 -0
- data/zabbirc_config.rb +14 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11ac19c79191e2fe87ffb43749fb6f92217eaf13
|
4
|
+
data.tar.gz: 378b42d0728fe9ceb3b01e337dcd468939c5eb0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a0c5cace32e565a365dca2b9dcbd023073e0d4f8450c2dfe1c6609c5383f6d992005ea30c96bf038981d5a6f06653e09d5ce15c921a1d732ca60aee3e2e08f2
|
7
|
+
data.tar.gz: 835ad3ee7213973e670ad79d7bd55b32349114befa11445ad74ae613acc5aa006ce7d6485b22f33e7ee4dbf2585d381205c100cd4f1c167625fca9aaf2200cc2
|
data/bin/zabbirc
CHANGED
data/bin/zabbirc-install
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
|
2
3
|
case ARGV.size
|
3
4
|
when 0
|
4
5
|
install_path = Dir.pwd
|
@@ -9,8 +10,40 @@ when 2
|
|
9
10
|
exit 1
|
10
11
|
end
|
11
12
|
|
13
|
+
require 'zabbirc'
|
14
|
+
require 'pry'
|
15
|
+
|
16
|
+
def chown_directory dir
|
17
|
+
puts "Using sudo to chown runtime directory `#{dir}`"
|
18
|
+
system "sudo chown #{Process.uid}:#{Process.gid} #{dir}"
|
19
|
+
end
|
20
|
+
|
21
|
+
begin
|
22
|
+
unless Dir.exists? Zabbirc::RUNTIME_DATA_DIR
|
23
|
+
puts "Using sudo to create runtime directory #{Zabbirc::RUNTIME_DATA_DIR}"
|
24
|
+
r = system "sudo mkdir #{Zabbirc::RUNTIME_DATA_DIR}"
|
25
|
+
if r
|
26
|
+
puts "Runtime directory #{Zabbirc::RUNTIME_DATA_DIR} created"
|
27
|
+
chown_directory Zabbirc::RUNTIME_DATA_DIR
|
28
|
+
else
|
29
|
+
puts "Could not create runtime directory #{Zabbirc::RUNTIME_DATA_DIR}"
|
30
|
+
puts "Please create this directory and check if it's accesible by user, that will run zabbirc bot"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Test if runtime directory is accessible
|
35
|
+
f = File.open(Zabbirc::RUNTIME_DATA_DIR.join("test"), "w")
|
36
|
+
f.puts "test"
|
37
|
+
f.close
|
38
|
+
FileUtils.rm f.path
|
39
|
+
rescue Errno::EACCES
|
40
|
+
unless chown_directory Zabbirc::RUNTIME_DATA_DIR
|
41
|
+
puts "Could not make runtime directory `#{Zabbirc::RUNTIME_DATA_DIR}` accessible"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
12
45
|
puts "Installing config file into: #{install_path}"
|
13
46
|
templates_path = Pathname.new(File.expand_path(Pathname.new(File.dirname(__FILE__)).join("../templates")))
|
14
47
|
|
15
48
|
FileUtils.cp(templates_path.join("zabbirc_config.rb"), install_path)
|
16
|
-
puts "Installed"
|
49
|
+
puts "Installed"
|
data/config/config.rb
CHANGED
@@ -3,6 +3,8 @@ Zabbirc.configure do |config|
|
|
3
3
|
config.zabbix_login = ENV['ZABBIX_LOGIN']
|
4
4
|
config.zabbix_password = ENV['ZABBIX_PASSWORD']
|
5
5
|
|
6
|
-
config.irc_server = "irc.devel.redhat.com"
|
7
|
-
config.irc_channels = ["#libra-zabbix"]
|
6
|
+
# config.irc_server = "irc.devel.redhat.com"
|
7
|
+
# config.irc_channels = ["#libra-zabbix"]
|
8
|
+
config.irc_server = "irc.freenode.org"
|
9
|
+
config.irc_channels = ["#zabbirc-test", "#zabbirc-test-2"]
|
8
10
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Zabbirc
|
2
|
+
module Irc
|
3
|
+
module Help
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
FEATURES = {}
|
6
|
+
|
7
|
+
def help_features
|
8
|
+
FEATURES
|
9
|
+
end
|
10
|
+
|
11
|
+
def zabbirc_help m
|
12
|
+
op = authenticate m
|
13
|
+
return unless op
|
14
|
+
help = "#{op.nick}: Zabbirc - Zabbix IRC Bot - available commands: #{help_features.keys.join(", ")}. Type '!zabbirc help <command>' for detailed help"
|
15
|
+
m.reply help
|
16
|
+
end
|
17
|
+
|
18
|
+
def zabbirc_help_detail m, command
|
19
|
+
op = authenticate m
|
20
|
+
return unless op
|
21
|
+
if cmd_help = help_features[command]
|
22
|
+
help = "HELP #{command}: #{cmd_help}"
|
23
|
+
else
|
24
|
+
help = "Uknown command: #{command}"
|
25
|
+
end
|
26
|
+
m.reply("#{op.nick}: #{help}")
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def register_help command, description
|
31
|
+
FEATURES[command] = description
|
32
|
+
end
|
33
|
+
|
34
|
+
def help_features
|
35
|
+
FEATURES
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/zabbirc/irc/plugin.rb
CHANGED
@@ -9,16 +9,33 @@ module Zabbirc
|
|
9
9
|
listen_to :join, method: :sync_ops
|
10
10
|
listen_to :leaving, method: :sync_ops
|
11
11
|
|
12
|
-
match
|
12
|
+
match /zabbirc help\s*/, method: :zabbirc_help
|
13
|
+
match /zabbirc help (.+)/, method: :zabbirc_help_detail
|
14
|
+
match /zabbirc status\s*/, method: :zabbirc_status
|
15
|
+
|
16
|
+
# Settings
|
17
|
+
register_help "settings", "Show your op specific settings. Usage: !settings"
|
13
18
|
match "settings", method: :show_settings
|
14
|
-
|
15
|
-
match /settings set
|
19
|
+
register_help "settings set", "Set your op specific settings. Usage: !setting set <setting-name> <setting-value>"
|
20
|
+
match /settings set ([#_a-zA-Z0-9]+)(?: ([#\-_a-zA-Z0-9]+))?/, method: :set_setting
|
21
|
+
match /(settings set)\s*$/, method: :zabbirc_help_detail
|
22
|
+
|
23
|
+
# Events
|
24
|
+
register_help "events", "Show events from last #{Zabbirc.config.notify_about_events_from_last.to_i / 60} minutes. Usage: !events"
|
16
25
|
match "events", method: :list_events
|
26
|
+
|
27
|
+
# Host
|
28
|
+
register_help "status", "Show status of host. Usage: !status <hostname>"
|
17
29
|
match /status ([a-zA-Z0-9\-.]+)/, method: :host_status
|
18
|
-
|
30
|
+
register_help "latest", "Show last <N> (default 8) events of host. Usage: !latest <hostname> [<N>]"
|
31
|
+
match /latest ([a-zA-Z0-9\-.]+)(?: (\d+))?/, method: :host_latest
|
32
|
+
match /(latest)\s*$/, method: :zabbirc_help_detail
|
33
|
+
|
34
|
+
# ACK
|
35
|
+
register_help "ack", "Acknowledges event with message. Usage: !ack <event-id> <ack-message>"
|
19
36
|
match /ack (\d+) (.*)/, method: :acknowledge_event
|
20
|
-
match /ack\s*$/, method: :
|
21
|
-
match /ack ([^ ]+)\s*$/, method: :
|
37
|
+
match /(ack)\s*$/, method: :zabbirc_help_detail
|
38
|
+
match /(ack) (?:[^ ]+)\s*$/, method: :zabbirc_help_detail
|
22
39
|
end
|
23
40
|
end
|
24
41
|
end
|
@@ -1,22 +1,25 @@
|
|
1
1
|
module Zabbirc
|
2
2
|
module Irc
|
3
3
|
module PluginMethods
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
include Help
|
6
|
+
extend Help::ClassMethods
|
4
7
|
|
5
8
|
def zabbirc_status m
|
6
|
-
ops_msg = ops.collect{|o| "#{o.nick} as #{o.login}"}
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
m.reply("
|
9
|
+
ops_msg = ops.find_all{|o| o.nick.present? }.collect{|o| "#{o.nick} as #{o.login}"}
|
10
|
+
msg = []
|
11
|
+
if Zabbix::Connection.test_connection
|
12
|
+
msg << "#{m.user.nick}: Zabbix API connection successfull"
|
13
|
+
else
|
14
|
+
msg << "#{m.user.nick}: Zabbix API connection FAILED !!!"
|
15
|
+
end
|
16
|
+
msg << "#{m.user.nick}: Identified ops: #{ops_msg.join(", ")}"
|
17
|
+
m.reply msg.join("\n")
|
15
18
|
end
|
16
19
|
|
17
20
|
def acknowledge_event m, eventid, message
|
18
|
-
|
19
|
-
|
21
|
+
op = authenticate m
|
22
|
+
return unless op
|
20
23
|
event = find_event m, eventid
|
21
24
|
return unless event
|
22
25
|
|
@@ -28,8 +31,8 @@ module Zabbirc
|
|
28
31
|
end
|
29
32
|
|
30
33
|
def host_status m, host
|
31
|
-
|
32
|
-
|
34
|
+
op = authenticate m
|
35
|
+
return unless op
|
33
36
|
host = find_host m, host
|
34
37
|
return unless host
|
35
38
|
|
@@ -47,10 +50,10 @@ module Zabbirc
|
|
47
50
|
m.reply msg.join("\n")
|
48
51
|
end
|
49
52
|
|
50
|
-
def host_latest m, host,
|
53
|
+
def host_latest m, host, limit
|
51
54
|
limit ||= 8
|
52
|
-
|
53
|
-
|
55
|
+
op = authenticate m
|
56
|
+
return unless op
|
54
57
|
host = find_host m, host
|
55
58
|
return unless host
|
56
59
|
|
@@ -74,21 +77,14 @@ module Zabbirc
|
|
74
77
|
|
75
78
|
### Settings
|
76
79
|
def show_settings m
|
77
|
-
|
78
|
-
|
80
|
+
op = authenticate m
|
81
|
+
return unless op
|
79
82
|
m.reply "#{op.nick}: #{op.setting}"
|
80
83
|
end
|
81
84
|
|
82
|
-
def
|
83
|
-
|
84
|
-
|
85
|
-
usage = "Usage: !setting set <setting-name> <setting-value>"
|
86
|
-
m.reply("#{op.nick}: #{usage}")
|
87
|
-
end
|
88
|
-
|
89
|
-
def set_setting m, key, _rest, value
|
90
|
-
return unless authenticate m
|
91
|
-
op = get_op m
|
85
|
+
def set_setting m, key, value
|
86
|
+
op = authenticate m
|
87
|
+
return unless op
|
92
88
|
case key
|
93
89
|
when "notify"
|
94
90
|
set_notify m, op, value
|
@@ -152,14 +148,15 @@ module Zabbirc
|
|
152
148
|
|
153
149
|
### Events
|
154
150
|
def list_events m
|
155
|
-
|
151
|
+
op = authenticate m
|
152
|
+
return unless op
|
156
153
|
events = Zabbix::Event.recent
|
157
154
|
msg = if events.any?
|
158
155
|
events.collect do |e|
|
159
|
-
"#{
|
156
|
+
"#{op.nick}: #{e.label}"
|
160
157
|
end.join("\n")
|
161
158
|
else
|
162
|
-
"#{
|
159
|
+
"#{op.nick}: No last events"
|
163
160
|
end
|
164
161
|
m.reply msg
|
165
162
|
end
|
@@ -169,26 +166,13 @@ module Zabbirc
|
|
169
166
|
end
|
170
167
|
|
171
168
|
### Authentication and helpers
|
172
|
-
def authenticate obj
|
173
|
-
login = get_login obj
|
174
|
-
ops.authenticate login
|
175
|
-
end
|
176
169
|
|
177
170
|
def get_op obj
|
178
171
|
login = get_login obj
|
179
172
|
ops.get login
|
180
173
|
end
|
181
174
|
|
182
|
-
|
183
|
-
case obj
|
184
|
-
when Cinch::Message
|
185
|
-
obj.user.nick
|
186
|
-
when Cinch::User
|
187
|
-
obj.nick
|
188
|
-
when String
|
189
|
-
obj
|
190
|
-
end
|
191
|
-
end
|
175
|
+
alias_method :authenticate, :get_op
|
192
176
|
|
193
177
|
def get_login obj
|
194
178
|
case obj
|
data/lib/zabbirc/op.rb
CHANGED
@@ -2,21 +2,40 @@ module Zabbirc
|
|
2
2
|
class Op
|
3
3
|
|
4
4
|
attr_reader :channels, :setting, :nick, :login
|
5
|
-
def initialize zabbix_user
|
5
|
+
def initialize zabbix_user
|
6
6
|
raise ArgumentError, 'zabbix_user' if zabbix_user.nil?
|
7
|
-
raise ArgumentError, 'irc_user' if irc_user.nil?
|
8
7
|
@login= zabbix_user.alias
|
9
|
-
@nick = irc_user.nick
|
10
8
|
@zabbix_user = zabbix_user
|
11
|
-
@irc_user = irc_user
|
12
9
|
|
13
10
|
@notified_events = {}
|
14
11
|
@channels = Set.new
|
15
12
|
@setting = Setting.new
|
13
|
+
@mutex = Mutex.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def synchronize &block
|
17
|
+
@mutex.synchronize &block
|
18
|
+
end
|
19
|
+
|
20
|
+
def set_irc_user irc_user
|
21
|
+
@irc_user = irc_user
|
22
|
+
@nick = irc_user.nick
|
23
|
+
end
|
24
|
+
|
25
|
+
def unset_irc_user
|
26
|
+
@irc_user = nil
|
27
|
+
@nick = nil
|
16
28
|
end
|
17
29
|
|
18
30
|
def primary_channel
|
19
|
-
|
31
|
+
synchronize do
|
32
|
+
return nil if @channels.empty?
|
33
|
+
channel = @channels.find{|c| c.name == @setting.get(:primary_channel) }
|
34
|
+
return channel if channel
|
35
|
+
channel = @channels.first
|
36
|
+
@setting.set(:primary_channel, channel.name)
|
37
|
+
channel
|
38
|
+
end
|
20
39
|
end
|
21
40
|
|
22
41
|
def interesting_priority
|
@@ -24,27 +43,23 @@ module Zabbirc
|
|
24
43
|
end
|
25
44
|
|
26
45
|
def add_channel channel
|
27
|
-
|
28
|
-
|
46
|
+
synchronize do
|
47
|
+
@setting.fetch :primary_channel, channel.name
|
48
|
+
@channels << channel
|
49
|
+
end
|
29
50
|
end
|
30
51
|
|
31
52
|
def remove_channel channel
|
32
|
-
|
53
|
+
synchronize do
|
54
|
+
@channels.delete channel
|
33
55
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
end
|
56
|
+
if channel == @setting.get(:primary_channel)
|
57
|
+
@setting.set :primary_channel, @channels.first.try(:name)
|
58
|
+
end
|
38
59
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
return if @notified_events.key? event.id
|
43
|
-
channel = primary_channel
|
44
|
-
return unless channel
|
45
|
-
channel.send "#{@nick}: #{event.label}"
|
46
|
-
@notified_events[event.id] = Time.now
|
47
|
-
clear_notified_events
|
60
|
+
unset_irc_user if @channels.empty?
|
61
|
+
true
|
62
|
+
end
|
48
63
|
end
|
49
64
|
|
50
65
|
def interested_in? event
|
data/lib/zabbirc/op_list.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
module Zabbirc
|
2
|
+
STORED_SETTINGS_FILE = Zabbirc::RUNTIME_DATA_DIR.join("ops_settings.yaml")
|
2
3
|
class OpList
|
3
4
|
include Enumerable
|
4
5
|
|
@@ -28,6 +29,17 @@ module Zabbirc
|
|
28
29
|
@ops[op.login] = op
|
29
30
|
end
|
30
31
|
|
32
|
+
def remove op
|
33
|
+
case op
|
34
|
+
when String
|
35
|
+
@ops[op] = nil
|
36
|
+
when Op
|
37
|
+
@ops[op.login] = nil
|
38
|
+
else
|
39
|
+
raise ArgumentError, "Stirng or Op expected"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
31
43
|
def each &block
|
32
44
|
@ops.values.each &block
|
33
45
|
end
|
@@ -38,9 +50,36 @@ module Zabbirc
|
|
38
50
|
|
39
51
|
def notify event
|
40
52
|
group_by(&:primary_channel).each do |channel, ops|
|
41
|
-
|
42
|
-
|
43
|
-
|
53
|
+
begin
|
54
|
+
next if channel.nil?
|
55
|
+
op_targets = ops.collect{|op| "#{op.nick}:" }.join(" ")
|
56
|
+
channel.send "#{op_targets} #{event.label}"
|
57
|
+
ops.each{ |op| op.event_notified event }
|
58
|
+
rescue =>e
|
59
|
+
binding.pry
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def dump_settings
|
65
|
+
dump = {}
|
66
|
+
each do |op|
|
67
|
+
dump[op.login] = op.setting.to_hash
|
68
|
+
end
|
69
|
+
|
70
|
+
file = File.open(STORED_SETTINGS_FILE, "w")
|
71
|
+
file.puts dump.to_yaml
|
72
|
+
file.close
|
73
|
+
true
|
74
|
+
end
|
75
|
+
|
76
|
+
def load_settings
|
77
|
+
return unless File.exists?(STORED_SETTINGS_FILE)
|
78
|
+
stored_settings = YAML.load_file(STORED_SETTINGS_FILE)
|
79
|
+
stored_settings.each do |login, setting|
|
80
|
+
op = get(login)
|
81
|
+
next unless op
|
82
|
+
op.setting.restore setting
|
44
83
|
end
|
45
84
|
end
|
46
85
|
end
|
data/lib/zabbirc/service.rb
CHANGED
@@ -45,6 +45,9 @@ module Zabbirc
|
|
45
45
|
def start join=true
|
46
46
|
return if @running
|
47
47
|
@running = true
|
48
|
+
|
49
|
+
pre_start_script
|
50
|
+
|
48
51
|
@cinch_bot_thread = Thread.new do
|
49
52
|
begin
|
50
53
|
@cinch_bot.start
|
@@ -57,6 +60,7 @@ module Zabbirc
|
|
57
60
|
begin
|
58
61
|
sleep
|
59
62
|
rescue StopError
|
63
|
+
pre_stop_script
|
60
64
|
@ops_service.stop
|
61
65
|
@events_service.stop
|
62
66
|
|
@@ -75,6 +79,19 @@ module Zabbirc
|
|
75
79
|
@cinch_bot_thread.join if join
|
76
80
|
end
|
77
81
|
|
82
|
+
def pre_stop_script
|
83
|
+
@ops.dump_settings
|
84
|
+
rescue => e
|
85
|
+
Zabbirc.logger.error "Exception `#{e}` while running pre_stop_script"
|
86
|
+
end
|
87
|
+
|
88
|
+
def pre_start_script
|
89
|
+
@ops_service.iterate
|
90
|
+
@ops.load_settings
|
91
|
+
rescue => e
|
92
|
+
Zabbirc.logger.error "Exception `#{e}` while running pre_start_script"
|
93
|
+
end
|
94
|
+
|
78
95
|
def stop
|
79
96
|
@controll_thread.raise StopError
|
80
97
|
end
|
data/lib/zabbirc/services/ops.rb
CHANGED
@@ -2,11 +2,7 @@ module Zabbirc
|
|
2
2
|
module Services
|
3
3
|
class Ops < Base
|
4
4
|
def iterate
|
5
|
-
|
6
|
-
@cinch_bot.channels.each do |channel|
|
7
|
-
sync_ops channel
|
8
|
-
end
|
9
|
-
end
|
5
|
+
sync_ops
|
10
6
|
end
|
11
7
|
|
12
8
|
private
|
@@ -19,12 +15,24 @@ module Zabbirc
|
|
19
15
|
channel.users.keys.find { |irc_user| irc_user.user.sub("~","") == login }
|
20
16
|
end
|
21
17
|
|
22
|
-
def sync_ops
|
18
|
+
def sync_ops
|
19
|
+
synchronize do
|
20
|
+
sync_zabbix
|
21
|
+
@cinch_bot.channels.each do |channel|
|
22
|
+
sync_irc channel
|
23
|
+
end
|
24
|
+
end
|
25
|
+
@service.ops.dump_settings
|
26
|
+
end
|
27
|
+
|
28
|
+
def sync_irc channel
|
23
29
|
logins = channel_logins channel
|
24
|
-
|
25
|
-
|
26
|
-
irc_user = channel_find_user channel,
|
27
|
-
op = @service.ops.
|
30
|
+
|
31
|
+
logins.each do |login|
|
32
|
+
irc_user = channel_find_user channel, login
|
33
|
+
op = @service.ops.get login
|
34
|
+
next unless op
|
35
|
+
op.set_irc_user irc_user
|
28
36
|
op.add_channel channel
|
29
37
|
end
|
30
38
|
|
@@ -33,6 +41,20 @@ module Zabbirc
|
|
33
41
|
end
|
34
42
|
true
|
35
43
|
end
|
44
|
+
|
45
|
+
def sync_zabbix
|
46
|
+
|
47
|
+
zabbix_users = Zabbix::User.get
|
48
|
+
zabbix_users.each do |zabbix_user|
|
49
|
+
op = Op.new(zabbix_user)
|
50
|
+
@service.ops.add op
|
51
|
+
end
|
52
|
+
|
53
|
+
zabbix_logins = zabbix_users.collect(&:alias)
|
54
|
+
@service.ops.each do |op|
|
55
|
+
@service.ops.remove op unless zabbix_logins.include? op.login
|
56
|
+
end
|
57
|
+
end
|
36
58
|
end
|
37
59
|
end
|
38
60
|
end
|
data/lib/zabbirc/setting.rb
CHANGED
@@ -1,15 +1,23 @@
|
|
1
1
|
module Zabbirc
|
2
2
|
class Setting
|
3
|
-
DEFAULTS = {
|
3
|
+
DEFAULTS = ActiveSupport::HashWithIndifferentAccess.new({
|
4
4
|
notify: true,
|
5
5
|
primary_channel: nil,
|
6
6
|
events_priority: :information
|
7
|
-
}
|
7
|
+
})
|
8
8
|
|
9
9
|
def initialize
|
10
10
|
@options = ActiveSupport::HashWithIndifferentAccess.new DEFAULTS.deep_dup
|
11
11
|
end
|
12
12
|
|
13
|
+
def restore stored_options
|
14
|
+
stored_options = ActiveSupport::HashWithIndifferentAccess.new stored_options
|
15
|
+
unknown_keys = stored_options.keys - DEFAULTS.keys
|
16
|
+
stored_options.delete_if{|k,_v| unknown_keys.include? k }
|
17
|
+
stored_options.merge DEFAULTS.deep_dup
|
18
|
+
@options = stored_options
|
19
|
+
end
|
20
|
+
|
13
21
|
def set name, value
|
14
22
|
@options[name] = value
|
15
23
|
end
|
@@ -27,5 +35,9 @@ module Zabbirc
|
|
27
35
|
"#{k}: #{v}"
|
28
36
|
end.join(", ")
|
29
37
|
end
|
38
|
+
|
39
|
+
def to_hash
|
40
|
+
@options.to_hash.deep_dup
|
41
|
+
end
|
30
42
|
end
|
31
43
|
end
|
@@ -15,7 +15,7 @@ module Zabbirc
|
|
15
15
|
else
|
16
16
|
self.new res.first
|
17
17
|
end
|
18
|
-
rescue Errno::ETIMEDOUT => e
|
18
|
+
rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED => e
|
19
19
|
Zabbirc.logger.error "Zabbix::Resource#find: #{e}"
|
20
20
|
nil
|
21
21
|
end
|
@@ -26,7 +26,7 @@ module Zabbirc
|
|
26
26
|
res.collect do |obj|
|
27
27
|
self.new obj
|
28
28
|
end
|
29
|
-
rescue Errno::ETIMEDOUT => e
|
29
|
+
rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED => e
|
30
30
|
Zabbirc.logger.error "Zabbix::Resource#get: #{e}"
|
31
31
|
[]
|
32
32
|
end
|
data/lib/zabbirc.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'active_support/all'
|
2
2
|
require 'singleton'
|
3
3
|
require 'dotenv'
|
4
|
+
require 'yaml'
|
4
5
|
Dotenv.load
|
5
6
|
|
6
7
|
def require_dir dir
|
@@ -11,6 +12,7 @@ def require_dir dir
|
|
11
12
|
end
|
12
13
|
|
13
14
|
module Zabbirc
|
15
|
+
RUNTIME_DATA_DIR = Pathname.new("/var/run/zabbirc") unless defined? RUNTIME_DATA_DIR
|
14
16
|
def self.synchronize &block
|
15
17
|
@mutex ||= Mutex.new
|
16
18
|
@mutex.synchronize &block
|
@@ -21,4 +23,8 @@ require_dir "zabbirc/*.rb"
|
|
21
23
|
require_dir "zabbirc/irc/*.rb"
|
22
24
|
require 'zabbirc/zabbix/resource/base'
|
23
25
|
require_dir "zabbirc/zabbix/*.rb"
|
24
|
-
|
26
|
+
require 'zabbirc/services/base'
|
27
|
+
require_dir "zabbirc/services/*.rb"
|
28
|
+
|
29
|
+
# TODO Error message when cannot connect to Zabbix
|
30
|
+
# TODO Dump op specific settings in loop, so it can be loaded on the fatal server failure
|
data/spec/bot_spec.rb
CHANGED
@@ -113,7 +113,7 @@ describe Zabbirc::Irc::PluginMethods do
|
|
113
113
|
|
114
114
|
it "should print latest events" do
|
115
115
|
expect(mock_message).to receive(:reply).with(expected_msg)
|
116
|
-
bot.host_latest mock_message, "Host1", nil
|
116
|
+
bot.host_latest mock_message, "Host1", nil
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
@@ -158,7 +158,7 @@ describe Zabbirc::Irc::PluginMethods do
|
|
158
158
|
let(:op) { bot.get_op mock_nick }
|
159
159
|
it "should set #{key} setting to #{value}" do
|
160
160
|
expect(mock_message).to receive(:reply).with(expected_msg)
|
161
|
-
bot.set_setting mock_message, key,
|
161
|
+
bot.set_setting mock_message, key, value
|
162
162
|
expect(op.setting.get(key)).to eq expected_setting_value
|
163
163
|
end
|
164
164
|
end
|
data/spec/support/mock_bot.rb
CHANGED
@@ -27,7 +27,8 @@ module Zabbirc
|
|
27
27
|
@@op_ids ||= 0
|
28
28
|
zabbix_user = Zabbix::User.new(alias: name, userid: (@@op_ids+=1))
|
29
29
|
irc_user = double "IrcUser", nick: name, user: name
|
30
|
-
op = Op.new(zabbix_user
|
30
|
+
op = Op.new(zabbix_user)
|
31
|
+
op.set_irc_user irc_user
|
31
32
|
settings.each do |key, value|
|
32
33
|
op.setting.set key, value
|
33
34
|
end if settings
|
data/tmp/module_test.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
module A
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def a_method
|
8
|
+
puts "a method"
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def a_class_method
|
13
|
+
puts "a class method"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module O
|
19
|
+
def o_method
|
20
|
+
puts "o method"
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
def o_class_method
|
25
|
+
puts "o class method"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module B
|
31
|
+
extend ActiveSupport::Concern
|
32
|
+
include A
|
33
|
+
include O
|
34
|
+
def b_method
|
35
|
+
puts "b method"
|
36
|
+
end
|
37
|
+
binding.pry
|
38
|
+
|
39
|
+
module ClassMethods
|
40
|
+
def b_class_method
|
41
|
+
puts "b class method"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class C
|
47
|
+
|
48
|
+
include B
|
49
|
+
def c_method
|
50
|
+
puts "c method"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
binding.pry
|
data/tmp/thread_test.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'logger'
|
3
|
+
StopError = Class.new(StandardError)
|
4
|
+
|
5
|
+
l = Logger.new("thread_test.log")
|
6
|
+
|
7
|
+
def interruptible
|
8
|
+
Thread.handle_interrupt(StopError => :immediate) do
|
9
|
+
yield if block_given?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def critical
|
14
|
+
Thread.handle_interrupt(StopError => :never) do
|
15
|
+
yield if block_given?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# t = Thread.new do
|
20
|
+
# begin
|
21
|
+
# loop do
|
22
|
+
# critical do
|
23
|
+
# l.info "Critical start"
|
24
|
+
# sleep 2
|
25
|
+
# l.info "Critical Mid"
|
26
|
+
# interruptible
|
27
|
+
# sleep 2
|
28
|
+
# l.info "Critical end"
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# interruptible do
|
32
|
+
# l.info "Interruptible start"
|
33
|
+
# sleep 5
|
34
|
+
# l.info "Interruptible end"
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# critical do
|
38
|
+
# l.info "Critical 2 start"
|
39
|
+
# sleep 5
|
40
|
+
# l.info "Critical 2 end"
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# sleep 2
|
44
|
+
# end
|
45
|
+
# rescue StopError
|
46
|
+
# l.info "Stopped"
|
47
|
+
# rescue => e
|
48
|
+
# l.info "Interrupted: #{e}"
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
main_thread = Thread.current
|
52
|
+
t = Thread.new do
|
53
|
+
begin
|
54
|
+
attempt = 0
|
55
|
+
l.info "Inside attempt: #{attempt}"
|
56
|
+
sleep 0.5
|
57
|
+
rescue => e
|
58
|
+
main_thread.raise e
|
59
|
+
end
|
60
|
+
l.info "After"
|
61
|
+
end
|
62
|
+
|
63
|
+
binding.pry
|
data/zabbirc_config.rb
ADDED
@@ -0,0 +1,14 @@
|
|
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
|
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.
|
4
|
+
version: 0.0.3
|
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-
|
11
|
+
date: 2014-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -117,6 +117,7 @@ files:
|
|
117
117
|
- config/config.rb
|
118
118
|
- lib/zabbirc.rb
|
119
119
|
- lib/zabbirc/configuration.rb
|
120
|
+
- lib/zabbirc/irc/help.rb
|
120
121
|
- lib/zabbirc/irc/plugin.rb
|
121
122
|
- lib/zabbirc/irc/plugin_methods.rb
|
122
123
|
- lib/zabbirc/logger.rb
|
@@ -141,8 +142,11 @@ files:
|
|
141
142
|
- spec/spec_helper.rb
|
142
143
|
- spec/support/mock_bot.rb
|
143
144
|
- templates/zabbirc_config.rb
|
145
|
+
- tmp/module_test.rb
|
144
146
|
- tmp/playground.rb
|
145
|
-
|
147
|
+
- tmp/thread_test.rb
|
148
|
+
- zabbirc_config.rb
|
149
|
+
homepage: https://github.com/tulak/zabbirc
|
146
150
|
licenses:
|
147
151
|
- MIT
|
148
152
|
metadata: {}
|