Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Friedrich committed Nov 27, 2016
1 parent c96f73b commit e376323
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 48 deletions.
18 changes: 1 addition & 17 deletions jobs/icinga2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@
puts "CIB Info: " + icinga.cib_data.to_s

# meter widget
#host_meter = ((total_problem_hosts.to_f / total_hosts.to_f) * 100).round(2)
# we'll update the patched meter widget with absolute values (set max dynamically)
host_meter = icinga.host_count_problems.to_f
host_meter_max = icinga.host_count_all

#service_meter = ((total_problem_services.to_f / total_services.to_f) * 100).round(2)
# we'll update the patched meter widget with absolute values (set max dynamically)
service_meter = icinga.service_count_problems.to_f
service_meter_max = icinga.service_count_all

Expand All @@ -54,23 +50,11 @@
# severity list
severity_stats = []
icinga.service_problems_severity.each do |name, state|
color = icinga.stateToColor(state.to_int, false)
severity_stats.push({ "label" => icinga.formatService(name), "color" => color})
severity_stats.push({ "label" => icinga.formatService(name), "color" => icinga.stateToColor(state.to_int, false)})
end

puts "Severity: " + severity_stats.to_s

### Events
send_event('icinga-version', {
value: icinga.version,
moreinfo: 'Revision: ' + icinga.version_revision
})

send_event('icinga-uptime', {
value: icinga.uptime.to_s,
moreinfo: icinga.app_starttime
})

send_event('icinga-host-meter', {
value: host_meter,
max: host_meter_max,
Expand Down
44 changes: 13 additions & 31 deletions lib/icinga2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,6 @@ class Icinga2
attr_reader :all_hosts_data
attr_reader :all_services_data

# internal
@@log
@@config
@@host
@@port
@@user
@@password
@@pkiPath
@@apiUrlBase
@@apiVersion
@@nodeName
@@hasCert
@@headers
@@options

def initialize(configFile)
# add logger
file = File.open('/tmp/dashing-icinga2.log', File::WRONLY | File::APPEND | File::CREAT)
Expand All @@ -91,7 +76,6 @@ def initialize(configFile)
end

# parse config
@log.debug(sprintf( ' config file : %s', configFile))
configFile = File.expand_path(configFile)
@log.debug(sprintf( ' config file : %s', configFile))

Expand Down Expand Up @@ -175,38 +159,30 @@ def getIcingaApplicationData()
apiUrl = sprintf('%s/status/IcingaApplication', @apiUrlBase)
restClient = RestClient::Resource.new(URI.encode(apiUrl), @options)
data = JSON.parse(restClient.get(@headers).body)
result = data['results'][0]['status'] #there's only one row

return result
return data['results'][0]['status'] #there's only one row
end

def getCIBData()
apiUrl = sprintf('%s/status/CIB', @apiUrlBase)
restClient = RestClient::Resource.new(URI.encode(apiUrl), @options)
data = JSON.parse(restClient.get(@headers).body)
result = data['results'][0]['status'] #there's only one row

return result
return data['results'][0]['status'] #there's only one row
end

def getHostObjects(attrs = nil, filter = nil, joins = nil)
apiUrl = sprintf('%s/objects/hosts', @apiUrlBase)
# TODO change to X-HTTP-Method-Override: GET in @headers, use POST and send filters, joins, attrs in request bondy
restClient = RestClient::Resource.new(URI.encode(apiUrl), @options)
data = JSON.parse(restClient.get(@headers).body)
result = data['results']

return result
return data['results']
end

def getServiceObjects(attrs = nil, filter = nil, joins = nil)
apiUrl = sprintf('%s/objects/services?joins=host', @apiUrlBase)
apiUrl = sprintf('%s/objects/services?joins=host', @apiUrlBase) # joins is required!
# TODO change to X-HTTP-Method-Override: GET in @headers, use POST and send filters, joins, attrs in request bondy
restClient = RestClient::Resource.new(URI.encode(apiUrl), @options)
data = JSON.parse(restClient.get(@headers).body)
result = data['results']

return result
return data['results']
end

def formatService(name)
Expand Down Expand Up @@ -300,6 +276,14 @@ def getHostSeverity(host)
else
severity += 256
end

if (attrs["acknowledgement"] != 0)
severity += 2
elsif (attrs["downtime_depth"] > 0)
severity += 1
else
severity += 4
end
end

return severity
Expand Down Expand Up @@ -358,7 +342,6 @@ def getProblemServices(max_items = 5)

@all_services_data.each do |service|
#puts "Severity for " + service["name"] + ": " + getServiceSeverity(service).to_s

if (service["attrs"]["state"] == 0)
next
end
Expand Down Expand Up @@ -444,6 +427,5 @@ def run

# severity
getProblemServices()

end
end

0 comments on commit e376323

Please sign in to comment.