Updating generate-monitoring to use TrackIPEntries

This commit is contained in:
2025-04-12 02:58:38 -05:00
parent 59b3181d61
commit 81960d92b0
2 changed files with 27 additions and 30 deletions

View File

@@ -3,7 +3,7 @@
# #
# Description: This file generates the DNS and DHCP files for pihole. # Description: This file generates the DNS and DHCP files for pihole.
# #
# Package: AniNIX/Ubiqtorate # Package: AniNIX/Kapisi
# Copyright: WTFPL # Copyright: WTFPL
# #
# Author: DarkFeather <darkfeather@aninix.net> # Author: DarkFeather <darkfeather@aninix.net>
@@ -13,43 +13,38 @@ import subprocess
import sys import sys
import re import re
import yaml import yaml
from kapisi_lib import *
rolepath='../roles/Sharingan/files' rolepath='../roles/Sharingan/files'
monfilepath=rolepath+"/monit/checks/availability" monfilepath=rolepath+"/monit/checks/availability"
def WriteMonitoringEntry(content,hosttype,hostclass): def WriteMonitoringEntry(entryset):
### Create the ping-based monitoring entry ### Create the ping-based monitoring entry
# param content: the yaml content to parse # param entryset: Entries matched from the inventory
# param hosttype: managed or unmanaged
# param hostclass: the type of host as classified in the yaml
global monfile global monfile
with open(monfilepath,'a') as monfile: with open(monfilepath,'a') as monfile:
# Write host entries # Write host entries
for host in content['all']['children'][hosttype]['children'][hostclass]['hosts']: for host in entryset:
try: try:
hostname= host + '.' + content['all']['vars']['replica_domain'] 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('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 ' + entryset[host][2] + ' is not online."\n\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')
except: except:
print(host + ' is not complete for monitoring.') print(host + ' is not complete for monitoring.')
def WriteSSHMonitoringEntry(content,hosttype,hostclass): def WriteSSHMonitoringEntry(entryset):
### Create the ping-based monitoring entry ### Create the ping-based monitoring entry
# param content: the yaml content to parse # param entryset: Entries matched from the inventory
# param hosttype: managed or unmanaged
# param hostclass: the type of host as classified in the yaml
global monfile global monfile
with open(monfilepath,'a') as monfile: with open(monfilepath,'a') as monfile:
# Write host entries # Write host entries
for host in content['all']['children'][hosttype]['children'][hostclass]['hosts']: for host in entryset:
try: try:
hostname= host + '.' + content['all']['vars']['replica_domain'] monfile.write('check program ' + host + '_ssh_mon with path "/usr/lib/monitoring-plugins/check_ssh -H ' + entryset[host][2] + '"\n')
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 ' + host + ' is not responding to SSH."\n\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')
except: except:
print(host + ' is not complete for monitoring.') print(host + ' is not complete for monitoring.')
@@ -62,19 +57,12 @@ def GenerateFiles(file):
os.mkdir(rolepath) os.mkdir(rolepath)
# Parse the yaml # Parse the yaml
with open(file, 'r') as stream: entryset = TrackIPEntries(file,searchstring='all.children.managed.**.ip')
content = yaml.safe_load(stream)
if os.path.isfile(monfilepath): os.remove(monfilepath) if os.path.isfile(monfilepath): os.remove(monfilepath)
# Add DNS entries for each host WriteSSHMonitoringEntry(entryset)
hosttype = 'managed' WriteMonitoringEntry(entryset)
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)
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) != 2: if len(sys.argv) != 2:

View File

@@ -10,11 +10,12 @@ from yamlpath.exceptions import YAMLPathException
def TrackIPEntries(yaml_file,searchstring='all.children.**.ip'): def TrackIPEntries(yaml_file,searchstring='all.children.**.ip'):
### Try to parse an Ansible inventory for hosts with the 'ip' attribute. ### Try to parse an Ansible inventory for hosts with the 'ip' attribute.
# param file: the file to parse # 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/ # Borrowing from upstream author's example at https://pypi.org/project/yamlpath/
entryset = {} entryset = {}
replicadomain = GetReplicaDomain(yaml_file)
# The various classes of this library must be able to write messages somewhere # The various classes of this library must be able to write messages somewhere
# when things go bad. # when things go bad.
@@ -32,7 +33,7 @@ def TrackIPEntries(yaml_file,searchstring='all.children.**.ip'):
yaml_path = YAMLPath(searchstring) yaml_path = YAMLPath(searchstring)
# Create a regex pattern to remove the end of the path # Create a regex pattern to remove the end of the path
ippattern = re.compile('\.ip$') ippattern = re.compile('\\.ip$')
try: try:
for node_coordinate in processor.get_nodes(yaml_path, mustexist=True): for node_coordinate in processor.get_nodes(yaml_path, mustexist=True):
# Strip the path to the host entry. # Strip the path to the host entry.
@@ -54,10 +55,18 @@ def TrackIPEntries(yaml_file,searchstring='all.children.**.ip'):
log.error(ex) log.error(ex)
# Add the host to the entryset. # Add the host to the entryset.
entryset.update({ hostname : [ip,mac] }) entryset.update({ hostname : [ip,mac,hostname+'.'+replicadomain] })
except YAMLPathException as ex: except YAMLPathException as ex:
log.error(ex) log.error(ex)
finally: finally:
return entryset 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']