From 81960d92b05088d14070695dc83b673665143e7c Mon Sep 17 00:00:00 2001 From: DarkFeather Date: Sat, 12 Apr 2025 02:58:38 -0500 Subject: [PATCH] Updating generate-monitoring to use TrackIPEntries --- bin/generate-monitoring.py | 42 ++++++++++++++------------------------ bin/kapisi_lib.py | 15 +++++++++++--- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/bin/generate-monitoring.py b/bin/generate-monitoring.py index c41f58e..6a13f81 100755 --- a/bin/generate-monitoring.py +++ b/bin/generate-monitoring.py @@ -3,7 +3,7 @@ # # Description: This file generates the DNS and DHCP files for pihole. # -# Package: AniNIX/Ubiqtorate +# Package: AniNIX/Kapisi # Copyright: WTFPL # # Author: DarkFeather @@ -13,43 +13,38 @@ import subprocess import sys import re import yaml +from kapisi_lib import * rolepath='../roles/Sharingan/files' monfilepath=rolepath+"/monit/checks/availability" -def WriteMonitoringEntry(content,hosttype,hostclass): +def WriteMonitoringEntry(entryset): ### Create the ping-based monitoring entry - # param content: the yaml content to parse - # param hosttype: managed or unmanaged - # param hostclass: the type of host as classified in the yaml + # param entryset: Entries matched from the inventory global monfile with open(monfilepath,'a') as monfile: # Write host entries - for host in content['all']['children'][hosttype]['children'][hostclass]['hosts']: + for host in entryset: try: - hostname= host + '.' + content['all']['vars']['replica_domain'] - monfile.write('check program ' + host + '_ping_mon with path "/usr/lib/monitoring-plugins/check_ping -H ' + hostname + ' -w 100,50% -c 1000,100% -p 3 -t 60 -4"\n') - monfile.write(' if status != 0 for 3 times within 5 cycles then exec "/etc/monit.d/scripts/critical ' + hostname + ' is not online."\n\n') + monfile.write('check program ' + host + '_ping_mon with path "/usr/lib/monitoring-plugins/check_ping -H ' + entryset[host][2] + ' -w 100,50% -c 1000,100% -p 3 -t 60 -4"\n') + monfile.write(' if status != 0 for 3 times within 5 cycles then exec "/etc/monit.d/scripts/critical ' + entryset[host][2] + ' is not online."\n\n') except: print(host + ' is not complete for monitoring.') -def WriteSSHMonitoringEntry(content,hosttype,hostclass): +def WriteSSHMonitoringEntry(entryset): ### Create the ping-based monitoring entry - # param content: the yaml content to parse - # param hosttype: managed or unmanaged - # param hostclass: the type of host as classified in the yaml + # param entryset: Entries matched from the inventory global monfile with open(monfilepath,'a') as monfile: # Write host entries - for host in content['all']['children'][hosttype]['children'][hostclass]['hosts']: + for host in entryset: try: - hostname= host + '.' + content['all']['vars']['replica_domain'] - monfile.write('check program ' + host + '_ssh_mon with path "/usr/lib/monitoring-plugins/check_ssh -H ' + hostname + '"\n') - monfile.write(' if status != 0 for 3 times within 5 cycles then exec "/etc/monit.d/scripts/critical ' + hostname + ' is not responding to SSH."\n\n') + monfile.write('check program ' + host + '_ssh_mon with path "/usr/lib/monitoring-plugins/check_ssh -H ' + entryset[host][2] + '"\n') + monfile.write(' if status != 0 for 3 times within 5 cycles then exec "/etc/monit.d/scripts/critical ' + host + ' is not responding to SSH."\n\n') except: print(host + ' is not complete for monitoring.') @@ -62,19 +57,12 @@ def GenerateFiles(file): os.mkdir(rolepath) # Parse the yaml - with open(file, 'r') as stream: - content = yaml.safe_load(stream) + entryset = TrackIPEntries(file,searchstring='all.children.managed.**.ip') if os.path.isfile(monfilepath): os.remove(monfilepath) - # Add DNS entries for each host - hosttype = 'managed' - for hostclass in ['physical','virtual','geth_hubs']: - #WriteMonitoringEntry(content,hosttype,hostclass) - WriteSSHMonitoringEntry(content,hosttype,hostclass) - hosttype = 'unmanaged' - for hostclass in ['ovas','appliances']: - WriteMonitoringEntry(content,hosttype,hostclass) + WriteSSHMonitoringEntry(entryset) + WriteMonitoringEntry(entryset) if __name__ == '__main__': if len(sys.argv) != 2: diff --git a/bin/kapisi_lib.py b/bin/kapisi_lib.py index 30ae6b9..f8a1d30 100644 --- a/bin/kapisi_lib.py +++ b/bin/kapisi_lib.py @@ -10,11 +10,12 @@ from yamlpath.exceptions import YAMLPathException def TrackIPEntries(yaml_file,searchstring='all.children.**.ip'): ### Try to parse an Ansible inventory for hosts with the 'ip' attribute. # param file: the file to parse - # return: a populated entry set in form [{Host,[ip,mac]},...] + # return: a populated entry set in form [{Host,[ip,mac,fqdn]},...] # Borrowing from upstream author's example at https://pypi.org/project/yamlpath/ entryset = {} + replicadomain = GetReplicaDomain(yaml_file) # The various classes of this library must be able to write messages somewhere # when things go bad. @@ -32,7 +33,7 @@ def TrackIPEntries(yaml_file,searchstring='all.children.**.ip'): yaml_path = YAMLPath(searchstring) # Create a regex pattern to remove the end of the path - ippattern = re.compile('\.ip$') + ippattern = re.compile('\\.ip$') try: for node_coordinate in processor.get_nodes(yaml_path, mustexist=True): # Strip the path to the host entry. @@ -54,10 +55,18 @@ def TrackIPEntries(yaml_file,searchstring='all.children.**.ip'): log.error(ex) # Add the host to the entryset. - entryset.update({ hostname : [ip,mac] }) + entryset.update({ hostname : [ip,mac,hostname+'.'+replicadomain] }) except YAMLPathException as ex: log.error(ex) finally: return entryset + +def GetReplicaDomain(file): + ''' + Return the defined replica domain + ''' + with open(file, 'r') as stream: + content = yaml.safe_load(stream) + return content['all']['vars']['replica_domain']