sg_get_network_io_stats(3)

NAME
sg_get_network_io_stats, sg_get_network_io_stats_r, sg_get_network_io_stats_diff, sg_get_network_io_stats_diff_between, sg_free_network_io_stats, sg_network_io_compare_name - get network statistics

SYNOPSIS
\FC#include <statgrab.h>\F[]

\FCsg_network_io_stats *sg_get_network_io_stats\F[]
\FC(size_t *entries);\F[]

\FCsg_network_io_stats *sg_get_network_io_stats_r\F[]
\FC(size_t *entries);\F[]

\FCsg_network_io_stats *sg_get_network_io_stats_diff\F[]
\FC(size_t *entries);\F[]

\FCsg_network_io_stats *sg_get_network_io_stats_diff_between\F[]
\FC(const sg_network_io_stats *cur, const sg_network_io_stats *last, size_t *entries);\F[]

\FCsg_error sg_free_network_io_stats\F[]
\FC(sg_network_iface_stats *data);\F[]

\FCint sg_network_io_compare_name\F[]
\FC(const void *va, const void *vb);\F[]

DESCRIPTION
The \FCsg_get_network_io_stats\F[] functions provide network interface I/O statistics on a per interface basis. All get- and diff-functions take an optional \FCentries\F[] parameter, which points (when given) to a size_t to take the number of returned vector entries.

The \FCsg_get_network_io_stats\F[]() and \FCsg_get_network_io_stats_r\F[]() functions deliver the I/O-statistics since the interface has been attached to the system. The \FCsg_get_network_io_stats_diff\F[]() and \FCsg_get_network_io_stats_diff_between\F[]() deliver the difference between two calls of \FCsg_get_network_io_stats\F[]() or \FCsg_get_network_io_stats_r\F[](), respectively.

API Shortcut
function | returns | data owner
sg_get_network_io_stats | sg_network_io_stats * | libstatgrab (thread local)
sg_get_network_io_stats_r | sg_network_io_stats * | caller
sg_get_network_io_stats_diff | sg_network_io_stats * | libstatgrab (thread local)
sg_get_network_io_stats_diff_between | sg_network_io_stats * | caller

\FCsg_network_io_stats\F[] vectors received from \FCsg_get_network_io_stats_r\F[]() or \FCsg_get_network_io_stats_diff_between\F[]() must be freed using \FCsg_free_network_io_stats\F[]() when not needed any more. The caller is responsible for doing it.

Additionally a support function for \FCqsort\F[](3) are available: \FCsg_network_io_compare_name\F[](). Example

\FC
size_t entries;
sg_network_io_stats *network_stats = NULL;
while( NULL != ( network_stats = sg_get_network_io_stats_diff(&entries) ) ) {
    /* sort interface by name */
    qsort( network_stats, entries, sizeof(network_stats[0]), &sg_network_io_compare_name );
    show_network_io_stats( network_stats );
}
        \F[]

\FCsg_get_network_io_stats\F[] returns the network traffic stored in the kernel which holds the amount of data transferred since device was attached. On some platforms, such as Solaris 7, this value is stored in a 32bit int, so wraps around when it reaches 4GB. Other platforms, such as Solaris 8, hold the value in a 64bit int, which wraps somewhere near 17 million terabytes. The \FCsg_get_network_io_stats_diff\F[]() function and the \FCsg_get_network_io_stats_diff_between\F[]() function care about these overflows and try to detect overflows when the diff is calculated.

RETURN VALUES
All network statistics return a pointer to a structure of type \FCsg_network_io_stats\F[].

\FC
typedef struct {
        char *interface_name;
        unsigned long long tx;
        unsigned long long rx;
        unsigned long long ipackets;
        unsigned long long opackets;
        unsigned long long ierrors;
        unsigned long long oerrors;
        unsigned long long collisions;
        time_t systime;
} sg_network_io_stats;
    \F[]

\FCinterface_name\F[]
    The name known to the operating system. (eg. on linux it might be eth0, on AIX en0 and on FreeBSD fxp0)

\FCtx\F[]
    The number of bytes transmitted.

\FCrx\F[]
    The number of bytes received.

\FCipackets\F[]
    The number of packets received.

\FCopackets\F[]
    The number of packets transmitted.

\FCierrors\F[]
    The number of receive errors.

\FCoerrors\F[]
    The number of transmit errors.

\FCcollisions\F[]
    The number of collisions.

\FCsystime\F[]
    The timestamp when the above stats where collected in seconds since epoch or the time period over which \FCtx\F[] and \FCrx\F[] were transferred.

BUGS
\FCsg_get_network_io_stats_diff\F[] and \FCsg_get_network_io_stats_diff_between\F[] compare two lists of network interface related I/O statistics. Each entry occurring only in the second list is passed through to the resulting list as if it would have been compared to an entry with all statistic values set to 0. This implies, on the very first call \FCsg_get_network_io_stats_diff\F[] will return the same as \FCsg_get_network_io_stats\F[].

On operating systems that hold only 32bits of data there is a problem if the values wrap twice. For example, on Solaris 7 if 9GB is transferred and the operating system wraps at 4GB, the \FCsg_get_network_io_stats_diff\F[] function will return 5GB.

SEE ALSO
statgrab(3)

WEBSITE
⟨https://libstatgrab.org/⟩