.\" Man page generated from reStructuredText. . .TH "PYTHON-IPFIX" "1" "Mar 07, 2020" "0.9" "python-ipfix" .SH NAME python-ipfix \- python-ipfix Documentation . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .sp IPFIX implementation for Python 3.3. .sp This module provides a Python interface to IPFIX message streams, and provides tools for building IPFIX Exporting and Collecting Processes. It handles message framing and deframing, encoding and decoding IPFIX data records using templates, and a bridge between IPFIX ADTs and appropriate Python data types. .sp Before using any of the functions of this module, it is necessary to populate the information model with Information Elements. \fI\%ipfix.ie.use_iana_default()\fP populates the default IANA IPFIX Information Element Registry shipped with the module; this is the current registry as of release time. \fI\%ipfix.ie.use_5103_default()\fP populates the reverse counterpart IEs as in \fI\%RFC 5103\fP\&. The module also supports the definition of enterprise\-specific Information Elements via \fI\%ipfix.ie.for_spec()\fP and \fI\%ipfix.ie.use_specfile()\fP; see \fI\%ipfix.ie\fP for more. .sp For reading and writing of records to IPFIX message streams with automatic message boundary management, see the \fI\%ipfix.reader\fP and \fI\%ipfix.writer\fP modules, respectively. For manual reading and writing of messages, see \fI\%ipfix.message\fP\&. In any case, exporters will need to define templates; see \fI\%ipfix.template\fP\&. .sp This module is copyright 2013 Brian Trammell. It is made available under the terms of the \fI\%GNU Lesser General Public License\fP, version 3 or, at your option, any later version. .sp Reference documentation for each module is found in the subsections below. .SH MODULE IPFIX.TYPES .sp Implementation of IPFIX abstract data types (ADT) and mappings to Python types. .sp Maps each IPFIX ADT to the corresponding Python type, as below: .TS center; |l|l|. _ T{ IPFIX Type T} T{ Python Type T} _ T{ octetArray T} T{ bytes T} _ T{ unsigned8 T} T{ int T} _ T{ unsigned16 T} T{ int T} _ T{ unsigned32 T} T{ int T} _ T{ unsigned64 T} T{ int T} _ T{ signed8 T} T{ int T} _ T{ signed16 T} T{ int T} _ T{ signed32 T} T{ int T} _ T{ signed64 T} T{ int T} _ T{ float32 T} T{ float T} _ T{ float64 T} T{ float T} _ T{ boolean T} T{ bool T} _ T{ macAddress T} T{ bytes T} _ T{ string T} T{ str T} _ T{ dateTimeSeconds T} T{ datetime T} _ T{ dateTimeMilliseconds T} T{ datetime T} _ T{ dateTimeMicroseconds T} T{ datetime T} _ T{ dateTimeNanoseconds T} T{ datetime T} _ T{ ipv4Address T} T{ ipaddress T} _ T{ ipv6Address T} T{ ipaddress T} _ .TE .sp Though client code generally will not use this module directly, it defines how each IPFIX abstract data type will be represented in Python, and the concrete IPFIX representation of each type. Type methods operate on buffers, as used internally by the \fI\%ipfix.message.MessageBuffer\fP class, so we\(aqll create one to illustrate encoding and decoding: .sp .nf .ft C >>> import ipfix.types >>> buf = memoryview(bytearray(16)) .ft P .fi .sp Each of the encoding methods returns the offset into the buffer of the first byte after the encoded value; since we\(aqre always encoding to the beginning of the buffer in this example, this is equivalent to the length. We use this to bound the encoded value on subsequent decode. .sp Integers are represented by the python int type: .sp .nf .ft C >>> unsigned32 = ipfix.types.for_name("unsigned32") >>> length = unsigned32.encode_single_value_to(42, buf, 0) >>> buf[0:length].tolist() [0, 0, 0, 42] >>> unsigned32.decode_single_value_from(buf, 0, length) 42 .ft P .fi .sp \&...floats by the float type, with the usual caveats about precision: .sp .nf .ft C >>> float32 = ipfix.types.for_name("float32") >>> length = float32.encode_single_value_to(42.03579, buf, 0) >>> buf[0:length].tolist() [66, 40, 36, 166] >>> float32.decode_single_value_from(buf, 0, length) 42.035789489746094 .ft P .fi .sp \&...strings by the str type, encoded as UTF\-8: .sp .nf .ft C >>> string = ipfix.types.for_name("string") >>> length = string.encode_single_value_to("Grüezi", buf, 0) >>> buf[0:length].tolist() [71, 114, 195, 188, 101, 122, 105] >>> string.decode_single_value_from(buf, 0, length) \(aqGrüezi\(aq .ft P .fi .sp \&...addresses as the IPv4Address and IPv6Address types in the ipaddress module: .sp .nf .ft C >>> from ipaddress import ip_address >>> ipv4Address = ipfix.types.for_name("ipv4Address") >>> length = ipv4Address.encode_single_value_to(ip_address("198.51.100.27"), buf, 0) >>> buf[0:length].tolist() [198, 51, 100, 27] >>> ipv4Address.decode_single_value_from(buf, 0, length) IPv4Address(\(aq198.51.100.27\(aq) >>> ipv6Address = ipfix.types.for_name("ipv6Address") >>> length = ipv6Address.encode_single_value_to(ip_address("2001:db8::c0:ffee"), buf, 0) >>> buf[0:length].tolist() [32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 255, 238] >>> ipv6Address.decode_single_value_from(buf, 0, length) IPv6Address(\(aq2001:db8::c0:ffee\(aq) .ft P .fi .sp \&...and the timestamps of various precision as a python datetime, encoded as per RFC5101bis: .sp .nf .ft C >>> from datetime import datetime >>> from datetime import timezone >>> dtfmt = "%Y\-%m\-%d %H:%M:%S.%f" >>> dt = datetime.strptime("2013\-06\-21 14:00:03.456789", dtfmt) .ft P .fi .sp dateTimeSeconds truncates microseconds: .sp .nf .ft C >>> dateTimeSeconds = ipfix.types.for_name("dateTimeSeconds") >>> length = dateTimeSeconds.encode_single_value_to(dt, buf, 0) >>> buf[0:length].tolist() [81, 196, 92, 99] >>> dateTimeSeconds.decode_single_value_from(buf, 0, length).strftime(dtfmt) \(aq2013\-06\-21 14:00:03.000000\(aq .ft P .fi .sp dateTimeMilliseconds truncates microseconds to the nearest millisecond: .sp .nf .ft C >>> dateTimeMilliseconds = ipfix.types.for_name("dateTimeMilliseconds") >>> length = dateTimeMilliseconds.encode_single_value_to(dt, buf, 0) >>> buf[0:length].tolist() [0, 0, 1, 63, 103, 8, 228, 128] >>> dateTimeMilliseconds.decode_single_value_from(buf, 0, length).strftime(dtfmt) \(aq2013\-06\-21 14:00:03.456000\(aq .ft P .fi .sp dateTimeMicroseconds exports microseconds fully in NTP format: .sp .nf .ft C >>> dateTimeMicroseconds = ipfix.types.for_name("dateTimeMicroseconds") >>> length = dateTimeMicroseconds.encode_single_value_to(dt, buf, 0) >>> buf[0:length].tolist() [81, 196, 92, 99, 116, 240, 32, 0] >>> dateTimeMicroseconds.decode_single_value_from(buf, 0, length).strftime(dtfmt) \(aq2013\-06\-21 14:00:03.456789\(aq .ft P .fi .sp dateTimeNanoseconds is also supported, but is identical to dateTimeMicroseconds, as the datetime class in Python only supports microsecond\-level timing. .INDENT 0.0 .TP .B class ipfix.types.IpfixType(name, num, valenc, valdec, valstr, valparse, roottype=None) Abstract interface for all IPFIX types. Used internally. .UNINDENT .INDENT 0.0 .TP .B exception ipfix.types.IpfixTypeError(*args) Raised when attempting to do an unsupported operation on a type .UNINDENT .INDENT 0.0 .TP .B class ipfix.types.OctetArrayType(name, num, valenc=, valdec=, valstr=, valparse=, roottype=None) Type encoded by byte array packing. Used internally. .UNINDENT .INDENT 0.0 .TP .B class ipfix.types.StructType(name, num, stel, valenc=, valdec=, valstr=, valparse=, roottype=None) Type encoded by struct packing. Used internally. .UNINDENT .INDENT 0.0 .TP .B ipfix.types.decode_varlen(buf, offset) Decode a IPFIX varlen encoded length; used internally by template .UNINDENT .INDENT 0.0 .TP .B ipfix.types.encode_varlen(buf, offset, length) Encode a IPFIX varlen encoded length; used internally by template .UNINDENT .INDENT 0.0 .TP .B ipfix.types.for_name(name) Return an IPFIX type for a given type name .INDENT 7.0 .TP .B Parameters \fBname\fP \-\- the name of the type to look up .TP .B Returns IpfixType \-\- type instance for that name .TP .B Raises IpfixTypeError .UNINDENT .UNINDENT .INDENT 0.0 .TP .B ipfix.types.use_integer_ipv4() Use integers instead of ipaddress.IPv4Address to store IPv4 addresses. Changes behavior globally; should be called before using any IPFIX types. Designed for use with numpy arrays, to not require a Python object for storing IP addresses. .UNINDENT .SH MODULE IPFIX.IE .sp IESpec\-based interface to IPFIX information elements, and interface to use the default IPFIX IANA Information Model .sp An IESpec is a string representation of an IPFIX information element, including all the information required to define it, as documented in Section 9 of \fI\%http://tools.ietf.org/html/draft\-ietf\-ipfix\-ie\-doctors\fP\&. It has the format: .INDENT 0.0 .INDENT 3.5 name(pen/num)[size] .UNINDENT .UNINDENT .sp To specify a new Information Element, a complete IESpec must be passed to for_spec(): .sp .nf .ft C >>> import ipfix.ie >>> e = ipfix.ie.for_spec("myNewInformationElement(35566/1)") >>> e InformationElement(\(aqmyNewInformationElement\(aq, 35566, 1, ipfix.types.for_name(\(aqstring\(aq), 65535) .ft P .fi .sp The string representation of an InformationElement is its IESpec: .sp .nf .ft C >>> str(e) \(aqmyNewInformationElement(35566/1)[65535]\(aq .ft P .fi .sp To get an Information Element already specified, an incomplete specification can be passed; a name or number is enough: .sp .nf .ft C >>> ipfix.ie.use_iana_default() >>> ipfix.ie.use_5103_default() >>> str(ipfix.ie.for_spec("octetDeltaCount")) \(aqoctetDeltaCount(0/1)[8]\(aq >>> str(ipfix.ie.for_spec("(2)")) \(aqpacketDeltaCount(0/2)[8]\(aq .ft P .fi .sp Reduced\-length encoding and fixed\-length sequence types are supported by the for_length method; this is used internally by templates. .sp .nf .ft C >>> str(e.for_length(32)) \(aqmyNewInformationElement(35566/1)[32]\(aq .ft P .fi .sp An Information Element object can also be used to translate between native Python and string representations of an Information Element value: .sp .nf .ft C >>> ipfix.ie.for_spec("sourceIPv4Address").parse("192.0.2.19") IPv4Address(\(aq192.0.2.19\(aq) >>> from datetime import datetime >>> ipfix.ie.for_spec("flowEndMilliseconds").unparse(datetime(2013,6,21,14)) \(aq2013\-06\-21 14:00:00.000\(aq .ft P .fi .sp Most client code will only need the \fI\%use_iana_default()\fP, \fI\%use_5103_default()\fP, and \fI\%use_specfile()\fP functions; client code using tuple interfaces will need \fI\%spec_list()\fP as well. .INDENT 0.0 .TP .B class ipfix.ie.InformationElement(name, pen, num, ietype=ipfix.types.for_name(\(aqoctetArray\(aq), length=None, valstr=None, valparse=None) An IPFIX Information Element (IE). This is essentially a five\-tuple of name, element number (num), a private enterprise number (pen; 0 if it is an IANA registered IE), a type, and a length. .sp Information Elements may also have value string and parser functions, for representing the values as strings; if not set, these default to .sp InformationElement instances should be obtained using the \fI\%for_spec()\fP or \fI\%for_template_entry()\fP functions. .INDENT 7.0 .TP .B for_length(length) Get an instance of this IE for the specified length. Used to support reduced\-length encoding (RLE). .INDENT 7.0 .TP .B Parameters \fBlength\fP \-\- length of the new IE .TP .B Returns this IE if length matches, or a new IE for the length .TP .B Raises ValueError .UNINDENT .UNINDENT .INDENT 7.0 .TP .B parse(s) Parse a string to a value using the conversion function for this Information Element. Uses the default string conversion for the IE\(aqs type if not overridden at IE creation time. .INDENT 7.0 .TP .B Parameters \fBs\fP \-\- string to parse using this IEs\(aqs string conversion .TP .B Returns value for given string .TP .B Raises ValueError is not a valid string representation for this IE .UNINDENT .UNINDENT .INDENT 7.0 .TP .B unparse(v) Unparse a value to a string using the conversion function for this Information Element. Uses the default string conversion for the IE\(aqs type if not overridden at IE creation time. .INDENT 7.0 .TP .B Parameters \fBv\fP \-\- value to unparse using this IEs\(aqs string conversion .TP .B Returns string representation of v .TP .B Raises ValueError if v is not a valid value for this IE .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B class ipfix.ie.InformationElementList(iterable=None) A hashable ordered list of Information Elements. .sp Used internally by templates, and to specify the order of tuples to the tuple append and iterator interfaces. Get an instance by calling \fI\%spec_list()\fP .UNINDENT .INDENT 0.0 .TP .B ipfix.ie.clear_infomodel() Reset the cache of known Information Elements. .UNINDENT .INDENT 0.0 .TP .B ipfix.ie.for_spec(spec) Get an IE from the cache of known IEs, or create a new IE if not found, given an IESpec. .INDENT 7.0 .TP .B Parameters \fBspec\fP \-\- IEspec, as in draft\-ietf\-ipfix\-ie\-doctors, of the form name(pen/num)[size]; some fields may be omitted unless creating a new IE in the cache. .TP .B Returns an IE for the name .TP .B Raises ValueError .UNINDENT .UNINDENT .INDENT 0.0 .TP .B ipfix.ie.for_template_entry(pen, num, length) Get an IE from the cache of known IEs, or create a new IE if not found, given a private enterprise number, element number, and length. Used internally by Templates. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBpen\fP \-\- private enterprise number, or 0 for an IANA IE .IP \(bu 2 \fBnum\fP \-\- IE number (Element ID) .IP \(bu 2 \fBlength\fP \-\- length of the IE in bytes .UNINDENT .TP .B Returns an IE for the given pen, num, and length. If the IE has not been previously added to the cache of known IEs, the IE will be named _ipfix_pen_num, and have octetArray as a type. .UNINDENT .UNINDENT .INDENT 0.0 .TP .B ipfix.ie.parse_spec(spec) Parse an IESpec into name, pen, number, typename, and length fields .UNINDENT .INDENT 0.0 .TP .B ipfix.ie.spec_list(specs) Given a list or iterable of IESpecs, return a hashable list of IEs. Pass this as the ielist argument to the tuple export and iterator functions. .INDENT 7.0 .TP .B Parameters \fBspecs\fP \-\- list of IESpecs .TP .B Returns a new Information Element List, suitable for use with the tuple export and iterator functions in \fBmessage\fP .TP .B Raises ValueError .UNINDENT .UNINDENT .INDENT 0.0 .TP .B ipfix.ie.use_5103_default() Load the module internal list of RFC 5103 reverse IEs for IANA registered IEs into the cache of known IEs. Normally, biflow\-aware client code should call this just after use_iana_default(). .UNINDENT .INDENT 0.0 .TP .B ipfix.ie.use_iana_default() Load the module internal list of IANA registered IEs into the cache of known IEs. Normally, client code should call this before using any other part of this module. .UNINDENT .INDENT 0.0 .TP .B ipfix.ie.use_specfile(filename) Load a file listing IESpecs into the cache of known IEs .INDENT 7.0 .TP .B Parameters \fBfilename\fP \-\- name of file containing IESpecs to open .TP .B Raises ValueError .UNINDENT .UNINDENT .SH MODULE IPFIX.TEMPLATE .sp Representation of IPFIX templates. Provides template\-based packing and unpacking of data in IPFIX messages. .sp For reading, templates are handled internally. For writing, use \fBfrom_ielist()\fP to create a template. .sp See \fI\%ipfix.message\fP for examples. .INDENT 0.0 .TP .B exception ipfix.template.IpfixDecodeError(*args) Raised when decoding a malformed IPFIX message .UNINDENT .INDENT 0.0 .TP .B exception ipfix.template.IpfixEncodeError(*args) Raised on internal encoding errors, or if message MTU is too small .UNINDENT .INDENT 0.0 .TP .B class ipfix.template.Template(tid=0, iterable=None) An IPFIX Template. .sp A template is an ordered list of IPFIX Information Elements with an ID. .INDENT 7.0 .TP .B append(ie) Append an IE to this Template .UNINDENT .INDENT 7.0 .TP .B count() Count IEs in this template .UNINDENT .INDENT 7.0 .TP .B decode_from(buf, offset, packplan=None) Decodes a record into a tuple containing values in template order .UNINDENT .INDENT 7.0 .TP .B decode_namedict_from(buf, offset, recinf=None) Decodes a record from a buffer into a dict keyed by IE name. .UNINDENT .INDENT 7.0 .TP .B decode_tuple_from(buf, offset, recinf=None) Decodes a record from a buffer into a tuple, ordered as the IEs in the InformationElementList given as recinf. .UNINDENT .INDENT 7.0 .TP .B encode_namedict_to(buf, offset, rec, recinf=None) Encodes a record from a dict containing values keyed by IE name .UNINDENT .INDENT 7.0 .TP .B encode_template_to(buf, offset, setid) Encodes the template to a buffer. Encodes as a Template if setid is TEMPLATE_SET_ID, as an Options Template if setid is OPTIONS_SET_ID. .UNINDENT .INDENT 7.0 .TP .B encode_to(buf, offset, vals, packplan=None) Encodes a record from a tuple containing values in template order .UNINDENT .INDENT 7.0 .TP .B encode_tuple_to(buf, offset, rec, recinf=None) Encodes a record from a tuple containing values ordered as the IEs in the template. .UNINDENT .INDENT 7.0 .TP .B finalize() Compile a default packing plan. Called after append()ing all IEs. .UNINDENT .INDENT 7.0 .TP .B fixlen_count() Count of fixed\-length IEs in this template before the first variable\-length IE; this is the size of the portion of the template which can be encoded/decoded efficiently. .UNINDENT .INDENT 7.0 .TP .B identical_to(other) Determine if two templates are identical to each other. .sp Two templates are considered identical if they contain the same IEs in the same order, and the same scope count. Template ID is not considered as part of the test for template identity. .UNINDENT .INDENT 7.0 .TP .B packplan_for_ielist Given a list of IEs, devise and cache a packing plan. Used by the tuple interfaces. .UNINDENT .UNINDENT .INDENT 0.0 .TP .B class ipfix.template.TemplatePackingPlan(tmpl, indices) Plan to pack/unpack a specific set of indices for a template. Used internally by Templates for efficient encoding and decoding. .UNINDENT .INDENT 0.0 .TP .B ipfix.template.decode_template_from(buf, offset, setid) Decodes a template from a buffer. Decodes as a Template if setid is TEMPLATE_SET_ID, as an Options Template if setid is OPTIONS_SET_ID. .UNINDENT .INDENT 0.0 .TP .B ipfix.template.for_specs(tid, *specs) Create a template from a template ID and a list of IESpecs .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBtid\fP \-\- Template ID, must be between 256 and 65535. .IP \(bu 2 \fB*specs\fP \-\- .sp List of IESpecs .UNINDENT .TP .B Returns A new Template, ready to use for writing to a Message .UNINDENT .UNINDENT .SH MODULE IPFIX.MESSAGE .sp Provides the MessageBuffer class for encoding and decoding IPFIX Messages. .sp This interface allows direct control over Messages; for reading or writing records automatically from/to streams, see \fI\%ipfix.reader\fP and \fI\%ipfix.writer\fP, respectively. .sp To create a message buffer: .sp .nf .ft C >>> import ipfix.message >>> msg = ipfix.message.MessageBuffer() >>> msg .ft P .fi .sp To prepare the buffer to write records: .sp .nf .ft C >>> msg.begin_export(8304) >>> msg .ft P .fi .sp Note that the buffer grows to contain the message header. .sp To write records to the buffer, first you\(aqll need a template: .sp .nf .ft C >>> import ipfix.ie >>> ipfix.ie.use_iana_default() >>> import ipfix.template >>> tmpl = ipfix.template.from_ielist(256, \&... ipfix.ie.spec_list(("flowStartMilliseconds", \&... "sourceIPv4Address", \&... "destinationIPv4Address", \&... "packetDeltaCount"))) >>> tmpl