Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite icinga2 job #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ gem 'thin'
gem 'eventmachine'

## Remove this if you don't need a twitter widget.
gem 'twitter', '>= 5.9.0'
# gem 'twitter', '>= 5.9.0'
gem 'rest-client'


17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ You may use the provided examples in your own implementation.

![Dashing Icinga 2](public/dashing_icinga2_overview.png "Dashing Icinga 2")

# Support
# Changes from master
Rewrite the `icinga2.rb` Job to use an own written Library (located in libs Directory) to seperate Logic an Job.
My Job uses an Configuration file (`config/icinga2.json`) for an better configuration way an - maybe - for more than one Icinga2 Instance.


Please open issues at [dev.icinga.org](https://dev.icinga.org/projects/icinga-tools). In case
you've created a PR/patch, open a new issue linking to it as well please :)

If you have any questions, please hop onto the [Icinga community channels](https://www.icinga.org/community/get-help/).
All my changes currently are **NOT TESTED**

# Support

# License

Expand All @@ -31,6 +34,12 @@ If you have any questions, please hop onto the [Icinga community channels](https

Gems:

require 'json'
require 'rest-client'
require 'openssl'
require 'logger'
require 'time'

gem install bundler
gem install dashing

Expand Down
Binary file added assets/images/icinga_logo4-300x109.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions config/icinga2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"icinga2": {
"server": {
"name": "%ICINGA2_HOST%",
"port": "%ICINGA2_PORT%"
},
"api": {
"user": "%ICINGA2_DASHING_APIUSER%",
"password": "%ICINGA2_DASHING_APIPASS%"
}
}
}
27 changes: 26 additions & 1 deletion dashboards/icinga2.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@
<div class="gridster">
ICINGA 2
<ul>
<!-- Stats -->
<!-- head -->
<li data-row="1" data-col="1" data-sizex="1" data-sizey="2">
<div data-view="Clock"></div>
<i class="icon-time icon-background"></i>
</li>
<li data-row="1" data-col="1" data-sizex="6" data-sizey="1">
<div data-id="chuck" data-view="Text" data-title="Team Facts"></div>
</li>
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-view="Image" data-image="/icinga_logo4-300x109.png" style="background: lightgrey;"></div>
</li>
<!-- head -->

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="icinga-uptime" data-view="Icinga2" data-title="Uptime"></div>
</li>

<li data-row="1" data-col="1" data-sizex="1" data-sizey="2">
<div data-id="icinga-hosts-latest" data-view="Table" data-title="Icinga host problems" class="icinga-latest-problems"></div>
</li>



<!--
<li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
<div data-id="icinga-uptime" data-view="Simplemon" data-title="Uptime"></div>
</li>
Expand Down Expand Up @@ -42,6 +65,8 @@
<li data-row="3" data-col="4" data-sizex="1" data-sizey="1">
<div data-id="icinga-service-downtime" data-view="Simplemon" data-title="Services in Downtime"></div>
</li>
-->

</ul>

</div>
9 changes: 0 additions & 9 deletions jobs/buzzwords.rb

This file was deleted.

38 changes: 38 additions & 0 deletions jobs/chuck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# original from: https://gist.github.com/windowfinn/f8c35551138babcc3e9e

require 'net/http'
require 'json'
require 'cgi'

#The Internet Chuck Norris Database
server = "http://api.icndb.com"

#Id of the widget
id = "chuck"

#Proxy details if you need them - see below
proxy_host = 'XXXXXXX'
proxy_port = 8080
proxy_user = 'XXXXXXX'
proxy_pass = 'XXXXXXX'

SCHEDULER.every '3m', :first_in => 0 do |job|

uri = URI("#{server}/jokes/random?limitTo=nerdy,explicit")

#This is for when there is no proxy
res = Net::HTTP.get(uri)

#This is for when there is a proxy
#res = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass).get(uri)

#marshal the json into an object
j = JSON[res]

#Get the joke
joke = CGI.unescapeHTML(j['value']['joke'])

#Send the joke to the text widget
send_event(id, { title: "Chuck Norris Facts", text: joke })
end
14 changes: 0 additions & 14 deletions jobs/convergence.rb

This file was deleted.

193 changes: 23 additions & 170 deletions jobs/icinga2.rb
Original file line number Diff line number Diff line change
@@ -1,186 +1,39 @@

#/******************************************************************************
# * Icinga 2 Dashing Job *
# * Copyright (C) 2015-2016 Icinga Development Team (https://www.icinga.org) *
# * *
# * This program is free software; you can redistribute it and/or *
# * modify it under the terms of the GNU General Public License *
# * as published by the Free Software Foundation; either version 2 *
# * of the License, or (at your option) any later version. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU General Public License for more details. *
# * *
# * You should have received a copy of the GNU General Public License *
# * along with this program; if not, write to the Free Software Foundation *
# * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
# ******************************************************************************/

require 'rest-client'
require './lib/icinga2'

$node_name = Socket.gethostbyname(Socket.gethostname).first
if defined? settings.icinga2_api_nodename
node_name = settings.icinga2_api_nodename
end
#$api_url_base = "https://192.168.33.5:5665"
$api_url_base = "https://localhost:5665"
if defined? settings.icinga2_api_url
api_url_base = settings.icinga2_api_url
end
$api_username = "dashing"
if defined? settings.icinga2_api_username
api_username = settings.icinga2_api_username
end
$api_password = "icinga2ondashingr0xx"
if defined? settings.icinga2_api_password
api_password = settings.icinga2_api_password
end

# prepare the rest client ssl stuff
def prepare_rest_client(api_url)
# check whether pki files are there, otherwise use basic auth
if File.file?("pki/" + $node_name + ".crt")
puts "PKI found, using client certificates for connection to Icinga 2 API"
cert_file = File.read("pki/" + $node_name + ".crt")
key_file = File.read("pki/" + $node_name + ".key")
ca_file = File.read("pki/ca.crt")

cert = OpenSSL::X509::Certificate.new(cert_file)
key = OpenSSL::PKey::RSA.new(key_file)

options = {:ssl_client_cert => cert, :ssl_client_key => key, :ssl_ca_file => ca_file, :verify_ssl => OpenSSL::SSL::VERIFY_NONE}
else
puts "PKI not found, using basic auth for connection to Icinga 2 API"

options = { :user => $api_username, :password => $api_password, :verify_ssl => OpenSSL::SSL::VERIFY_NONE }
end

res = RestClient::Resource.new(URI.encode(api_url), options)
return res
end

def get_stats()
api_url = $api_url_base + "/v1/status/CIB"
rest_client = prepare_rest_client(api_url)
headers = {"Content-Type" => "application/json", "Accept" => "application/json"}

return rest_client.get(headers)
end

def get_app()
api_url = $api_url_base + "/v1/status/IcingaApplication"
rest_client = prepare_rest_client(api_url)
headers = {"Content-Type" => "application/json", "Accept" => "application/json"}

return rest_client.get(headers)
end


SCHEDULER.every '1s' do

total_critical = 0
total_warning = 0
total_ack = 0
total = 0

app = get_app()
result = JSON.parse(app.body)
icingaapplication = result["results"][0] # there's only one row
app_info = icingaapplication["status"]

puts "App Info: " + app_info.to_s

version = app_info["icingaapplication"]["app"]["version"]

res = get_stats()
result = JSON.parse(res.body)
cib = result["results"][0] # there's only one row
status = cib["status"]

puts "Status: " + status.to_s

uptime = status["uptime"].round(2)
uptime = Time.at(uptime).utc.strftime("%H:%M:%S")
avg_latency = status["avg_latency"].round(2)
avg_execution_time = status["avg_execution_time"].round(2)

services_ok = status["num_services_ok"].to_int
services_warning = status["num_services_warning"].to_int
services_critical = status["num_services_critical"].to_int
services_unknown = status["num_services_unknown"].to_int
services_ack = status["num_services_acknowledged"].to_int
services_downtime = status["num_services_in_downtime"].to_int

hosts_up = status["num_hosts_up"].to_int
hosts_down = status["num_hosts_down"].to_int
hosts_ack = status["num_hosts_acknowledged"].to_int
hosts_downtime = status["num_hosts_in_downtime"].to_int

total_critical = services_critical + hosts_down
total_warning = services_warning

if total_critical > 0 then
color = 'red'
value = total_critical.to_s
elsif total_warning > 0 then
color = 'yellow'
value = total_warning.to_s
else
color = 'green'
value = total.to_s
end

# events
send_event('icinga-overview', {
value: value,
color: color })
icinga = Icinga2.new( 'config/icinga2.json' )

send_event('icinga-version', {
value: version.to_s,
color: 'blue' })
SCHEDULER.every '3m' do

send_event('icinga-uptime', {
value: uptime.to_s,
color: 'blue' })
icinga.run

send_event('icinga-latency', {
value: avg_latency.to_s + "s",
color: 'blue' })
send_event( 'icinga-uptime', {
value: icinga.uptime,
color: 'blue' }
)

send_event('icinga-execution-time', {
value: avg_execution_time.to_s + "s",
color: 'blue' })
send_event( 'icinga-version', {
value: icinga.version,
color: 'blue' }
)

# down, critical, warning
send_event('icinga-host-down', {
value: hosts_down.to_s,
color: 'red' })
send_event( 'icinga-hosts-latest', {
rows: icinga.status_hosts["latest"],
moreinfo: icinga.status_hosts["latest_moreinfo"]
})

send_event('icinga-service-critical', {
value: services_critical.to_s,
color: 'red' })
# icinga-hosts-latest

send_event('icinga-service-warning', {
value: services_warning.to_s,
color: 'yellow' })

# ack, downtime
send_event('icinga-service-ack', {
value: services_ack.to_s,
color: 'blue' })
puts " ----------------------------- "

send_event('icinga-host-ack', {
value: hosts_ack.to_s,
color: 'blue' })
puts "uptime : " + icinga.uptime.to_s
puts "services ok : " + icinga.services_ok.to_s
puts "hosts up : " + icinga.hosts_up.to_s
puts "hosts down : " + icinga.hosts_down.to_s

send_event('icinga-service-downtime', {
value: services_downtime.to_s,
color: 'orange' })
puts " ----------------------------- "

send_event('icinga-host-downtime', {
value: hosts_downtime.to_s,
color: 'orange' })
end

Loading