sg_get_network_iface_stats(3)

NAME
sg_get_network_iface_stats, sg_get_network_iface_stats_r, sg_free_network_iface_stats, sg_network_iface_compare_name - get network interface statistics

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

\FCsg_network_iface_stats *sg_get_network_iface_stats\F[]
\FC(size_t *entries);\F[]

\FCsg_network_iface_stats *sg_get_network_iface_stats_r\F[]
\FC(size_t *entries);\F[]

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

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

DESCRIPTION
The functions \FCsg_get_network_iface_stats\F[]() and \FCsg_get_network_iface_stats_r\F[]() return statistics about the network interfaces in the machine. Specifically, they return the speed of the interface, the duplex state, and whether it is currently up. Both functions take an optional \FCentries\F[] parameter, which points (when given) to a size_t to take the number of returned vector entries.

API Shortcut
function | returns | data owner
sg_get_network_iface_stats | \FCsg_network_iface_stats\F[] * | libstatgrab (thread local)
sg_get_network_iface_stats_r | \FCsg_network_iface_stats\F[] * | caller

The \FCsg_network_iface_stats\F[] vectors received from \FCsg_get_network_iface_stats_r\F[]() must be freed using \FCsg_free_network_iface_stats\F[]() when not needed any more. The caller is responsible for doing it.

Additionally a support function for \FCqsort\F[](3) in available: \FCsg_network_iface_compare_name\F[]().

Sort Example

\FC
size_t entries;
sg_network_iface_stats *network_iface_stats = NULL;
while( NULL != ( network_iface_stats = sg_get_network_iface_stats(&entries) ) ) {
    /* order entries alphabetically using the mountpoint */
    qsort( network_iface_stats, entries, sizeof(network_iface_stats[0]), &sg_network_iface_compare_name );
    show_network_iface_stats( network_iface_stats );
}
        \F[]

RETURN VALUES
The \FCsg_get_network_iface_stats\F[] returns a pointer to a structure of type \FCsg_network_iface_stats\F[].

\FC
typedef enum{
        SG_IFACE_DUPLEX_FULL,
        SG_IFACE_DUPLEX_HALF,
        SG_IFACE_DUPLEX_UNKNOWN
}sg_iface_duplex;
    \F[]

Note: The \FCSG_IFACE_DUPLEX_UNKNOWN\F[] value could mean that duplex hasn't been negotiated yet.

\FC
typedef struct {
        char *interface_name;
        int speed;
        sg_iface_duplex duplex;
        int up;
        time_t systime;
} sg_network_iface_stats;
    \F[]

\FCinterface_name\F[]
    The name known to the operating system. (eg. on linux it might be eth0)

\FCspeed\F[]
    The speed of the interface, in megabits/sec.

\FCduplex\F[]
    The duplex state the interface is in. See sg_iface_duplex for permitted values.

\FCup\F[]
    Whether the interface is up.

\FCsystime\F[]
    The timestamp when the above stats where collected in seconds since epoch

BUGS
Getting specific details may require elevated privileges, eg. on Linux the interface speed isn't visible to non-privileged processes.

The compare functions exist rather for backward compatibility than for functionality enhancements. Limited flexibility (e.g. reverse order) and lack of optimising opportunities for the compiler leads to the recommendation to implement the required compare routines locally.

SEE ALSO
statgrab(3)

WEBSITE
⟨https://libstatgrab.org/⟩