nagios_mklivestatus 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +21 -0
- data/lib/nagios_mklivestatus.rb +147 -0
- data/lib/nagios_mklivestatus/query.rb +82 -0
- metadata +69 -0
data/README
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
In order to use the gem, you have to install it into your rubygem environment.
|
2
|
+
|
3
|
+
gem install nagios_maklivestatus
|
4
|
+
|
5
|
+
To integrate it in your applications, it can be done just by adding the two following lines :
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'nagios_mklivestatus'
|
9
|
+
|
10
|
+
And to query the mklivestatus :
|
11
|
+
|
12
|
+
mklivesocket = Nagios::MkLiveStatus.new(path)
|
13
|
+
mklivesocket.query(mkliveQuery)
|
14
|
+
|
15
|
+
Please refer to the rdoc in order to have a complete description of each class.
|
16
|
+
|
17
|
+
TODO:
|
18
|
+
- managing exceptions throws by the socket
|
19
|
+
- controlling livestatus query validity
|
20
|
+
- adding OR and AND query filter support
|
21
|
+
- adding support to http
|
@@ -0,0 +1,147 @@
|
|
1
|
+
# This module holds the definition of the class used to query Nagios MkLiveStatus
|
2
|
+
#
|
3
|
+
# Author:: Esco-lan Team (mailto:team@esco-lan.org)
|
4
|
+
# Copyright:: Copyright (c) 2011 GIP RECIA
|
5
|
+
# License:: General Public Licence
|
6
|
+
module Nagios
|
7
|
+
# This class is used to create and querying a socket
|
8
|
+
# It accepts the following socket:
|
9
|
+
# * TCP : new("tcp://<host>:<port>")
|
10
|
+
# * Unix : new("/var/lib/nagios/rw/live")
|
11
|
+
#
|
12
|
+
# Author:: Esco-lan Team (mailto:team@esco-lan.org)
|
13
|
+
# Copyright:: Copyright (c) 2011 GIP RECIA
|
14
|
+
# License:: General Public Licence
|
15
|
+
class MkLiveStatus
|
16
|
+
|
17
|
+
require File.dirname(__FILE__)+'/nagios_mklivestatus/query'
|
18
|
+
require 'socket'
|
19
|
+
|
20
|
+
# path informations of nagios sockets
|
21
|
+
@mk_livestatus_socket_path=""
|
22
|
+
|
23
|
+
# type of the socket
|
24
|
+
@mk_livestatus_socket_type=""
|
25
|
+
|
26
|
+
#debug mode : default to false
|
27
|
+
@debug = false
|
28
|
+
|
29
|
+
# Initialize the nagios mklivestatus socket informations.
|
30
|
+
#
|
31
|
+
# Two type of socket are supported for now:
|
32
|
+
# * TCP : path equal to "tcp://<host>:<port>"
|
33
|
+
# * File : where path is the path to the file
|
34
|
+
#
|
35
|
+
# If debug is set to true it will indicates the information used for the socket
|
36
|
+
# and the query and result of each queries call
|
37
|
+
def initialize(path,debug=false)
|
38
|
+
|
39
|
+
#set debug mode
|
40
|
+
if debug
|
41
|
+
@debug = true
|
42
|
+
end
|
43
|
+
|
44
|
+
puts ""
|
45
|
+
#check socket type
|
46
|
+
# if the path start with tcp:// => tcp socket
|
47
|
+
if path.strip.start_with?("tcp://")
|
48
|
+
tmp = path[6,path.length]
|
49
|
+
table = tmp.partition(":")
|
50
|
+
@mk_livestatus_socket_type = "tcp"
|
51
|
+
@mk_livestatus_socket_path = Hash.new
|
52
|
+
@mk_livestatus_socket_path[:ip] = table[0]
|
53
|
+
@mk_livestatus_socket_path[:port] = table[2]
|
54
|
+
|
55
|
+
puts "type : "+@mk_livestatus_socket_type if @debug
|
56
|
+
puts "ip : "+@mk_livestatus_socket_path[:ip] if @debug
|
57
|
+
puts "port : "+@mk_livestatus_socket_path[:port] if @debug
|
58
|
+
# default socket type is set to file
|
59
|
+
elsif File.exists? path
|
60
|
+
@mk_livestatus_socket_path = path
|
61
|
+
@mk_livestatus_socket_type = "file"
|
62
|
+
|
63
|
+
puts "type : "+@mk_livestatus_socket_type if @debug
|
64
|
+
puts "file : "+@mk_livestatus_socket_path if @debug
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# The method opens the socket and send the query then return the response of nagios
|
69
|
+
#
|
70
|
+
# The query parameter must be set and be a string reprensting the MKLiveStatus Query :
|
71
|
+
# GET hosts
|
72
|
+
# or
|
73
|
+
# GET hosts
|
74
|
+
# Columns: host_name
|
75
|
+
# Filter: host_name ~ test
|
76
|
+
# ....
|
77
|
+
#
|
78
|
+
def query(query=nil)
|
79
|
+
|
80
|
+
socket=nil
|
81
|
+
|
82
|
+
puts ""
|
83
|
+
#open socket depending on the type of connection
|
84
|
+
case @mk_livestatus_socket_type
|
85
|
+
when "tcp"
|
86
|
+
socket=TCPSocket.open(@mk_livestatus_socket_path[:ip], @mk_livestatus_socket_path[:port])
|
87
|
+
puts "Ouverture du socket TCP : "+@mk_livestatus_socket_path[:ip]+" "+@mk_livestatus_socket_path[:port] if @debug
|
88
|
+
when "file"
|
89
|
+
socket=UNIXSocket.open(@mk_livestatus_socket_path)
|
90
|
+
puts "Ouverture du socket Unix : "+@mk_livestatus_socket_path if @debug
|
91
|
+
end
|
92
|
+
|
93
|
+
#if socket is generated and query exists
|
94
|
+
if socket != nil and query != nil and query.to_s.upcase.start_with?("GET ")
|
95
|
+
|
96
|
+
strQuery = query.to_s
|
97
|
+
#the endline must be empty
|
98
|
+
if not strQuery.end_with?("\n")
|
99
|
+
strQuery << "\n"
|
100
|
+
end
|
101
|
+
|
102
|
+
puts ""
|
103
|
+
puts "---" if @debug
|
104
|
+
puts strQuery if @debug
|
105
|
+
puts "---" if @debug
|
106
|
+
|
107
|
+
# set response header to 16
|
108
|
+
strQuery << "ResponseHeader: fixed16\n"
|
109
|
+
|
110
|
+
# query the socket
|
111
|
+
socket.puts strQuery
|
112
|
+
|
113
|
+
# close the socket
|
114
|
+
socket.shutdown(Socket::SHUT_WR)
|
115
|
+
|
116
|
+
# check data reception
|
117
|
+
recieving = socket.recv(16)
|
118
|
+
check_receiving_error recieving
|
119
|
+
|
120
|
+
# get all the line of the socket
|
121
|
+
response = ""
|
122
|
+
while(line = socket.gets) do
|
123
|
+
response << line
|
124
|
+
end
|
125
|
+
|
126
|
+
puts response if @debug
|
127
|
+
|
128
|
+
return response
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
# Check if the recived datas have a status code greater than 200.
|
136
|
+
# If the case is matched then we raise an exception in order to stop the process
|
137
|
+
#
|
138
|
+
# datas : from socket.recv
|
139
|
+
def check_receiving_error( datas )
|
140
|
+
error_code = datas.split(" ")[0].to_i
|
141
|
+
if error_code > 200
|
142
|
+
raise Exception, "Nagios::MkLiveStatus.query return an error_code #{error_code}"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# This class is used to create a nagios mklivestatus query
|
2
|
+
# * get : is the object we are looking for
|
3
|
+
# * columns : are the fields we need from the object
|
4
|
+
# * filters : are the filters applied to the query
|
5
|
+
#
|
6
|
+
# By default the query get all the columns of the object without filter.
|
7
|
+
# If you add any columns or filter they will take place in the query
|
8
|
+
#
|
9
|
+
# Author:: Esco-lan Team (mailto:team@esco-lan.org)
|
10
|
+
# Copyright:: Copyright (c) 2011 GIP RECIA
|
11
|
+
# License:: General Public Licence
|
12
|
+
class Nagios::MkLiveStatus::Query
|
13
|
+
|
14
|
+
@get=nil
|
15
|
+
@columns=nil
|
16
|
+
@filters=nil
|
17
|
+
|
18
|
+
# Constructor of the Query instance
|
19
|
+
# base is by default nil but if its filled the base if associated to the GET of the query
|
20
|
+
def initialize(base=nil)
|
21
|
+
get(base)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Get method is used to set the object to search for in nagios mklivestatus
|
25
|
+
def get(get)
|
26
|
+
if get != nil and not get.empty?
|
27
|
+
@get = get
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Add a field to get in the query for the GET object
|
32
|
+
def addColumn(name)
|
33
|
+
if not name == nil or name.empty?
|
34
|
+
if @columns == nil
|
35
|
+
@columns=Array.new
|
36
|
+
end
|
37
|
+
|
38
|
+
@columns.push(name)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Add a filter to the query
|
43
|
+
def addFilter(expression)
|
44
|
+
if not expression == nil or expression.empty?
|
45
|
+
if @filters == nil
|
46
|
+
@filters=Array.new
|
47
|
+
end
|
48
|
+
|
49
|
+
@filters.push(expression)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
#short cut to the method to_socket
|
54
|
+
def to_s
|
55
|
+
return to_socket
|
56
|
+
end
|
57
|
+
|
58
|
+
# method used to generate the query from the field
|
59
|
+
# if get is not set then the method return an empty string
|
60
|
+
def to_socket
|
61
|
+
query = String.new
|
62
|
+
if @get != nil
|
63
|
+
query << "GET "+@get+"\n"
|
64
|
+
|
65
|
+
if @columns != nil and @columns.length > 0
|
66
|
+
query << "Columns:"
|
67
|
+
@columns.each do |column|
|
68
|
+
query << " "+column
|
69
|
+
end
|
70
|
+
query << "\n"
|
71
|
+
end
|
72
|
+
|
73
|
+
if @filters != nil and @filters.length > 0
|
74
|
+
@filters.each do |filter|
|
75
|
+
query << "Filter: "+filter+"\n"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
return query
|
81
|
+
end
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nagios_mklivestatus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Esco-lan Team
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-11-02 00:00:00 +00:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Querying Nagios MKLiveStatus through TCP or Unix sockets
|
23
|
+
email: team@esco-lan.org
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README
|
30
|
+
files:
|
31
|
+
- lib/nagios_mklivestatus.rb
|
32
|
+
- lib/nagios_mklivestatus/query.rb
|
33
|
+
- README
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://blogs.cocoondev.org/crafterm/
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
hash: 3
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.3.7
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Querying Nagios MKLiveStatus through sockets TCP or Unix
|
68
|
+
test_files: []
|
69
|
+
|