def setUp(self): self.username = '******' self.password = '******' #set up Django user self.user = User(username=self.username) self.user.set_password(self.password) self.user.save() #set up Django testing client self.client = Client() self.client.login(username=self.username, password=self.password) #set up events types for name in ['INFO', 'WARNING', 'CRITICAL', 'ERROR']: event_type = EventType(name=name, user=self.user, alert_level=1) event_type.save() #set up some hosts for i in xrange(10): h = Host(name='host_%i' % i, description='description number %i' % i, ipv4='127.0.0.%i' % (i+1), ipv6='0:0:0:0:0:0:7f00:%i' % (i+1), user=self.user) h.save() types = EventType.objects.all() hosts = Host.objects.all() for i in xrange(10): e = Event(message='event_%i' % i, short_message='short message #%i' % i, event_type=random.choice(types), protocol='SMTP', timestamp='%s' % str(datetime.datetime.now()), source_host = hosts[i], fields_class='Class%i' % i, fields_data='' ) e.save()
def setUp(self): self.username = '******' self.password = '******' #set up Django user self.user = User(username=self.username) self.user.set_password(self.password) self.user.save() #set up Django testing client self.client = Client() self.client.login(username=self.username, password=self.password) #set up events types for name in ['INFO', 'WARNING', 'CRITICAL', 'ERROR']: event_type = EventType(name=name, user=self.user, alert_level=1) event_type.save() #set up some hosts for i in xrange(10): h = Host(name='host_%i' % i, description='description number %i' % i, ipv4='127.0.0.%i' % (i + 1), ipv6='0:0:0:0:0:0:7f00:%i' % (i + 1), user=self.user) h.save() types = EventType.objects.all() hosts = Host.objects.all() for i in xrange(10): e = Event(message='event_%i' % i, short_message='short message #%i' % i, event_type=random.choice(types), protocol='SMTP', timestamp='%s' % str(datetime.datetime.now()), source_host=hosts[i], fields_class='Class%i' % i, fields_data='') e.save()
def setUp(self): self.client = Client() self.user = User.objects.create_user('user', '*****@*****.**', 'userpassword') self.user.save() self.client.login(username='******', password='******') self.source_host = Host(name='Host', ipv4='1.2.3.4', user=self.user) self.source_host.save() event_type = EventType(name='INFO', user=self.user) event_type.save() event_data = { 'message': 'Message', 'short_message': 'short message', 'event_type': event_type, 'timestamp': '%s' % str(datetime.datetime.now()), 'source_host': self.source_host } self.event = Event(**event_data) self.event.save()
def create(self, request): """ Receives one or more notifications and saves them to the database. This method is a part of private API. Method: POST URL: /api/event/report/ Case 1: Reporting single event (notification) --------------------------------------------- Request parameters: * description - message describing the event * short_description - shorter message, e.g. to use in list * timestamp - when the event occurred * event_type - type of the event which should also describe its importance * protocol - network protocol related to the event * hostname - name of the source host * source_host_ipv4, source_host_ipv6 - IPv4 and IPv6 addresses of the source host * fields_class - monitoring module identifier Any additional data provided with the event will be serialized and saved together with fields described above. Response: * status - **ok** or **error** * message - details of the result Case 2: Reporting multiple events at once ----------------------------------------- Request parameters: * events - list of events serialized with JSON Response: * status - **ok** or **error** * message - details of the result """ if request.POST.get('events'): try: events = json.loads(request.POST.get('events', '')) except ValueError: return api_error(_('No events could be read')) for event_dict in events: try: event_data = get_event_data(request, event_dict) except EventParseError, e: message = str(e) return api_error(_(message)) event = Event(**event_data) event.save() if event.event_type.notify: notifier.manager.add(event.short_message, event.message, event.user(), event) return api_ok(_('Events reported successfully'))
return api_error(_(message)) event = Event(**event_data) event.save() if event.event_type.notify: notifier.manager.add(event.short_message, event.message, event.user(), event) return api_ok(_('Events reported successfully')) try: event_data = get_event_data(request, request.POST) except EventParseError, e: message = str(e) return api_error(_(message)) event = Event(**event_data) event.save() if event.event_type.notify: notifier.manager.add(event.short_message, event.message, event.user(), event) return api_ok(_('Event reported successfully')) def read(self, request, event_id=None): """ The part of the public API. If the event_id parameter is specified, returns event details, otherwise returns events list ordered by timestamp. In the second case, events may be filtered by source host or timestamp and their number may be limited.
class EventTest(TestCase): """Tests for hosts """ def setUp(self): self.client = Client() self.user = User.objects.create_user('user', '*****@*****.**', 'userpassword') self.user.save() self.client.login(username='******', password='******') self.source_host = Host(name='Host', ipv4='1.2.3.4', user=self.user) self.source_host.save() event_type = EventType(name='INFO', user=self.user) event_type.save() event_data = { 'message': 'Message', 'short_message': 'short message', 'event_type': event_type, 'timestamp': '%s' % str(datetime.datetime.now()), 'source_host': self.source_host } self.event = Event(**event_data) self.event.save() def test_event_detail(self): """Get event's details """ url = '/event/%i/' % self.event.pk response = self.client.get(url) self.assertEqual(response.status_code, 200) def test_event_list(self): """Get events list """ url = '/event/list/' response = self.client.get(url) self.assertEqual(response.status_code, 200) def test_shared_event_detail(self): """ Make Other User the owner of the source host and then: 1. Make sure that User hasn't access to the event 2. Share source host with User and check if he has access to the event. """ other_user = User.objects.create_user('other', '*****@*****.**', 'otherpassword') other_user.save() self.source_host.user = other_user self.source_host.save() url = '/event/%i/' % self.event.pk response = self.client.get(url) self.assertEqual(response.status_code, 404) grant_access(self.source_host, self.user) url = '/event/%i/' % self.event.pk response = self.client.get(url) self.assertEqual(response.status_code, 200) self.source_host.user = self.user self.source_host.save()
message = str(e) return api_error(_(message)) event = Event(**event_data) event.save() if event.event_type.notify: notifier.push(user=request.user, event=event) return api_ok(_('Events reported successfully')) try: event_data = get_event_data(request, request.POST) except EventParseError, e: message = str(e) return api_error(_(message)) event = Event(**event_data) event.save() if event.event_type.notify: notifier.push(user=request.user, event=event) return api_ok(_('Event reported successfully')) def read(self, request, event_id=None): """ The part of the public API. If the event_id parameter is specified, returns event details, otherwise returns events list ordered by timestamp. In the second case, events may be filtered by source host or timestamp and their number may be limited. Method: GET