From d5d15aadfa16b69fcc7e4a611e5010ebca4bbfd5 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Sun, 4 Dec 2022 12:41:22 +0100 Subject: [PATCH] Added pip packaging --- configure.ac.in | 2 ++ python/Makefile | 10 ++++++++++ python/__init__.py.in | 10 ++++++++++ python/flow.py | 17 +++++++---------- python/host.py | 33 +++++++++++++++------------------ python/interface.py | 18 ++++++++---------- python/setup.py.in | 13 +++++++++++++ python/test.py | 5 +---- 8 files changed, 66 insertions(+), 42 deletions(-) create mode 100644 python/Makefile create mode 100644 python/__init__.py.in create mode 100644 python/setup.py.in diff --git a/configure.ac.in b/configure.ac.in index f22bc6689b..bb75865fa5 100644 --- a/configure.ac.in +++ b/configure.ac.in @@ -844,6 +844,8 @@ AC_CONFIG_FILES(httpdocs/misc/ntopng-utils-manage-updates) AC_CONFIG_FILES(packages/wizard/ntopng-config) AC_CONFIG_FILES(packages/etc/systemd/system/ntopng.service) AC_CONFIG_FILES(packages/etc/systemd/system/ntopng@.service) +AC_CONFIG_FILES(python/setup.py) +AC_CONFIG_FILES(python/__init__.py) AC_OUTPUT diff --git a/python/Makefile b/python/Makefile new file mode 100644 index 0000000000..8581d905de --- /dev/null +++ b/python/Makefile @@ -0,0 +1,10 @@ +build: + rm -rf ntopng_package + mkdir -p ntopng_package/ntopng + cp __init__.py ntopng_package/ntopng + cp flow.py host.py interface.py ntopng.py ntopng_package/ntopng + cp setup.py ntopng_package + + +install: + cd ntopng_package; pip install . diff --git a/python/__init__.py.in b/python/__init__.py.in new file mode 100644 index 0000000000..308c5654e3 --- /dev/null +++ b/python/__init__.py.in @@ -0,0 +1,10 @@ +""" +ntopng + +ntopng Python library. +""" + +__version__ = "@NTOPNG_VERSION@" +__author__ = 'packager@ntop.org' + +__all__ = [ 'ntopng', 'interface', 'flow', 'host' ] diff --git a/python/flow.py b/python/flow.py index c9aa989547..b8115e6d1a 100644 --- a/python/flow.py +++ b/python/flow.py @@ -6,10 +6,7 @@ # https://www.ntop.org/guides/ntopng/api/rest/api_v2.html # -import requests -import json -from requests.auth import HTTPBasicAuth -from ntopng import ntopng +import ntopng class flow: def __init__(self, ntopng_obj): @@ -18,22 +15,22 @@ class flow: self.rest_pro_v2_url = "/lua/pro/rest/v2" def get_active_flows_paginated(self, ifid, currentPage, perPage): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/flow/active.lua", {"ifid": ifid, "currentPage": currentPage, "perPage": perPage})) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/flow/active.lua", {"ifid": ifid, "currentPage": currentPage, "perPage": perPage})) def get_active_host_flows_paginated(self, ifid, host, vlan, currentPage, perPage): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/flow/active.lua", {"ifid": ifid, "host": host, "vlan": vlan, "currentPage": currentPage, "perPage": perPage})) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/flow/active.lua", {"ifid": ifid, "host": host, "vlan": vlan, "currentPage": currentPage, "perPage": perPage})) def get_active_l4_proto_flow_counters(self, ifid): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/flow/l4/counters.lua", {"ifid": ifid })) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/flow/l4/counters.lua", {"ifid": ifid })) def get_active_l7_proto_flow_counters(self, ifid): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/flow/l7/counters.lua", {"ifid": ifid })) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/flow/l7/counters.lua", {"ifid": ifid })) def get_historical_flows(self, ifid, epoch_begin, epoch_end, max_hits, where_clause): - return(ntopng.request(self.ntopng_obj, self.rest_pro_v2_url + "/get/db/flows.lua", {"ifid": ifid, "begin_time_clause": epoch_begin, "end_time_clause": epoch_end, "maxhits_clause": max_hits, "where_clause": where_clause })) + return(self.ntopng_obj.request(self.rest_pro_v2_url + "/get/db/flows.lua", {"ifid": ifid, "begin_time_clause": epoch_begin, "end_time_clause": epoch_end, "maxhits_clause": max_hits, "where_clause": where_clause })) def get_historical_topk_flows(self, ifid, epoch_begin, epoch_end, max_hits, where_clause): - return(ntopng.request(self.ntopng_obj, self.rest_pro_v2_url + "/get/db/topk_flows.lua", {"ifid": ifid, "begin_time_clause": epoch_begin, "end_time_clause": epoch_end, "maxhits_clause": max_hits, "where_clause": where_clause })) + return(self.ntopng_obj.request(self.rest_pro_v2_url + "/get/db/topk_flows.lua", {"ifid": ifid, "begin_time_clause": epoch_begin, "end_time_clause": epoch_end, "maxhits_clause": max_hits, "where_clause": where_clause })) diff --git a/python/host.py b/python/host.py index e37348aea7..0895ccce7b 100644 --- a/python/host.py +++ b/python/host.py @@ -6,10 +6,7 @@ # https://www.ntop.org/guides/ntopng/api/rest/api_v2.html # -import requests -import json -from requests.auth import HTTPBasicAuth -from ntopng import ntopng +import ntopng class host: def __init__(self, ntopng_obj): @@ -18,19 +15,19 @@ class host: self.rest_pro_v2_url = "/lua/pro/rest/v2" def get_active_hosts(self, ifid): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/host/active.lua", {"ifid": ifid})) + 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(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/host/active.lua", {"ifid": ifid, "currentPage": currentPage, "perPage": 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(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/host/interfaces.lua", { "host": host })) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/interfaces.lua", { "host": host })) def get_host_data(self, ifid, host): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/host/data.lua", { "ifid": ifid, "host": host })) + 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): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/host/l7/stats.lua", { "ifid": ifid, "host": host, "vlan": vlan, "breed": True, "ndpi_category": True, "collapse_stats": False })) + 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 })) def get_host_dscp_stats(self, ifid, host, vlan, direction_rcvd): if(direction_rcvd): @@ -38,31 +35,31 @@ class host: else: direction = "sent" - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/host/dscp/stats.lua", { "ifid": ifid, "host": host, "vlan": vlan, "direction": direction })) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/host/dscp/stats.lua", { "ifid": ifid, "host": host, "vlan": vlan, "direction": direction })) def get_top_local_talkers(self, ifid): - return(ntopng.request(self.ntopng_obj, self.rest_pro_v2_url + "/get/interface/top/local/talkers.lua", { "ifid": ifid })) + return(self.ntopng_obj.request(self.rest_pro_v2_url + "/get/interface/top/local/talkers.lua", { "ifid": ifid })) def get_top_remote_talkers(self, ifid): - return(ntopng.request(self.ntopng_obj, self.rest_pro_v2_url + "/get/interface/top/remote/talkers.lua", { "ifid": ifid })) + return(self.ntopng_obj.request(self.rest_pro_v2_url + "/get/interface/top/remote/talkers.lua", { "ifid": ifid })) def get_alert_types(self): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/alert/type/consts.lua", None)) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/alert/type/consts.lua", None)) def get_alert_severities(self): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/alert/severity/consts.lua", None)) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/alert/severity/consts.lua", None)) def get_historical_alert_type_counters(self, ifid, epoch_begin, epoch_end): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/alert/type/counters.lua", { "ifid": ifid, "status": "historical", "epoch_begin": epoch_begin, "epoch_end": epoch_end })) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/alert/type/counters.lua", { "ifid": ifid, "status": "historical", "epoch_begin": epoch_begin, "epoch_end": epoch_end })) def get_historical_flows_type_counters(self, ifid, epoch_begin, epoch_end): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/alert/type/counters.lua", { "ifid": ifid, "status": "historical", "epoch_begin": epoch_begin, "epoch_end": epoch_end })) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/alert/type/counters.lua", { "ifid": ifid, "status": "historical", "epoch_begin": epoch_begin, "epoch_end": epoch_end })) def get_historical_alert_severity_counters(self, ifid, epoch_begin, epoch_end): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/alert/severity/counters.lua", { "ifid": ifid, "status": "historical", "epoch_begin": epoch_begin, "epoch_end": epoch_end })) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/alert/severity/counters.lua", { "ifid": ifid, "status": "historical", "epoch_begin": epoch_begin, "epoch_end": epoch_end })) def get_historical_flows_severity_counters(self, ifid, epoch_begin, epoch_end): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/alert/severity/counters.lua", { "ifid": ifid, "status": "historical", "epoch_begin": epoch_begin, "epoch_end": epoch_end })) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/alert/severity/counters.lua", { "ifid": ifid, "status": "historical", "epoch_begin": epoch_begin, "epoch_end": epoch_end })) def self_test(self, ifid): diff --git a/python/interface.py b/python/interface.py index ea31bf9ac2..2e821265b4 100644 --- a/python/interface.py +++ b/python/interface.py @@ -6,10 +6,8 @@ # https://www.ntop.org/guides/ntopng/api/rest/api_v2.html # -import requests -import json -from requests.auth import HTTPBasicAuth -from ntopng import ntopng +import ntopng + class interface: def __init__(self, ntopng_obj): @@ -17,16 +15,16 @@ class interface: self.rest_v2_url = "/lua/rest/v2" def get_data(self, ifid): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/interface/data.lua", {"ifid": ifid})) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/interface/data.lua", {"ifid": ifid})) def get_broadcast_domains(self, ifid): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/interface/bcast_domains.lua", {"ifid": ifid})) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/interface/bcast_domains.lua", {"ifid": ifid})) def get_address(self, ifid): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/interface/address.lua", {"ifid": ifid})) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/interface/address.lua", {"ifid": ifid})) def get_l7_stats(self, ifid, max_num_results): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/interface/l7/stats.lua", + return(self.ntopng_obj.request(self.rest_v2_url + "/get/interface/l7/stats.lua", {"ifid": ifid, 'ndpistats_mode': 'count', 'breed': True, @@ -37,10 +35,10 @@ class interface: })) def get_dscp_stats(self, ifid): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/interface/dscp/stats.lua", {"ifid": ifid})) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/interface/dscp/stats.lua", {"ifid": ifid})) def get_interfaces(self): - return(ntopng.request(self.ntopng_obj, self.rest_v2_url + "/get/ntopng/interfaces.lua", None)) + return(self.ntopng_obj.request(self.rest_v2_url + "/get/ntopng/interfaces.lua", None)) def self_test(self, ifid): print("----------------------------") diff --git a/python/setup.py.in b/python/setup.py.in new file mode 100644 index 0000000000..792efedbcb --- /dev/null +++ b/python/setup.py.in @@ -0,0 +1,13 @@ +from setuptools import setup + +setup( + name='ntopng', + version='@NTOPNG_VERSION@', + description='ntopng Python package', + url='https://github.com/ntop/ntopng', + author='ntop', + author_email='packager@ntop.org', + license='GPL', + packages=['ntopng'], + install_requires=['requests', 'simplejson' ], + ) diff --git a/python/test.py b/python/test.py index fb86e9c095..80b443a8e3 100755 --- a/python/test.py +++ b/python/test.py @@ -4,10 +4,7 @@ # Test application for python API # -from ntopng import ntopng -from interface import interface -from host import host -from flow import flow +from ntopng import ntopng, interface, host, flow import os my_ntopng = ntopng('admin', 'admin', 'http://localhost:3000')