Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow to configure the Icinga Web 2 Iframe URL
This is a little brainfuck, as we need to create
an instance of the icinga2 class for reading the configuration
details in `config.ru`.

We'll expose the setting inside a helper function, which is
called inside the HTML erb template for the dashboard.

Note: This is done every time the user presses refresh. Job execution
won't trigger this. The previous attempt with updating this on send_event
caused flickering.
  • Loading branch information
Michael Friedrich committed Jan 22, 2018
1 parent e465440 commit 132ba78
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -181,6 +181,9 @@ In case you want to use client certificates, set the `client_cn` accordingly.
Copy the example configuration from `config/icinga2.json` into `config/icinga2.local.json`
and adjust the settings for the Icinga 2 API credentials.

The `icingaweb2` section allows you to specify the Icinga Web 2 URL
for the iframe widgets. This is read on startup once.

```
cp config/icinga2.json config/icinga2.local.json
```
Expand All @@ -196,6 +199,9 @@ vim config/icinga2.local.json
"user": "dashing",
"password": "icinga2ondashingr0xx"
}
},
"icingaweb2": {
"url": "http://localhost/icingaweb2"
}
}
```
Expand All @@ -214,6 +220,9 @@ explicitly.
"user": "dashing",
"pki_path": "pki/"
}
},
"icingaweb2": {
"url": "http://localhost/icingaweb2"
}
}
```
Expand All @@ -238,6 +247,7 @@ ICINGA2\_API\_USERNAME | **Optional.** Required for basic auth as username.
ICINGA2\_API\_PASSWORD | **Optional.** Required for basic auth as password.
ICINGA2\_API\_CERT\_PATH | **Optional.** Client certificate path.
ICINGA2\_API\_NODENAME | **Optional.** If client certificates do not match the host name, override it.
ICINGAWEB2\_URL | **Optional.** Set the Icinga Web 2 Url. Defaults to `http://localhost/icingaweb2`.

> **Note**
>
Expand Down
14 changes: 14 additions & 0 deletions config.ru
@@ -1,4 +1,5 @@
require 'dashing'
require './lib/icinga2'

configure do
set :auth_token, 'YOUR_AUTH_TOKEN'
Expand All @@ -14,6 +15,19 @@ configure do
# Put any authentication code you want in here.
# This method is run before accessing any resource.
end

# Specify Icinga Web 2 URL
# https://github.com/Shopify/dashing/issues/509
def getIcingaWeb2Url()
# read configuration and try to fetch the correct path
icinga = Icinga2.new('config/icinga2.json') # fixed path

if icinga.icingaweb2_url != nil
return icinga.icingaweb2_url
else
return 'http://192.168.33.5/icingaweb2'
end
end
end
end

Expand Down
3 changes: 3 additions & 0 deletions config/icinga2.json
Expand Up @@ -6,5 +6,8 @@
"user": "dashing",
"password": "icinga2ondashingr0xx"
}
},
"icingaweb2": {
"url": "http://localhost/icingaweb2"
}
}
6 changes: 3 additions & 3 deletions dashboards/icinga2.erb
Expand Up @@ -44,12 +44,12 @@ $(function() {
<div data-id="icinga-severity" data-view="List" data-unordered="true" data-title="Problems" style="background-color: #0095bf;"></div>
</li>

<!-- Icinga Web 2 iFrame -->
<!-- Icinga Web 2 iFrame. getIcingaWeb2Url() is defined in config.ru and reads from config/icinga2*.json -->
<li data-row="3" data-col="1" data-sizex="2" data-sizey="2">
<div data-id="iframe" data-view="Iframe" data-url="http://192.168.33.5/icingaweb2/monitoring/list/hosts?host_problem=1&sort=host_severity&showFullscreen&showCompact"></div>
<div data-id="iframe" data-view="Iframe" data-url="<%=getIcingaWeb2Url()%>/monitoring/list/hosts?host_problem=1&sort=host_severity&showFullscreen&showCompact"></div>
</li>
<li data-row="3" data-col="3" data-sizex="2" data-sizey="2">
<div data-id="iframe" data-view="Iframe" data-url="http://192.168.33.5/icingaweb2/monitoring/list/services?service_problem=1&sort=service_severity&dir=desc&showFullscreen&showCompact"></div>
<div data-id="iframe" data-view="Iframe" data-url="<%=getIcingaWeb2Url()%>/monitoring/list/services?service_problem=1&sort=service_severity&dir=desc&showFullscreen&showCompact"></div>
</li>
</ul>

Expand Down
32 changes: 26 additions & 6 deletions lib/icinga2.rb
Expand Up @@ -30,6 +30,7 @@ class Icinga2
attr_reader :node_name
attr_reader :app_starttime
attr_reader :uptime
attr_reader :icingaweb2_url

# general stats
attr_reader :avg_latency
Expand Down Expand Up @@ -109,6 +110,9 @@ def getConfEnv()
@pkiPath = ENV['ICINGA2_API_CERT_PATH']
@nodeName = ENV['ICINGA2_API_NODENAME']

# external attribute
@icingaweb2_url = ENV['ICINGAWEB2_URL']

# check for the least required variables, the rest is read later on
if [@host, @port].all? {|value| value.nil? or value == ""}
raise ArgumentError.new('Required environment variables not found!')
Expand Down Expand Up @@ -139,12 +143,25 @@ def getConfFile(configFile)
if (File.exist?(realConfigFile))
file = File.read(realConfigFile)
@config = JSON.parse(file)
@host = @config["icinga2"]["api"]["host"]
@port = @config["icinga2"]["api"]["port"]
@user = @config["icinga2"]["api"]["user"]
@password = @config["icinga2"]["api"]["password"]
@pkiPath = @config["icinga2"]["api"]["pki_path"]
@nodeName = @config['icinga2']['api']['node_name']

if @config.key? 'icinga2'
config_icinga2 = @config['icinga2']

if config_icinga2.key? 'api'
@host = @config["icinga2"]["api"]["host"]
@port = @config["icinga2"]["api"]["port"]
@user = @config["icinga2"]["api"]["user"]
@password = @config["icinga2"]["api"]["password"]
@pkiPath = @config["icinga2"]["api"]["pki_path"]
@nodeName = @config['icinga2']['api']['node_name']
end
end

puts "Reading config" + @config.to_s
if @config.key? 'icingaweb2'
# external attribute
@icingaweb2_url = @config['icingaweb2']['url']
end
else
@log.warn(sprintf('Config file %s not found! Using default config.', configFile))
@host = "localhost"
Expand All @@ -153,6 +170,9 @@ def getConfFile(configFile)
@password = "icinga2ondashingr0xx"
@pkiPath = "pki/"
@nodeName = nil

# external attribute
@icingaweb2_url = 'http://localhost/icingaweb2'
end

rescue JSON::ParserError => e
Expand Down

0 comments on commit 132ba78

Please sign in to comment.