zabbirc 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc13d87c01a24f572c2335c3b833a67bd69ac32e
4
- data.tar.gz: f52a51ece4b4cadbccf80667bc0318ef49200624
3
+ metadata.gz: 1d4081d968390062febd6847ba0792fd884837ef
4
+ data.tar.gz: a06fc08a9ce62d8732a7452d881446abf71b3452
5
5
  SHA512:
6
- metadata.gz: 6dce36ab81b1e7ec021e638a3df1a005ecd028031bb882823cfc86a50f10f905156c191de858cb121ab3ae69741313d3be0187a2a16e77b352cfe97a91193d1b
7
- data.tar.gz: 0bd55518c88310f0d03ba5bff6308a19bd8a657690dfcc324a673850e898af0efad0e99a8956625fea044888cab047a0f0b8bc53cab1f3e1215d9cec46670df8
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
 
@@ -1,4 +1,5 @@
1
1
  require 'active_support/configurable'
2
+ require 'active_support/all'
2
3
 
3
4
  require 'zabbirc/priority'
4
5
 
@@ -129,6 +129,7 @@ module Zabbirc
129
129
  end
130
130
 
131
131
  def parse_priority priority
132
+ priority = priority.to_i if priority =~ /^\d+$/
132
133
  Priority.new(priority)
133
134
  rescue ArgumentError => e
134
135
  reply("#{e}")
@@ -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
- hostgroups_flag = @args.shift
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
@@ -1,3 +1,3 @@
1
1
  module Zabbirc
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -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| %start -> %end >> %name %targets"
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
@@ -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
@@ -29,4 +29,9 @@ def get_event short_id
29
29
  selectRelatedObject: :extend,
30
30
  selectHosts: :extend
31
31
  )
32
- end
32
+ end
33
+
34
+
35
+ include Zabbirc::Zabbix
36
+ es = Event.get(selectHosts: :extend, sortfield: :clock);
37
+ Event.preload_host_groups es;
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.0
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-13 00:00:00.000000000 Z
11
+ date: 2015-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport