zabbirc 0.0.11 → 0.1.0
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 +4 -4
- data/lib/zabbirc/irc/base_command.rb +120 -0
- data/lib/zabbirc/irc/event_command.rb +72 -0
- data/lib/zabbirc/irc/help_command.rb +20 -0
- data/lib/zabbirc/irc/host_command.rb +65 -0
- data/lib/zabbirc/irc/plugin.rb +6 -20
- data/lib/zabbirc/irc/plugin_methods.rb +16 -194
- data/lib/zabbirc/irc/settings_command.rb +159 -0
- data/lib/zabbirc/op.rb +16 -6
- data/lib/zabbirc/setting.rb +49 -12
- data/lib/zabbirc/version.rb +3 -0
- data/lib/zabbirc/zabbix/event.rb +23 -0
- data/lib/zabbirc/zabbix/host.rb +1 -0
- data/lib/zabbirc/zabbix/host_group.rb +8 -0
- data/lib/zabbirc/zabbix/resource/associations.rb +12 -6
- data/lib/zabbirc/zabbix/resource/base.rb +10 -2
- data/lib/zabbirc/zabbix/resource/finders.rb +1 -1
- data/lib/zabbirc.rb +2 -1
- data/spec/event_command_spec.rb +114 -0
- data/spec/host_command_spec.rb +106 -0
- data/spec/settings_command_spec.rb +125 -0
- data/spec/support/ops_builder.rb +23 -0
- data/tmp/playground.rb +22 -0
- data/tmp/stopwatch.rb +52 -0
- metadata +18 -7
- data/spec/bot_spec.rb +0 -212
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
module Zabbirc
|
|
2
|
+
module Irc
|
|
3
|
+
class SettingsCommand < BaseCommand
|
|
4
|
+
register_help "settings", "Manage your op specific settings. Usage: !settings [show | set]"
|
|
5
|
+
register_help "settings set", [
|
|
6
|
+
"Set your op specific settings. Usage: !setting set <setting-name> <setting-value> [hostgroups <host-group-name>[,<host-group-name>] | hostgroups-all]",
|
|
7
|
+
"Some settings can be set per hostgroup. Flag `hostgroup-all` means that it will rewrite setting for all hostgroups.",
|
|
8
|
+
"When used without optional part(hostgroup), it will change default settings."
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
def perform
|
|
13
|
+
sub_cmd = @args.shift
|
|
14
|
+
case sub_cmd
|
|
15
|
+
when nil, "show"
|
|
16
|
+
show
|
|
17
|
+
when "set"
|
|
18
|
+
set
|
|
19
|
+
else
|
|
20
|
+
reply "settings: unknown command `#{sub_cmd}`"
|
|
21
|
+
reply help_features["settings"], prefix: "HELP settings: "
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def show
|
|
26
|
+
inline = @op.setting.collect do |key, value|
|
|
27
|
+
next if key == "host_groups"
|
|
28
|
+
"#{key}: #{value}"
|
|
29
|
+
end.compact.join(", ")
|
|
30
|
+
|
|
31
|
+
reply "Default settings: #{inline}"
|
|
32
|
+
|
|
33
|
+
host_group_options = []
|
|
34
|
+
@op.setting.get(:host_groups).each do |group_id, options|
|
|
35
|
+
if options.any?
|
|
36
|
+
group = Zabbix::HostGroup.find(group_id)
|
|
37
|
+
group_options = options.collect{|k,v| "#{k}: #{v}" }.sort.join(", ")
|
|
38
|
+
host_group_options << " - #{group.name}: #{group_options}"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
if host_group_options.any?
|
|
43
|
+
reply "Host group settings:"
|
|
44
|
+
reply host_group_options
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def set
|
|
49
|
+
key, value, host_groups_flag, host_groups = parse_set_command
|
|
50
|
+
case key
|
|
51
|
+
when "notify", "notify_recoveries"
|
|
52
|
+
value = validate_boolean key, value
|
|
53
|
+
when "events_priority"
|
|
54
|
+
value = validate_events_priority value
|
|
55
|
+
when "primary_channel"
|
|
56
|
+
value = validate_primary_channel value
|
|
57
|
+
when nil, ""
|
|
58
|
+
reply help_features["settings set"]
|
|
59
|
+
else
|
|
60
|
+
reply "unknown setting `#{key}`"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
set_value key, value, host_groups_flag, host_groups
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def validate_boolean key, value
|
|
67
|
+
raise UserInputError, "#{key} allowed values: true, on, 1, false, off, 0" if value.blank?
|
|
68
|
+
|
|
69
|
+
case value
|
|
70
|
+
when "true", "on", "1" then true
|
|
71
|
+
when "false", "off", "0" then false
|
|
72
|
+
else
|
|
73
|
+
raise UserInputError, "uknown value `#{value}`. Allowed values: true, on, 1, false, off, 0"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def validate_events_priority value
|
|
78
|
+
allowed_values = "#{Priority::PRIORITIES.values.collect{|v| "`#{v}`"}.join(', ')} or numeric #{Priority::PRIORITIES.keys.join(", ")} "
|
|
79
|
+
raise UserInputError, "events_priority allowed values: #{allowed_values}" if value.blank?
|
|
80
|
+
begin
|
|
81
|
+
value = value.to_i if value =~ /^\d+$/
|
|
82
|
+
priority = Priority.new value
|
|
83
|
+
rescue ArgumentError
|
|
84
|
+
raise UserInputError, "uknown value `#{value}`. Allowed values: #{allowed_values}"
|
|
85
|
+
end
|
|
86
|
+
priority.code
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def validate_primary_channel value
|
|
90
|
+
channel_names = @op.channels.collect(&:name)
|
|
91
|
+
raise UserInputError, "primary_channel allowed values: #{channel_names.join(", ")}" if value.blank?
|
|
92
|
+
raise UserInputError, "uknown value `#{value}`. Allowed values: #{channel_names.join(", ")}" unless channel_names.include? value
|
|
93
|
+
value
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def set_value key, value, host_groups_flag, host_groups
|
|
97
|
+
case host_groups_flag
|
|
98
|
+
when :none
|
|
99
|
+
@op.setting.set key, value
|
|
100
|
+
reply "setting `#{key}` has been set to `#{@op.setting.get key}`"
|
|
101
|
+
when :all
|
|
102
|
+
@op.setting.set key, value
|
|
103
|
+
host_groups.each do |host_group|
|
|
104
|
+
@op.setting.set key, value, host_group_id: host_group.id
|
|
105
|
+
end
|
|
106
|
+
reply "setting `#{key}` has been set to `#{@op.setting.get key}` for all host groups"
|
|
107
|
+
when :some
|
|
108
|
+
host_groups.each do |host_group|
|
|
109
|
+
@op.setting.set key, value, host_group_id: host_group.id
|
|
110
|
+
end
|
|
111
|
+
reply "setting `#{key}` has been set to `#{value}` for host groups: #{host_groups.collect(&:name).join(", ")}"
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
def parse_set_command
|
|
117
|
+
key = @args.shift
|
|
118
|
+
value = @args.shift
|
|
119
|
+
host_groups_arg = @args.shift
|
|
120
|
+
|
|
121
|
+
case host_groups_arg
|
|
122
|
+
when 'hostgroups-all'
|
|
123
|
+
host_groups = Zabbix::HostGroup.get
|
|
124
|
+
host_groups_flag = :all
|
|
125
|
+
when 'hostgroups'
|
|
126
|
+
names = @args.join(" ").split(",").collect(&:strip)
|
|
127
|
+
raise UserInputError, ['no hostgroups specified'] + Array.wrap(help_features["settings set"]) if names.empty?
|
|
128
|
+
host_groups = find_host_groups names
|
|
129
|
+
host_groups_flag = :some
|
|
130
|
+
when nil, ""
|
|
131
|
+
host_groups = []
|
|
132
|
+
host_groups_flag = :none
|
|
133
|
+
else
|
|
134
|
+
raise UserInputError, help_features["settings set"]
|
|
135
|
+
end
|
|
136
|
+
[key, value, host_groups_flag, host_groups]
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def find_host_groups names
|
|
140
|
+
names.collect do |name|
|
|
141
|
+
host_group = Zabbix::HostGroup.get(search: { name: name })
|
|
142
|
+
case host_group.size
|
|
143
|
+
when 0
|
|
144
|
+
raise UserInputError, "Cannot find hostgroup with name: `#{name}`"
|
|
145
|
+
when 1
|
|
146
|
+
host_group.first
|
|
147
|
+
when 2..10
|
|
148
|
+
raise UserInputError, "Found #{host_group.size} host groups using name: `#{name}` be more specific. Host groups: #{host_group.collect(&:name).join(", ")}"
|
|
149
|
+
else
|
|
150
|
+
raise UserInputError, "Found #{host_group.size} host groups using name: `#{name}` be more specific"
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# TODO filtrovanie eventov podla hostgroup
|
data/lib/zabbirc/op.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module Zabbirc
|
|
2
2
|
class Op
|
|
3
3
|
|
|
4
|
-
attr_reader :channels, :setting, :nick, :login
|
|
4
|
+
attr_reader :channels, :setting, :nick, :login, :irc_user
|
|
5
5
|
def initialize zabbix_user
|
|
6
6
|
raise ArgumentError, 'zabbix_user' if zabbix_user.nil?
|
|
7
7
|
@login= zabbix_user.alias
|
|
@@ -38,8 +38,10 @@ module Zabbirc
|
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
def
|
|
42
|
-
|
|
41
|
+
def interesting_priorities host_groups
|
|
42
|
+
host_settings(:events_priority, host_groups).collect do |setting|
|
|
43
|
+
Priority.new setting
|
|
44
|
+
end
|
|
43
45
|
end
|
|
44
46
|
|
|
45
47
|
def add_channel channel
|
|
@@ -63,10 +65,18 @@ module Zabbirc
|
|
|
63
65
|
end
|
|
64
66
|
|
|
65
67
|
def interested_in? event
|
|
66
|
-
|
|
68
|
+
host_groups = event.host_groups
|
|
69
|
+
return false if host_settings(:notify, host_groups).all?{|x| x == false }
|
|
67
70
|
return false if @notified_events.key? event.id
|
|
68
|
-
return false if event.value == :ok and
|
|
69
|
-
|
|
71
|
+
return false if event.value == :ok and host_settings(:notify_recoveries, host_groups).all?{|x| x == false }
|
|
72
|
+
interesting_priorities(host_groups).any? do |interesting_priority|
|
|
73
|
+
event.priority >= interesting_priority
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def host_settings name, host_groups
|
|
78
|
+
settings = host_groups.collect{|hg| setting.get name, host_group_id: hg.id }
|
|
79
|
+
settings.uniq
|
|
70
80
|
end
|
|
71
81
|
|
|
72
82
|
def event_notified event
|
data/lib/zabbirc/setting.rb
CHANGED
|
@@ -4,9 +4,11 @@ module Zabbirc
|
|
|
4
4
|
notify: true,
|
|
5
5
|
notify_recoveries: true,
|
|
6
6
|
primary_channel: nil,
|
|
7
|
-
events_priority: Zabbirc.config.default_events_priority
|
|
7
|
+
events_priority: Zabbirc.config.default_events_priority,
|
|
8
|
+
host_groups: ActiveSupport::HashWithIndifferentAccess.new
|
|
8
9
|
})
|
|
9
10
|
|
|
11
|
+
delegate :collect, to: :@options
|
|
10
12
|
def initialize
|
|
11
13
|
@options = ActiveSupport::HashWithIndifferentAccess.new DEFAULTS.deep_dup
|
|
12
14
|
end
|
|
@@ -18,26 +20,61 @@ module Zabbirc
|
|
|
18
20
|
@options = DEFAULTS.deep_dup.merge(stored_options)
|
|
19
21
|
end
|
|
20
22
|
|
|
21
|
-
def set name, value
|
|
22
|
-
|
|
23
|
+
def set name, value, *options
|
|
24
|
+
host_group_id = parse_host_group_id(options)
|
|
25
|
+
if host_group_id
|
|
26
|
+
set_with_host_group name, value, host_group_id
|
|
27
|
+
else
|
|
28
|
+
@options[name] = value
|
|
29
|
+
end
|
|
23
30
|
end
|
|
24
31
|
|
|
25
|
-
def get name
|
|
26
|
-
|
|
32
|
+
def get name, *options
|
|
33
|
+
host_group_id = parse_host_group_id(options)
|
|
34
|
+
if host_group_id
|
|
35
|
+
first_not_nil(host_group_options(host_group_id)[name], @options[name])
|
|
36
|
+
else
|
|
37
|
+
@options[name]
|
|
38
|
+
end
|
|
27
39
|
end
|
|
28
40
|
|
|
29
|
-
def fetch name, value
|
|
30
|
-
|
|
31
|
-
end
|
|
41
|
+
def fetch name, value, *options
|
|
42
|
+
host_group_id = parse_host_group_id(options)
|
|
32
43
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
if host_group_id
|
|
45
|
+
if host_group_options(host_group_id)[name].nil?
|
|
46
|
+
set_with_host_group name, value, host_group_id
|
|
47
|
+
else
|
|
48
|
+
get name, host_group_id: host_group_id
|
|
49
|
+
end
|
|
50
|
+
else
|
|
51
|
+
@options[name] ||= value
|
|
52
|
+
end
|
|
37
53
|
end
|
|
38
54
|
|
|
39
55
|
def to_hash
|
|
40
56
|
@options.to_hash.deep_dup
|
|
41
57
|
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
def parse_host_group_id args
|
|
61
|
+
args.extract_options![:host_group_id]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def set_with_host_group name, value, host_group_id
|
|
65
|
+
if get(name) == value
|
|
66
|
+
host_group_options(host_group_id).delete(name)
|
|
67
|
+
else
|
|
68
|
+
host_group_options(host_group_id)[name] = value
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def host_group_options host_group_id
|
|
73
|
+
@options[:host_groups][host_group_id] ||= ActiveSupport::HashWithIndifferentAccess.new
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def first_not_nil *args
|
|
77
|
+
args.find{|x| not x.nil? }
|
|
78
|
+
end
|
|
42
79
|
end
|
|
43
80
|
end
|
data/lib/zabbirc/zabbix/event.rb
CHANGED
|
@@ -12,13 +12,28 @@ module Zabbirc
|
|
|
12
12
|
selectHosts: :extend
|
|
13
13
|
}.merge(options)
|
|
14
14
|
|
|
15
|
+
|
|
16
|
+
|
|
15
17
|
priority_from = Priority.new(params.delete(:priority_from))
|
|
16
18
|
events = get params
|
|
19
|
+
|
|
20
|
+
preload_host_groups events
|
|
21
|
+
|
|
17
22
|
events = events.find_all{|e| e.priority >= priority_from }
|
|
18
23
|
events.sort{|x,y| x.priority <=> y.priority }
|
|
19
24
|
end
|
|
20
25
|
|
|
26
|
+
def self.preload_host_groups events
|
|
27
|
+
host_ids = events.flat_map(&:hosts).collect(&:id).uniq
|
|
28
|
+
hosts = Host.get(hostids: host_ids, selectGroups: :extend)
|
|
29
|
+
events.each do |event|
|
|
30
|
+
event_host_ids = event.hosts.collect(&:id)
|
|
31
|
+
event.host_groups = hosts.select{|h| event_host_ids.include? h.id }.flat_map(&:groups)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
21
35
|
attr_reader :attrs
|
|
36
|
+
attr_writer :host_groups
|
|
22
37
|
|
|
23
38
|
delegate :priority, :priority_code, to: :related_object
|
|
24
39
|
|
|
@@ -28,6 +43,14 @@ module Zabbirc
|
|
|
28
43
|
@related_object ||= determine_related_object
|
|
29
44
|
end
|
|
30
45
|
|
|
46
|
+
def host_groups
|
|
47
|
+
@host_groups ||= begin
|
|
48
|
+
host_ids = hosts.collect(&:id).uniq
|
|
49
|
+
hosts = Host.get(hostids: host_ids, selectGroups: :extend)
|
|
50
|
+
hosts.flat_map(&:groups)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
31
54
|
def acknowledge message
|
|
32
55
|
res = api.event.acknowledge(eventids: id, message: message)
|
|
33
56
|
res["eventids"].collect(&:to_i).include? id.to_i
|
data/lib/zabbirc/zabbix/host.rb
CHANGED
|
@@ -2,16 +2,22 @@ module Zabbirc
|
|
|
2
2
|
module Zabbix
|
|
3
3
|
module Resource
|
|
4
4
|
module Associations
|
|
5
|
-
def has_many name
|
|
5
|
+
def has_many name, *options
|
|
6
|
+
options = options.extract_options!.reverse_merge({
|
|
7
|
+
zabbix_attribute: name,
|
|
8
|
+
class_name: name.to_s.singularize.camelize
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
extend_key = :"select#{options[:zabbix_attribute].to_s.camelize.pluralize || name.to_s.camelize}"
|
|
6
12
|
define_method name do
|
|
7
13
|
@associations ||= ActiveSupport::HashWithIndifferentAccess.new
|
|
8
14
|
@associations[name] ||= begin
|
|
9
|
-
assoc_class = Zabbix.const_get(
|
|
10
|
-
hash_data = @attrs[
|
|
15
|
+
assoc_class = Zabbix.const_get(options[:class_name])
|
|
16
|
+
hash_data = @attrs[options[:zabbix_attribute]]
|
|
11
17
|
if hash_data.blank?
|
|
12
|
-
this = self.class.find id,
|
|
13
|
-
raise StandardError, "zabbix response does not contain #{
|
|
14
|
-
hash_data = this[
|
|
18
|
+
this = self.class.find id, extend_key => :extend
|
|
19
|
+
raise StandardError, "zabbix response does not contain #{options[:zabbix_attribute]}" if this[options[:zabbix_attribute]].blank?
|
|
20
|
+
hash_data = this[options[:zabbix_attribute]]
|
|
15
21
|
end
|
|
16
22
|
|
|
17
23
|
hash_data.collect do |obj|
|
|
@@ -18,6 +18,14 @@ module Zabbirc
|
|
|
18
18
|
@model_name ||= name.split(/::/).last.underscore
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
+
def self.id_attr_name
|
|
22
|
+
@id_attr_name ||= "#{model_name}id"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.set_id_attr_name id_attr_name
|
|
26
|
+
@id_attr_name = id_attr_name
|
|
27
|
+
end
|
|
28
|
+
|
|
21
29
|
def self.api
|
|
22
30
|
Connection.get_connection.client
|
|
23
31
|
end
|
|
@@ -28,11 +36,11 @@ module Zabbirc
|
|
|
28
36
|
|
|
29
37
|
def initialize attrs
|
|
30
38
|
@attrs = ActiveSupport::HashWithIndifferentAccess.new attrs
|
|
31
|
-
raise ArgumentError, "attribute `#{self.class.
|
|
39
|
+
raise ArgumentError, "attribute `#{self.class.id_attr_name}` not found, probably not a #{self.class.model_name}" unless @attrs.key? :"#{self.class.id_attr_name}"
|
|
32
40
|
end
|
|
33
41
|
|
|
34
42
|
def id
|
|
35
|
-
@attrs[
|
|
43
|
+
@attrs[self.class.id_attr_name]
|
|
36
44
|
end
|
|
37
45
|
|
|
38
46
|
def [] attr
|
data/lib/zabbirc.rb
CHANGED
|
@@ -2,6 +2,7 @@ require 'active_support/all'
|
|
|
2
2
|
require 'singleton'
|
|
3
3
|
require 'dotenv'
|
|
4
4
|
require 'yaml'
|
|
5
|
+
require 'pathname'
|
|
5
6
|
Dotenv.load
|
|
6
7
|
|
|
7
8
|
def require_dir dir
|
|
@@ -29,4 +30,4 @@ require_dir "zabbirc/irc/*.rb"
|
|
|
29
30
|
require 'zabbirc/zabbix/resource/base'
|
|
30
31
|
require_dir "zabbirc/zabbix/*.rb"
|
|
31
32
|
require 'zabbirc/services/base'
|
|
32
|
-
require_dir "zabbirc/services/*.rb"
|
|
33
|
+
require_dir "zabbirc/services/*.rb"
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
describe Zabbirc::Irc::HostCommand do
|
|
2
|
+
let(:ops_builder) { Zabbirc::OpsBuilder.new }
|
|
3
|
+
let(:mock_nick) { "op1" }
|
|
4
|
+
let(:mock_op) { ops_builder.build_op(mock_nick, mock_user_settings) }
|
|
5
|
+
let(:mock_message) { double("Cinch::Message", user: mock_op.irc_user) }
|
|
6
|
+
let(:mock_user_settings) { nil }
|
|
7
|
+
let(:event_command) { Zabbirc::Irc::EventCommand.new ops_builder.ops, mock_message, cmd }
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
describe "events", focus: true do
|
|
11
|
+
context "no host arg" do
|
|
12
|
+
let(:cmd) { "events" }
|
|
13
|
+
let(:event1) { double "Event", label: "Event 1 label", priority: Zabbirc::Priority.new(:high) }
|
|
14
|
+
let(:event2) { double "Event", label: "Event 2 label", priority: Zabbirc::Priority.new(:high) }
|
|
15
|
+
let(:events) { [event1, event2] }
|
|
16
|
+
|
|
17
|
+
let(:expected_msg) do
|
|
18
|
+
events.collect do |event|
|
|
19
|
+
"#{mock_nick}: #{event.label}"
|
|
20
|
+
end.join("\n")
|
|
21
|
+
end
|
|
22
|
+
before do
|
|
23
|
+
allow(Zabbirc::Zabbix::Event).to receive(:recent).and_return(events)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "should report latest events" do
|
|
27
|
+
expect(mock_message).to receive(:reply).with(expected_msg)
|
|
28
|
+
event_command.run
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context "with priority arg" do
|
|
33
|
+
let(:priority) { Zabbirc::Priority.new "high" }
|
|
34
|
+
let(:cmd) { "events #{priority}" }
|
|
35
|
+
let(:event_high) { double "Event", label: "Event High label", priority: Zabbirc::Priority.new(:high) }
|
|
36
|
+
let(:event_average) { double "Event", label: "Event Average label", priority: Zabbirc::Priority.new(:average) }
|
|
37
|
+
let(:events) { [event_high, event_average] }
|
|
38
|
+
let(:expected_events) { events.select{|e| e.priority == priority } }
|
|
39
|
+
|
|
40
|
+
let(:expected_msg) do
|
|
41
|
+
expected_events.collect do |event|
|
|
42
|
+
"#{mock_nick}: #{event.label}"
|
|
43
|
+
end.join("\n")
|
|
44
|
+
end
|
|
45
|
+
before do
|
|
46
|
+
allow(Zabbirc::Zabbix::Event).to receive(:recent).and_return(events)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "should report latest events" do
|
|
50
|
+
expect(mock_message).to receive(:reply).with(expected_msg)
|
|
51
|
+
event_command.run
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
context "with priority and host arg" do
|
|
57
|
+
let(:host) { "host1" }
|
|
58
|
+
let(:priority) { Zabbirc::Priority.new "high" }
|
|
59
|
+
let(:cmd) { "events #{priority} #{host}" }
|
|
60
|
+
let(:event_high_good_host) { double "Event", label: "Event High label", priority: Zabbirc::Priority.new(:high) }
|
|
61
|
+
let(:event_high_bad_host) { double "Event", label: "Event High label", priority: Zabbirc::Priority.new(:high) }
|
|
62
|
+
let(:event_average) { double "Event", label: "Event Average label", priority: Zabbirc::Priority.new(:average) }
|
|
63
|
+
let(:events) { [event_high_good_host, event_high_bad_host, event_average] }
|
|
64
|
+
let(:expected_events) { events.select{|e| e.priority == priority }.select{|e| e.any_host_matches? host } }
|
|
65
|
+
|
|
66
|
+
let(:expected_msg) do
|
|
67
|
+
expected_events.collect do |event|
|
|
68
|
+
"#{mock_nick}: #{event.label}"
|
|
69
|
+
end.join("\n")
|
|
70
|
+
end
|
|
71
|
+
before do
|
|
72
|
+
allow(Zabbirc::Zabbix::Event).to receive(:recent).and_return(events)
|
|
73
|
+
allow(event_high_good_host).to receive(:any_host_matches?).with(/#{host}/).and_return(true)
|
|
74
|
+
allow(event_high_bad_host).to receive(:any_host_matches?).with(/#{host}/).and_return(false)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "should report latest events" do
|
|
78
|
+
expect(mock_message).to receive(:reply).with(expected_msg)
|
|
79
|
+
event_command.run
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
describe "ack" do
|
|
85
|
+
let(:event) { double "Event", eventid: 1, label: "Event to ack" }
|
|
86
|
+
let(:message) { "ack message" }
|
|
87
|
+
let(:cmd) { "ack #{short_event_id} #{message}" }
|
|
88
|
+
|
|
89
|
+
context "bad short_event_id" do
|
|
90
|
+
let(:short_event_id) { "XXX" }
|
|
91
|
+
let(:expected_msg) { "#{mock_nick}: Bad event id `#{short_event_id}`" }
|
|
92
|
+
|
|
93
|
+
it "should report error message" do
|
|
94
|
+
expect(mock_message).to receive(:reply).with(expected_msg)
|
|
95
|
+
event_command.run
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
context "good short_event_id" do
|
|
100
|
+
let(:short_event_id) { Zabbirc.events_id_shortener.get_shorten_id(event.eventid) }
|
|
101
|
+
let(:expected_msg) { "#{mock_nick}: Event `#{event.label}` acknowledged with message: #{message}" }
|
|
102
|
+
before do
|
|
103
|
+
allow(Zabbirc::Zabbix::Event).to receive(:find).with(event.eventid, anything).and_return(event)
|
|
104
|
+
allow(event).to receive(:acknowledge).and_return(true)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it "should acknowledge event" do
|
|
108
|
+
expect(mock_message).to receive(:reply).with(expected_msg)
|
|
109
|
+
event_command.run
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
describe Zabbirc::Irc::HostCommand do
|
|
2
|
+
let(:ops_builder) { Zabbirc::OpsBuilder.new }
|
|
3
|
+
let(:mock_nick) { "op1" }
|
|
4
|
+
let(:mock_op) { ops_builder.build_op(mock_nick, mock_user_settings) }
|
|
5
|
+
let(:mock_message) { double("Cinch::Message", user: mock_op.irc_user) }
|
|
6
|
+
let(:mock_user_settings) { nil }
|
|
7
|
+
let(:host_command) { Zabbirc::Irc::HostCommand.new ops_builder.ops, mock_message, cmd }
|
|
8
|
+
|
|
9
|
+
describe "#host_status" do
|
|
10
|
+
before do
|
|
11
|
+
allow(Zabbirc::Zabbix::Host).to receive(:get).and_return(hosts)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
context "reporting" do
|
|
15
|
+
before do
|
|
16
|
+
allow(Zabbirc::Zabbix::Trigger).to receive(:get).and_return(problem_triggers)
|
|
17
|
+
end
|
|
18
|
+
let(:cmd) { "status #{host.name}" }
|
|
19
|
+
let(:host) { double "Host", id: 1, name: "Host-1" }
|
|
20
|
+
let(:hosts) { [host] }
|
|
21
|
+
let(:problem_trigger) { double "Trigger", priority: Zabbirc::Priority.new(1), label: "problem_trigger", value: 1 }
|
|
22
|
+
context "problem trigger" do
|
|
23
|
+
let(:problem_triggers) { [problem_trigger] }
|
|
24
|
+
let(:expected_msg) do
|
|
25
|
+
msg = ["#{mock_nick}: Host: #{host.name} - status: #{problem_triggers.size} problems"]
|
|
26
|
+
problem_triggers.each do |trigger|
|
|
27
|
+
msg << "#{mock_nick}: status: #{trigger.label}"
|
|
28
|
+
end
|
|
29
|
+
msg.join("\n")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "should report problem" do
|
|
33
|
+
expect(mock_message).to receive(:reply).with(expected_msg)
|
|
34
|
+
host_command.run
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
context "ok trigger" do
|
|
39
|
+
let(:problem_triggers) { [] }
|
|
40
|
+
let(:expected_msg) { "#{mock_nick}: Host: #{host.name} - status: OK" }
|
|
41
|
+
it "should report problem" do
|
|
42
|
+
expect(mock_message).to receive(:reply).with(expected_msg)
|
|
43
|
+
host_command.run
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end # context reporting
|
|
47
|
+
|
|
48
|
+
context "host identification" do
|
|
49
|
+
let(:cmd) { "status #{host_name_value}" }
|
|
50
|
+
context "no hosts" do
|
|
51
|
+
let(:hosts) { [] }
|
|
52
|
+
let(:host_name_value) { "undefined_host_name" }
|
|
53
|
+
it "should not found host" do
|
|
54
|
+
expect(mock_message).to receive(:reply).with("#{mock_nick}: Host not found `#{host_name_value}`")
|
|
55
|
+
host_command.run
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
context "2 - 10 hosts" do
|
|
60
|
+
let(:host1) { double "Host1", id: 1, name: "host-1" }
|
|
61
|
+
let(:host2) { double "Host2", id: 2, name: "host-2" }
|
|
62
|
+
let(:hosts) { [host1, host2] }
|
|
63
|
+
let(:host_name_value) { "host" }
|
|
64
|
+
let(:expected_msg) { "#{mock_nick}: Found #{hosts.size} hosts: #{hosts.collect(&:name).join(', ')}. Be more specific" }
|
|
65
|
+
it "should print host names" do
|
|
66
|
+
expect(mock_message).to receive(:reply).with(expected_msg)
|
|
67
|
+
host_command.run
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
context "more than 10 hosts" do
|
|
72
|
+
let(:hosts) { double "HostsArray", size: 11 }
|
|
73
|
+
let(:host_name_value) { "host" }
|
|
74
|
+
let(:expected_msg) { "#{mock_nick}: Found #{hosts.size} Be more specific" }
|
|
75
|
+
it "should print host names" do
|
|
76
|
+
expect(mock_message).to receive(:reply).with(expected_msg)
|
|
77
|
+
host_command.run
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end # context host identification
|
|
81
|
+
end # context #host_status
|
|
82
|
+
|
|
83
|
+
context "#host_latest" do
|
|
84
|
+
let(:host) { double "Host", id: 1, name: "Host1" }
|
|
85
|
+
let(:hosts) { [host] }
|
|
86
|
+
let(:event1) { double "Event", label: "Event 1 label" }
|
|
87
|
+
let(:events) { [event1] }
|
|
88
|
+
let(:expected_msg) do
|
|
89
|
+
msg = ["#{mock_nick}: Host: #{host.name} - showing last #{events.size} events"]
|
|
90
|
+
events.each do |event|
|
|
91
|
+
msg << "#{mock_nick}: !latest: #{event.label}"
|
|
92
|
+
end
|
|
93
|
+
msg.join("\n")
|
|
94
|
+
end
|
|
95
|
+
before do
|
|
96
|
+
allow(Zabbirc::Zabbix::Host).to receive(:get).and_return(hosts)
|
|
97
|
+
allow(Zabbirc::Zabbix::Event).to receive(:get).and_return(events)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
let(:cmd) { "latest Host1" }
|
|
101
|
+
it "should print latest events" do
|
|
102
|
+
expect(mock_message).to receive(:reply).with(expected_msg)
|
|
103
|
+
host_command.run
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|