Advertisement
Guest User

Untitled

a guest
Sep 10th, 2013
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.86 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. #
  3. # munin-graphite.rb
  4. #
  5. # A Munin-Node to Graphite bridge
  6. #
  7. # Author:: Adam Jacob (<adam@hjksolutions.com>)
  8. # Copyright:: Copyright (c) 2008 HJK Solutions, LLC
  9. # License:: GNU General Public License version 2 or later
  10. #
  11. # This program and entire repository is free software; you can
  12. # redistribute it and/or modify it under the terms of the GNU
  13. # General Public License as published by the Free Software
  14. # Foundation; either version 2 of the License, or any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  24. #
  25.  
  26. require 'socket'
  27.  
  28. class Munin
  29.   def initialize(host='localhost', port=4949)
  30.     @host = host
  31.     @port = port
  32.     puts "[#{Time.now}] Contacting munin host at #{host}:#{port}"
  33.     @munin = TCPSocket.new(host, port)
  34.     @munin.gets
  35.   end
  36.  
  37.   def get_response(cmd)
  38.     @munin.puts(cmd)
  39.     stop = false
  40.     response = Array.new
  41.     while stop == false
  42.       line = @munin.gets
  43.       line.chomp!
  44.       if line == '.'
  45.         stop = true
  46.       else
  47.         response << line
  48.         stop = true if cmd == "list"
  49.       end
  50.     end
  51.     response
  52.   end
  53.  
  54.   def close
  55.     @munin.close
  56.   end
  57. end
  58.  
  59. class Carbon
  60.   def initialize(host='192.168.15.179', port=2003)
  61.     @host = host
  62.     @port = port
  63.     puts "[#{Time.now}] Contacting Carbon host at #{host}:#{port}"
  64.     @carbon = TCPSocket.new(host, port)
  65.   end
  66.  
  67.   def send(msg)
  68.     @carbon.puts(msg)
  69.   end
  70.  
  71.   def close
  72.     @carbon.close
  73.   end
  74. end
  75.  
  76. while true
  77.   metric_base = "servers."
  78.   all_metrics = Array.new
  79.  
  80.   munin = Munin.new(ARGV[0])
  81.   munin.get_response("nodes").each do |node|
  82.     metric_base << node.split(".").reverse.join(".")
  83.     puts "Doing #{metric_base}"
  84.     munin.get_response("list")[0].split(" ").each do |metric|
  85.       puts "Grabbing #{metric}"
  86.       mname = "#{metric_base}"
  87.       has_category = false
  88.       base = false
  89.       munin.get_response("config #{metric}").each do |configline|
  90.         if configline =~ /graph_category (.+)/
  91.           mname << ".#{$1}"
  92.           has_category = true
  93.         end
  94.         if configline =~ /graph_args.+--base (\d+)/
  95.           base = $1
  96.         end
  97.       end
  98.       mname << ".other" unless has_category
  99.       munin.get_response("fetch #{metric}").each do |line|
  100.         line =~ /^(.+)\.value\s+(.+)$/
  101.         field = $1
  102.         value = $2
  103.         all_metrics << "#{mname}.#{metric}.#{field} #{value} #{Time.now.to_i}"
  104.       end
  105.     end
  106.   end
  107.  
  108.   carbon = Carbon.new(ARGV[1])
  109.   all_metrics.each do |m|
  110.     puts "Sending #{m}"
  111.     carbon.send(m)
  112.   end
  113.   sleep 60
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement