forked from ripienaar/ruby-nagios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnagsrv.rb
executable file
·160 lines (144 loc) · 4.81 KB
/
nagsrv.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/ruby
# A tool to do mass operations on nagios services. It is intended to be run on
# the server that hosts nagios and it needs read access to the status.log file
# typically found in the var dir.
#
# Command options are broken up into several types:
#
# == General
# --statusfile
# Where to find the status file
#
# == Output Selectors
# --list-hosts
# List hostnames that match certain criteria
#
# --list-service
# List services that match certain criteria
#
# == Selectors
# --with-service
# Pass a specific service name or a regex in the form
# /pattern/ if you pass a regex you can only pass this
# option once, if you pass specific services you can
# use this option many times the services will be searches
# in an OR fasion
#
# --for-host
# Restrict the selection of services to a specific host or
# regex match of hosts, same regex rules as for --with-service
#
# --notify-enabled
# List only services with notifications enabled, in this mode
# the output will be in the form host:service
#
# == Actions
# --enable-notify / --disable-notify
# Enable or Disable notifications for selected services
#
# --enable-checks / --disable-checks
# Enable of Disable checks for selected services
#
# --force-check
# Force checks for selected services
#
# --acknowledge
# Ackknowledge services without sending notifies
#
# Released under the terms of the Apache version 2
# license
#
# Please open an issue at ruby-nagios.googlecode.com
# with any queries
require 'nagios/status.rb'
require 'getoptlong'
def showhelp
begin
require 'rdoc/ri/ri_paths'
require 'rdoc/usage'
RDoc::usage
rescue LoadError
puts ("Install RDoc::usage or view the comments in the top of the script to get detailed help")
end
end
opts = GetoptLong.new(
[ '--statusfile', '-s', GetoptLong::REQUIRED_ARGUMENT],
[ '--list-hosts', GetoptLong::NO_ARGUMENT],
[ '--list-services', GetoptLong::NO_ARGUMENT],
[ '--notify-enabled', GetoptLong::NO_ARGUMENT],
[ '--notify-disabled', GetoptLong::NO_ARGUMENT],
[ '--for-host', GetoptLong::REQUIRED_ARGUMENT],
[ '--with-service', GetoptLong::REQUIRED_ARGUMENT],
[ '--service-status', GetoptLong::REQUIRED_ARGUMENT],
[ '--enable-notify', GetoptLong::NO_ARGUMENT],
[ '--disable-notify', GetoptLong::NO_ARGUMENT],
[ '--enable-checks', GetoptLong::NO_ARGUMENT],
[ '--disable-checks', GetoptLong::NO_ARGUMENT],
[ '--force-check', GetoptLong::NO_ARGUMENT],
[ '--acknowledge', GetoptLong::NO_ARGUMENT]
)
statusfile = "status.log"
listhosts = false
withservice = []
listservices = false
servicestatus = false
forhost = []
notify = nil
action = nil
options = nil
begin
opts.each do |opt, arg|
case opt
when "--statusfile"
statusfile = arg
when "--list-hosts"
listhosts = true
when "--list-services"
listservices = true
when "--service-status"
servicestatus = true
when "--with-service"
withservice << arg
when "--for-host"
forhost << arg
when "--enable-notify"
action = "[${tstamp}] ENABLE_SVC_NOTIFICATIONS;${host};${service}"
when "--disable-notify"
action = "[${tstamp}] DISABLE_SVC_NOTIFICATIONS;${host};${service}"
when "--force-check"
action = "[${tstamp}] SCHEDULE_FORCED_SVC_CHECK;${host};${service};${tstamp}"
when "--enable-checks"
action = "[${tstamp}] ENABLE_SVC_CHECK;${host};${service};${tstamp}"
when "--disable-checks"
action = "[${tstamp}] DISABLE_SVC_CHECK;${host};${service};${tstamp}"
when "--acknowledge"
action = "[${tstamp}] ACKNOWLEDGE_SVC_PROBLEM;${host};${service};1;0;1;#{ENV['USER']};Acknowledged from CLI"
when "--notify-enabled"
notify = 1
when "--notify-disabled"
notify = 0
end
end
rescue
showhelp
exit 1
end
nagios = Nagios::Status.new
nagios.parsestatus(statusfile)
# We want hosts so abuse the action field to print just the hostname
# and select all hosts unless other action/forhost was desigred then
# this really is just a noop and it reverts to noral behaviour
if listhosts
action = "${host}" if action == nil
forhost = "/." if forhost.size == 0
end
options = {:forhost => forhost, :notifyenabled => notify, :action => action, :withservice => withservice}
services = nagios.find_services(options)
puts services.join("\n")
require 'pp'
if servicestatus
puts withservice
puts nagios.status["hosts"][forhost[0]]["servicestatus"][withservice[0]]["current_state"]
puts nagios.status["hosts"][forhost[0]]["servicestatus"][withservice[0]]["plugin_output"]
end
# vi:tabstop=4:expandtab:ai