Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit ef6d340d3d945c2f88f03afa5346c6c3d3ca162b @sosedoff sosedoff committed Feb 19, 2011
Showing with 82 additions and 0 deletions.
  1. +6 −0 lib/munin-ruby.rb
  2. +3 −0 lib/munin-ruby/munin-ruby.rb
  3. +54 −0 lib/munin-ruby/node.rb
  4. +16 −0 lib/munin-ruby/stat.rb
  5. +3 −0 lib/munin-ruby/version.rb
@@ -0,0 +1,6 @@
+require 'socket'
+
+require 'munin-ruby/munin-ruby'
+require 'munin-ruby/version'
+require 'munin-ruby/stat'
+require 'munin-ruby/node'
@@ -0,0 +1,3 @@
+module Munin
+ class SessionError < Exception ; end
+end
@@ -0,0 +1,54 @@
+module Munin
+ class Node
+ attr_reader :host, :port
+ attr_reader :stats, :version, :services
+
+ def initialize(host, port=4949)
+ @host = host
+ @port = port
+ @stats = {}
+ @services = []
+ @version = ''
+ run
+ end
+
+ def service(s)
+ @stats[s]
+ end
+
+ private
+
+ def run
+ begin
+ @socket = TCPSocket.new(@host, @port)
+ @socket.sync = true ; @socket.gets
+ fetch_version
+ fetch_services
+ @socket.close
+ rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError => ex
+ raise Munin::SessionError, ex.message
+ end
+ end
+
+ def fetch_version
+ @socket.puts("version")
+ @version = @socket.readline.strip.split(' ').last
+ end
+
+ def fetch_services
+ @socket.puts("list")
+ services = @socket.readline.split(' ').map { |s| s.strip }
+ services.each { |s| @services << s ; @stats[s] = fetch(s) }
+ end
+
+ def fetch(service)
+ @socket.puts("fetch #{service}")
+ content = []
+ while(str = @socket.readline) do
+ break if str.strip == '.'
+ content << str.strip.split(' ')
+ end
+ Stat.new(service, content)
+ end
+ end
+end
@@ -0,0 +1,16 @@
+module Munin
+ class Stat
+ attr_reader :name, :params
+
+ def initialize(name, rows=[])
+ @name = name
+ @params = {}
+ unless rows.empty?
+ rows.each do |r|
+ name = r.first.scan(/^([a-z\d\_\-]{1,}).value/i).to_s
+ @params[name] = r.last
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module Munin
+ VERSION = '0.1.0'
+end

0 comments on commit ef6d340

Please sign in to comment.