I've written a python script dependency tracker (below) which is really just a fancy search tool. It's designed to run on the Splunk server and run from the directory from where you want to track. It recurses from the current folder looking for a search term in a .conf file. If it finds it it will return the filename and the KO name in which the search term occurs. """
Script which searches from local directory and finds KOs which include a search term
Filters searches files by .conf extension. Starts searching from present working directory
Can be run from any linux host with Splunk config files.
It works by scanning .conf files from pwd. For each file it gets a list of all line numbers with a match. It then
gets a list of all KOs (using pattern match: [some ko]). For each search match it will get the KO match at or above
(i.e. lower than) the search match line number. It will then return a list of files and KOs.
Was designed to be used by python 3.7.1-1
usage:
#ssh to Splunk host
cd $SPLUNK_HOME/etc/apps #apps only
cd $SPLUNK_HOME/etc/ #apps and user config
/path/to/python3.7.x /path/to/dependency_tracker.py --search_term whatever
"""
import argparse
import sys
import os
import glob
import re
def get_parser():
"""
Build an Argument Parser.
Returns:
parser: An argument parser
"""
parser = argparse.ArgumentParser()
parser.add_argument("--search_term", help="The name of the KO which is the dependecny", required=True)
return parser
def get_matching_files(search_term):
#Use glob to recursively search for .conf files from pwd
pwd = os.getcwd()
search_path = '{}/**/*.conf'.format(pwd)
regex_searchterm = re.compile(search_term)
regex_ko = re.compile("\[(.*)\]")
matches = []
for filepath in glob.iglob(search_path, recursive=True):
matching_lines = []
file_kos = []
for i, line in enumerate(open(filepath)):
searchterm_matches = re.findall(regex_searchterm, line)
if searchterm_matches != []:
matching_lines.append(i)
ko_matches = re.match(regex_ko, line)
if ko_matches:
kos = {"ko_name" : ko_matches.group(1), "ko_line" : i}
file_kos.append(kos)
if matching_lines != []:
matching_kos_in_file = []
for matching_line in matching_lines:
for i in range(len(file_kos)):
is_match = False
if i == len(file_kos)-1:
is_match = True
else:
next_ko_line = file_kos[i+1]["ko_line"]
if next_ko_line >= matching_line:
is_match = True
if is_match:
matching_ko = file_kos[i]["ko_name"]
if matching_ko not in matching_kos_in_file:
matching_kos_in_file.append(matching_ko)
file_match = {"filepath": filepath, "kos": matching_kos_in_file}
matches.append(file_match)
return matches
def main(args=None):
parser = get_parser()
args = parser.parse_args()
matching_files = get_matching_files(search_term=args.search_term)
print("Searching for {}".format(args.search_term))
for matching_file in matching_files:
print("File {} contains following KOs:".format(matching_file["filepath"]))
for matching_ko in matching_file["kos"]:
print(matching_ko)
if __name__ == "__main__":
main()
... View more