def test_cmd_cleanup(self): future = datetimes.utcnow() + datetime.timedelta(days=1) pod_id_1 = self.make_pod_id(1) pod_id_2 = self.make_pod_id(2) self.create_pod_dir(pod_id_1, self.sample_config) self.create_pod_dir(pod_id_2, self.sample_config) self.assertEqual(self.list_active(), [pod_id_1, pod_id_2]) self.assertEqual(self.list_graveyard(), []) self.assertEqual(self.list_tmp(), []) ref_path = self.test_repo_path / 'ref' pods.cmd_add_ref(pod_id_1, ref_path) pods.cmd_cleanup(future) self.assertEqual(self.list_active(), [pod_id_1]) self.assertEqual(self.list_graveyard(), []) self.assertEqual(self.list_tmp(), []) ref_path.unlink() self.mock_journals.remove_journal_dir.assert_called_once_with(pod_id_2) self.mock_journals.remove_journal_dir.reset_mock() with self.using_exclusive(pods._get_pod_dir_path(pod_id_1)): pods.cmd_cleanup(future) self.assertEqual(self.list_active(), [pod_id_1]) self.assertEqual(self.list_graveyard(), []) self.assertEqual(self.list_tmp(), []) self.mock_journals.remove_journal_dir.assert_not_called() self.mock_journals.remove_journal_dir.reset_mock() pods.cmd_cleanup(future) self.assertEqual(self.list_active(), []) self.assertEqual(self.list_graveyard(), []) self.assertEqual(self.list_tmp(), []) self.mock_journals.remove_journal_dir.assert_called_once_with(pod_id_1)
def _journal_get_timestamp(entry): timestamp = (entry.get('_SOURCE_REALTIME_TIMESTAMP') or entry.get('__REALTIME_TIMESTAMP')) if timestamp is None: return datetimes.utcnow() return datetimes.utcfromtimestamp( times.convert( times.Units.MICROSECONDS, times.Units.SECONDS, int(timestamp), ), )
def _parse_syslog_entry(rules, raw_message, host): rule, match = _search_rules(rules, raw_message) if rule is None: return None kwargs = match.groupdict() kwargs.setdefault('title', 'syslog') kwargs.setdefault('raw_message', raw_message) return Message( host=host, # TODO: Parse timestamp of log entry. timestamp=datetimes.utcnow(), **rule.template.evaluate(kwargs), )
def rfc_7231_date(now=None): if not now: now = datetimes.utcnow() # We can't handle non-UTC time zone at the moment. ASSERT.is_(now.tzinfo, datetime.timezone.utc) return RFC_7231_FORMAT.format( year=now.year, month=RFC_7231_MONTHS[now.month - 1], day_name=RFC_7231_DAY_NAMES[now.weekday()], day=now.day, hour=now.hour, minute=now.minute, second=now.second, )
def cmd_send(args): if args.systemd_service_result is not None: if args.systemd_service_result == 'success': level = alerts.Message.Levels.GOOD else: level = alerts.Message.Levels.ERROR else: level = args.level alerts.load().send( alerts.Message( host=os.uname().nodename, level=level, title=args.title, description=args.description, timestamp=datetimes.utcnow(), ) ) return 0
def _parse_collectd_notification(rules, input_file): kwargs = { 'host': os.uname().nodename, 'level': Message.Levels.INFO.name, 'timestamp': datetimes.utcnow(), } headers = {} while True: header = input_file.readline().strip() if not header: break _parse_collectd_header(header, kwargs, headers) kwargs['title'] = _make_title_from_collectd_headers(headers) kwargs['raw_message'] = input_file.read() rule, match = _search_rules(rules, kwargs['raw_message']) if rule is None: return None kwargs.update(match.groupdict()) return Message( host=kwargs['host'], timestamp=kwargs['timestamp'], **rule.template.evaluate(kwargs), )
def make_grace_period_kwargs(args): return {'expiration': datetimes.utcnow() - args.grace_period}