Updating generate-monitoring to use TrackIPEntries
This commit is contained in:
@@ -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 <darkfeather@aninix.net>
|
||||
@@ -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:
|
||||
|
@@ -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']
|
||||
|
Reference in New Issue
Block a user