Reference

sg_free_network_iface_stats(3)

get network interface statistics

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[0]

FCsg_network_iface_stats *sg_get_network_iface_statsF[0] FC(size_t *entries);F[0]

FCsg_network_iface_stats *sg_get_network_iface_stats_rF[0] FC(size_t *entries);F[0]

FCsg_error sg_free_network_iface_statsF[0] FC(sg_network_iface_stats *data);F[0]

FCint sg_network_iface_compare_nameF[0] FC(const void *va, const void *vb);F[0]

DESCRIPTION

The functions FCsg_get_network_iface_statsF[0]() and FCsg_get_network_iface_stats_rF[0]() 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 FCentriesF[0] parameter, which points (when given) to a size_t to take the number of returned vector entries.

API Shortcut

functionreturnsdata owner
sg_get_network_iface_statsFCsg_network_iface_statsF[0] *libstatgrab (thread local)
sg_get_network_iface_stats_rFCsg_network_iface_statsF[0] *caller

The FCsg_network_iface_statsF[0] vectors received from FCsg_get_network_iface_stats_rF[0]() must be freed using FCsg_free_network_iface_statsF[0]() when not needed any more. The caller is responsible for doing it.

Additionally a support function for FCqsortF[0](3) in available: FCsg_network_iface_compare_nameF[0]().

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[0]

RETURN VALUES

The FCsg_get_network_iface_statsF[0] returns a pointer to a structure of type FCsg_network_iface_statsF[0].

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

Note: The FCSG_IFACE_DUPLEX_UNKNOWNF[0] 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[0]

FCinterface_nameF[0]

The name known to the operating system. (eg. on linux it might be eth0)

FCspeedF[0]

The speed of the interface, in megabits/sec.

FCduplexF[0]

The duplex state the interface is in. See sg_iface_duplex for permitted values.

FCupF[0]

Whether the interface is up.

FCsystimeF[0]

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/⟩

History