import sys
import subprocess
import re


if __name__ == '__main__':
    try:
        file_name = sys.argv[1]

        # call nagios with the given config file and parse output
        output = subprocess.check_output("nagios3 -v " + file_name, shell=True).decode()
        if output:
            # check for total errors expression
            regex = re.compile(r"^.*Total\s*Errors:\s*(\d+).*$", re.VERBOSE | re.IGNORECASE | re.MULTILINE)
            r = regex.search(output)
            if r:
                errors = r.group(1)
                print(errors)
                exit()
        print("ERROR: THERE WAS A PROBLEM PARSING ERRORS")
    except Exception as e:
        print("ERROR: CANNOT FIND CONFIG FILE NAME")


