def read(self, request, network_id=None): """ If the network_id is specified returns network details, otherwise returns networks list. Method: GET Request parematers: * network_id (optional) - network which details we want * get_hosts (optional) - if 'true' and network_id is specified, then list of hosts in the network is returned * order_by (optional) - if network_id is not specified then networks list will be sorted according to this parameter; the allowed values are: ** name - order by name ** last_event - order by occurance of last event Network details response: * network_id * network_name - name of the network * network_description - description of the network * hosts (optional) ** id - host identifier ** name - host name Networks list reponse: * networks ** id - network identifier ** name - network name """ if not network_id: networks = Network.objects.filter(user=request.user) if not networks: return api_error(_('The networks list is empty')) order_by = request.GET.get('order_by', 'name') if order_by == 'name': networks = networks.order_by('name') elif order_by == 'latest_event': networks = sorted(networks, key=lambda net: net.latest_event()) response = { 'networks': [net.api_list() for net in networks] } return api_response(response) try: network = Network.objects.get(pk=network_id, user=request.user) except Network.DoesNotExist: return api_error(_('Network does not exist')) response = network.api_detail() get_hosts = request.GET.get('get_hosts', 'true') if get_hosts.lower() == 'true': response['hosts'] = [host.api_list() for host in network.hosts()] return api_response(response)
def read(self, request, host_id=None): """ Returns host details if host_id is specified, otherwise returns list of hosts. Method: GET Request parameters: * host_id (optional) - if specified, host details are returned * order_by (optional) - if host_id is not specified, order hosts list according to this parameter; allowed values are: ** name - order by name ** last_event - order by occurance of last event * limit - maximum number of hosts on a list Host details response: * host_id * host_name * host_description * ipv4 - IPv4 address of the host * ipv6 - IPv6 address of the host * events (optional) - list of events for the host ** id - event identifier ** message - event message Hosts list response: * hosts ** id - host identifier ** name - host name """ if not host_id: hosts = Host.objects.filter(user=request.user) if not hosts: return api_error(_('The hosts list is empty')) order_by = request.GET.get('order_by', 'name') if order_by == 'name': hosts = hosts.order_by('name') elif order_by == 'latest_event': hosts = sorted(hosts, key=lambda host: host.latest_event()) limit = request.GET.get('limit') if limit: hosts = hosts[:limit] response = { 'hosts': [host.api_list() for host in hosts] } return api_response(response) try: host = Host.objects.get(pk=host_id, user=request.user) except Host.DoesNotExist: return api_error(_('Host does not exist')) return api_response(host.api_detail())
def read(self, request, network_id=None): """ If the network_id is specified returns network details, otherwise returns networks list. Method: GET Request parematers: * network_id (optional) - network which details we want * get_hosts (optional) - if 'true' and network_id is specified, then list of hosts in the network is returned * order_by (optional) - if network_id is not specified then networks list will be sorted according to this parameter; the allowed values are: ** name - order by name ** last_event - order by occurance of last event Network details response: * network_id * network_name - name of the network * network_description - description of the network * hosts (optional) ** id - host identifier ** name - host name Networks list reponse: * networks ** id - network identifier ** name - network name """ if not network_id: networks = Network.objects.filter(user=request.user) if not networks: return api_error(_('The networks list is empty')) order_by = request.GET.get('order_by', 'name') if order_by == 'name': networks = networks.order_by('name') elif order_by == 'latest_event': networks = sorted(networks, key=lambda net: net.latest_event()) response = {'networks': [net.api_list() for net in networks]} return api_response(response) try: network = Network.objects.get(pk=network_id, user=request.user) except Network.DoesNotExist: return api_error(_('Network does not exist')) response = network.api_detail() get_hosts = request.GET.get('get_hosts', 'true') if get_hosts.lower() == 'true': response['hosts'] = [host.api_list() for host in network.hosts()] return api_response(response)
def read(self, request, host_id=None): """ Returns host details if host_id is specified, otherwise returns list of hosts. Method: GET Request parameters: * host_id (optional) - if specified, host details are returned * order_by (optional) - if host_id is not specified, order hosts list according to this parameter; allowed values are: ** name - order by name ** last_event - order by occurance of last event * limit - maximum number of hosts on a list Host details response: * host_id * host_name * host_description * ipv4 - IPv4 address of the host * ipv6 - IPv6 address of the host * events (optional) - list of events for the host ** id - event identifier ** message - event message Hosts list response: * hosts ** id - host identifier ** name - host name """ if not host_id: hosts = Host.objects.filter(user=request.user) if not hosts: return api_error(_('The hosts list is empty')) order_by = request.GET.get('order_by', 'name') if order_by == 'name': hosts = hosts.order_by('name') elif order_by == 'latest_event': hosts = sorted(hosts, key=lambda host: host.latest_event()) limit = request.GET.get('limit') if limit: hosts = hosts[:limit] response = {'hosts': [host.api_list() for host in hosts]} return api_response(response) try: host = Host.objects.get(pk=host_id, user=request.user) except Host.DoesNotExist: return api_error(_('Host does not exist')) return api_response(host.api_detail())
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 Request parameters: * source_host - identifier of a source host * time_from - include only those events which timestamp is greater or equal than this value * time_to - include only those events which timestamp if less than this value * limit - maximal number of events on a list Response for events list: * events - list of events ** id - event identifier ** message - event message Response for event details: * event_id * description - event message * description - event short message * timestamp - event timestamp * event_type - type of event * source_host_id - identifier of source host * module_id - identifier of monitoring module * module_fields - fields defined by monitoring module """ if not event_id: events = Event.objects.all().order_by('-timestamp') if not events: return api_error(_('The events list is empty')) source_host = request.GET.get('source_host') if source_host: events = events.filter(source_host__pk=source_host) time_from = request.GET.get('time_from') time_to = request.GET.get('time_to') if time_from: time_from = datetime.datetime.fromtimestamp(float(time_from)) events = events.filter(timestamp__gte=time_from) if time_to: time_to = datetime.datetime.fromtimestamp(float(time_to)) events = events.filter(timestamp__lt=time_to) # because of limited JOINs, we cannot use events.filter(source_host__user=request.user) events = filter(lambda event: event.user == request.user, events) limit = request.GET.get('limit') if limit and limit > 0: events = events[:limit] response = { 'events': [event.api_list() for event in events] } return api_response(response) try: event = Event.objects.get(pk=event_id) except Event.DoesNotExist: return api_error(_('Event does not exist')) if event.source_host.user != request.user: return api_error(_('Event does not exist')) return api_response(event.api_detail())
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 Request parameters: * source_host - identifier of a source host * time_from - include only those events which timestamp is greater or equal than this value * time_to - include only those events which timestamp if less than this value * limit - maximal number of events on a list Response for events list: * events - list of events ** id - event identifier ** message - event message Response for event details: * event_id * description - event message * description - event short message * timestamp - event timestamp * event_type - type of event * source_host_id - identifier of source host * module_id - identifier of monitoring module * module_fields - fields defined by monitoring module """ if not event_id: events = Event.objects.all().order_by('-timestamp') if not events: return api_error(_('The events list is empty')) source_host = request.GET.get('source_host') if source_host: events = events.filter(source_host__pk=source_host) time_from = request.GET.get('time_from') time_to = request.GET.get('time_to') if time_from: time_from = datetime.datetime.fromtimestamp(float(time_from)) events = events.filter(timestamp__gte=time_from) if time_to: time_to = datetime.datetime.fromtimestamp(float(time_to)) events = events.filter(timestamp__lt=time_to) # because of limited JOINs, we cannot use events.filter(source_host__user=request.user) events = filter(lambda event: event.user == request.user, events) limit = request.GET.get('limit') if limit and limit > 0: events = events[:limit] response = {'events': [event.api_list() for event in events]} return api_response(response) try: event = Event.objects.get(pk=event_id) except Event.DoesNotExist: return api_error(_('Event does not exist')) if event.source_host.user != request.user: return api_error(_('Event does not exist')) return api_response(event.api_detail())