nagiosharder 0.5.0.rc1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,10 +17,24 @@ class NagiosHarder
17
17
  def run
18
18
  return_value = case command
19
19
  when 'status'
20
+ status_checks = []
21
+ hostnames_and_check_names = [param,*the_rest].compact
22
+
23
+ # for each hostname or checkname we got
24
+ hostnames_and_check_names.each do |check|
25
+ host,service = check.split("/")
26
+ check_info = client.host_status(host)
27
+
28
+ # If we were asked about a specific service, get rid of the ones
29
+ # that they didn't ask for
30
+ check_info.reject! { |name,s| service and service != name }
31
+
32
+ # accumulate our results
33
+ status_checks += check_info.map { |name,service| service }
34
+ end
35
+
20
36
  service_table do
21
- client.host_status(host).select do |name, s|
22
- service.nil? || service == name
23
- end.map { |name, s| s }
37
+ status_checks
24
38
  end
25
39
  true
26
40
  when /^(ack|acknowledged)$/
@@ -42,10 +56,15 @@ class NagiosHarder
42
56
  client.schedule_host_check(host)
43
57
  end
44
58
  when 'downtime'
45
- if service
46
- client.schedule_service_downtime(host, service, :type => :fixed, :start_time => Time.now, :end_time => Time.now + the_rest.first.to_i.minutes)
59
+ if the_rest == []
60
+ puts "Downtime requires duration in minutes."
61
+ false
47
62
  else
48
- client.schedule_host_downtime(host, :type => :fixed, :start_time => Time.now, :end_time => Time.now + the_rest.first.to_i.minutes)
63
+ if service
64
+ client.schedule_service_downtime(host, service, :type => :fixed, :start_time => Time.now, :end_time => Time.now + the_rest.first.to_i.minutes)
65
+ else
66
+ client.schedule_host_downtime(host, :type => :fixed, :start_time => Time.now, :end_time => Time.now + the_rest.first.to_i.minutes)
67
+ end
49
68
  end
50
69
  when 'problems'
51
70
  service_table do
@@ -96,6 +115,16 @@ class NagiosHarder
96
115
  client.service_status(params)
97
116
  end
98
117
  true
118
+ when /^servicegroups/
119
+ groups_table do
120
+ client.servicegroups_summary()
121
+ end
122
+ true
123
+ when /^hostgroups/
124
+ groups_table do
125
+ client.hostgroups_summary()
126
+ end
127
+ true
99
128
  when /^muted/
100
129
  service_table do
