Class: Pencil::Config

Inherits:
Object
  • Object
show all
Includes:
Models
Defined in:
lib/pencil/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pencil/config.rb', line 14

def initialize
  port = 9292
  @rawconfig = {}
  @confdir = "."
  @recursive = false

  optparse = OptionParser.new do |o|
    o.on("-d", "--config-dir DIR",
      "location of the config directory (default .)") do |arg|
      @confdir = arg
      @recursive = true
    end
    o.on("-p", "--port PORT", "port to bind to (default 9292)") do |arg|
      port = arg.to_i
    end
  end

  optparse.parse!
  reload!
  @global_config[:port] = port
end

Instance Attribute Details

#clustersObject (readonly)

Returns the value of attribute clusters.



11
12
13
# File 'lib/pencil/config.rb', line 11

def clusters
  @clusters
end

#dashboardsObject (readonly)

Returns the value of attribute dashboards.



8
9
10
# File 'lib/pencil/config.rb', line 8

def dashboards
  @dashboards
end

#global_configObject (readonly)

Returns the value of attribute global_config.



12
13
14
# File 'lib/pencil/config.rb', line 12

def global_config
  @global_config
end

#graphsObject (readonly)

Returns the value of attribute graphs.



9
10
11
# File 'lib/pencil/config.rb', line 9

def graphs
  @graphs
end

#hostsObject (readonly)

Returns the value of attribute hosts.



10
11
12
# File 'lib/pencil/config.rb', line 10

def hosts
  @hosts
end

Instance Method Details

#reload!Object



36
37
38
39
40
41
42
43
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
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pencil/config.rb', line 36

def reload!
  # only do a recursive search if "-d" is specified
  configs = Dir.glob("#{@confdir}/#{@recursive ? '**/' : ''}*.y{a,}ml")

  configs.each do |c|
    yml = YAML.load(File.read(c))
    next unless yml
    @rawconfig[:config] = yml[:config] if yml[:config]
    a = @rawconfig[:dashboards] 
    b = yml[:dashboards] 
    c = @rawconfig[:graphs]
    d = yml[:graphs]

    if a && b
      a.merge!(b)
    elsif b
      @rawconfig[:dashboards] = b
    end
    if c && d
      c.merge!(d)
    elsif d
      @rawconfig[:graphs] = d
    end
  end
  @rawconfig = Map(@rawconfig)

  [:graphs, :dashboards, :config].each do |c|
    if not @rawconfig[c.to_s]
      raise "Missing config name '#{c.to_s}'"
    end
  end

  @global_config = @rawconfig[:config]
  # do some sanity checking of other configuration parameters
  [:graphite_url, :url_opts].each do |c|
    if not @global_config[c]
      raise "Missing config name '#{c.to_s}'"
    end
  end

  # possibly check more url_opts here as well
  if @global_config[:url_opts][:start]
    if !ChronicDuration.parse(@global_config[:url_opts][:start])
      raise "bad default timespec in :url_opts"
    end
  end

  @global_config[:default_colors] ||=
    ["blue", "green", "yellow", "red", "purple", "brown", "aqua", "gold"]

  if @global_config[:refresh_rate]
    duration = ChronicDuration.parse(@global_config[:refresh_rate].to_s)
    if !duration
      raise "couldn't parse key :refresh_rate"
    end
    @global_config[:refresh_rate] = duration
  end

  @global_config[:metric_format] ||= "%m.%c.%h"
  if @global_config[:metric_format] !~ /%m/
    raise "missing metric (%m) in :metric_format"
  elsif @global_config[:metric_format] !~ /%c/
    raise "missing cluster (%c) in :metric_format"
  elsif @global_config[:metric_format] !~ /%h/
    raise "missing host (%h) in :metric_format"
  end

  graphs_new = []
  @rawconfig[:graphs].each do |name, config|
    graphs_new << Graph.new(name, config.merge(@global_config))
  end

  dashboards_new = []
  @rawconfig[:dashboards].each do |name, config|
    dashboards_new << Dashboard.new(name, config.merge(@global_config))
  end

  hosts_new = Set.new
  clusters_new = Set.new

  # generate host and cluster information at init time
  graphs_new.each do |g|
    hosts, clusters = g.hosts_clusters
    hosts.each { |h| hosts_new << h }
    clusters.each { |h| clusters_new << h }
  end

  @dashboards, @graphs = dashboards_new, graphs_new
  @hosts, @clusters = hosts_new, clusters_new
end