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)
Exemplo n.º 2
0
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=====")
Exemplo n.º 3
0
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))
Exemplo n.º 4
0
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_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)