Class: Munin2Graphite::Scheduler
- Inherits:
-
Object
- Object
- Munin2Graphite::Scheduler
- Defined in:
- lib/munin2graphite/scheduler.rb
Overview
This class holds the main scheduler of the system, it will perform the applicacion loops
Instance Attribute Summary collapse
-
#scheduler ⇒ Object
Returns the value of attribute scheduler.
Instance Method Summary collapse
- #carbon=(socket) ⇒ Object
- #category_from_config(config) ⇒ Object
-
#initialize(config) ⇒ Scheduler
constructor
A new instance of Scheduler.
- #metric_loop(worker) ⇒ Object
- #munin_config(reload = false) ⇒ Object
- #obtain_graphs ⇒ Object
-
#obtain_metrics(worker = "global") ⇒ Object
This is the loop of the metrics scheduling.
- #start ⇒ Object
- #start1r_graphs ⇒ Object
- #start1r_metrics ⇒ Object
- #workers ⇒ Object
Constructor Details
#initialize(config) ⇒ Scheduler
Returns a new instance of Scheduler.
27 28 29 |
# File 'lib/munin2graphite/scheduler.rb', line 27 def initialize(config) @config = config end |
Instance Attribute Details
#scheduler ⇒ Object
Returns the value of attribute scheduler.
25 26 27 |
# File 'lib/munin2graphite/scheduler.rb', line 25 def scheduler @scheduler end |
Instance Method Details
#carbon=(socket) ⇒ Object
31 32 33 |
# File 'lib/munin2graphite/scheduler.rb', line 31 def carbon=(socket) @carbon = Carbon.new(socket) end |
#category_from_config(config) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/munin2graphite/scheduler.rb', line 35 def category_from_config(config) config.each_line do |configline| if configline =~ /^graph_category ([\w\-_\.]+)$/ return configline.split[1] end end raise "CategoryNotFound in #{config}" end |
#metric_loop(worker) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/munin2graphite/scheduler.rb', line 195 def metric_loop(worker) config = @config.config_for_worker worker retries = 3 begin obtain_metrics(worker) rescue => e config.log.error("Exception found: (#{e.to_s})") e.backtrace.each { |line| config.log.error(line) } sleep 1 retries -= 1 config.log.error("Retrying") retry unless retries < 0 config.log.error("Exitting, exception not solved") exit(1) end end |
#munin_config(reload = false) ⇒ Object
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 |
# File 'lib/munin2graphite/scheduler.rb', line 44 def munin_config(reload = false) return @munin_config if @munin_config && !reload @munin_config = {} @config.log.info("Obtaining metrics configuration") @munin_config[:workers] = [] semaphore = Mutex.new threads = [] workers.each do |worker| threads << Thread.new do current_config = {} config = @config.config_for_worker(worker) begin munin_worker = Munin::Node.new(config["munin_hostname"],config["munin_port"]) nodes = config["munin_nodes"] ? config["munin_nodes"].split(",") : munin_worker.nodes rescue Exception => e config.log.error("Error when trying to connect to munin-node on #{config["munin_hostname"]}:#{config["munin_port"]}") config.log.error("This node will be skipped") Thread.current.exit end current_config[:nodes] = {} semaphore_nodes = Mutex.new threads_nodes = [] nodes.each do |node| threads_nodes << Thread.new do munin = Munin::Node.new(config["munin_hostname"],config["munin_port"]) metrics = munin.list(node) config.log.info("Config for node #{worker}::#{node}") semaphore_nodes.synchronize do current_config[:nodes][node] = { :metrics => {} } end metrics.each do |metric| begin raw_config = munin.config(metric,true)[metric] category = category_from_config(raw_config) # We prepend the worker name to the graph title for clarity if config["graph_legend_prepend"] == "true" nodename = config["graphite_name_schema"] == "worker" ? worker : node if raw_config.match("graph_title ") raw_config.gsub!("graph_title ","graph_itle #{nodename} ") else raw_config << "\ngraph_title #{nodename}" end end semaphore_nodes.synchronize do current_config[:nodes][node][:metrics][metric] = { :config => munin.config(metric)[metric], :raw_config => raw_config, :category => category } end rescue Exception config.log.error("Error when trying to obtain graph conf for #{worker}::#{node}::#{metric} Ignored") end end end end threads_nodes.each { |i| i.join } # @config.log.debug(current_config.inspect) semaphore.synchronize do @munin_config[worker] = current_config @munin_config[:workers] << worker end munin_worker.disconnect config.log.info("Config for #{worker} obtained") end end threads.each { |i| i.join } @munin_config end |
#obtain_graphs ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/munin2graphite/scheduler.rb', line 173 def obtain_graphs munin_config(true) munin_config[:workers].each do |worker| time = Time.now config = @config.config_for_worker worker @config.log.info("Begin : Sending Graph Information to Graphite for worker #{worker}") Graphite::Base.set_connection(config["graphite_endpoint"]) Graphite::Base.authenticate(config["graphite_user"],config["graphite_password"]) munin_config[worker][:nodes].keys.each do |node| @config.log.info("Graphs for #{node}") munin_config[worker][:nodes][node][:metrics].each do |metric,value| @config.log.info("Configuring #{metric}") munin_graph = MuninGraph.graph_for value[:raw_config] munin_graph.config = config.merge("metric" => "#{metric}","hostname" => node.split(".").first) @config.log.debug("Saving graph #{metric}") munin_graph.to_graphite.save! end end config.log.info("End : Sending Graph Information to Graphite for worker #{worker}, elapsed time (#{Time.now - time}s)") end end |
#obtain_metrics(worker = "global") ⇒ Object
This is the loop of the metrics scheduling
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 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/munin2graphite/scheduler.rb', line 120 def obtain_metrics(worker = "global") my_munin_config = munin_config.dup time = Time.now config = @config.config_for_worker(worker) config.log.info("Worker #{worker}") metric_base = [config["graphite_user"], config["graphite_prefix"]].reject{|i| i== ""}.compact.join(".") my_munin_config[worker][:nodes].each do |node,node_info| node_name = metric_base + "." + node.split(".").first config.log.debug("Doing #{node_name}") values = {} config.log.debug("Asking for: #{node}") metric_time = Time.now metrics = node_info[:metrics].keys config.log.debug("Metrics " + metrics.join(",")) metrics_threads = [] categories = {} metrics.each do |metric| metrics_threads << Thread.new do begin local_munin = Munin::Node.new(config["munin_hostname"],config["munin_port"]) values[metric] = local_munin.fetch metric local_munin.disconnect rescue Exception => e @config.log.error("There was a problem when getting the metric #{metric} for #{node} , Ignored") @config.log.error(e.) @config.log.error(e.backtrace.inspect) end end end metrics_threads.each {|i| i.join} config.log.debug(values.inspect) config.log.info("Done with: #{node} (#{Time.now - metric_time} s)") carbon = @carbon || Carbon.new(config["carbon_hostname"],config["carbon_port"]) string_to_send = "" values.each do |metric,results| category = node_info[:metrics][metric][:category] results.each do |k,v| v.each do |c_metric,c_value| name = "#{node_name}.#{category}.#{metric}.#{c_metric}".gsub("-","_") string_to_send += "#{name} #{c_value} #{Time.now.to_i}\n" if c_value != "U" end end end @config.log.debug(string_to_send) send_time = Time.now carbon.send(string_to_send) carbon.flush carbon.close end if my_munin_config[worker] @config.log.info("End getting metrics for worker #{worker}, elapsed time (#{Time.now - time}s)") end |
#start ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/munin2graphite/scheduler.rb', line 225 def start @config.log.info("Scheduler started") obtain_graphs @scheduler = Rufus::Scheduler.start_new workers.each do |worker| config = @config.config_for_worker worker config.log.info("Scheduling worker #{worker} every #{config["scheduler_metrics_period"]} ") metric_loop(worker) @scheduler.every config["scheduler_metrics_period"] do metric_loop(worker) end =begin # Graph rereading is disabled by now there are concurrency problems @scheduler.every config["scheduler_graphs_period"] do obtain_graphs end =end end end |
#start1r_graphs ⇒ Object
220 221 222 223 |
# File 'lib/munin2graphite/scheduler.rb', line 220 def start1r_graphs @config.log.info("One-run started: graphs") obtain_graphs end |
#start1r_metrics ⇒ Object
212 213 214 215 216 217 218 |
# File 'lib/munin2graphite/scheduler.rb', line 212 def start1r_metrics @config.log.info("One-run started: metrics") workers.each do |worker| config = @config.config_for_worker worker metric_loop(worker) end end |
#workers ⇒ Object
114 115 116 |
# File 'lib/munin2graphite/scheduler.rb', line 114 def workers @workers ||= (@config.workers.empty? ? ["global"] : @config.workers ) end |