From 2abb164233f912cf491b129b17afda9534736272 Mon Sep 17 00:00:00 2001 From: Azef1 Date: Mon, 11 Jul 2016 16:08:32 +0200 Subject: [PATCH 1/9] Add send state_id --- module/module.py | 50 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/module/module.py b/module/module.py index c0c0d7c..3add7d4 100644 --- a/module/module.py +++ b/module/module.py @@ -251,7 +251,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'] - service_description = b.data['service_description'] + state_id = b.data['state_id'] + service_description = b.data['service_description'] service_id = host_name+"/"+service_description logger.debug("[Graphite] service check result: %s", service_id) @@ -272,9 +273,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) @@ -302,6 +303,22 @@ def manage_service_check_result_brok(self, b): path = '.'.join((hname, self.graphite_data_source, desc)) 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) + try: + self.send_packet(state_packet) + except IOError: + logger.error("[Graphite broker] Failed sendind state o the Graphite Carbon.") + + if len(couples) == 0: + logger.debug("[Graphite] no metrics to send ...") + return + lines = [] # Send a bulk of all metrics at once @@ -316,7 +333,8 @@ def manage_service_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'] # 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) @@ -326,9 +344,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) @@ -355,6 +373,20 @@ 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) + try: + self.send_packet(state_packet) + except IOError: + logger.error("[Graphite broker] Failed sendind state o 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: @@ -371,4 +403,4 @@ def main(self): l = self.to_q.get() for b in l: b.prepare() - self.manage_brok(b) + self.manage_brok(b) From 4b6a18b2b1ed729c3e5999f604bd0729159b98fd Mon Sep 17 00:00:00 2001 From: Azef1 Date: Mon, 11 Jul 2016 16:26:47 +0200 Subject: [PATCH 2/9] syntax correction --- module/module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/module.py b/module/module.py index 3add7d4..68554e6 100644 --- a/module/module.py +++ b/module/module.py @@ -403,4 +403,4 @@ def main(self): l = self.to_q.get() for b in l: b.prepare() - self.manage_brok(b) + self.manage_brok(b) From 886a6ec00e087d517382aee0a47a3c01e8307e39 Mon Sep 17 00:00:00 2001 From: Azef1 Date: Mon, 11 Jul 2016 16:30:44 +0200 Subject: [PATCH 3/9] syntax --- module/module.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/module/module.py b/module/module.py index 68554e6..44b96e7 100644 --- a/module/module.py +++ b/module/module.py @@ -252,7 +252,7 @@ def manage_initial_host_status_brok(self, b): def manage_service_check_result_brok(self, b): host_name = b.data['host_name'] state_id = b.data['state_id'] - service_description = b.data['service_description'] + service_description = b.data['service_description'] service_id = host_name+"/"+service_description logger.debug("[Graphite] service check result: %s", service_id) @@ -303,7 +303,6 @@ def manage_service_check_result_brok(self, b): path = '.'.join((hname, self.graphite_data_source, desc)) else: path = '.'.join((hname, desc)) - #Send state to Graphite state_query = [] @@ -333,8 +332,7 @@ def manage_service_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'] + state_id = b.data['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) @@ -373,7 +371,7 @@ def manage_host_check_result_brok(self, b): else: path = hname - #Send state to Graphite + #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' @@ -383,7 +381,7 @@ def manage_host_check_result_brok(self, b): except IOError: logger.error("[Graphite broker] Failed sendind state o the Graphite Carbon.") - if len(couples) == 0: + if len(couples) == 0: logger.debug("[Graphite] no metrics to send ...") return @@ -403,4 +401,4 @@ def main(self): l = self.to_q.get() for b in l: b.prepare() - self.manage_brok(b) + self.manage_brok(b) From 58756abfc3d70f860653df66af30c12b83be69a5 Mon Sep 17 00:00:00 2001 From: Azef1 Date: Tue, 12 Jul 2016 12:20:54 +0200 Subject: [PATCH 4/9] Add options to enable\disable sending state In graphite2.cfg, options are: state_enable 1 #1=enable send state / 0=disable state_host 1 #1=enable send state for host / 0=disable state_service 1 #1=enable send state for service / 0=disable --- module/module.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/module/module.py b/module/module.py index 44b96e7..5e704bb 100644 --- a/module/module.py +++ b/module/module.py @@ -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 @@ -303,16 +311,17 @@ def manage_service_check_result_brok(self, b): path = '.'.join((hname, self.graphite_data_source, desc)) 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) - try: - self.send_packet(state_packet) - except IOError: - logger.error("[Graphite broker] Failed sendind state o the Graphite Carbon.") + if (self.state_enable == 1 and self.state_service == 1): + try: + self.send_packet(state_packet) + except IOError: + logger.error("[Graphite broker] Failed sending state to the Graphite Carbon.") if len(couples) == 0: logger.debug("[Graphite] no metrics to send ...") @@ -376,11 +385,11 @@ def manage_host_check_result_brok(self, b): 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) - try: - self.send_packet(state_packet) - except IOError: - logger.error("[Graphite broker] Failed sendind state o the Graphite Carbon.") - + if (self.state_enable == 1 and self.state_host == 1): + try: + self.send_packet(state_packet) + 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 From 244c74294998ae82024e43b21f04f927b27f96f0 Mon Sep 17 00:00:00 2001 From: Azef1 Date: Tue, 12 Jul 2016 12:27:58 +0200 Subject: [PATCH 5/9] Add optons for state --- etc/modules/graphite2.cfg | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/etc/modules/graphite2.cfg b/etc/modules/graphite2.cfg index 6bd0a6e..7fad6be 100644 --- a/etc/modules/graphite2.cfg +++ b/etc/modules/graphite2.cfg @@ -73,5 +73,12 @@ 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 + state_enable 1 # Enable or disable this feature + state_host 1 # If state_enable is enable, will send state for host + state_service 1 # If state_enable is enable, will send state for service } From 91859da46375092855ab1a3341b8a62a3c269d87 Mon Sep 17 00:00:00 2001 From: Azef1 Date: Tue, 12 Jul 2016 14:15:33 +0200 Subject: [PATCH 6/9] Default sending state is disable --- etc/modules/graphite2.cfg | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/etc/modules/graphite2.cfg b/etc/modules/graphite2.cfg index 7fad6be..ee12bff 100644 --- a/etc/modules/graphite2.cfg +++ b/etc/modules/graphite2.cfg @@ -78,7 +78,7 @@ define module { # 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 - state_enable 1 # Enable or disable this feature - state_host 1 # If state_enable is enable, will send state for host - state_service 1 # If state_enable is enable, will send state for service + state_enable 0 # Enable or disable this feature + state_host 0 # If state_enable is enable, will send state for host + state_service 0 # If state_enable is enable, will send state for service } From 92e2d1eb25a82b62d404b90a4372d68ee871d447 Mon Sep 17 00:00:00 2001 From: Azef1 Date: Tue, 12 Jul 2016 14:57:15 +0200 Subject: [PATCH 7/9] Send state only last_state_id != state_id --- module/module.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/module/module.py b/module/module.py index 5e704bb..cb9187b 100644 --- a/module/module.py +++ b/module/module.py @@ -260,6 +260,7 @@ def manage_initial_host_status_brok(self, b): 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) @@ -317,10 +318,11 @@ def manage_service_check_result_brok(self, b): 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): + if (self.state_enable == 1 and self.state_service == 1 and state_id != last_state_id): try: self.send_packet(state_packet) - except IOError: + #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: @@ -338,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): - host_name = b.data['host_name'] + 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) @@ -385,9 +388,11 @@ def manage_host_check_result_brok(self, b): 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): + + 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: From ec2dc203d0865f3c814fee57ec032fc330392d31 Mon Sep 17 00:00:00 2001 From: Azef1 Date: Tue, 12 Jul 2016 15:00:42 +0200 Subject: [PATCH 8/9] syntax correction --- module/module.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/module/module.py b/module/module.py index cb9187b..ee30931 100644 --- a/module/module.py +++ b/module/module.py @@ -82,14 +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')) + # 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')) + 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 @@ -260,7 +260,7 @@ def manage_initial_host_status_brok(self, b): 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'] + 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) @@ -312,18 +312,18 @@ def manage_service_check_result_brok(self, b): path = '.'.join((hname, self.graphite_data_source, desc)) 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.") + 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 ...") @@ -341,10 +341,10 @@ def manage_service_check_result_brok(self, b): # A host check result brok has just arrived, we UPDATE data info with this def manage_host_check_result_brok(self, b): - host_name = b.data['host_name'] + 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'] + 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) @@ -388,13 +388,13 @@ def manage_host_check_result_brok(self, b): 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.") + 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 From ce52e2836f208a472a93cbed415b25740c0b3104 Mon Sep 17 00:00:00 2001 From: Azef1 Date: Tue, 12 Jul 2016 16:00:07 +0200 Subject: [PATCH 9/9] More details in graphite2.cfg --- etc/modules/graphite2.cfg | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/etc/modules/graphite2.cfg b/etc/modules/graphite2.cfg index ee12bff..66c1d30 100644 --- a/etc/modules/graphite2.cfg +++ b/etc/modules/graphite2.cfg @@ -77,8 +77,12 @@ define module { # 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 - state_enable 0 # Enable or disable this feature - state_host 0 # If state_enable is enable, will send state for host - state_service 0 # If state_enable is enable, will send state for service + # 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 }