コード例 #1
0
ファイル: gui.py プロジェクト: jyogi/purvar-agent
def windows_flare():
    case_id, ok = QInputDialog.getInteger(
        None, "Flare",
        "Your logs and configuration files are going to be collected and "
        "sent to Datadog Support. Please enter your ticket number if you have one:",
        value=0, min=0
    )
    if not ok:
        info_popup("Flare cancelled")
        return
    case_id = int(case_id) if case_id != 0 else None
    f = Flare(case_id=case_id)
    f.collect()
    email, ok = QInputDialog.getText(
        None, "Your email",
        "Logs and configuration files have been collected."
        " Please enter your email address:"
    )
    if not ok:
        info_popup("Flare cancelled. You can still use {0}".format(f.tar_path))
        return
    try:
        case_id = f.upload(email=str(email))
        info_popup("Your logs were successfully uploaded. For future reference,"
                   " your internal case id is {0}".format(case_id))
    except Exception as e:
        warning_popup('The upload failed. Please send the following file by email'
                      ' to support: {0}\n\n{1}'.format(f.tar_path, str(e)))
    finally:
        return
コード例 #2
0
 def test_uri_password(self, mock_config, mock_tempdir, mock_strftime):
     f = Flare()
     _, password_found = f._strip_password(os.path.join(get_mocked_temp(), mock_cfgs['uri_password']))
     self.assertEqual(
         password_found,
         " - this file contains a password in a uri which has been removed in the version collected"
     )
コード例 #3
0
 def test_uri_password(self, mock_config, mock_tempdir, mock_strftime):
     f = Flare()
     _, password_found = f._strip_password(os.path.join(get_mocked_temp(), mock_cfgs['uri_password']))
     self.assertEqual(
         password_found,
         " - this file contains a password in a uri which has been removed in the version collected"
     )
コード例 #4
0
ファイル: gui.py プロジェクト: dadicool/dd-agent
def windows_flare():
    case_id, ok = QInputDialog.getInteger(
        None, "Flare",
        "Your logs and configuration files are going to be collected and "
        "sent to Datadog Support. Please enter your ticket number if you have one:",
        value=0, min=0
    )
    if not ok:
        info_popup("Flare cancelled")
        return
    case_id = int(case_id) if case_id != 0 else None
    f = Flare(case_id=case_id)
    f.collect()
    email, ok = QInputDialog.getText(
        None, "Your email",
        "Logs and configuration files have been collected."
        " Please enter your email address:"
    )
    if not ok:
        info_popup("Flare cancelled. You can still use {0}".format(f.tar_path))
        return
    try:
        case_id = f.upload(email=str(email))
        info_popup("Your logs were successfully uploaded. For future reference,"
                   " your internal case id is {0}".format(case_id))
    except Exception, e:
        warning_popup('The upload failed. Please send the following file by email'
                      ' to support: {0}\n\n{1}'.format(f.tar_path, str(e)))
