SWIG Interface File(s)¶
The following modular SWIG 4.x interface files in interface/ wrap the C++17 ezsnmp_data_logger engine for the 4 supported target languages: Python, JavaScript (Node.js N-API), C# (.NET P/Invoke), and Go (cgo).
For more information on SWIG interface syntax, see the SWIG 4 Documentation.
Top-Level Module (ezsnmp_data_logger.i)¶
%module ezsnmp_data_logger
#ifdef SWIGPYTHON
%feature("autodoc", "2");
%feature("python:annotations", "c");
%pythonbegin %{
import os
import sys
# On Windows (Python 3.8+), register package directory and candidate DLL directories
# so dependent native libraries (netsnmp.dll, libcrypto-*.dll) can be located by LoadLibraryEx.
if sys.platform == "win32" and hasattr(os, "add_dll_directory"):
_pkg_dir = os.path.dirname(os.path.abspath(__file__))
if os.path.isdir(_pkg_dir):
try:
os.add_dll_directory(_pkg_dir)
except OSError:
pass
for _env_var in ("EZSNMP_NETSNMP_BIN_DIR", "EZSNMP_OPENSSL_BIN_DIR"):
_cand = os.environ.get(_env_var, "").strip()
if _cand and os.path.isdir(_cand):
try:
os.add_dll_directory(_cand)
except OSError:
pass
for _std_dir in (
r"C:\ezsnmp-netsnmp-dist\x64\bin\release",
r"C:\ezsnmp-netsnmp-dist\AMD64\bin\release",
r"C:\ezsnmp-netsnmp-dist\x86\bin\release",
r"C:\ezsnmp-netsnmp-dist\ARM64\bin\release",
r"C:\Program Files\OpenSSL\bin",
r"C:\Program Files\OpenSSL-Win64\bin",
r"C:\OpenSSL-Win64\bin",
):
if os.path.isdir(_std_dir):
try:
os.add_dll_directory(_std_dir)
except OSError:
pass
for _path_entry in os.environ.get("PATH", "").split(os.pathsep):
_pe_lower = _path_entry.lower()
if _path_entry and (
"netsnmp" in _pe_lower or "snmp" in _pe_lower or "openssl" in _pe_lower
):
if os.path.isdir(_path_entry):
try:
os.add_dll_directory(_path_entry)
except OSError:
pass
%}
#endif
%include <stdint.i>
%include <std_string.i>
%include <std_vector.i>
%include "exceptions.i"
%include "datatypes.i"
%include "config.i"
%include "data_logger.i"
Exception Mapping (exceptions.i)¶
// SWIG exception translation across supported target languages (python, javascript, csharp, go)
%include <exception.i>
%{
#include "ezsnmp_data_logger/exceptions.h"
%}
%exception {
try {
$action
} catch (const ezsnmp_data_logger::ConfigError& e) {
SWIG_exception(SWIG_ValueError, e.what());
} catch (const ezsnmp_data_logger::SnmpError& e) {
SWIG_exception(SWIG_IOError, e.what());
} catch (const ezsnmp_data_logger::SinkError& e) {
SWIG_exception(SWIG_IOError, e.what());
} catch (const ezsnmp_data_logger::CliError& e) {
SWIG_exception(SWIG_SyntaxError, e.what());
} catch (const ezsnmp_data_logger::LoggerError& e) {
SWIG_exception(SWIG_RuntimeError, e.what());
} catch (const std::invalid_argument& e) {
SWIG_exception(SWIG_ValueError, e.what());
} catch (const std::out_of_range& e) {
SWIG_exception(SWIG_IndexError, e.what());
} catch (const std::exception& e) {
SWIG_exception(SWIG_RuntimeError, e.what());
}
}
Telemetry Datatypes (datatypes.i)¶
%{
#include "ezsnmp_data_logger/datatypes.h"
%}
%include "../include/ezsnmp_data_logger/datatypes.h"
%template(SampleVector) std::vector<ezsnmp_data_logger::Sample>;
%template(BatchVector) std::vector<ezsnmp_data_logger::Batch>;
%template(DutProbeResultVector) std::vector<ezsnmp_data_logger::DutProbeResult>;
%template(InspectRowVector) std::vector<ezsnmp_data_logger::InspectRow>;
#ifdef SWIGPYTHON
%extend ezsnmp_data_logger::Sample {
std::string __repr__() const {
return "Sample(metric_name='" + $self->metric_name + "', oid='" + $self->oid +
"', value='" + $self->display_value() + "', status=" +
std::to_string($self->status) + ")";
}
}
%extend ezsnmp_data_logger::Batch {
std::string __repr__() const {
return "Batch(dut_name='" + $self->dut_name + "', dut_ip='" + $self->dut_ip +
"', samples=" + std::to_string($self->samples.size()) + ")";
}
}
%extend ezsnmp_data_logger::LoggerStats {
std::string __repr__() const {
return $self->to_json(-1);
}
}
#endif
JSON Configuration & Validation (config.i)¶
%{
#include "ezsnmp_data_logger/config.h"
%}
%template(StringVector) std::vector<std::string>;
%include "../include/ezsnmp_data_logger/config.h"
%template(MetricConfigVector) std::vector<ezsnmp_data_logger::MetricConfig>;
%template(DutConfigVector) std::vector<ezsnmp_data_logger::DutConfig>;
%template(SinkConfigVector) std::vector<ezsnmp_data_logger::SinkConfig>;
#ifdef SWIGPYTHON
%extend ezsnmp_data_logger::LoggerConfig {
std::string __repr__() const {
return "LoggerConfig(duts=" + std::to_string($self->duts.size()) +
", sinks=" + std::to_string($self->sinks.size()) + ")";
}
}
#endif
DataLogger Engine & CLI Runner (data_logger.i)¶
%{
#include "ezsnmp_data_logger/version.h"
#include "ezsnmp_data_logger/data_logger.h"
#include "ezsnmp_data_logger/cli_runner.h"
%}
#ifdef SWIGPYTHON
%thread ezsnmp_data_logger::DataLogger::run;
%thread ezsnmp_data_logger::DataLogger::run_once;
%thread ezsnmp_data_logger::DataLogger::stop;
%thread ezsnmp_data_logger::DataLogger::probe;
%thread ezsnmp_data_logger::DataLogger::probe_json;
%thread ezsnmp_data_logger::DataLogger::request_json;
%thread ezsnmp_data_logger::DataLogger::set_json;
%thread ezsnmp_data_logger::CliRunner::run;
%thread ezsnmp_data_logger::CliRunner::execute;
#endif
%include "../include/ezsnmp_data_logger/version.h"
%include "../include/ezsnmp_data_logger/data_logger.h"
%include "../include/ezsnmp_data_logger/cli_runner.h"