def setUp(self):
        self.everything = netuitive.Event('elementId', 'INFO', 'title',
                                          'message', 'INFO',
                                          [('name0', 'value0'),
                                           ('name1', 'value1')], 1434110794,
                                          'source')

        self.notags = netuitive.Event('elementId',
                                      'INFO',
                                      'title',
                                      'message',
                                      'INFO',
                                      timestamp=1434110794,
                                      source='source')

        self.minimum = netuitive.Event('elementId', 'INFO', 'title', 'message',
                                       'INFO')

        self.everythingjson = json.dumps([self.everything],
                                         default=lambda o: o.__dict__,
                                         sort_keys=True)

        self.notagsjson = json.dumps([self.notags],
                                     default=lambda o: o.__dict__,
                                     sort_keys=True)

        self.minimumjson = json.dumps([self.minimum],
                                      default=lambda o: o.__dict__,
                                      sort_keys=True)
Exemplo n.º 2
0
    def test_success(self, mock_logging, mock_post):

        mock_post.return_value = MockResponse(code=202)

        # test infrastructure endpoint url creation
        a = netuitive.Client(api_key='apikey')

        e = netuitive.Event('test', 'INFO', 'test event',
                            'big old test message', 'INFO')

        resp = a.post_event(e)

        self.assertTrue(resp)

        self.assertEqual(mock_logging.exception.call_args_list, [])
    def test_failure_general(self, mock_logging, mock_post):
        mock_post.side_effect = urllib2.URLError('something')

        # test infrastructure endpoint url creation
        a = netuitive.Client(api_key='apikey')

        e = netuitive.Event(
            'test', 'INFO', 'test event', 'big old test message', 'INFO')

        resp = a.post_event(e)

        self.assertNotEqual(resp, True)

        self.assertEqual(mock_logging.exception.call_args_list[0][0][
                         0], 'error posting payload to api ingest endpoint (%s): %s')
    def test_failure_general_http(self, mock_logging, mock_post):

        mock_post.return_value = MockResponse(code=500)

        # test infrastructure endpoint url creation
        a = netuitive.Client(api_key='apikey')
        mock_post.side_effect = urllib2.HTTPError(a.url, 500, '', {}, None)

        e = netuitive.Event(
            'test', 'INFO', 'test event', 'big old test message', 'INFO')

        resp = a.post_event(e)

        self.assertNotEqual(resp, True)

        self.assertEqual(mock_logging.exception.call_args_list[0][0][
                         0], 'error posting payload to api ingest endpoint (%s): %s')
    def test_kill_switch_410(self, mock_logging, mock_post):

        mock_post.return_value = MockResponse(code=410)
        # test infrastructure endpoint url creation
        a = netuitive.Client(api_key='apikey')
        mock_post.side_effect = urllib2.HTTPError(a.url, 410, '', {}, None)

        e = netuitive.Event(
            'test', 'INFO', 'test event', 'big old test message', 'INFO')

        resp = a.post_event(e)
        resp2 = a.post_event(e)

        self.assertNotEqual(resp, True)
        self.assertFalse(resp2)
        self.assertTrue(a.disabled)
        self.assertEqual(mock_logging.exception.call_args_list[0][0][
                         0], 'Posting has been disabled.See previous errors for details.')
Exemplo n.º 6
0
MyElement = netuitive.Element()

MyElement.add_attribute('Language', 'Python')
MyElement.add_attribute('app_version', '7.0')

MyElement.add_relation('my_child_element')

MyElement.add_tag('Production', 'True')
MyElement.add_tag('app_tier', 'True')

timestamp = int(time.mktime(time.localtime()))
MyElement.add_sample('app.error', timestamp, 1, host='appserver01')
MyElement.add_sample('app.request', timestamp, 10, host='appserver01')

ApiClient.post(MyElement)

MyElement.clear_samples()

MyEvent = netuitive.Event('appserver01', 'INFO', 'test event',
                          'this is a test message', 'INFO')

ApiClient.post_event(MyEvent)

MyCheck = netuitive.Check('heartbeat', 'element', 60)

ApiClient.post_check(MyCheck)

if ApiClient.time_insync():
    print('we have time sync with the server')
Exemplo n.º 7
0
    def submit(self, message, ts):
        """
        process incoming messages
        """

        timestamp = int(ts)

        try:

            self.packet_count += 1

            messages = parse_message(message)

            if messages is not None:
                self.sample_count += float(messages['counts']['messages'])
                self.event_count += float(messages['counts']['events'])

                # process an event message
                if len(messages['events']) > 0:

                    for e in messages['events']:

                        title = e['title']
                        text = e['text']
                        tgs = e['tags']
                        tags = []

                        for t in tgs:
                            for k, v in t.items():
                                tags.append((k, v))

                        if e['hostname'] is None:
                            eid = self.hostname
                        else:
                            eid = e['hostname']

                        lvl = 'INFO'
                        if e['priority'] is not None:
                            tags.append(('priority', e['priority']))

                            if e['priority'].upper() == "CRITICAL":
                                lvl = "CRITICAL"

                            if e['priority'].upper() == "WARNING":
                                lvl = "WARNING"

                        if e['date_happened'] is not None:
                            tags.append(('date_happened', e['date_happened']))

                        if e['aggregation_key'] is not None:
                            tags.append(
                                ('aggregation_key', e['aggregation_key']))

                        if e['source_type_name'] is not None:
                            tags.append(
                                ('source_type_name', e['source_type_name']))

                        if e['alert_type'] is not None:
                            tags.append(('alert_type', e['alert_type']))

                        with self.lock:
                            self.events.append(
                                netuitive.Event(eid, 'INFO', title, text, lvl,
                                                tags, timestamp,
                                                'netuitive-statsd'))

                # process a metric/sample message
                if len(messages['metrics']) > 0:
                    for m in messages['metrics']:
                        with self.lock:
                            self.elements.add(
                                self.metric_prefix + '.' + m['name']
                                if self.metric_prefix != "" else m['name'],
                                timestamp, m['value'], m['type'], m['sign'],
                                m['rate'], m['tags'], m['hostname'])

        except Exception as e:
            logger.error('Invalid Packet Format: "' + str(message).rstrip() +
                         '"')
            logger.error(e, exc_info=True)