def configure_pingstatus(keys, all_metrics, archive_dir, client): """ Puts the pingstatus metric info in its necessary config file Uses the data gathered from configure_metrics.py to insert the hosts to ping. This function also prompts the user for the timeout time and number of pings to send per host. """ _utils.seperator() print("\n=====Configuring the pingstatus metric=====") hosts = [] for key in keys: data = all_metrics[key] host = '.'.join(data['name'].split('.')[1:]) hosts.append(host) questions = [{ 'type': 'input', 'name': 'ping_count', 'message': 'How many times do you want to ping each host' }, { 'type': 'input', 'name': 'timeout', 'message': 'How many seconds to wait before timing out' }] answers = _utils.confirm_choice(questions) ping_count = answers['ping_count'] timeout = answers['timeout'] if client == "POSIX": pingstatus_dir = "{0}/POSIX/NodePingPUSHClient/modules/pingstatus".format( archive_dir) pingstatus_file = join(pingstatus_dir, "variables.sh") hosts = ' '.join(hosts) with open(pingstatus_file, 'w', newline='\n') as f: f.write("ping_hosts=\"%s\"\n" % hosts) f.write("ping_count=%s\n" % ping_count) f.write("timeout=%s\n" % timeout) elif client in ("Python", "Python3"): pingstatus_dir = "{0}/{1}/NodePing{1}PUSH/metrics/pingstatus".format( archive_dir, client) pingstatus_file = join(pingstatus_dir, "config.py") with open(pingstatus_file, 'w', newline='\n') as f: f.write("ping_hosts = %s\n" % str(hosts)) f.write("ping_count = \"%s\"\n" % ping_count) f.write("timeout = \"%s\"\n" % timeout)
def configure_pgsqlstat(archive_dir, client): """ Sets up the client configuration for pgsqlstat metric Asks the user for information to connect to their PostgreSQL database Places the connection information in the config file """ _utils.seperator() print("\n=====Configuring the pgsqlstat metric=====") questions = [{ 'type': 'input', 'name': 'username', 'message': 'What is the database username for authentication' }, { 'type': 'input', 'name': 'querystring', 'message': 'Query to run (empty for default of getting db count)' }] answers = _utils.confirm_choice(questions) username = answers['username'] querystring = answers['querystring'] if client == 'POSIX': pgsqlstat_dir = "{0}/POSIX/NodePingPUSHClient/modules/pgsqlstat".format( archive_dir) vars_file = join(pgsqlstat_dir, "variables.sh") if not querystring: with open(vars_file, 'r', newline='\n') as f: for line in f: if "querystring" in line: querystring = line.split('=')[1] with open(vars_file, 'w', newline='\n') as f: f.write("username=\"%s\"\n" % username) f.write("querystring=\"%s\"" % querystring) else: pass print("\n=====pgsqlstat client configuration complete=====")
def configure_zpool(keys, all_metrics, archive_dir, client): """ """ _utils.seperator() print("\n=====Configuring the zpool metric=====") zpools = [] for key in keys: data = all_metrics[key] zpool = '.'.join(data['name'].split('.')[1:]) zpools.append(zpool) if client == 'POSIX': zpool_dir = "{0}/POSIX/NodePingPUSHClient/modules/zpool".format( archive_dir) zpools_file = join(zpool_dir, "pools.sh") zpools = ' '.join(zpools) with open(zpools_file, 'w', newline='\n') as f: f.write("pools=\"%s\"\n" % zpools)
def configure_smartctl(keys, all_metrics, archive_dir, client): """ """ _utils.seperator() print("\n=====Configuring the smartctl metric=====") devices = [] for key in keys: data = all_metrics[key] dev = '.'.join(data['name'].split('.')[1:]) devices.append(dev) if client == "POSIX": smartctl_dir = "{0}/POSIX/NodePingPUSHClient/modules/smartctl".format( archive_dir) smartctl_file = join(smartctl_dir, "smartdrives.sh") devices = ' '.join(devices) with open(smartctl_file, 'w', newline='\n') as f: f.write("devices=\"%s\"\n" % devices)
def configure_httpcheck(archive_dir, client): """ Configure the client for the httpcheck metric Configure the httpcheck metric in the client code """ _utils.seperator() print("\n=====Configuring the httpcheck metric=====") questions = [{ 'type': 'input', 'name': 'url', 'message': 'The URL you want to check' }, { 'type': 'list', 'name': 'http_method', 'message': 'HTTP Method', 'choices': ['GET', 'POST', 'PUT', 'DELETE'] }, { 'type': 'input', 'name': 'content_type', 'message': 'Content type being sent (if applicable)' }, { 'type': 'input', 'name': 'data', 'message': 'Data for POST/PUT (if applicable)' }] answers = _utils.confirm_choice(questions) url = answers['url'] http_method = answers['http_method'] content_type = answers['content_type'] data = answers['data'] if client == 'POSIX': httpcheck_dir = "{0}/POSIX/NodePingPUSHClient/modules/httpcheck".format( archive_dir) vars_file = join(httpcheck_dir, "variables.sh") with open(vars_file, 'r', newline='\n') as f: filedata = f.read() filedata = filedata.replace('url="https://example.com"', 'url="%s"' % url) filedata = filedata.replace('http_method="GET"', 'http_method="%s"' % http_method) filedata = filedata.replace('content_type="application/json"', 'content_type="%s"' % content_type) filedata = filedata.replace("data=''", 'data=\'%s\'' % data) with open(vars_file, 'w', newline='\n') as f: f.write(filedata) elif client in ("Python", "Python3"): httpcheck_dir = "{0}/{1}/NodePing{1}PUSH/metrics/httpcheck".format( archive_dir, client) httpcheck_file = join(httpcheck_dir, "config.py") with open(httpcheck_file, 'r', newline='\n') as f: filedata = f.read() filedata = filedata.replace('url = ""', 'url = "%s"' % url) filedata = filedata.replace('http_method = "GET"', 'http_method = "%s"' % http_method) filedata = filedata.replace('content_type = ""', 'content_type = "%s"' % content_type) filedata = filedata.replace('data = \'\'', 'data = \'%s\'' % data) with open(httpcheck_file, 'w', newline='\n') as f: f.write(filedata) elif client == 'PowerShell': httpcheck_dir = "{0}/{1}/NodePing{1}PUSH/modules/httpcheck".format( archive_dir, client) httpcheck_file = join(httpcheck_dir, "httpcheck.json") # Clear contents of the httpcheck.json file open(httpcheck_file, 'w').close() with open(httpcheck_file, 'w') as f: f.write(json.dumps(answers, indent=8, sort_keys=True))
def configure_dnslookup(archive_dir, client): """ Sets up the client configuration for dnslookup metric Asks user for the information to perform a DNS query, such as the server to query, query, expected result, and record type """ _utils.seperator() print("\n=====Configuring the dnslookup metric=====") # False if the user is still adding information. This will be true if # the user says the info they input is correct complete = False while not complete: check_questions = [ { 'type': 'input', 'name': 'dns_ip', 'message': 'IP address of the DNS server to query (optional)' }, { 'type': 'input', 'name': 'to_resolve', 'message': 'Hostname or IP to resolve' }, { 'type': 'input', 'name': 'record_type', 'message': 'Record type to query (A, AAAA, CNAME, etc.)' } ] dns_answers = prompt(check_questions) dns_results = [] # Remains true if the user is still adding IPs that are expected # as output from DNS query adding_ips = True while adding_ips: result_questions = [ { 'type': 'input', 'name': 'output', 'message': 'Expected output' }, { 'type': 'confirm', 'name': 'adding_ips', 'message': 'Are there other expected results from the query?' } ] result_answers = prompt(result_questions) dns_results.append(result_answers['output']) adding_ips = result_answers['adding_ips'] print("DNS Server IP: %s" % dns_answers['dns_ip']) print("FQDN/IP to resolve: %s" % dns_answers['to_resolve']) print("Query type: %s" % dns_answers['record_type']) print("Expected results: %s " % str(dns_results)) complete = _utils.inquirer_confirm("Are these IP addresses correct?") if client == 'POSIX': dnslookup_dir = "{0}/POSIX/NodePingPUSHClient/modules/dnslookup".format( archive_dir) vars_file = join(dnslookup_dir, "variables.sh") results = ' '.join(dns_results) with open(vars_file, 'w', newline='\n') as f: f.write("# DNS server IP to query\n") f.write("dns_ip='{0}'\n".format(dns_answers['dns_ip'])) f.write("\n# Hostname to resolve\n") f.write("to_resolve='{0}'\n".format(dns_answers['to_resolve'])) f.write("\n# Expected IP or hostname as output.\n") f.write("expected_output='{0}'\n".format(results)) f.write("record_type='{0}'".format(dns_answers['record_type'])) elif client in ("Python", "Python3"): dnslookup_dir = "{0}/{1}/NodePing{1}PUSH/metrics/dnslookup".format( archive_dir, client) dnslookup_file = join(dnslookup_dir, "config.py") with open(dnslookup_file, 'r', newline='\n') as f: filedata = f.read() filedata = filedata.replace('dns_ip = "64.34.204.155"', 'dns_ip = "%s"' % dns_answers['dns_ip']) filedata = filedata.replace('to_resolve = "nodeping.com"', 'to_resolve = "%s"' % dns_answers['to_resolve']) filedata = filedata.replace('expected_output = "167.114.101.137"', 'expected_output = %s' % str(dns_results)) filedata = filedata.replace('query_type = "A"', 'query_type = "%s"' % dns_answers['record_type']) with open(dnslookup_file, 'w', newline='\n') as f: f.write(filedata)
def configure_mysqlstat(archive_dir, client): """ Sets up the client configuration for mysqlstat metric Asks the user for information to connect to their MySQL database Places the connection information in the config file """ _utils.seperator() print("\n=====Configuring the mysqlstat metric=====") questions = [{ 'type': 'input', 'name': 'username', 'message': 'What is the database username for authentication' }, { 'type': 'password', 'name': 'password', 'message': 'What is the user\'s password? (Will not echo)' }, { 'type': 'input', 'name': 'hostname', 'message': 'What host will you be connecting on (e.g. 127.0.0.1)' }, { 'type': 'input', 'name': 'querystring', 'message': 'Query to run (empty for default of getting db count)' }] answers = _utils.confirm_choice(questions) username = answers['username'] password = answers['password'] host = answers['hostname'] querystring = answers['querystring'] if client == 'POSIX': mysqlstat_dir = "{0}/POSIX/NodePingPUSHClient/modules/mysqlstat".format( archive_dir) vars_file = join(mysqlstat_dir, "variables.sh") if not querystring: with open(vars_file, 'r', newline='\n') as f: for line in f: if "querystring" in line: querystring = line.split('=')[1] with open(vars_file, 'w', newline='\n') as f: f.write("username=\"%s\"\n" % username) f.write("password=\"%s\"\n" % password) f.write("host=\"%s\"\n" % host) f.write("querystring=\"%s\"" % querystring) elif client in ("Python", "Python3"): py_questions = [{ 'type': 'input', 'name': 'unix_socket', 'message': 'Path to the mysql.sock socket (required if not Windows, usually in /tmp)' }] py_answers = prompt(py_questions) unix_socket = py_answers['unix_socket'] mysqlstat_dir = "{0}/{1}/NodePing{1}PUSH/metrics/mysqlstat".format( archive_dir, client) mysqlstat_file = join(mysqlstat_dir, "config.py") with open(mysqlstat_file, 'w', newline='\n') as f: f.write("username = \'%s\'\n" % username) if not host: f.write("host = \'localhost\'\n") else: f.write("host = \'%s\'\n" % host) if not unix_socket: f.write("unix_socket = \'\'\n") else: f.write("unix_socket = \'%s\'\n" % unix_socket) f.write("password = \'%s\'\n" % password) if not querystring: f.write( "querystring = \'SELECT * FROM information_schema.SCHEMATA\'\n" ) else: f.write("querystring = \'%s\'\n" % querystring) f.write("return_db_count = True\n") print("\n=====mysqlstat client configuration complete=====")
def configure_ip_addrs(archive_dir, client): """ Sets up the client configuration for ip_addrs metric Asks the user for IP addresses that they except to be on their system. If any other IP appears, they will be alerted. """ _utils.seperator() print("\n=====Configuring the ip_addrs metric=====") VAR_NAME = 'acceptable_ips' host_ips = [] adding_ips = True complete = False while not complete: while adding_ips: check_questions = [{ 'type': 'input', 'name': 'ip', 'message': 'Allowed IP address' }, { 'type': 'confirm', 'name': 'adding_ips', 'message': 'Add another IP address' }] check_answers = prompt(check_questions) host_ips.append(check_answers['ip']) adding_ips = check_answers['adding_ips'] print(str(host_ips)) complete = _utils.inquirer_confirm("Are these IP addresses correct?") if client == 'POSIX': ip_addrs_dir = "{0}/POSIX/NodePingPUSHClient/modules/ip_addrs".format( archive_dir) vars_file = join(ip_addrs_dir, "variables.sh") ips = ' '.join(host_ips) with open(vars_file, 'w', newline='\n') as f: f.write("{0}='{1}'\n".format(VAR_NAME, ips)) elif client in ("Python", "Python3"): ip_addrs_dir = "{0}/{1}/NodePing{1}PUSH/metrics/ip_addrs".format( archive_dir, client) ip_addrs_file = join(ip_addrs_dir, "config.py") with open(ip_addrs_file, 'r', newline='\n') as f: filedata = f.read() filedata = filedata.replace('acceptable_addrs = ["192.168.0.16"]', 'acceptable_addrs = %s' % host_ips) with open(ip_addrs_file, 'w', newline='\n') as f: f.write(filedata) elif client == 'PowerShell': ip_addrs_dir = "{0}/{1}/NodePing{1}PUSH/modules/ip_addrs".format( archive_dir, client) ip_addrs_file = join(ip_addrs_dir, "ip_addrs.json") # Clear contents of the ip_addrs.json file open(ip_addrs_file, 'w').close() with open(ip_addrs_file, 'w') as f: f.write(json.dumps(host_ips, indent=8, sort_keys=True))
def configure_mongodbstat(archive_dir, client): """ Sets up the client configuration for mongodbstat metric Asks the user for information such as the comand to evaluate, expected output (which would be a substring), optional authentication, and optionally the path to the mongo executable """ _utils.seperator() print("\n=====Configuring the mongodbstat metric=====") # False if the user is still adding information. This will be true if # the user says the info they input is correct complete = False while not complete: check_questions = [{ 'type': 'input', 'name': 'eval_string', 'message': 'Command for mongo to evaluate', 'default': 'db.runCommand( {connectionStatus: 1});' }, { 'type': 'input', 'name': 'expected_output', 'message': 'Expected output or substring', 'default': '"ok" : 1' }, { 'type': 'input', 'name': 'username', 'message': 'Username for authentication to database (skip if none)', 'default': '' }, { 'type': 'password', 'name': 'password', 'message': 'Password for authentication to the database', 'default': '', 'when': lambda answers: answers['username'] }, { 'type': 'input', 'name': 'mongo_path', 'message': 'What is the path to the mongo executable', 'default': 'mongo' }] answers = prompt(check_questions) if not answers['username']: username = None else: username = answers['username'] try: password = answers['password'] except KeyError: password = None answers.update({"password": ""}) print("Evaluated command: %s" % answers['eval_string']) print("Expected response in output: %s" % answers['expected_output']) if answers['username']: print("Username: %s" % answers['username']) print("Password: ****") print("Mongo path: %s " % answers['mongo_path']) complete = _utils.inquirer_confirm("Is this information correct?") if client != "PowerShell": answers['expected_output'] = answers['expected_output'].replace( '"', r'\"') if client == 'POSIX': mongodbstat_dir = "{0}/POSIX/NodePingPUSHClient/modules/mongodbstat".format( archive_dir) vars_file = join(mongodbstat_dir, "variables.sh") if not username: username = "" if not password: password = "" with open(vars_file, 'w', newline='\n') as f: f.write( "# This string will be evaluated by mongo with the --eval flag\n" ) f.write("eval_string=\"{0}\"\n".format(answers['eval_string'])) f.write( "# All of or a portion of what you expect in the result to be successful\n" ) f.write("expected_output=\"{0}\"\n".format( answers['expected_output'])) f.write("# Optional user authentication\n") f.write("username=\"{0}\"\n".format(username)) f.write("# Optional password authentication\n") f.write("password=\"{0}\"\n".format(password)) elif client in ("Python", "Python3"): mongodbstat_dir = "{0}/{1}/NodePing{1}PUSH/metrics/mongodbstat".format( archive_dir, client) mongodbstat_file = join(mongodbstat_dir, "config.py") with open(mongodbstat_file, 'r', newline='\n') as f: filedata = f.read() if isinstance(username, str): username = '******' % username if isinstance(password, str): password = '******' % password filedata = filedata.replace( 'eval_string = "db.runCommand( {connectionStatus: 1});"', 'eval_string = "%s"' % answers['eval_string']) filedata = filedata.replace( 'expected_output = "\"ok\" : 1"', 'expected_output = "%s"' % answers['expected_output']) filedata = filedata.replace('username = None', 'username = %s' % username) filedata = filedata.replace('password = None', 'password = %s' % password) filedata = filedata.replace( 'mongo_path = r"mongo"', 'mongo_path = r"%s"' % answers['mongo_path']) with open(mongodbstat_file, 'w', newline='\n') as f: f.write(filedata) elif client == 'PowerShell': mongodbstat_dir = "{0}/{1}/NodePing{1}PUSH/modules/mongodbstat".format( archive_dir, client) mongodbstat_file = join(mongodbstat_dir, "mongodbstat.json") # Clear contents of the mongodbstat.json file open(mongodbstat_file, 'w').close() with open(mongodbstat_file, 'w') as f: f.write(json.dumps(answers, indent=8, sort_keys=True))
def configure_redismaster(archive_dir, client): """ Sets up the client configuration for redismaster metric Asks the user for information to connect to their Redis database via a Redis Sentinel. Places the connection information in the config file """ _utils.seperator() print("\n=====Configuring the redismaster metric=====") questions = [{ 'type': 'input', 'name': 'redis_master', 'message': 'What is the master-group-name that is monitored' }, { 'type': 'input', 'name': 'redis_master_ip', 'message': 'What is the IP address of the Redis master server' }, { 'type': 'input', 'name': 'sentinel_ip', 'message': 'What is the IP address of the sentinel to connect to' }, { 'type': 'input', 'name': 'port', 'message': 'What is the IP address of the sentinel port' }] answers = _utils.confirm_choice(questions) redis_master = answers['redis_master'] redis_master_ip = answers['redis_master_ip'] sentinel_ip = answers['sentinel_ip'] port = answers['port'] if client == 'POSIX': redismaster_dir = "{0}/POSIX/NodePingPUSHClient/modules/redismaster".format( archive_dir) vars_file = join(redismaster_dir, "variables.sh") with open(vars_file, 'r', newline='\n') as f: filedata = f.read() filedata = filedata.replace('redis_master=""', 'redis_master="%s"' % redis_master) filedata = filedata.replace('redis_master_ip=""', 'redis_master_ip="%s"' % redis_master_ip) filedata = filedata.replace('sentinel_ip=""', 'sentinel_ip="%s"' % sentinel_ip) filedata = filedata.replace('port=""', 'port="%s"' % port) with open(vars_file, 'w', newline='\n') as f: f.write(filedata) elif client == 'Python' or client == 'Python3': redismaster_dir = "{0}/{1}/NodePing{1}PUSH/metrics/redismaster".format( archive_dir, client) redismaster_file = join(redismaster_dir, "config.py") with open(redismaster_file, 'r', newline='\n') as f: filedata = f.read() filedata = filedata.replace('REDIS_MASTER = ""', 'REDIS_MASTER = "%s"' % redis_master) filedata = filedata.replace('REDIS_MASTER_IP = ""', 'REDIS_MASTER_IP = "%s"' % redis_master_ip) filedata = filedata.replace('SENTINEL_IP = ""', 'SENTINEL_IP = "%s"' % sentinel_ip) filedata = filedata.replace('PORT = ""', 'PORT = "%s"' % port) with open(redismaster_file, 'w', newline='\n') as f: f.write(filedata)