Rework host API

This commit is contained in:
Alfredo Cardigliano 2023-01-19 16:51:57 +01:00
parent b066a9ef60
commit ccbabba0b4
4 changed files with 118 additions and 99 deletions

View file

@ -1,7 +1,7 @@
"""
Host
====================================
The Host class can be used to access information about hosts through the
The Host class can be used to access information about an host through the
REST API (https://www.ntop.org/guides/ntopng/api/rest/api_v2.html).
"""
@ -11,79 +11,57 @@ class Host:
:param ntopng_obj: The ntopng handle
"""
def __init__(self, ntopng_obj):
def __init__(self, ntopng_obj, ifid, ip, vlan=None):
"""
Construct a new Host object
:param ntopng_obj: The ntopng handle
:param ntopng_obj: The ntopng handle (Ntopng instance)
:param ifid: The interface ID
:type ifid: int
:param ip: The host IP address
:type ip: string
:param vlan: The host VLAN ID (if any)
:type vlan: int
"""
self.ntopng_obj = ntopng_obj
self.ifid = ifid
self.ip = ip
self.vlan = vlan
self.rest_v2_url = "/lua/rest/v2"
self.rest_pro_v2_url = "/lua/pro/rest/v2"
def get_active_hosts(self, ifid):
"""
Retrieve the list of active hosts for the specified interface
:param ifid: The interface ID
:type ifid: int
:return: All active hosts
:rtype: array
"""
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/active.lua", {"ifid": ifid}))
def get_active_hosts_paginated(self, ifid, currentPage, perPage):
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/active.lua", {"ifid": ifid, "currentPage": currentPage, "perPage": perPage}))
def get_host_interfaces(self, host):
"""
Return all ntopng interfaces for a given host
:param host: The host
:type host: string
:return: List of interfaces
:rtype: array
"""
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/interfaces.lua", { "host": host }))
def get_host_data(self, ifid, host):
def get_host_data(self):
"""
Return all available information about a single host
:param ifid: The interface ID
:type ifid: int
:param host: The host
:type host: string
:return: Information about the host
:rtype: object
"""
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/data.lua", { "ifid": ifid, "host": host }))
def get_host_l7_stats(self, ifid, host, vlan):
params = { "ifid": self.ifid, "host": self.ip }
if(self.vlan is not None):
params['vlan'] = self.vlan
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/data.lua", params))
def get_l7_stats(self):
"""
Return statistics about Layer 7 protocols for an host
Return statistics about Layer 7 protocols for the host
:param ifid: The interface ID
:type ifid: int
:param host: The host
:type host: string
:param vlan: The host VLAN ID (if any)
:type vlan: string
:return: Layer 7 protocol statistics
:rtype: object
"""
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/l7/stats.lua", { "ifid": ifid, "host": host, "vlan": vlan, "breed": True, "ndpi_category": True, "collapse_stats": False }))
params = { "ifid": self.ifid, "host": self.ip, "breed": True, "ndpi_category": True, "collapse_stats": False }
if(self.vlan is not None):
params['vlan'] = self.vlan
def get_host_dscp_stats(self, ifid, host, vlan, direction_rcvd):
print("BBB")
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/l7/stats.lua", params))
def get_dscp_stats(self, direction_rcvd):
"""
Return statistics about DSCP per traffic direction for an host
:param ifid: The interface ID
:type ifid: int
:param host: The host
:type host: string
:param vlan: The host VLAN ID (if any)
:type vlan: string
:param direction_rcvd: The traffic direction (True for received traffic, False for sent)
:type direction_rcvd: boolean
:return: DSCP statistics
@ -94,48 +72,22 @@ class Host:
else:
direction = "sent"
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/dscp/stats.lua", { "ifid": ifid, "host": host, "vlan": vlan, "direction": direction }))
params = { "ifid": self.ifid, "host": self.ip, "direction": direction }
if(self.vlan is not None):
params['vlan'] = self.vlan
def get_top_local_talkers(self, ifid):
"""
Return Top Local hosts generating more traffic
:param ifid: The interface ID
:type ifid: int
:return: The top local hosts
:rtype: array
"""
return(self.ntopng_obj.request(self.rest_pro_v2_url + "/get/interface/top/local/talkers.lua", { "ifid": ifid }))
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/dscp/stats.lua", params))
def get_top_remote_talkers(self, ifid):
"""
Return Top Remote hosts generating more traffic
:param ifid: The interface ID
:type ifid: int
:return: The top remote hosts
:rtype: array
"""
return(self.ntopng_obj.request(self.rest_pro_v2_url + "/get/interface/top/remote/talkers.lua", { "ifid": ifid }))
def self_test(self, ifid, host):
def self_test(self):
try:
print("----------------------------")
print(self.get_active_hosts(ifid))
print("----------------------------")
print(self.get_active_hosts_paginated(ifid, 1, 100))
print("----------------------------")
print(self.get_host_interfaces(host))
print("----------------------------")
print(self.get_host_data(ifid, host))
print("----------------------------")
print(self.get_host_l7_stats(ifid, host, 0))
print("----------------------------")
print(self.get_host_dscp_stats(ifid, host, 0, True))
print("----------------------------")
print(self.get_top_local_talkers(ifid))
print("----------------------------")
print(self.get_top_remote_talkers(ifid))
print("Host Data ----------------------------")
print(self.get_host_data())
print("L7 Stats ----------------------------")
print(self.get_l7_stats())
print("DSCP Stats (RX) ----------------------------")
print(self.get_dscp_stats(True))
print("DSCP Stats (TX) ----------------------------")
print(self.get_dscp_stats(False))
print("----------------------------")
except:
raise ValueError("Invalid interface ID or host specified")

View file

@ -5,6 +5,8 @@ The Interface class can be used to access information about interface statistics
REST API (https://www.ntop.org/guides/ntopng/api/rest/api_v2.html).
"""
from ntopng.host import Host
class Interface:
"""
Interface provides information about a Network interface
@ -24,6 +26,7 @@ class Interface:
self.ntopng_obj = ntopng_obj
self.ifid = ifid
self.rest_v2_url = "/lua/rest/v2"
self.rest_pro_v2_url = "/lua/pro/rest/v2"
def get_data(self):
"""
@ -80,18 +83,71 @@ class Interface:
"""
return(self.ntopng_obj.request(self.rest_v2_url + "/get/interface/dscp/stats.lua", {"ifid": self.ifid}))
def get_host(self, ip, vlan=None):
"""
Return an Host instance
:param ifid: The interface ID
:type ifid: int
:param ip: The host IP address
:type ip: string
:param vlan: The host VLAN ID (if any)
:type vlan: int
:return: The host instance
:rtype: ntopng.Host
"""
return Host(self.ntopng_obj, self.ifid, ip, vlan)
def get_active_hosts(self):
"""
Retrieve the list of active hosts for the specified interface
:return: All active hosts
:rtype: array
"""
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/active.lua", {"ifid": self.ifid}))
def get_active_hosts_paginated(self, currentPage, perPage):
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/active.lua", {"ifid": self.ifid, "currentPage": currentPage, "perPage": perPage}))
def get_top_local_talkers(self):
"""
Return Top Local hosts generating more traffic
:return: The top local hosts
:rtype: array
"""
return(self.ntopng_obj.request(self.rest_pro_v2_url + "/get/interface/top/local/talkers.lua", { "ifid": self.ifid }))
def get_top_remote_talkers(self):
"""
Return Top Remote hosts generating more traffic
:return: The top remote hosts
:rtype: array
"""
return(self.ntopng_obj.request(self.rest_pro_v2_url + "/get/interface/top/remote/talkers.lua", { "ifid": self.ifid }))
def self_test(self):
print(self.get_data())
try:
print("----------------------------")
print("Broadcast Domains ----------------------------")
print(self.get_broadcast_domains())
print("----------------------------")
print("Address ----------------------------")
print(self.get_address())
print("----------------------------")
print("L7 Stats ----------------------------")
max_num_records = 100
print(self.get_l7_stats(max_num_records))
print("----------------------------")
print("DSCP Stats ----------------------------")
print(self.get_dscp_stats())
print("Active Hosts ----------------------------")
print(self.get_active_hosts())
print("Active Hosts (100) ----------------------------")
print(self.get_active_hosts_paginated(1, 100))
print("Top Local Talkers ----------------------------")
print(self.get_top_local_talkers())
print("Top Remote Talkers ----------------------------")
print(self.get_top_remote_talkers())
print("----------------------------")
except:
raise ValueError("Invalid interface ID specified")

View file

@ -160,13 +160,24 @@ class Ntopng:
"""
return(self.request(self.rest_v2_url + "/get/ntopng/interfaces.lua", None))
def get_host_interfaces_list(self, host):
"""
Return all ntopng interfaces for a given host
:param host: The host
:type host: string
:return: List of interfaces
:rtype: array
"""
return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/interfaces.lua", { "host": host }))
def self_test(self):
try:
print("----------------------------")
print("Alert Types ----------------------------")
print(self.get_alert_types())
print("----------------------------")
print("Severities ----------------------------")
print(self.get_alert_severities())
print("----------------------------")
print("Interfaces List ----------------------------")
print(self.get_interfaces_list())
print("----------------------------")
except:

View file

@ -86,8 +86,8 @@ try:
my_interface.self_test()
print("\n\n==========================\nHost")
my_host = Host(my_ntopng)
my_host.self_test(iface_id, host_ip)
my_host = my_interface.get_host(host_ip)
my_host.self_test()
print("\n\n==========================\nFlow")
my_flow = Flow(my_ntopng)