101
130
  params = {
@@ -107,6 +136,17 @@ class NagiosHarder
107
136
  client.service_status(params)
108
137
  end
109
138
  true
139
+ when /^scheduled_downtime/
140
+ service_table do
141
+ params = {
142
+ :service_props => [
143
+ :scheduled_downtime,
144
+ ]
145
+ }
146
+ params[:group] = param
147
+ client.service_status(params)
148
+ end
149
+ true
110
150
  when /^(acked|acknowledged)/
111
151
  service_table do
112
152
  params = {
@@ -130,7 +170,7 @@ class NagiosHarder
130
170
  if return_value
131
171
  0
132
172
  else
133
- puts "Sorry, bro, nagios didn't like that."
173
+ puts "Sorry yo, nagios didn't like that."
134
174
  1
135
175
  end
136
176
  end
@@ -193,6 +233,30 @@ class NagiosHarder
193
233
  options
194
234
  end
195
235
 
236
+ def groups_table
237
+ table = Terminal::Table.new(:headings => ['Group', 'Host Up', 'Host Down', 'Service Ok', 'Service Warning', 'Service Critical', 'Service Unknown']) do |t|
238
+ groups = yield
239
+ groups.each do |name, group|
240
+ t << group_row(group)
241
+ end
242
+ t
243
+ end
244
+ table.align_column(1, :right)
245
+ puts table
246
+ end
247
+
248
+ def group_row(group)
249
+ [
250
+ group['group'],
251
+ group['host_status_counts']['up'],
252
+ group['host_status_counts']['down'],
253
+ group['service_status_counts']['ok'],
254
+ group['service_status_counts']['warning'],
255
+ group['service_status_counts']['critical'],
256
+ group['service_status_counts']['unknown']
257
+ ]
258
+ end
259
+
196
260
  def service_table
197
261
  table = Terminal::Table.new(:headings => ['Service', 'Status', 'Details']) do |t|
198
262
  services = yield
@@ -208,6 +272,7 @@ class NagiosHarder
208
272
  def service_row(service)
209
273
  service['status'] << "/ACK" if service['acknowledged']
210
274
  service['status'] << "/MUTE" if service['notifications_disabled']
275
+ service['status'] << "/DOWNTIME" if service['downtime']
211
276
  service['status'] << "/COMMENT" if service['comments_url']
212
277
  [
213
278
  service['host']+"/"+service["service"],
@@ -275,6 +340,9 @@ COMMANDS:
275
340
  nagiosharder triage
276
341
  nagiosharder unhandled
277
342
  nagiosharder unhandled http-services
343
+
344
+ nagiosharder hostgroups
345
+ nagiosharder servicegroups
278
346
  HELP
279
347
  end
280
348
  end
@@ -0,0 +1,173 @@
1
+ class NagiosHarder
2
+ COMMANDS = {
3
+ :none => 0,
4
+ :add_host_comment => 1,
5
+ :del_host_comment => 2,
6
+ :add_service_comment => 3,
7
+ :del_service_comment => 4,
8
+ :enable_service_check => 5,
9
+ :disable_service_check => 6,
10
+ :schedule_service_check => 7,
11
+ :delay_service_notification => 9,
12
+ :delay_host_notification => 10,
13
+ :disable_notifications => 11,
14
+ :enable_notifications => 12,
15
+ :restart_process => 13,
16
+ :shutdown_process => 14,
17
+ :enable_host_service_checks => 15,
18
+ :disable_host_service_checks => 16,
19
+ :schedule_host_service_checks => 17,
20
+ :delay_host_service_notifications => 19,
21
+ :del_all_host_comments => 20,
22
+ :del_all_service_comments => 21,
23
+ :enable_service_notifications => 22,
24
+ :disable_service_notifications => 23,
25
+ :enable_host_notifications => 24,
26
+ :disable_host_notifications => 25,
27
+ :enable_all_notifications_beyond_host => 26,
28
+ :disable_all_notifications_beyond_host => 27,
29
+ :enable_host_service_notifications => 28,
30
+ :disable_host_service_notifications => 29,
31
+ :process_service_check_result => 30,
32
+ :save_state_information => 31,
33
+ :read_state_information => 32,
34
+ :acknowledge_host_problem => 33,
35
+ :acknowledge_service_problem => 34,
36
+ :start_executing_service_checks => 35,
37
+ :stop_executing_service_checks => 36,
38
+ :start_accepting_passive_service_checks => 37,
39
+ :stop_accepting_passive_service_checks => 38,
40
+ :enable_passive_service_checks => 39,
41
+ :disable_passive_service_checks => 40,
42
+ :enable_event_handlers => 41,
43
+ :disable_event_handlers => 42,
44
+ :enable_host_event_handler => 43,
45
+ :disable_host_event_handler => 44,
46
+ :enable_service_event_handler => 45,
47
+ :disable_service_event_handler => 46,
48
+ :enable_host_check => 47,
49
+ :disable_host_check => 48,
50
+ :start_obsessing_over_service_checks => 49,
51
+ :stop_obsessing_over_service_checks => 50,
52
+ :remove_host_acknowledgement => 51,
53
+ :remove_service_acknowledgement => 52,
54
+ :schedule_forced_host_service_checks => 53,
55
+ :schedule_forced_service_check => 54,
56
+ :schedule_host_downtime => 55,
57
+ :schedule_service_downtime => 56,
58
+ :enable_host_flap_detection => 57,
59
+ :disable_host_flap_detection => 58,
60
+ :enable_service_flap_detection => 59,
61
+ :disable_service_flap_detection => 60,
62
+ :enable_flap_detection => 61,
63
+ :disable_flap_detection => 62,
64
+ :enable_hostgroup_service_notifications => 63,
65
+ :disable_hostgroup_service_notifications => 64,
66
+ :enable_hostgroup_host_notifications => 65,
67
+ :disable_hostgroup_host_notifications => 66,
68
+ :enable_hostgroup_service_checks => 67,
69
+ :disable_hostgroup_service_checks => 68,
70
+ :unimplemented_69 => 69,
71
+ :unimplemented_70 => 70,
72
+ :unimplemented_71 => 71,
73
+ :unimplemented_72 => 72,
74
+ :unimplemented_73 => 73,
75
+ :unimplemented_74 => 74,
76
+ :unimplemented_75 => 75,
77
+ :unimplemented_76 => 76,
78
+ :unimplemented_77 => 77,
79
+ :del_host_downtime => 78,
80
+ :del_service_downtime => 79,
81
+ :enable_performance_data => 82,
82
+ :disable_performance_data => 83,
83
+ :schedule_hostgroup_host_downtime => 84,
84
+ :schedule_hostgroup_service_downtime => 85,
85
+ :schedule_host_service_downtime => 86,
86
+ :process_host_check_result => 87,
87
+ :start_executing_host_checks => 88,
88
+ :stop_executing_host_checks => 89,
89
+ :start_accepting_passive_host_checks => 90,
90
+ :stop_accepting_passive_host_checks => 91,
91
+ :enable_passive_host_checks => 92,
92
+ :disable_passive_host_checks => 93,
93
+ :start_obsessing_over_host_checks => 94,
94
+ :stop_obsessing_over_host_checks => 95,
95
+ :schedule_host_check => 96,
96
+ :schedule_forced_host_check => 98,
97
+ :start_obsessing_over_svc => 99,
98
+ :stop_obsessing_over_svc => 100,
99
+ :start_obsessing_over_host => 101,
100
+ :stop_obsessing_over_host => 102,
101
+ :enable_hostgroup_host_checks => 103,
102
+ :disable_hostgroup_host_checks => 104,
103
+ :enable_hostgroup_passive_service_checks => 105,
104
+ :disable_hostgroup_passive_service_checks => 106,
105
+ :enable_hostgroup_passive_host_checks => 107,
106
+ :disable_hostgroup_passive_host_checks => 108,
107
+ :enable_servicegroup_service_notifications => 109,
108
+ :disable_servicegroup_service_notifications => 110,
109
+ :enable_servicegroup_host_notifications => 111,
110
+ :disable_servicegroup_host_notifications => 112,
111
+ :enable_servicegroup_service_checks => 113,
112
+ :disable_servicegroup_service_checks => 114,
113
+ :enable_servicegroup_host_checks => 115,
114
+ :disable_servicegroup_host_checks => 116,
115
+ :enable_servicegroup_passive_service_checks => 117,
116
+ :disable_servicegroup_passive_service_checks => 118,
117
+ :enable_servicegroup_passive_host_checks => 119,
118
+ :disable_servicegroup_passive_host_checks => 120,
119
+ :schedule_servicegroup_host_downtime => 121,
120
+ :schedule_servicegroup_service_downtime => 122,
121
+ :change_global_host_event_handler => 123,
122
+ :change_global_service_event_handler => 124,
123
+ :change_host_event_handler => 125,
124
+ :change_service_event_handler => 126,
125
+ :change_host_check_command => 127,
126
+ :change_service_check_command => 128,
127
+ :change_normal_host_check_interval => 129,
128
+ :change_normal_service_check_interval => 130,
129
+ :change_retry_service_check_interval => 131,
130
+ :change_max_host_check_attempts => 132,
131
+ :change_max_service_check_attempts => 133,
132
+ :schedule_and_propagate_triggered_host_downtime => 134,
133
+ :enable_host_and_child_notifications => 135,
134
+ :disable_host_and_child_notifications => 136,
135
+ :schedule_and_propagate_host_downtime => 137,
136
+ :enable_service_freshness_checks => 138,
137
+ :disable_service_freshness_checks => 139,
138
+ :enable_host_freshness_checks => 140,
139
+ :disable_host_freshness_checks => 141,
140
+ :set_host_notification_number => 142,
141
+ :set_service_notification_number => 143,
142
+ :change_host_check_timeperiod => 144,
143
+ :change_service_check_timeperiod => 145,
144
+ :process_file => 146,
145
+ :change_custom_host_var => 147,
146
+ :change_custom_service_var => 148,
147
+ :change_custom_contact_var => 149,
148
+ :enable_contact_host_notifications => 150,
149
+ :disable_contact_host_notifications => 151,
150
+ :enable_contact_service_notifications => 152,
151
+ :disable_contact_service_notifications => 153,
152
+ :enable_contactgroup_host_notifications => 154,
153
+ :disable_contactgroup_host_notifications => 155,
154
+ :enable_contactgroup_service_notifications => 156,
155
+ :disable_contactgroup_service_notifications => 157,
156
+ :change_retry_host_check_interval => 158,
157
+ :send_custom_host_notification => 159,
158
+ :send_custom_service_notification => 160,
159
+ :change_host_notification_timeperiod => 161,
160
+ :change_service_notification_timeperiod => 162,
161
+ :change_contact_host_notification_timeperiod => 163,
162
+ :change_contact_service_notification_timeperiod => 164,
163
+ :change_host_modattr => 165,
164
+ :change_service_modattr => 166,
165
+ :change_contact_modattr => 167,
166
+ :change_contact_modhattr => 168,
167
+ :change_contact_modsattr => 169,
168
+ :del_downtime_by_host_name => 170,
169
+ :del_downtime_by_hostgroup_name => 171,
170
+ :del_downtime_by_start_time_comment => 172,
171
+ :custom_command => 999
172
+ }.freeze unless defined? COMMANDS
173
+ end
@@ -56,6 +56,13 @@ module Nagiosharder
56
56
  NOTIFICATION_HOST_FLAP=4096
57
57
  NOTIFICATION_SERVICE_CUSTOM=8192
58
58
  NOTIFICATION_HOST_CUSTOM=16384
59
+
60
+
61
+ #/********************** HOST AND SERVICE STATE TYPES **********************/
62
+
63
+ HISTORY_STATE_ALL=0
64
+ HISTORY_STATE_SOFT=1
65
+ HISTORY_STATE_HARD=2
59
66
 
60
67
 
61
68
  #/********************** HOST AND SERVICE ALERT TYPES **********************/
@@ -1,29 +1,31 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
1
+ # encoding: utf-8
2
+ $:.push File.expand_path('../lib', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
- s.name = "nagiosharder"
6
- s.version = "0.5.0.rc1"
5
+ s.name = 'nagiosharder'
6
+ s.version = '0.5.0'
7
+ s.date = Time.now.strftime('%Y-%m-%d')
7
8
  s.platform = Gem::Platform::RUBY
8
- s.authors = ["Josh Nichols", "Jesse Newland"]
9
- s.email = ["josh@technicalpickles.com", "jesse@github.com"]
10
- s.homepage = "http://github.com/railsmachine/nagiosharder"
9
+ s.authors = ['Josh Nichols', 'Jesse Newland', 'Travis Graham']
10
+ s.email = ['josh@technicalpickles.com', 'jesse@github.com', 'travis@railsmachine.com']
11
+ s.homepage = 'http://github.com/railsmachine/nagiosharder'
11
12
  s.summary = %q{Nagios access at your ruby fingertips}
12
13
  s.description = %q{Nagios access at your ruby fingertips}
13
14
 
14
- s.rubyforge_project = "nagiosharder"
15
+ s.rubyforge_project = 'nagiosharder'
15
16
 
16
- s.add_dependency 'rest-client', '~> 1.6.1'
17
+ s.add_dependency 'rest-client'
17
18
  s.add_dependency 'nokogiri'
18
19
  s.add_dependency 'activesupport'
19
20
  s.add_dependency 'i18n'
20
21
  s.add_dependency 'terminal-table'
21
- s.add_dependency 'httparty', '~> 0.8.3'
22
+ s.add_dependency 'httparty'
22
23
  s.add_dependency 'hashie', '~> 1.2.0'
23
- s.add_development_dependency "rspec", ">= 1.2.9"
24
+ s.add_development_dependency 'rspec', '>= 1.2.9'
25
+ s.add_development_dependency 'webmock'
24
26
 
25
27
  s.files = `git ls-files`.split("\n")
26
28
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
27
29
  s.executables = ['nagiosharder']
28
- s.require_paths = ["lib"]
30
+ s.require_paths = ['lib']
29
31
  end
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'NagiosHarder::Site' do
4
+ let(:client) do
5
+ client = NagiosHarder::Site.new(ENV['NAGIOS_URL'] || "",
6
+ ENV['NAGIOS_USER'] || "",
7
+ ENV['NAGIOS_PASSWORD'] || "")
8
+ client.stub(:post)
9
+ client
10
+ end
11
+
12
+ let(:successful_response) do
13
+ Class.new() do
14
+ def code
15
+ 200
16
+ end
17
+
18
+ def body
19
+ "successful"
20
+ end
21
+ end.new
22
+ end
23
+
24
+ it 'should initialize' do
25
+ client
26
+ end
27
+
28
+ it 'should call post in post_command' do
29
+ client.should_receive(:post) do |url, params|
30
+ params[:body][:foo].should == :bar
31
+ # Return an instance that makes the rest of the method work
32
+ successful_response
33
+ end
34
+
35
+ client.post_command({:foo => :bar}).should == true
36
+ end
37
+
38
+ it 'should let you acknowledge a service' do
39
+ client.should_receive(:post_command) do |params|
40
+ params[:host].should == 'example.com'
41
+ params[:service].should == 'http'
42
+ params[:com_data].should == 'Looking'
43
+ params[:com_author].should == client.user
44
+ params[:cmd_typ].should == 34
45
+ end
46
+
47
+ client.acknowledge_service('example.com', 'http', 'Looking')
48
+ end
49
+
50
+ it 'should let you acknowledge a host' do
51
+ client.should_receive(:post_command) do |params|
52
+ params[:host].should == 'example.com'
53
+ params[:com_data].should == 'Looking'
54
+ params[:com_author].should == client.user
55
+ params[:cmd_typ].should == 33
56
+ end
57
+
58
+ client.acknowledge_host('example.com', 'Looking')
59
+ end
60
+
61
+ it 'should let you unacknowledge a service' do
62
+ client.should_receive(:post_command) do |params|
63
+ params[:host].should == 'example.com'
64
+ params[:service].should == 'http'
65
+ params[:cmd_typ].should == 52
66
+ end
67
+
68
+ client.unacknowledge_service('example.com', 'http')
69
+ end
70
+
71
+ it 'should let you schedule service downtime' do
72
+ client.should_receive(:post_command) do |params|
73
+ params[:host].should == 'example.com'
74
+ params[:service].should == 'http'
75
+ params[:cmd_typ].should == 56
76
+ params[:com_author].should_not be_blank
77
+ params[:com_data].should_not be_blank
78
+ end
79
+
80
+ client.schedule_service_downtime('example.com', 'http')
81
+ end
82
+
83
+ it 'should let you schedule host downtime' do
84
+ client.should_receive(:post_command) do |params|
85
+ params[:host].should == 'example.com'
86
+ params[:cmd_typ].should == 55
87
+ params[:com_author].should_not be_blank
88
+ params[:com_data].should_not be_blank
89
+ end
90
+
91
+ client.schedule_host_downtime('example.com')
92
+ end
93
+
94
+ it 'should let you cancel downtime' do
95
+ client.should_receive(:post_command) do |params|
96
+ params[:cmd_typ].should == 78
97
+ params[:down_id].should == 0
98
+ end
99
+
100
+ client.cancel_downtime(0)
101
+ end
102
+
103
+ it 'should let you schedule a host check' do
104
+ client.should_receive(:post_command) do |params|
105
+ params[:host].should == 'example.com'
106
+ params[:cmd_typ].should == 96
107
+ params[:force_check].should == true
108
+ end
109
+
110
+ client.schedule_host_check('example.com')
111
+ end
112
+
113
+ it 'should let you schedule a service check' do
114
+ client.should_receive(:post_command) do |params|
115
+ params[:host].should == 'example.com'
116
+ params[:service].should == 'http'
117
+ params[:cmd_typ].should == 7
118
+ params[:force_check].should == true
119
+ end
120
+
121
+ client.schedule_service_check('example.com','http')
122
+ end
123
+
124
+ it 'should let you disable service notifications' do
125
+ client.should_receive(:post_command) do |params|
126
+ params[:host].should == 'example.com'
127
+ params[:service].should == 'http'
128
+ params[:cmd_typ].should == 23
129
+ end
130
+ client.disable_service_notifications('example.com', 'http')
131
+ end
132
+
133
+ it 'should let you enable service notifications' do
134
+ client.should_receive(:post_command) do |params|
135
+ params[:host].should == 'example.com'
136
+ params[:service].should == 'http'
137
+ params[:cmd_typ].should == 22
138
+ end
139
+ client.enable_service_notifications('example.com', 'http')
140
+ end
141
+ end