diff --git a/attributes/default.rb b/attributes/default.rb index 0bff8c43..ca713e68 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -49,3 +49,16 @@ default['grafana']['grafana_index'] = 'grafana-index' default['grafana']['unsaved_changes_warning'] = 'true' default['grafana']['playlist_timespan'] = '1m' +default['grafana']['datasources'] = { + 'graphite' => { + 'type' => "'graphite'", + 'url' => 'window.location.protocol+"//"+window.location.hostname+":"+window.location.port+"/_graphite"', + 'default' => true + }, + 'elasticsearch' => { + 'type' => "'elasticsearch'", + 'url' => 'window.location.protocol+"//"+window.location.hostname+":"+window.location.port', + 'index' => "'#{node['grafana']['grafana_index']}'", + 'grafanaDB' => true + } +} diff --git a/libraries/javascript_pp.rb b/libraries/javascript_pp.rb new file mode 100644 index 00000000..69375d4b --- /dev/null +++ b/libraries/javascript_pp.rb @@ -0,0 +1,20 @@ +module JavascriptPP + def pprint(obj, indent=1) + case obj + when Hash + res = "{\n" + obj.each do |k,v| + res += " " * indent + res += k.to_s + ": " + res += pprint(v, indent+1) + res += ",\n" + end + res.gsub!(/,\n$/, "\n") + res += " " * (indent - 1) + res += "}" + else + res = obj.to_s + end + res + end +end diff --git a/recipes/default.rb b/recipes/default.rb index 0accfa95..df00f6eb 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -42,8 +42,12 @@ template "#{node['grafana']['web_dir']}/config.js" do source node['grafana']['config_template'] cookbook node['grafana']['config_cookbook'] + variables({ + 'datasources' => node['grafana']['datasources'], + }) mode '0644' user grafana_user + helpers(::JavascriptPP) end unless node['grafana']['webserver'].empty? diff --git a/templates/default/config.js.erb b/templates/default/config.js.erb index 0d093254..97a0da2b 100644 --- a/templates/default/config.js.erb +++ b/templates/default/config.js.erb @@ -3,25 +3,14 @@ // config.js is where you will find the core Grafana configuration. This file contains parameter that // must be set before Grafana is run for the first time. /// +<% require 'json' %> define(['settings'], function (Settings) { - return new Settings({ // datasources, you can add multiple - datasources: { - graphite: { - type: 'graphite', - url: window.location.protocol+"//"+window.location.hostname+":"+window.location.port+"/_graphite", - default: true - } - }, - - // elasticsearch url - // used for storing and loading dashboards, optional - // For Basic authentication use: http://username:password@domain.com:9200 - elasticsearch: window.location.protocol+"//"+window.location.hostname+":"+window.location.port, + datasources: <%= pprint(@datasources,3) %>, // default start dashboard default_route: '/dashboard/file/default.json', @@ -38,9 +27,6 @@ function (Settings) { // timezoneOffset: <%= node['grafana']['timezone_offset'] %>, - // Elasticsearch index for storing dashboards - grafana_index: "<%= node['grafana']['grafana_index'] %>", - // set to false to disable unsaved changes warning unsaved_changes_warning: <%= node['grafana']['unsaved_changes_warning'] %>, @@ -48,7 +34,6 @@ function (Settings) { // Example: "1m", "1h" playlist_timespan: "<%= node['grafana']['playlist_timespan'] %>", - // Add your own custom pannels plugins: { panels: []