Class: Nagios::MkLiveStatus
- Inherits:
-
Object
- Object
- Nagios::MkLiveStatus
- Defined in:
- lib/nagios_mklivestatus.rb
Overview
This class is used to create and querying a socket It accepts the following socket:
-
TCP : new(“tcp://<host>:<port>”)
-
Unix : new(“/var/lib/nagios/rw/live”)
- Author
-
Esco-lan Team ([email protected])
- Copyright
-
Copyright © 2011 GIP RECIA
- License
-
General Public Licence
Defined Under Namespace
Classes: Query
Instance Method Summary collapse
-
#initialize(path, debug = false) ⇒ MkLiveStatus
constructor
Initialize the nagios mklivestatus socket informations.
-
#query(query = nil) ⇒ Object
The method opens the socket and send the query then return the response of nagios.
Constructor Details
#initialize(path, debug = false) ⇒ MkLiveStatus
Initialize the nagios mklivestatus socket informations.
Two type of socket are supported for now:
-
TCP : path equal to “tcp://<host>:<port>”
-
File : where path is the path to the file
If debug is set to true it will indicates the information used for the socket and the query and result of each queries call
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 |
# File 'lib/nagios_mklivestatus.rb', line 37 def initialize(path,debug=false) #set debug mode if debug @debug = true end puts "" #check socket type # if the path start with tcp:// => tcp socket if path.strip.start_with?("tcp://") tmp = path[6,path.length] table = tmp.partition(":") @mk_livestatus_socket_type = "tcp" @mk_livestatus_socket_path = Hash.new @mk_livestatus_socket_path[:ip] = table[0] @mk_livestatus_socket_path[:port] = table[2] puts "type : "+@mk_livestatus_socket_type if @debug puts "ip : "+@mk_livestatus_socket_path[:ip] if @debug puts "port : "+@mk_livestatus_socket_path[:port] if @debug # default socket type is set to file elsif File.exists? path @mk_livestatus_socket_path = path @mk_livestatus_socket_type = "file" puts "type : "+@mk_livestatus_socket_type if @debug puts "file : "+@mk_livestatus_socket_path if @debug end end |
Instance Method Details
#query(query = nil) ⇒ Object
The method opens the socket and send the query then return the response of nagios
The query parameter must be set and be a string reprensting the MKLiveStatus Query :
GET hosts
or
GET hosts
Columns: host_name
Filter: host_name ~ test
....
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 |
# File 'lib/nagios_mklivestatus.rb', line 78 def query(query=nil) socket=nil puts "" #open socket depending on the type of connection case @mk_livestatus_socket_type when "tcp" socket=TCPSocket.open(@mk_livestatus_socket_path[:ip], @mk_livestatus_socket_path[:port]) puts "Ouverture du socket TCP : "+@mk_livestatus_socket_path[:ip]+" "+@mk_livestatus_socket_path[:port] if @debug when "file" socket=UNIXSocket.open(@mk_livestatus_socket_path) puts "Ouverture du socket Unix : "+@mk_livestatus_socket_path if @debug end #if socket is generated and query exists if socket != nil and query != nil and query.to_s.upcase.start_with?("GET ") strQuery = query.to_s #the endline must be empty if not strQuery.end_with?("\n") strQuery << "\n" end puts "" puts "---" if @debug puts strQuery if @debug puts "---" if @debug # set response header to 16 strQuery << "ResponseHeader: fixed16\n" # query the socket socket.puts strQuery # close the socket socket.shutdown(Socket::SHUT_WR) # check data reception recieving = socket.recv(16) check_receiving_error recieving # get all the line of the socket response = "" while(line = socket.gets) do response << line end puts response if @debug return response end end |