zabbirc 0.2.0 → 0.2.1
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/bin/zabbirc +3 -2
- data/lib/zabbirc/configuration.rb +1 -0
- data/lib/zabbirc/irc/base_command.rb +1 -0
- data/lib/zabbirc/irc/event_command.rb +1 -1
- data/lib/zabbirc/irc/maintenance_command.rb +9 -2
- data/lib/zabbirc/op.rb +1 -0
- data/lib/zabbirc/version.rb +1 -1
- data/lib/zabbirc/zabbix/event.rb +18 -2
- data/lib/zabbirc/zabbix/maintenance.rb +29 -3
- data/spec/event_command_spec.rb +2 -2
- data/spec/maintenance_command_spec.rb +18 -0
- data/tmp/playground.rb +6 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d4081d968390062febd6847ba0792fd884837ef
|
4
|
+
data.tar.gz: a06fc08a9ce62d8732a7452d881446abf71b3452
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f4874d643b926dcb8e6fd7fb82fd10d4d3e087bd5ac14d2f232b93f9b02fb0eaa9e3a1662246cefdb0a144e68a6ad32f84b1f80a1982f7398a9ef910045665d
|
7
|
+
data.tar.gz: 88c8c869678986755f6738a1efe8a8fb75b5792146a7efc5de2d16fb1228eb9ae4893a0809f06bf4b6ba05e7f17fc20b0ef00d37e8b125861cab0a18f2eddb87
|
data/bin/zabbirc
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
require 'zabbirc'
|
2
|
+
require 'zabbirc/configuration'
|
3
3
|
config_path = File.expand_path(ARGV[0])
|
4
|
-
|
5
4
|
unless config_path
|
6
5
|
$stderr.puts "No config path specified"
|
7
6
|
exit false
|
@@ -13,6 +12,8 @@ end
|
|
13
12
|
|
14
13
|
require_relative config_path
|
15
14
|
|
15
|
+
require 'zabbirc'
|
16
|
+
|
16
17
|
Zabbirc.logger # initializes logger
|
17
18
|
exit false unless Zabbirc::Zabbix::Connection.test_connection
|
18
19
|
|
@@ -21,7 +21,7 @@ module Zabbirc
|
|
21
21
|
|
22
22
|
events = Zabbix::Event.recent
|
23
23
|
events = events.select{|e| e.priority >= priority }
|
24
|
-
events = events.select{|e| e.any_host_matches? /#{host}/ } if host.present?
|
24
|
+
events = events.select{|e| e.any_host_matches? /#{host}/i } if host.present?
|
25
25
|
msg = if events.any?
|
26
26
|
events.collect do |e|
|
27
27
|
"#{e.label}"
|
@@ -3,7 +3,7 @@ module Zabbirc
|
|
3
3
|
class MaintenanceCommand < BaseCommand
|
4
4
|
register_help "maint", [
|
5
5
|
"Show active maintenances: !maint",
|
6
|
-
"Schedule a maintenance: !maint [hostgroups] '<host_name>|<hostgroup_name>[, <host_name>|<hostgroup_name>]' <duration> <reason>",
|
6
|
+
"Schedule a maintenance: !maint [no-data] [hostgroups] '<host_name>|<hostgroup_name>[, <host_name>|<hostgroup_name>]' <duration> <reason>",
|
7
7
|
" - duration format: 1h, 30m, 1h30m. h - hour, m - minute.",
|
8
8
|
"Delete a maintenance: !maint delete <maintenance-id>"
|
9
9
|
]
|
@@ -34,7 +34,14 @@ module Zabbirc
|
|
34
34
|
|
35
35
|
def perform_create
|
36
36
|
params = {}
|
37
|
-
|
37
|
+
without_data_collection_flag = @args.shift
|
38
|
+
if without_data_collection_flag == "no-data"
|
39
|
+
params[:without_data_collection] = true
|
40
|
+
hostgroups_flag = @args.shift
|
41
|
+
else
|
42
|
+
hostgroups_flag = without_data_collection_flag
|
43
|
+
end
|
44
|
+
|
38
45
|
if hostgroups_flag == "hostgroups"
|
39
46
|
target_names = @args.shift
|
40
47
|
raise UserInputError, help_features["maint"] unless target_names
|
data/lib/zabbirc/op.rb
CHANGED
@@ -69,6 +69,7 @@ module Zabbirc
|
|
69
69
|
return false if host_settings(:notify, host_groups).all?{|x| x == false }
|
70
70
|
return false if @notified_events.key? event.id
|
71
71
|
return false if event.value == :ok and host_settings(:notify_recoveries, host_groups).all?{|x| x == false }
|
72
|
+
return false if event.maintenance?
|
72
73
|
interesting_priorities(host_groups).any? do |interesting_priority|
|
73
74
|
event.priority >= interesting_priority
|
74
75
|
end
|
data/lib/zabbirc/version.rb
CHANGED
data/lib/zabbirc/zabbix/event.rb
CHANGED
@@ -60,6 +60,21 @@ module Zabbirc
|
|
60
60
|
acknowledged.to_i == 1
|
61
61
|
end
|
62
62
|
|
63
|
+
def maintenance?
|
64
|
+
maintenace_host_ids = Maintenance.cached(created_at).flat_map{|m| m.hosts.map(&:id) }
|
65
|
+
event_host_ids = hosts.flat_map(&:id)
|
66
|
+
return true if (maintenace_host_ids & event_host_ids).any?
|
67
|
+
|
68
|
+
maintenace_group_ids = Maintenance.cached(created_at).flat_map{|m| m.groups.map(&:id) }
|
69
|
+
event_group_ids = host_groups.flat_map(&:id)
|
70
|
+
return true if (maintenace_group_ids & event_group_ids).any?
|
71
|
+
false
|
72
|
+
end
|
73
|
+
|
74
|
+
def maintenance_label
|
75
|
+
" [MAINT]" if maintenance?
|
76
|
+
end
|
77
|
+
|
63
78
|
def created_at
|
64
79
|
Time.at(clock.to_i)
|
65
80
|
end
|
@@ -94,7 +109,7 @@ module Zabbirc
|
|
94
109
|
end
|
95
110
|
|
96
111
|
def label
|
97
|
-
format_label "|%sid| %time [%priority-code] %msg - %state"
|
112
|
+
format_label "|%sid| %time%maint [%priority-code] %msg - %state"
|
98
113
|
end
|
99
114
|
|
100
115
|
def format_label fmt
|
@@ -104,7 +119,8 @@ module Zabbirc
|
|
104
119
|
gsub("%msg", "#{message}").
|
105
120
|
gsub("%id", "#{id}").
|
106
121
|
gsub("%sid", "#{shorten_id}").
|
107
|
-
gsub("%state", "#{state}")
|
122
|
+
gsub("%state", "#{state}").
|
123
|
+
gsub("%maint", "#{maintenance_label}")
|
108
124
|
end
|
109
125
|
|
110
126
|
def any_host_matches? regexp
|
@@ -2,6 +2,7 @@ module Zabbirc
|
|
2
2
|
module Zabbix
|
3
3
|
class Maintenance < Resource::Base
|
4
4
|
|
5
|
+
CACHE_TTL = 1.minute
|
5
6
|
has_many :hosts
|
6
7
|
has_many :groups, class_name: "HostGroup"
|
7
8
|
|
@@ -15,7 +16,8 @@ module Zabbirc
|
|
15
16
|
def self.create *options
|
16
17
|
default_options = {
|
17
18
|
host_ids: [],
|
18
|
-
host_group_ids: []
|
19
|
+
host_group_ids: [],
|
20
|
+
without_data_collection: false
|
19
21
|
}
|
20
22
|
|
21
23
|
options = options.extract_options!
|
@@ -34,6 +36,7 @@ module Zabbirc
|
|
34
36
|
active_till: maint_end.to_i,
|
35
37
|
hostids: host_ids,
|
36
38
|
groupids: host_group_ids,
|
39
|
+
maintenance_type: (options[:without_data_collection] ? 1 : 0),
|
37
40
|
timeperiods: [
|
38
41
|
{
|
39
42
|
timeperiod_type: 0,
|
@@ -46,6 +49,20 @@ module Zabbirc
|
|
46
49
|
r["maintenanceids"].first
|
47
50
|
end
|
48
51
|
|
52
|
+
def self.cached timestamp = nil
|
53
|
+
if @maintenances_timestamp and @maintenances_timestamp < CACHE_TTL.ago
|
54
|
+
@maintenances, @maintenances_timestamp = nil, nil
|
55
|
+
end
|
56
|
+
@maintenances_timestamp ||= Time.current
|
57
|
+
@maintenances ||= Maintenance.get(selectHosts: :extend, selectGroups: :extend)
|
58
|
+
|
59
|
+
if timestamp
|
60
|
+
@maintenances.select{|maintenance| maintenance.active_range.cover?(timestamp) }
|
61
|
+
else
|
62
|
+
@maintenances
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
49
66
|
def shorten_id
|
50
67
|
@shorten_id ||= Zabbirc.maintenances_id_shortener.get_shorten_id id
|
51
68
|
end
|
@@ -58,12 +75,20 @@ module Zabbirc
|
|
58
75
|
Time.at(super.to_i)
|
59
76
|
end
|
60
77
|
|
78
|
+
def active_range
|
79
|
+
active_since..active_till
|
80
|
+
end
|
81
|
+
|
61
82
|
def active?
|
62
83
|
(active_since..active_till).cover? Time.current
|
63
84
|
end
|
64
85
|
|
86
|
+
def data_collection_label
|
87
|
+
" [NO-DATA-COL]" if maintenance_type.to_i == 1
|
88
|
+
end
|
89
|
+
|
65
90
|
def label
|
66
|
-
format_label "|%sid
|
91
|
+
format_label "|%sid|%data-collection-label %start -> %end >> %name %targets"
|
67
92
|
end
|
68
93
|
|
69
94
|
def format_label fmt
|
@@ -72,7 +97,8 @@ module Zabbirc
|
|
72
97
|
gsub("%name", "#{name}").
|
73
98
|
gsub("%id", "#{id}").
|
74
99
|
gsub("%sid", "#{shorten_id}").
|
75
|
-
gsub("%targets", "#{target_labels}")
|
100
|
+
gsub("%targets", "#{target_labels}").
|
101
|
+
gsub("%data-collection-label", "#{data_collection_label}")
|
76
102
|
end
|
77
103
|
|
78
104
|
def target_labels
|
data/spec/event_command_spec.rb
CHANGED
@@ -70,8 +70,8 @@ describe Zabbirc::Irc::HostCommand do
|
|
70
70
|
end
|
71
71
|
before do
|
72
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)
|
73
|
+
allow(event_high_good_host).to receive(:any_host_matches?).with(/#{host}/i).and_return(true)
|
74
|
+
allow(event_high_bad_host).to receive(:any_host_matches?).with(/#{host}/i).and_return(false)
|
75
75
|
end
|
76
76
|
|
77
77
|
it "should report latest events" do
|
@@ -76,6 +76,15 @@ describe Zabbirc::Irc::MaintenanceCommand do
|
|
76
76
|
expect(mock_message).to receive(:reply)
|
77
77
|
maintenance_command.run
|
78
78
|
end
|
79
|
+
|
80
|
+
context "without data collection" do
|
81
|
+
let(:cmd) { "no-data '#{host1.name},#{host2.name}' #{duration} #{reason}" }
|
82
|
+
it "should create maintenance" do
|
83
|
+
expect(Zabbirc::Zabbix::Maintenance).to receive(:create).with(duration: duration_int, host_ids: hosts.collect(&:id), name: reason, without_data_collection: true)
|
84
|
+
expect(mock_message).to receive(:reply)
|
85
|
+
maintenance_command.run
|
86
|
+
end
|
87
|
+
end
|
79
88
|
end
|
80
89
|
|
81
90
|
context "host groups" do
|
@@ -85,6 +94,15 @@ describe Zabbirc::Irc::MaintenanceCommand do
|
|
85
94
|
expect(mock_message).to receive(:reply)
|
86
95
|
maintenance_command.run
|
87
96
|
end
|
97
|
+
|
98
|
+
context "without data collection" do
|
99
|
+
let(:cmd) { "no-data hostgroups '#{host_group1.name},#{host_group2.name}' #{duration} #{reason}" }
|
100
|
+
it "should create maintenance" do
|
101
|
+
expect(Zabbirc::Zabbix::Maintenance).to receive(:create).with(duration: duration_int, host_group_ids: host_groups.collect(&:id), name: reason, without_data_collection: true)
|
102
|
+
expect(mock_message).to receive(:reply)
|
103
|
+
maintenance_command.run
|
104
|
+
end
|
105
|
+
end
|
88
106
|
end
|
89
107
|
|
90
108
|
|
data/tmp/playground.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Filip Zachar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|