Class: CirconusMuninServer

Inherits:
Object
  • Object
show all
Defined in:
lib/circonus-munin.rb

Instance Method Summary collapse

Constructor Details

#initializeCirconusMuninServer

Returns a new instance of CirconusMuninServer.



54
55
56
57
58
59
# File 'lib/circonus-munin.rb', line 54

def initialize
  fetch_results(true)
  EventMachine::add_periodic_timer(60) do
    @results = fetch_results
  end
end

Instance Method Details

#call(env) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/circonus-munin.rb', line 119

def call(env)
  req = Rack::Request.new(env)
  case req.path
  when /\/immediate$/
    Rack::Response.new(fetch_results(true).to_s, 200).finish
  else 
    Rack::Response.new(@results.to_s, 200).finish
  end
end

#fetch_results(wait = false) ⇒ Object



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
# File 'lib/circonus-munin.rb', line 61

def fetch_results(wait=false)
  fetch_thread = Thread.new do
    puts "Fetching results..."
    munin = CirconusMunin.new

    xml_document = LibXML::XML::Document.new
    resmon_results = LibXML::XML::Node.new("ResmonResults")

    munin.get_response("list")[0].split(" ").each do |service|
      resmon_result = LibXML::XML::Node.new("ResmonResult")
      resmon_result["service"] = service 

      module_name = nil
      munin.get_response("config #{service}").each do |configline|
        if configline =~ /graph_category (.+)/
          module_name = $1
        end
      end
      resmon_result["module"] = module_name ? module_name : "other" 

      begin_time = Time.now
      munin.get_response("fetch #{service}").each do |line|
        line =~ /^(.+)\.value\s+(.+)$/
        field = $1
        value = $2
        metric = LibXML::XML::Node.new("metric")
        metric["name"] = field
        metric.content = value.to_s
        resmon_result << metric
      end
      end_time = Time.now
      runtime = end_time - begin_time

      last_runtime_seconds = LibXML::XML::Node.new("last_runtime_seconds")
      last_runtime_seconds.content = runtime.to_s
      resmon_result << last_runtime_seconds

      last_update = LibXML::XML::Node.new("last_update")
      last_update.content = Time.now.to_i.to_s
      resmon_result << last_update

      state = LibXML::XML::Node.new("state")
      state.content = "OK"
      resmon_result << state

      resmon_results << resmon_result
    end
  
    xml_document.root = resmon_results

    munin.close
   
    @results = xml_document
  end
  fetch_thread.join if wait
  @results
end