コード例 #5
0
def test_flare_basic(zip_contents, requests_ok, requests_ok_no_case):
    my_flare = Flare(paths=[zip_contents])

    expected_flare_path = "datadog-agent-{}.zip".format(
        datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S"))

    assert os.path.basename(my_flare.get_archive_path()) == expected_flare_path

    flare_path = my_flare.create_archive()
    assert os.path.exists(flare_path)
    assert zipfile.is_zipfile(flare_path)

    with zipfile.ZipFile(flare_path, 'r') as flare_zip:
        archive_contents = flare_zip.infolist()
        for content in archive_contents:
            assert os.path.basename(content.filename) in CONTENTS
            assert content.compress_type == zipfile.ZIP_DEFLATED

    with mock.patch('requests.post', return_value=requests_ok):
        success, case = my_flare.submit()
        assert success
        assert case == CASE_NO

    with mock.patch('requests.post', return_value=requests_ok_no_case):
        success, case = my_flare.submit()
        assert success
        assert case is None

    my_flare.cleanup()
    assert not os.path.exists(flare_path)
コード例 #6
0
ファイル: test_flare.py プロジェクト: thomgw/dd-agent
 def test_uri_set_proxy(self, mock_config, mock_tempdir, mock_strftime,
                        mock_os_remove):
     f = Flare()
     request_options = {}
     f.set_proxy(request_options)
     expected = 'http://*****:*****@proxy.host.com:3128'
     self.assertEqual(expected, request_options['proxies']['https'])
コード例 #7
0
 def test_endpoint(self, mock_config, mock_temp, mock_stfrtime):
     f = Flare()
     f._ask_for_email = lambda: None
     try:
         f.upload()
         raise Exception('Should fail before')
     except Exception, e:
         self.assertEqual(str(e), "Your request is incorrect: Invalid inputs: 'API key unknown'")
コード例 #8
0
ファイル: test_flare.py プロジェクト: AquaBindi/dd-agent
 def test_endpoint(self, mock_config, mock_temp, mock_stfrtime):
     f = Flare()
     f._ask_for_email = lambda: None
     try:
         f.upload(confirmation=False)
         raise Exception('Should fail before')
     except Exception, e:
         self.assertEqual(str(e), "Your request is incorrect: Invalid inputs: 'API key unknown'")
コード例 #9
0
 def test_uri_password(self, mock_config, mock_tempdir, mock_strftime):
     f = Flare()
     _, credentials_log = f._strip_credentials(
         os.path.join(get_mocked_temp(), mock_cfgs['uri_password']),
         f.CHECK_CREDENTIALS)
     self.assertEqual(
         credentials_log,
         " - this file contains a credential (password in a uri) which has been removed in the collected version"
     )
コード例 #10
0
    def test_proxy_user_pass_regex(self, mock_config, mock_tempdir,
                                   mock_strftime, mock_os_remove):
        f = Flare()
        file_path, _ = f._strip_credentials(
            os.path.join(get_mocked_temp(), 'proxy.conf'), f.MAIN_CREDENTIALS)
        with open(file_path) as f:
            contents = f.read()

        self.assertEqual(contents, "proxy_user: ********\n"
                         "proxy_password: ********\n")
コード例 #11
0
ファイル: test_flare.py プロジェクト: SupersonicAds/dd-agent
 def test_uri_password(self, mock_config, mock_tempdir, mock_strftime):
     f = Flare()
     _, credentials_log = f._strip_credentials(
         os.path.join(get_mocked_temp(), mock_cfgs['uri_password']),
         f.CHECK_CREDENTIALS
     )
     self.assertEqual(
         credentials_log,
         " - this file contains a credential (password in a uri) which has been removed in the collected version"
     )
コード例 #12
0
def test_flare_too_large(zip_contents):
    my_flare = Flare(paths=[zip_contents])
    my_flare.MAX_UPLOAD_SIZE = 1
    my_flare.create_archive()

    assert not my_flare._validate_size()
    with mock.patch('requests.post', return_value=requests_ok):
        assert not my_flare.submit()

    my_flare.cleanup()
    assert not os.path.exists(my_flare.get_archive_path())
コード例 #13
0
 def test_password_regex(self, mock_config, mock_tempdir, mock_strftime,
                         mock_os_remove):
     f = Flare()
     file_path, credentials_log = f._strip_credentials(
         os.path.join(get_mocked_temp(), 'password.yaml'),
         f.CHECK_CREDENTIALS)
     with open(file_path) as f:
         contents = f.read()
     self.assertEqual(
         contents, "instances:\n"
         "  - pass: ********\n"
         "    other_password: ********\n")
コード例 #14
0
ファイル: test_flare.py プロジェクト: wjsl/dd-agent
    def test_api_keys_regex(self, mock_config, mock_tempdir, mock_strftime):
        f = Flare()
        file_path, _ = f._strip_credentials(
            os.path.join(get_mocked_temp(), 'apikeys.conf'),
            f.MAIN_CREDENTIALS)
        with open(file_path) as f:
            contents = f.read()

        self.assertEqual(
            contents, """api_key: *************************aaaaa
other_api_keys: **************************bbbbb, **************************ccccc, **************************dddd

""")
コード例 #15
0
ファイル: test_flare.py プロジェクト: thomgw/dd-agent
    def test_uri_verify_ssl_default(self, mock_config, mock_tempdir,
                                    mock_strftime, mock_os_remove):
        f = Flare()
        request_options = {}
        f.set_ssl_validation(request_options)

        expected_verify = None
        if Platform.is_windows():
            expected_verify = os.path.realpath(
                os.path.join(os.path.dirname(os.path.realpath(__file__)),
                             os.pardir, os.pardir, 'datadog-cert.pem'))

        self.assertEquals(request_options.get('verify'), expected_verify)
コード例 #16
0
ファイル: test_flare.py プロジェクト: SupersonicAds/dd-agent
    def test_uri_verify_ssl_default(self, mock_config, mock_tempdir, mock_strftime):
        f = Flare()
        request_options = {}
        f.set_ssl_validation(request_options)

        expected_verify = None
        if Platform.is_windows():
            expected_verify = os.path.realpath(os.path.join(
                os.path.dirname(os.path.realpath(__file__)),
                os.pardir, os.pardir,
                'datadog-cert.pem'
            ))

        self.assertEquals(request_options.get('verify'), expected_verify)
コード例 #17
0
ファイル: test_flare.py プロジェクト: alanbover/dd-agent
    def test_whitespace_proxy_user_pass_regex(self, mock_config, mock_tempdir, mock_strftime, mock_os_remove):
        f = Flare()
        file_path, _ = f._strip_credentials(
            os.path.join(get_mocked_temp(), 'whitespace_proxy.conf'),
            f.MAIN_CREDENTIALS
        )
        with open(file_path) as f:
            contents = f.read()

        self.assertEqual(
            contents,
            "proxy_user: ********\n"
            "proxy_password: ********\n"
        )
コード例 #18
0
def test_flare_400(zip_contents, requests_nok):
    my_flare = Flare(paths=[zip_contents])
    my_flare.create_archive()

    with mock.patch('requests.post', return_value=requests_nok):
        success, case = my_flare.submit()
        assert not success
        assert case is None

    my_flare.cleanup()
    assert not os.path.exists(my_flare.get_archive_path())
コード例 #19
0
ファイル: test_flare.py プロジェクト: DylanFrese/dd-agent
    def test_api_keys_regex(self, mock_config, mock_tempdir, mock_strftime):
        f = Flare()
        file_path, _ = f._strip_credentials(
            os.path.join(get_mocked_temp(), 'apikeys.conf'),
            f.MAIN_CREDENTIALS
        )
        with open(file_path) as f:
            contents = f.read()

        self.assertEqual(
            contents,
            """api_key: *************************aaaaa
other_api_keys: **************************bbbbb, **************************ccccc, **************************dddd

"""
        )
コード例 #20
0
 def test_init(self, mock_config, mock_version, mock_tempdir, mock_strftime):
     f = Flare(case_id=1337)
     conf = mock_config()
     self.assertEqual(f._case_id, 1337)
     self.assertEqual(f._api_key, conf['api_key'])
     self.assertEqual(f._url, 'https://6-6-6-flare.agent.datadoghq.com/support/flare')
     self.assertEqual(f.tar_path, os.path.join(get_mocked_temp(), "datadog-agent-1.tar.bz2"))
コード例 #21
0
def test_flare_proxy_timeout(zip_contents):
    my_flare = Flare(paths=[zip_contents])
    my_flare.create_archive()

    with mock.patch('requests.post') as requests_mock:
        requests_mock.side_effect = requests.exceptions.Timeout(
            'fake proxy timeout')
        success, case = my_flare.submit()
        assert not success
        assert case is None

    my_flare.cleanup()
    assert not os.path.exists(my_flare.get_archive_path())
コード例 #22
0
ファイル: test_flare.py プロジェクト: jboyle2001/dd-agent
    def test_upload_with_case(self, mock_config, mock_tempdir, mock_stfrtime,
                              mock_version, mock_requests):
        f = Flare(case_id=1337)

        assert not mock_requests.called
        f.upload(confirmation=False)
        assert mock_requests.called
        args, kwargs = mock_requests.call_args_list[0]
        self.assertEqual(args, (
            'https://6-6-6-flare.agent.datadoghq.com/support/flare/1337?api_key=APIKEY',
        ))
        self.assertEqual(
            kwargs['files']['flare_file'].name,
            os.path.join(get_mocked_temp(), "datadog-agent-1.tar.bz2"))
        self.assertEqual(kwargs['data']['case_id'], 1337)
        self.assertEqual(kwargs['data']['email'], '')
        assert kwargs['data']['hostname']
コード例 #23
0
ファイル: test_flare.py プロジェクト: AquaBindi/dd-agent
    def test_upload_with_case(self, mock_config, mock_tempdir, mock_stfrtime, mock_version, mock_requests):
        f = Flare(case_id=1337)

        assert not mock_requests.called
        f.upload(confirmation=False)
        assert mock_requests.called
        args, kwargs = mock_requests.call_args_list[0]
        self.assertEqual(
            args,
            ('https://6-6-6-flare.agent.datadoghq.com/support/flare/1337?api_key=APIKEY',)
        )
        self.assertEqual(
            kwargs['files']['flare_file'].name,
            os.path.join(get_mocked_temp(), "datadog-agent-1.tar.bz2")
        )
        self.assertEqual(kwargs['data']['case_id'], 1337)
        self.assertEqual(kwargs['data']['email'], '')
        assert kwargs['data']['hostname']
コード例 #24
0
    def test_upload_no_case(self, mock_config, mock_tempdir, mock_stfrtime,
                            mock_version, mock_requests):
        f = Flare()
        f._ask_for_email = lambda: '*****@*****.**'

        assert not mock_requests.called
        f.upload()
        assert mock_requests.called
        args, kwargs = mock_requests.call_args_list[0]
        self.assertEqual(args, (
            'https://6-6-6-flare.agent.datadoghq.com/support/flare?api_key=APIKEY',
        ))
        self.assertEqual(
            kwargs['files']['flare_file'].name,
            os.path.join(get_mocked_temp(), "datadog-agent-1.tar.bz2"))
        self.assertEqual(kwargs['data']['case_id'], None)
        self.assertEqual(kwargs['data']['email'], '*****@*****.**')
        assert kwargs['data']['hostname']
コード例 #25
0
ファイル: test_flare.py プロジェクト: KnownSubset/dd-agent
    def test_upload_no_case(self, mock_config, mock_tempdir, mock_stfrtime, mock_version, mock_requests):
        f = Flare()
        f._ask_for_email = lambda: '*****@*****.**'

        assert not mock_requests.called
        f.upload()
        assert mock_requests.called
        args, kwargs = mock_requests.call_args_list[0]
        self.assertEqual(
            args,
            ('https://6-6-6-flare.agent.datadoghq.com/support/flare?api_key=APIKEY',)
        )
        self.assertEqual(
            kwargs['files']['flare_file'].name,
            os.path.join(get_mocked_temp(), "datadog-agent-1.tar.bz2")
        )
        self.assertEqual(kwargs['data']['case_id'], None)
        self.assertEqual(kwargs['data']['email'], '*****@*****.**')
        assert kwargs['data']['hostname']
コード例 #26
0
    def test_endpoint(self, mock_config, mock_temp, mock_stfrtime):
        f = Flare()
        f._ask_for_email = lambda: None
        f._open_tarfile()
        f._tar.close()

        with self.assertRaises(Exception) as cm:
            f.upload()

        self.assertEqual(str(cm.exception), "Your request is incorrect: Invalid inputs: 'API key unknown'")
コード例 #27
0
ファイル: test_flare.py プロジェクト: 7040210/dd-agent
    def test_upload_with_case_proxy(self, mock_config, mock_tempdir, mock_stfrtime, mock_version, mock_requests):
        f = Flare(case_id=1337)
        f._ask_for_email = lambda: '*****@*****.**'

        assert not mock_requests.called
        f.upload()
        assert mock_requests.called
        args, kwargs = mock_requests.call_args_list[0]
        self.assertEqual(
            args,
            ('https://6-6-6-flare.agent.datadoghq.com/support/flare/1337?api_key=APIKEY',)
        )
        self.assertEqual(
            kwargs['files']['flare_file'].name,
            os.path.join(get_mocked_temp(), "datadog-agent-1.tar.bz2")
        )
        self.assertEqual(kwargs['data']['case_id'], 1337)
        self.assertEqual(kwargs['data']['email'], '*****@*****.**')
        assert kwargs['data']['hostname']
        assert kwargs['proxies']['https'] == 'http://*****:*****@proxy.host.com:3128'
        assert not kwargs['verify']
コード例 #28
0
    def test_upload_with_case_proxy(self, mock_config, mock_tempdir,
                                    mock_stfrtime, mock_version,
                                    mock_requests):
        f = Flare(case_id=1337)
        f._ask_for_email = lambda: '*****@*****.**'

        assert not mock_requests.called
        f.upload()
        assert mock_requests.called
        args, kwargs = mock_requests.call_args_list[0]
        self.assertEqual(args, (
            'https://6-6-6-flare.agent.datamonitorhq.com/support/flare/1337?api_key=APIKEY',
        ))
        self.assertEqual(
            kwargs['files']['flare_file'].name,
            os.path.join(get_mocked_temp(), "datamonitor-agent-1.tar.bz2"))
        self.assertEqual(kwargs['data']['case_id'], 1337)
        self.assertEqual(kwargs['data']['email'], '*****@*****.**')
        assert kwargs['data']['hostname']
        assert kwargs['proxies'][
            'https'] == 'http://*****:*****@proxy.host.com:3128'
        assert not kwargs['verify']
コード例 #29
0
    def test_uri_password_regex(self, mock_config, mock_tempdir, mock_strftime):
        f = Flare()
        line = re.sub(f.URI_REGEX, r'\1://\2:********@', password_tests['uri_password'])
        self.assertEqual(
            line,
            password_tests['uri_password_expected']
        )

        line = re.sub(f.URI_REGEX, r'\1://\2:********@', password_tests['uri_password_2'])
        self.assertEqual(
            line,
            password_tests['uri_password_expected']
        )
コード例 #30
0
ファイル: test_flare.py プロジェクト: vb-tremendous/dd-agent
    def test_endpoint(self, mock_config, mock_temp, mock_stfrtime, mock_os_remove):
        if os.environ.get('FLARE_BROKEN'):
            raise unittest.case.SkipTest('Flare broken, acknowledged')
        f = Flare()
        f._ask_for_email = lambda: None
        f._open_tarfile()
        f._tar.close()

        with self.assertRaises(Exception) as cm:
            f.upload()

        self.assertEqual(str(cm.exception), "403 Client Error: Forbidden for url: https://app.datadoghq.com/support/flare?api_key=APIKEY")
コード例 #31
0
    def test_endpoint(self, mock_config, mock_temp, mock_stfrtime, mock_os_remove):
        if os.environ.get('FLARE_BROKEN'):
            raise unittest.case.SkipTest('Flare broken, acknowledged')
        f = Flare()
        f._ask_for_email = lambda: None
        f._open_tarfile()
        f._tar.close()

        with self.assertRaises(Exception) as cm:
            f.upload()

        self.assertEqual(str(cm.exception), "Your request is incorrect: Invalid inputs: 'API key unknown'")
コード例 #32
0
    def test_uri_password_regex(self, mock_config, mock_tempdir,
                                mock_strftime):
        f = Flare()
        password_uri_pattern = filter(
            lambda cred_pattern: cred_pattern.label == 'password in a uri',
            f.CHECK_CREDENTIALS).pop()

        line = re.sub(password_uri_pattern.pattern,
                      password_uri_pattern.replacement,
                      password_tests['uri_password'])
        self.assertEqual(line, password_tests['uri_password_expected'])

        line = re.sub(password_uri_pattern.pattern,
                      password_uri_pattern.replacement,
                      password_tests['uri_password_2'])
        self.assertEqual(line, password_tests['uri_password_expected'])
コード例 #33
0
ファイル: test_flare.py プロジェクト: jalaziz/dd-agent
    def test_endpoint(self, mock_config, mock_temp, mock_stfrtime):
        if os.environ['FLARE_BROKEN']:
            raise unittest.case.SkipTest('Flare broken, acknowledged')
        f = Flare()
        f._ask_for_email = lambda: None
        f._open_tarfile()
        f._tar.close()

        with self.assertRaises(Exception) as cm:
            f.upload()

        self.assertEqual(str(cm.exception), "Your request is incorrect: Invalid inputs: 'API key unknown'")
コード例 #34
0
ファイル: test_flare.py プロジェクト: long0419/sts-agent
    def test_upload_with_case(self, mock_config, mock_tempdir, mock_stfrtime,
                              mock_version, mock_requests, mock_os_remove):
        f = Flare(case_id=1337)
        f._ask_for_email = lambda: '*****@*****.**'
        f._open_tarfile()
        f._tar.close()

        assert not mock_requests.called
        f.upload()
        assert mock_requests.called
        args, kwargs = mock_requests.call_args_list[0]
        self.assertEqual(
            args,
            ('https://app.datadoghq.com/support/flare/1337?api_key=APIKEY', ))
        self.assertEqual(
            kwargs['files']['flare_file'].name,
            os.path.join(get_mocked_temp(), "stackstate-agent-1.tar.bz2"))
        self.assertEqual(kwargs['data']['case_id'], 1337)
        self.assertEqual(kwargs['data']['email'], '*****@*****.**')
        assert kwargs['data']['hostname']
コード例 #35
0
ファイル: agent.py プロジェクト: degemer/dd-agent
def main():
    options, args = get_parsed_args()
    agentConfig = get_config(options=options)
    autorestart = agentConfig.get('autorestart', False)
    hostname = get_hostname(agentConfig)

    COMMANDS_AGENT = [
        'start',
        'stop',
        'restart',
        'status',
        'foreground',
    ]

    COMMANDS_NO_AGENT = [
        'info',
        'check',
        'configcheck',
        'jmx',
        'flare',
    ]

    COMMANDS = COMMANDS_AGENT + COMMANDS_NO_AGENT

    if len(args) < 1:
        sys.stderr.write("Usage: %s %s\n" % (sys.argv[0], "|".join(COMMANDS)))
        return 2

    command = args[0]
    if command not in COMMANDS:
        sys.stderr.write("Unknown command: %s\n" % command)
        return 3

    # Deprecation notice
    if command not in DD_AGENT_COMMANDS:
        # Will become an error message and exit after deprecation period
        from utils.deprecations import deprecate_old_command_line_tools
        deprecate_old_command_line_tools()

    if command in COMMANDS_AGENT:
        agent = Agent(PidFile('dd-agent').get_path(), autorestart)

    if command in START_COMMANDS:
        log.info('Agent version %s' % get_version())

    if 'start' == command:
        log.info('Start daemon')
        agent.start()

    elif 'stop' == command:
        log.info('Stop daemon')
        agent.stop()

    elif 'restart' == command:
        log.info('Restart daemon')
        agent.restart()

    elif 'status' == command:
        agent.status()

    elif 'info' == command:
        return Agent.info(verbose=options.verbose)

    elif 'foreground' == command:
        logging.info('Running in foreground')
        if autorestart:
            # Set-up the supervisor callbacks and fork it.
            logging.info('Running Agent with auto-restart ON')

            def child_func():
                agent.start(foreground=True)

            def parent_func():
                agent.start_event = False

            AgentSupervisor.start(parent_func, child_func)
        else:
            # Run in the standard foreground.
            agent.start(foreground=True)

    elif 'check' == command:
        if len(args) < 2:
            sys.stderr.write(
                "Usage: %s check <check_name> [check_rate]\n"
                "Add check_rate as last argument to compute rates\n" %
                sys.argv[0])
            return 1

        check_name = args[1]
        try:
            import checks.collector
            # Try the old-style check first
            print getattr(checks.collector, check_name)(log).check(agentConfig)
        except Exception:
            # If not an old-style check, try checks.d
            checks = load_check_directory(agentConfig, hostname)
            for check in checks['initialized_checks']:
                if check.name == check_name:
                    check.run()
                    print check.get_metrics()
                    print check.get_events()
                    print check.get_service_checks()
                    if len(args) == 3 and args[2] == 'check_rate':
                        print "Running 2nd iteration to capture rate metrics"
                        time.sleep(1)
                        check.run()
                        print check.get_metrics()
                        print check.get_events()
                        print check.get_service_checks()
                    check.stop()

    elif 'configcheck' == command or 'configtest' == command:
        configcheck()

    elif 'jmx' == command:
        from jmxfetch import JMX_LIST_COMMANDS, JMXFetch

        if len(args) < 2 or args[1] not in JMX_LIST_COMMANDS.keys():
            print "#" * 80
            print "JMX tool to be used to help configuring your JMX checks."
            print "See http://docs.datadoghq.com/integrations/java/ for more information"
            print "#" * 80
            print "\n"
            print "You have to specify one of the following commands:"
            for command, desc in JMX_LIST_COMMANDS.iteritems():
                print "      - %s [OPTIONAL: LIST OF CHECKS]: %s" % (command,
                                                                     desc)
            print "Example: sudo /etc/init.d/datadog-agent jmx list_matching_attributes tomcat jmx solr"
            print "\n"

        else:
            jmx_command = args[1]
            checks_list = args[2:]
            confd_directory = get_confd_path(get_os())

            jmx_process = JMXFetch(confd_directory, agentConfig)
            jmx_process.configure()
            should_run = jmx_process.should_run()

            if should_run:
                jmx_process.run(jmx_command, checks_list, reporter="console")
            else:
                print "Couldn't find any valid JMX configuration in your conf.d directory: %s" % confd_directory
                print "Have you enabled any JMX check ?"
                print "If you think it's not normal please get in touch with Datadog Support"

    elif 'flare' == command:
        Flare.check_user_rights()
        case_id = int(args[1]) if len(args) > 1 else None
        f = Flare(True, case_id)
        f.collect()
        try:
            f.upload()
        except Exception, e:
            print 'The upload failed:\n{0}'.format(str(e))
コード例 #36
0
ファイル: agent.py プロジェクト: geowa4/dd-agent
def main():
    options, args = get_parsed_args()
    agentConfig = get_config(options=options)
    autorestart = agentConfig.get('autorestart', False)
    hostname = get_hostname(agentConfig)
    in_developer_mode = agentConfig.get('developer_mode')
    COMMANDS_AGENT = [
        'start',
        'stop',
        'restart',
        'status',
        'foreground',
    ]

    COMMANDS_NO_AGENT = [
        'info',
        'check',
        'configcheck',
        'jmx',
        'flare',
    ]

    COMMANDS = COMMANDS_AGENT + COMMANDS_NO_AGENT

    if len(args) < 1:
        sys.stderr.write("Usage: %s %s\n" % (sys.argv[0], "|".join(COMMANDS)))
        return 2

    command = args[0]
    if command not in COMMANDS:
        sys.stderr.write("Unknown command: %s\n" % command)
        return 3

    # Deprecation notice
    if command not in DD_AGENT_COMMANDS:
        # Will become an error message and exit after deprecation period
        from utils.deprecations import deprecate_old_command_line_tools
        deprecate_old_command_line_tools()

    if command in COMMANDS_AGENT:
        agent = Agent(PidFile(PID_NAME, PID_DIR).get_path(),
                      autorestart,
                      in_developer_mode=in_developer_mode)

    if command in START_COMMANDS:
        log.info('Agent version %s' % get_version())

    if 'start' == command:
        log.info('Start daemon')
        agent.start()

    elif 'stop' == command:
        log.info('Stop daemon')
        agent.stop()

    elif 'restart' == command:
        log.info('Restart daemon')
        agent.restart()

    elif 'status' == command:
        agent.status()

    elif 'info' == command:
        return Agent.info(verbose=options.verbose)

    elif 'foreground' == command:
        logging.info('Running in foreground')
        if autorestart:
            # Set-up the supervisor callbacks and fork it.
            logging.info('Running Agent with auto-restart ON')

            def child_func():
                agent.start(foreground=True)

            def parent_func():
                agent.start_event = False

            AgentSupervisor.start(parent_func, child_func)
        else:
            # Run in the standard foreground.
            agent.start(foreground=True)

    elif 'check' == command:
        if len(args) < 2:
            sys.stderr.write(
                "Usage: %s check <check_name> [check_rate]\n"
                "Add check_rate as last argument to compute rates\n" %
                sys.argv[0])
            return 1

        check_name = args[1]
        try:
            import checks.collector
            # Try the old-style check first
            print getattr(checks.collector, check_name)(log).check(agentConfig)
        except Exception:
            # If not an old-style check, try checks.d
            checks = load_check_directory(agentConfig, hostname)
            for check in checks['initialized_checks']:
                if check.name == check_name:
                    if in_developer_mode:
                        check.run = AgentProfiler.wrap_profiling(check.run)

                    cs = Collector.run_single_check(check, verbose=True)
                    print CollectorStatus.render_check_status(cs)

                    if len(args) == 3 and args[2] == 'check_rate':
                        print "Running 2nd iteration to capture rate metrics"
                        time.sleep(1)
                        cs = Collector.run_single_check(check, verbose=True)
                        print CollectorStatus.render_check_status(cs)

                    check.stop()

    elif 'configcheck' == command or 'configtest' == command:
        configcheck()

        if agentConfig.get('service_discovery', False):
            # set the TRACE_CONFIG flag to True to make load_check_directory return
            # the source of config objects.
            # Then call load_check_directory here and pass the result to sd_configcheck
            # to avoid circular imports
            agentConfig[TRACE_CONFIG] = True
            configs = {
                # check_name: (config_source, config)
            }
            print("\nLoading check configurations...\n\n")
            configs = load_check_directory(agentConfig, hostname)
            sd_configcheck(agentConfig, configs)

    elif 'jmx' == command:
        jmx_command(args[1:], agentConfig)

    elif 'flare' == command:
        Flare.check_user_rights()
        case_id = int(args[1]) if len(args) > 1 else None
        f = Flare(True, case_id)
        f.collect()
        try:
            f.upload()
        except Exception, e:
            print 'The upload failed:\n{0}'.format(str(e))
コード例 #37
0
ファイル: agent.py プロジェクト: alanbover/dd-agent
def main():
    options, args = get_parsed_args()
    agentConfig = get_config(options=options)
    autorestart = agentConfig.get('autorestart', False)
    hostname = get_hostname(agentConfig)
    in_developer_mode = agentConfig.get('developer_mode')

    COMMANDS_AGENT = [
        'start',
        'stop',
        'restart',
        'status',
        'foreground',
    ]

    COMMANDS_NO_AGENT = [
        'info',
        'check',
        'configcheck',
        'jmx',
        'flare',
    ]

    COMMANDS = COMMANDS_AGENT + COMMANDS_NO_AGENT

    if len(args) < 1:
        sys.stderr.write("Usage: %s %s\n" % (sys.argv[0], "|".join(COMMANDS)))
        return 2

    command = args[0]
    if command not in COMMANDS:
        sys.stderr.write("Unknown command: %s\n" % command)
        return 3

    # TODO: actually kill the start/stop/restart/status command for 5.11
    if command in ['start', 'stop', 'restart', 'status'] and not in_developer_mode:
        logging.error('Please use supervisor to manage the agent')
        return 1

    if command in COMMANDS_AGENT:
        agent = Agent(PidFile(PID_NAME, PID_DIR).get_path(), autorestart, in_developer_mode=in_developer_mode)

    if 'start' == command:
        log.info('Start daemon')
        agent.start()

    elif 'stop' == command:
        log.info('Stop daemon')
        agent.stop()

    elif 'restart' == command:
        log.info('Restart daemon')
        agent.restart()

    elif 'status' == command:
        agent.status()

    elif 'info' == command:
        return Agent.info(verbose=options.verbose)

    elif 'foreground' == command:
        log.info('Agent version %s' % get_version())
        if autorestart:
            # Set-up the supervisor callbacks and fork it.
            logging.info('Running Agent with auto-restart ON')

            def child_func():
                agent.start(foreground=True)

            def parent_func():
                agent.start_event = False

            AgentSupervisor.start(parent_func, child_func)
        else:
            # Run in the standard foreground.
            agent.start(foreground=True)

    elif 'check' == command:
        if len(args) < 2:
            sys.stderr.write(
                "Usage: %s check <check_name> [check_rate]\n"
                "Add check_rate as last argument to compute rates\n"
                % sys.argv[0]
            )
            return 1

        check_name = args[1]
        try:
            import checks.collector
            # Try the old-style check first
            print getattr(checks.collector, check_name)(log).check(agentConfig)
        except Exception:
            # If not an old-style check, try checks.d
            checks = load_check_directory(agentConfig, hostname)
            for check in checks['initialized_checks']:
                if check.name == check_name:
                    if in_developer_mode:
                        check.run = AgentProfiler.wrap_profiling(check.run)

                    cs = Collector.run_single_check(check, verbose=True)
                    print CollectorStatus.render_check_status(cs)

                    if len(args) == 3 and args[2] == 'check_rate':
                        print "Running 2nd iteration to capture rate metrics"
                        time.sleep(1)
                        cs = Collector.run_single_check(check, verbose=True)
                        print CollectorStatus.render_check_status(cs)

                    check.stop()

    elif 'configcheck' == command or 'configtest' == command:
        configcheck()
        sd_configcheck(agentConfig)

    elif 'jmx' == command:
        jmx_command(args[1:], agentConfig)

    elif 'flare' == command:
        Flare.check_user_rights()
        case_id = int(args[1]) if len(args) > 1 else None
        f = Flare(True, case_id)
        f.collect()
        try:
            f.upload()
        except Exception as e:
            print 'The upload failed:\n{0}'.format(str(e))

    return 0
コード例 #38
0
 def test_uri_verify_ssl_default(self, mock_config, mock_tempdir,
                                 mock_strftime):
     f = Flare()
     request_options = {}
     f.set_ssl_validation(request_options)
     self.assertEquals(request_options.get('verify'), None)
コード例 #39
0
 def test_uri_no_verify_ssl(self, mock_config, mock_tempdir, mock_strftime):
     f = Flare()
     request_options = {}
     f.set_ssl_validation(request_options)
     self.assertFalse(request_options['verify'])
コード例 #40
0
    def flare(cls, config, case_id):
        email = input('Please enter your contact email address: ').lower()
        case_id = int(case_id) if case_id else None
        myflare = Flare(version=AGENT_VERSION, case_id=case_id, email=email)
        myflare.add_path(config.get('conf_path'))
        myflare.add_path(config.get_loaded_config())
        myflare.add_path(config.get('logging').get('agent_log_file'))
        myflare.add_path(config.get('logging').get('dogstatsd_log_file'))
        myflare.add_path(config.get('additional_checksd'))

        flarepath = myflare.create_archive(
            status=cls.status(config, to_screen=False))

        print('The flare is going to be uploaded to Datadog')
        choice = input('Do you want to continue [Y/n]? ')
        if choice.strip().lower() not in ['yes', 'y', '']:
            print('Aborting (you can still use {0})'.format(flarepath))
            sys.exit(0)

        success, case_id = myflare.submit()
        if success:
            if case_id:
                print(
                    'Your flare was uploaded successfully, this is your case id: {}'
                    .format(case_id))
            else:
                print(
                    'Your flare was uploaded successfully, but a case id could not be retrieved.'
                )

            myflare.cleanup()
コード例 #41
0
ファイル: agent.py プロジェクト: WPMedia/dd-agent
def main():
    options, args = get_parsed_args()
    agentConfig = get_config(options=options)
    autorestart = agentConfig.get("autorestart", False)
    hostname = get_hostname(agentConfig)
    in_developer_mode = agentConfig.get("developer_mode")
    COMMANDS_AGENT = ["start", "stop", "restart", "status", "foreground"]

    COMMANDS_NO_AGENT = ["info", "check", "configcheck", "jmx", "flare"]

    COMMANDS = COMMANDS_AGENT + COMMANDS_NO_AGENT

    if len(args) < 1:
        sys.stderr.write("Usage: %s %s\n" % (sys.argv[0], "|".join(COMMANDS)))
        return 2

    command = args[0]
    if command not in COMMANDS:
        sys.stderr.write("Unknown command: %s\n" % command)
        return 3

    # Deprecation notice
    if command not in DD_AGENT_COMMANDS:
        # Will become an error message and exit after deprecation period
        from utils.deprecations import deprecate_old_command_line_tools

        deprecate_old_command_line_tools()

    if command in COMMANDS_AGENT:
        agent = Agent(PidFile(PID_NAME, PID_DIR).get_path(), autorestart, in_developer_mode=in_developer_mode)

    if command in START_COMMANDS:
        log.info("Agent version %s" % get_version())

    if "start" == command:
        log.info("Start daemon")
        agent.start()

    elif "stop" == command:
        log.info("Stop daemon")
        agent.stop()

    elif "restart" == command:
        log.info("Restart daemon")
        agent.restart()

    elif "status" == command:
        agent.status()

    elif "info" == command:
        return Agent.info(verbose=options.verbose)

    elif "foreground" == command:
        logging.info("Running in foreground")
        if autorestart:
            # Set-up the supervisor callbacks and fork it.
            logging.info("Running Agent with auto-restart ON")

            def child_func():
                agent.start(foreground=True)

            def parent_func():
                agent.start_event = False

            AgentSupervisor.start(parent_func, child_func)
        else:
            # Run in the standard foreground.
            agent.start(foreground=True)

    elif "check" == command:
        if len(args) < 2:
            sys.stderr.write(
                "Usage: %s check <check_name> [check_rate]\n"
                "Add check_rate as last argument to compute rates\n" % sys.argv[0]
            )
            return 1

        check_name = args[1]
        try:
            import checks.collector

            # Try the old-style check first
            print getattr(checks.collector, check_name)(log).check(agentConfig)
        except Exception:
            # If not an old-style check, try checks.d
            checks = load_check_directory(agentConfig, hostname)
            for check in checks["initialized_checks"]:
                if check.name == check_name:
                    if in_developer_mode:
                        check.run = AgentProfiler.wrap_profiling(check.run)

                    cs = Collector.run_single_check(check, verbose=True)
                    print CollectorStatus.render_check_status(cs)

                    if len(args) == 3 and args[2] == "check_rate":
                        print "Running 2nd iteration to capture rate metrics"
                        time.sleep(1)
                        cs = Collector.run_single_check(check, verbose=True)
                        print CollectorStatus.render_check_status(cs)

                    check.stop()

    elif "configcheck" == command or "configtest" == command:
        configcheck()

        if agentConfig.get("service_discovery", False):
            # set the TRACE_CONFIG flag to True to make load_check_directory return
            # the source of config objects.
            # Then call load_check_directory here and pass the result to sd_configcheck
            # to avoid circular imports
            agentConfig[TRACE_CONFIG] = True
            configs = {
                # check_name: (config_source, config)
            }
            print ("\nLoading check configurations...\n\n")
            configs = load_check_directory(agentConfig, hostname)
            sd_configcheck(agentConfig, configs)

    elif "jmx" == command:
        jmx_command(args[1:], agentConfig)

    elif "flare" == command:
        Flare.check_user_rights()
        case_id = int(args[1]) if len(args) > 1 else None
        f = Flare(True, case_id)
        f.collect()
        try:
            f.upload()
        except Exception, e:
            print "The upload failed:\n{0}".format(str(e))
コード例 #42
0
ファイル: test_flare.py プロジェクト: SupersonicAds/dd-agent
 def test_uri_no_verify_ssl(self, mock_config, mock_tempdir, mock_strftime):
     f = Flare()
     request_options = {}
     f.set_ssl_validation(request_options)
     self.assertFalse(request_options['verify'])
コード例 #43
0
ファイル: test_flare.py プロジェクト: SupersonicAds/dd-agent
 def test_uri_set_proxy(self, mock_config, mock_tempdir, mock_strftime):
     f = Flare()
     request_options = {}
     f.set_proxy(request_options)
     expected = 'http://*****:*****@proxy.host.com:3128'
     self.assertEqual(expected, request_options['proxies']['https'])
コード例 #44
0
ファイル: agent.py プロジェクト: miketheman/dd-agent
def main():
    options, args = get_parsed_args()
    agentConfig = get_config(options=options)
    autorestart = agentConfig.get("autorestart", False)
    hostname = get_hostname(agentConfig)

    COMMANDS = ["start", "stop", "restart", "foreground", "status", "info", "check", "configcheck", "jmx", "flare"]

    if len(args) < 1:
        sys.stderr.write("Usage: %s %s\n" % (sys.argv[0], "|".join(COMMANDS)))
        return 2

    command = args[0]
    if command not in COMMANDS:
        sys.stderr.write("Unknown command: %s\n" % command)
        return 3

    pid_file = PidFile("dd-agent")

    if options.clean:
        pid_file.clean()

    agent = Agent(pid_file.get_path(), autorestart)

    if command in START_COMMANDS:
        log.info("Agent version %s" % get_version())

    if "start" == command:
        log.info("Start daemon")
        agent.start()

    elif "stop" == command:
        log.info("Stop daemon")
        agent.stop()

    elif "restart" == command:
        log.info("Restart daemon")
        agent.restart()

    elif "status" == command:
        agent.status()

    elif "info" == command:
        return agent.info(verbose=options.verbose)

    elif "foreground" == command:
        logging.info("Running in foreground")
        if autorestart:
            # Set-up the supervisor callbacks and fork it.
            logging.info("Running Agent with auto-restart ON")

            def child_func():
                agent.start(foreground=True)

            def parent_func():
                agent.start_event = False

            AgentSupervisor.start(parent_func, child_func)
        else:
            # Run in the standard foreground.
            agent.start(foreground=True)

    elif "check" == command:
        if len(args) < 2:
            sys.stderr.write(
                "Usage: %s check <check_name> [check_rate]\n"
                "Add check_rate as last argument to compute rates\n" % sys.argv[0]
            )
            return 1

        check_name = args[1]
        try:
            import checks.collector

            # Try the old-style check first
            print getattr(checks.collector, check_name)(log).check(agentConfig)
        except Exception:
            # If not an old-style check, try checks.d
            checks = load_check_directory(agentConfig, hostname)
            for check in checks["initialized_checks"]:
                if check.name == check_name:
                    check.run()
                    print check.get_metrics()
                    print check.get_events()
                    print check.get_service_checks()
                    if len(args) == 3 and args[2] == "check_rate":
                        print "Running 2nd iteration to capture rate metrics"
                        time.sleep(1)
                        check.run()
                        print check.get_metrics()
                        print check.get_events()
                        print check.get_service_checks()
                    check.stop()

    elif "configcheck" == command or "configtest" == command:
        configcheck()

    elif "jmx" == command:
        from jmxfetch import JMX_LIST_COMMANDS, JMXFetch

        if len(args) < 2 or args[1] not in JMX_LIST_COMMANDS.keys():
            print "#" * 80
            print "JMX tool to be used to help configuring your JMX checks."
            print "See http://docs.datadoghq.com/integrations/java/ for more information"
            print "#" * 80
            print "\n"
            print "You have to specify one of the following commands:"
            for command, desc in JMX_LIST_COMMANDS.iteritems():
                print "      - %s [OPTIONAL: LIST OF CHECKS]: %s" % (command, desc)
            print "Example: sudo /etc/init.d/datadog-agent jmx list_matching_attributes tomcat jmx solr"
            print "\n"

        else:
            jmx_command = args[1]
            checks_list = args[2:]
            confd_directory = get_confd_path(get_os())

            jmx_process = JMXFetch(confd_directory, agentConfig)
            should_run = jmx_process.run(jmx_command, checks_list, reporter="console")
            if not should_run:
                print "Couldn't find any valid JMX configuration in your conf.d directory: %s" % confd_directory
                print "Have you enabled any JMX check ?"
                print "If you think it's not normal please get in touch with Datadog Support"

    elif "flare" == command:
        Flare.check_user_rights()
        case_id = int(args[1]) if len(args) > 1 else None
        f = Flare(True, case_id)
        f.collect()
        try:
            f.upload()
        except Exception, e:
            print "The upload failed:\n{0}".format(str(e))
コード例 #45
0
ファイル: test_flare.py プロジェクト: 7040210/dd-agent
 def test_uri_verify_ssl_default(self, mock_config, mock_tempdir, mock_strftime):
     f = Flare()
     request_options = {}
     f.set_ssl_validation(request_options)
     self.assertEquals(request_options.get('verify'), None)
コード例 #46
0
ファイル: agent.py プロジェクト: KnownSubset/dd-agent
def main():
    options, args = get_parsed_args()
    agentConfig = get_config(options=options)
    autorestart = agentConfig.get('autorestart', False)
    hostname = get_hostname(agentConfig)
    in_developer_mode = agentConfig.get('developer_mode')
    COMMANDS_AGENT = [
        'start',
        'stop',
        'restart',
        'status',
        'foreground',
    ]

    COMMANDS_NO_AGENT = [
        'info',
        'check',
        'configcheck',
        'jmx',
        'flare',
    ]

    COMMANDS = COMMANDS_AGENT + COMMANDS_NO_AGENT

    if len(args) < 1:
        sys.stderr.write("Usage: %s %s\n" % (sys.argv[0], "|".join(COMMANDS)))
        return 2

    command = args[0]
    if command not in COMMANDS:
        sys.stderr.write("Unknown command: %s\n" % command)
        return 3

    # Deprecation notice
    if command not in DD_AGENT_COMMANDS:
        # Will become an error message and exit after deprecation period
        from utils.deprecations import deprecate_old_command_line_tools
        deprecate_old_command_line_tools()

    if command in COMMANDS_AGENT:
        agent = Agent(PidFile('dd-agent').get_path(), autorestart, in_developer_mode=in_developer_mode)

    if command in START_COMMANDS:
        log.info('Agent version %s' % get_version())

    if 'start' == command:
        log.info('Start daemon')
        agent.start()

    elif 'stop' == command:
        log.info('Stop daemon')
        agent.stop()

    elif 'restart' == command:
        log.info('Restart daemon')
        agent.restart()

    elif 'status' == command:
        agent.status()

    elif 'info' == command:
        return Agent.info(verbose=options.verbose)

    elif 'foreground' == command:
        logging.info('Running in foreground')
        if autorestart:
            # Set-up the supervisor callbacks and fork it.
            logging.info('Running Agent with auto-restart ON')

            def child_func():
                agent.start(foreground=True)

            def parent_func():
                agent.start_event = False

            AgentSupervisor.start(parent_func, child_func)
        else:
            # Run in the standard foreground.
            agent.start(foreground=True)

    elif 'check' == command:
        if len(args) < 2:
            sys.stderr.write(
                "Usage: %s check <check_name> [check_rate]\n"
                "Add check_rate as last argument to compute rates\n"
                % sys.argv[0]
            )
            return 1

        check_name = args[1]
        try:
            import checks.collector
            # Try the old-style check first
            print getattr(checks.collector, check_name)(log).check(agentConfig)
        except Exception:
            # If not an old-style check, try checks.d
            checks = load_check_directory(agentConfig, hostname)
            for check in checks['initialized_checks']:
                if check.name == check_name:
                    if in_developer_mode:
                        check.run = AgentProfiler.wrap_profiling(check.run)

                    cs = Collector.run_single_check(check, verbose=True)
                    print CollectorStatus.render_check_status(cs)

                    if len(args) == 3 and args[2] == 'check_rate':
                        print "Running 2nd iteration to capture rate metrics"
                        time.sleep(1)
                        cs = Collector.run_single_check(check, verbose=True)
                        print CollectorStatus.render_check_status(cs)

                    check.stop()

    elif 'configcheck' == command or 'configtest' == command:
        configcheck()

    elif 'jmx' == command:
        if len(args) < 2 or args[1] not in JMX_LIST_COMMANDS.keys():
            print "#" * 80
            print "JMX tool to be used to help configuring your JMX checks."
            print "See http://docs.datadoghq.com/integrations/java/ for more information"
            print "#" * 80
            print "\n"
            print "You have to specify one of the following commands:"
            for command, desc in JMX_LIST_COMMANDS.iteritems():
                print "      - %s [OPTIONAL: LIST OF CHECKS]: %s" % (command, desc)
            print "Example: sudo /etc/init.d/datadog-agent jmx list_matching_attributes tomcat jmx solr"
            print "\n"

        else:
            jmx_command = args[1]
            checks_list = args[2:]
            confd_directory = get_confd_path(get_os())

            jmx_process = JMXFetch(confd_directory, agentConfig)
            jmx_process.configure()
            should_run = jmx_process.should_run()

            if should_run:
                jmx_process.run(jmx_command, checks_list, reporter="console")
            else:
                print "Couldn't find any valid JMX configuration in your conf.d directory: %s" % confd_directory
                print "Have you enabled any JMX check ?"
                print "If you think it's not normal please get in touch with Datadog Support"

    elif 'flare' == command:
        Flare.check_user_rights()
        case_id = int(args[1]) if len(args) > 1 else None
        f = Flare(True, case_id)
        f.collect()
        try:
            f.upload()
        except Exception, e:
            print 'The upload failed:\n{0}'.format(str(e))
コード例 #47
0
def main():
    options, args = get_parsed_args()
    agentConfig = get_config(options=options)
    autorestart = agentConfig.get('autorestart', False)
    hostname = get_hostname(agentConfig)
    in_developer_mode = agentConfig.get('developer_mode')

    COMMANDS_AGENT = [
        'start',
        'stop',
        'restart',
        'status',
        'foreground',
    ]

    COMMANDS_NO_AGENT = [
        'info',
        'check',
        'configcheck',
        'jmx',
        'flare',
    ]

    COMMANDS = COMMANDS_AGENT + COMMANDS_NO_AGENT

    if len(args) < 1:
        sys.stderr.write("Usage: %s %s\n" % (sys.argv[0], "|".join(COMMANDS)))
        return 2

    command = args[0]
    if command not in COMMANDS:
        sys.stderr.write("Unknown command: %s\n" % command)
        return 3

    # TODO: actually kill the start/stop/restart/status command for 5.11
    if command in ['start', 'stop', 'restart', 'status'
                   ] and not in_developer_mode:
        logging.error('Please use supervisor to manage the agent')
        return 1

    if command in COMMANDS_AGENT:
        agent = Agent(PidFile(PID_NAME, PID_DIR).get_path(),
                      autorestart,
                      in_developer_mode=in_developer_mode)

    if 'start' == command:
        log.info('Start daemon')
        agent.start()

    elif 'stop' == command:
        log.info('Stop daemon')
        agent.stop()

    elif 'restart' == command:
        log.info('Restart daemon')
        agent.restart()

    elif 'status' == command:
        agent.status()

    elif 'info' == command:
        return Agent.info(verbose=options.verbose)

    elif 'foreground' == command:
        log.info('Agent version %s' % get_version())
        if autorestart:
            # Set-up the supervisor callbacks and fork it.
            logging.info('Running Agent with auto-restart ON')

            def child_func():
                agent.start(foreground=True)

            def parent_func():
                agent.start_event = False

            AgentSupervisor.start(parent_func, child_func)
        else:
            # Run in the standard foreground.
            agent.start(foreground=True)

    elif 'check' == command:
        if len(args) < 2:
            sys.stderr.write(
                "Usage: %s check <check_name> [check_rate]\n"
                "Add check_rate as last argument to compute rates\n" %
                sys.argv[0])
            return 1

        check_name = args[1]
        try:
            import checks.collector
            # Try the old-style check first
            print getattr(checks.collector, check_name)(log).check(agentConfig)
        except Exception:
            # If not an old-style check, try checks.d
            checks = load_check_directory(agentConfig, hostname)
            for check in checks['initialized_checks']:
                if check.name == check_name:
                    if in_developer_mode:
                        check.run = AgentProfiler.wrap_profiling(check.run)

                    cs = Collector.run_single_check(check, verbose=True)
                    print CollectorStatus.render_check_status(cs)

                    if len(args) == 3 and args[2] == 'check_rate':
                        print "Running 2nd iteration to capture rate metrics"
                        time.sleep(1)
                        cs = Collector.run_single_check(check, verbose=True)
                        print CollectorStatus.render_check_status(cs)

                    check.stop()

    elif 'configcheck' == command or 'configtest' == command:
        configcheck()
        sd_configcheck(agentConfig)

    elif 'jmx' == command:
        jmx_command(args[1:], agentConfig)

    elif 'flare' == command:
        Flare.check_user_rights()
        case_id = int(args[1]) if len(args) > 1 else None
        f = Flare(True, case_id)
        f.collect()
        try:
            f.upload()
        except Exception as e:
            print 'The upload failed:\n{0}'.format(str(e))

    return 0
コード例 #48
0
    def flare(cls, case_id):
        email = input('Please enter your contact email address: ').lower()
        case_id = int(case_id) if case_id else None
        myflare = Flare(case_id=case_id, email=email)
        myflare.add_path(config.get('conf_path'))
        myflare.add_path(config.get_loaded_config())
        myflare.add_path(config.get('logging').get('agent_log_file'))
        myflare.add_path(config.get('logging').get('dogstatsd_log_file'))
        myflare.add_path(config.get('additional_checksd'))

        flarepath = myflare.create_archive()

        print('The flare is going to be uploaded to Datadog')
        choice = input('Do you want to continue [Y/n]? ')
        if choice.strip().lower() not in ['yes', 'y', '']:
            print('Aborting (you can still use {0})'.format(flarepath))
            sys.exit(0)

        if myflare.submit():
            myflare.cleanup()