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

Send state_id over time to have availability #23

Open
wants to merge 9 commits into
base: refactored
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion etc/modules/graphite2.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,16 @@ define module {
#send_warning False
#send_critical False
#send_min False
#send_max False
#send_max False

# Options to send state for hosts or services.
# This feature is useful to have a availability graph
# 1 is to enable - 0 is to disable
# Default all is disable
#-- Enable or disable this feature
state_enable 0
#-- If state_enable is enable, will send state for all hosts
state_host 0
#-- If state_enable is enable, will send state for all services
state_service 0
}
60 changes: 52 additions & 8 deletions module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ def __init__(self, modconf):
logger.info('[Graphite] Configuration - maximum cache commit volume: %d packets', self.cache_commit_volume)
self.cache = deque(maxlen=self.cache_max_length)

# Options to send state
self.state_enable = int(getattr(modconf, 'state_enable', '0'))
logger.info('[Graphite] Configuration - Send state to Graphite: %d ', self.state_enable)
self.state_host = int(getattr(modconf, 'state_host', '0'))
logger.info('[Graphite] Configuration - Send state for hosts to Graphite: %d ', self.state_host)
self.state_service = int(getattr(modconf, 'state_service', '0'))
logger.info('[Graphite] Configuration - Send state for service to Graphite: %d ', self.state_service)

# Used to reset check time into the scheduled time.
# Carbon/graphite does not like latency data and creates blanks in graphs
# Every data with "small" latency will be considered create at scheduled time
Expand Down Expand Up @@ -251,6 +259,8 @@ def manage_initial_host_status_brok(self, b):
# A service check result brok has just arrived ...
def manage_service_check_result_brok(self, b):
host_name = b.data['host_name']
state_id = b.data['state_id']
last_state_id = b.data['last_state_id']
service_description = b.data['service_description']
service_id = host_name+"/"+service_description
logger.debug("[Graphite] service check result: %s", service_id)
Expand All @@ -272,9 +282,9 @@ def manage_service_check_result_brok(self, b):
couples = self.get_metric_and_value(service_description, b.data['perf_data'])

# If no values, we can exit now
if len(couples) == 0:
logger.debug("[Graphite] no metrics to send ...")
return
#if len(couples) == 0:
# logger.debug("[Graphite] no metrics to send ...")
# return

# Custom hosts variables
hname = self.illegal_char_hostname.sub('_', host_name)
Expand Down Expand Up @@ -303,6 +313,23 @@ def manage_service_check_result_brok(self, b):
else:
path = '.'.join((hname, desc))

#Send state to Graphite
state_query = []
state_query.append("%s.available %s %d" % (path, state_id, check_time))
state_packet = '\n'.join(state_query) + '\n'
#logger.error("---SERVICE--- %s", state_packet)
if (self.state_enable == 1 and self.state_service == 1 and state_id != last_state_id):
try:
self.send_packet(state_packet)
#logger.error("[Graphite broker] -------------- Service %s last: %d | now: %d",path, last_state_id ,state_id)
except IOError:
logger.error("[Graphite broker] Failed sending state to the Graphite Carbon.")

if len(couples) == 0:
logger.debug("[Graphite] no metrics to send ...")
return


lines = []
# Send a bulk of all metrics at once
for (metric, value) in couples:
Expand All @@ -313,10 +340,11 @@ def manage_service_check_result_brok(self, b):
self.send_packet(packet)

# A host check result brok has just arrived, we UPDATE data info with this
def manage_host_check_result_brok(self, b):
def manage_host_check_result_brok(self, b):
host_name = b.data['host_name']
logger.debug("[Graphite] host check result: %s", host_name)

state_id = b.data['state_id']
last_state_id = b.data['last_state_id']
# If host initial status brok has not been received, ignore ...
if host_name not in self.hosts_cache:
logger.warning("[Graphite] received service check result for an unknown host: %s", host_name)
Expand All @@ -326,9 +354,9 @@ def manage_host_check_result_brok(self, b):
couples = self.get_metric_and_value('host_check', b.data['perf_data'])

# If no values, we can exit now
if len(couples) == 0:
logger.debug("[Graphite] no metrics to send ...")
return
#if len(couples) == 0:
# logger.debug("[Graphite] no metrics to send ...")
# return

# Custom hosts variables
hname = self.illegal_char_hostname.sub('_', host_name)
Expand All @@ -355,6 +383,22 @@ def manage_host_check_result_brok(self, b):
else:
path = hname

#Send state to Graphite
state_query = []
state_query.append("%s.available %s %d" % (path, state_id, check_time))
state_packet = '\n'.join(state_query) + '\n'
#logger.error("---HOST--- %s", state_packet)

if (self.state_enable == 1 and self.state_host == 1 and state_id != last_state_id):
try:
self.send_packet(state_packet)
#logger.error("---HOST STATE SENDING TO GRAPHITE--- previous_state: %d | new_state: %d",last_state_id, state_id)
except IOError:
logger.error("[Graphite broker] Failed sending state to the Graphite Carbon.")
if len(couples) == 0:
logger.debug("[Graphite] no metrics to send ...")
return

lines = []
# Send a bulk of all metrics at once
for (metric, value) in couples:
Expand Down