Class: MuninGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/munin_graph.rb

Overview

This class allows the transformation between graphite and munin config. It constructs an AST parsing the munin config information and outputs a valid graphic in graphite url format Jose Fernandez 2011

Constant Summary collapse

TOKENS =

This array of hashes will be used to match what kind of line we are dealing with and to know the corresponding ast node class

[
       {:matcher => /^graph_title .*/, :klass => GraphTitleGlobalDeclarationNode},
       {:matcher => /^create_args .*/, :klass => CreateArgsGlobalDeclarationNode},
       {:matcher => /^graph_args .*/, :klass => GraphArgsGlobalDeclarationNode},
       {:matcher => /^graph_category .*/, :klass => GraphCategoryGlobalDeclarationNode},
       {:matcher => /^graph_info .*/, :klass => GraphInfoGlobalDeclarationNode},
       {:matcher => /^graph_order .*/, :klass => GraphOrderGlobalDeclarationNode},
       {:matcher => /^graph_vlabel .*/, :klass => GraphVLabelGlobalDeclarationNode},
       {:matcher => /^graph_total .*/, :klass => GraphTotalGlobalDeclarationNode},
       {:matcher => /^graph_scale .*/, :klass => GraphScaleGlobalDeclarationNode},
       {:matcher => /^graph .*/, :klass => GraphGlobalDeclarationNode},
       {:matcher => /^host_name .*/, :klass => HostNameGlobalDeclarationNode},
       {:matcher => /^update .*/, :klass => UpdateGlobalDeclarationNode},
       {:matcher => /^graph_period .*/, :klass => GraphPeriodGlobalDeclarationNode},
       {:matcher => /^graph_vtitle .*/, :klass => GraphVTitleGlobalDeclarationNode},
       {:matcher => /^service_order .*/, :klass => ServiceOrderGlobalDeclarationNode},
       {:matcher => /^graph_width .*/, :klass => GraphWidthGlobalDeclarationNode},
       {:matcher => /^graph_height .*/, :klass => GraphHeightGlobalDeclarationNode},
       {:matcher => /^graph_printfformat .*/, :klass => GraphPrintFormatGlobalDeclarationNode},
       {:matcher => /([\w_]+)\.label\ .*$/,:klass => LabelFieldPropertyNode},
       {:matcher => /([\w_]+)\.cdef\ .*$/,:klass => CDefFieldPropertyNode},
       {:matcher => /([\w_]+)\.draw\ .*$/,:klass => DrawFieldPropertyNode},
       {:matcher => /([\w_]+)\.graph\ .*$/,:klass => GraphFieldPropertyNode},
       {:matcher => /([\w_]+)\.info\ .*$/,:klass => InfoFieldPropertyNode},
       {:matcher => /([\w_]+)\.extinfo\ .*$/,:klass => ExtInfoFieldPropertyNode},
       {:matcher => /([\w_]+)\.max\ .*$/,:klass => MaxFieldPropertyNode},
       {:matcher => /([\w_]+)\.min\ .*$/,:klass => MinFieldPropertyNode},
       {:matcher => /([\w_]+)\.negative\ .*$/,:klass => NegativeFieldPropertyNode},
       {:matcher => /([\w_]+)\.type\ .*$/,:klass => TypeFieldPropertyNode},
       {:matcher => /([\w_]+)\.warning\ .*$/,:klass => WarningFieldPropertyNode},
       {:matcher => /([\w_]+)\.critical\ .*$/,:klass => CriticalFieldPropertyNode},
       {:matcher => /([\w_]+)\.colour\ .*$/,:klass => ColourFieldPropertyNode},
       {:matcher => /([\w_]+)\.skipdraw\ .*$/,:klass => SkipDrawFieldPropertyNode},
       {:matcher => /([\w_]+)\.sum\ .*$/,:klass => SumFieldPropertyNode},
       {:matcher => /([\w_]+)\.stack\ .*$/,:klass => StackFieldPropertyNode},
       {:matcher => /([\w_]+)\.linevalue\[:color\[:label\]\]\ .*$/,:klass => LineValueFieldPropertyNode},
       {:matcher => /([\w_]+)\.oldname\ .*$/,:klass => OldNameFieldPropertyNode},
       {:matcher => /([\w_]+)\.value\ .*$/,:klass => ValueFieldPropertyNode} 
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ MuninGraph

Returns a new instance of MuninGraph.



34
35
36
37
# File 'lib/munin_graph.rb', line 34

def initialize(config)
  @raw_config = config
  parse_config
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



65
66
67
# File 'lib/munin_graph.rb', line 65

def root
  @root
end

Class Method Details

.graph_for(config) ⇒ Object



30
31
32
# File 'lib/munin_graph.rb', line 30

def self.graph_for(config)
  MuninGraph.new(config)
end

Instance Method Details

#config=(config) ⇒ Object



39
40
41
42
# File 'lib/munin_graph.rb', line 39

def config=(config)
  @config = config
  self.root.config = config
end

#parse_configObject



109
110
111
112
113
114
115
116
117
118
119
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
# File 'lib/munin_graph.rb', line 109

def parse_config
   @root = ASTNode.new("")
   @root.parent = nil
   current_node = @root
   @raw_config.each_line do |line|
     # For every line of config we match against every token
     TOKENS.each do |token|
       if line =~ token[:matcher]
         # When we find a match...
         if token[:klass].new("").is_a? FieldPropertyNode
           # In Property field we have to set it to a FieldDeclarationNode (an artificial node grouping those fields)
           if !current_node.is_a? FieldDeclarationNode
             # A new FieldDeclaration has to ve created
             node = FieldDeclarationNode.new("")
             node.properties[:field_name] = $1
             current_node.add_child node
             current_node = node
           elsif current_node.properties[:field_name] != $1
             if (aux = @root.children_of_class(FieldDeclarationNode).find { |i| i.properties[:field_name] == $1 } )
               current_node =  aux
             else
               # We use the one declared before (note that different metrics could not be interlaced)
               node = FieldDeclarationNode.new("")
               node.properties[:field_name] = $1
               current_node.parent.add_child node
               current_node = node
             end
           end
           current_node.add_child token[:klass].send("new",line)
         else
           @root.add_child token[:klass].send("new",line)
         end
         break
      end
    end
  end
end

#to_gdashObject



60
61
62
63
# File 'lib/munin_graph.rb', line 60

def to_gdash
  self.root.compile
  self.root.to_gdash
end

#to_graphiteObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/munin_graph.rb', line 44

def to_graphite
  graph = Graphite::MyGraph.new
  self.root.compile
  graph.url = self.root.url
  self.root.properties[:category] ||= "other"
  
  if @config["graphite_prefix"] == "" || !@config["graphite_prefix"]  && @config["graphite_graph_prefix"] && @config["graphite_graph_prefix"] != ""
    puts "DEPRECATION WARNING: parameter graphite_graph_prefix is not used anymore, please use graphite_prefix instead."
    @config["graphite_prefix"] =  @config["graphite_graph_prefix"]
  end

  graph.name = "#{@config["hostname"]}.#{self.root.properties["category"]}.#{self.root.properties["metric"]}"
  graph.name = "#{@config["graphite_prefix"]}.#{graph.name}" if @config["graphite_prefix"] && @config["graphite_prefix"] != "" 
  return graph
end