~noskcaj/ubuntu/vivid/xfce4-netload-plugin/1.2.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* $Id: solaris.c 307 2003-08-31 12:54:36Z bwalle $ */


/*****************************************************************************
 *
 * init_osspecific()
 *
 * Init function
 *
 ****************************************************************************/

void init_osspecific(netdata* data)
{
    /* nothing */
    
#ifdef DEBUG
    fprintf( stderr, "The netload plugin was initialized for Sun Solaris.\n" );
#endif

}

/*****************************************************************************
 *
 * checkinterface()
 *
 * check if a given interface exists, return TRUE if it does and FALSE if not
 *
 ****************************************************************************/

int checkinterface(netdata* data)
{
    int validinterface = FALSE;
    int sockfd, i, numifs, numifreqs;
    size_t bufsize;
    char *buf;
    struct ifreq ifr, *ifrp;
    struct ifconf ifc;
    unsigned long rx_o, tx_o;

    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {   
        perror("socket");
        return FALSE;
    }
    if (ioctl(sockfd, SIOCGIFNUM, (char *) &numifs) < 0)
    {   
        perror("SIOCGIFNUM");
        close(sockfd);
        return FALSE;
    }
    bufsize = ((size_t) numifs) * sizeof(struct ifreq);
    buf = (char *) malloc(bufsize);
    if (!buf)
    {   
        perror("malloc");
        close(sockfd);
        return FALSE;
    }

    ifc.ifc_len = bufsize;
    ifc.ifc_buf = buf;

    if (ioctl(sockfd, SIOCGIFCONF, (char *) &ifc) < 0)
    {   
        perror("SIOCGIFCONF");
        close(sockfd);
        free(buf);
        return FALSE;
    }

    ifrp = ifc.ifc_req;
    numifreqs = ifc.ifc_len / sizeof(struct ifreq);

    for (i = 0; i < numifreqs; i++, ifrp++)
    {
        memset((char *)&ifr, 0, sizeof(ifr));
        strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
        /* do not check for loopback device as it cannot be monitored */
        if (!strncmp(ifr.ifr_name, "lo", 2))
            continue;
        if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
        {   
            perror("SIOCGIFFLAGS");
            continue;
        }
        if (!strcmp(data->ifdata.if_name, ifr.ifr_name) && (ifr.ifr_flags & IFF_UP))
        {
            validinterface = TRUE;
            break;
        }
    }
    free(buf);
    close(sockfd);

    return validinterface;
}

/*****************************************************************************
 *
 * get_stat()
 *
 * use the Solaris kstat_*() interface to gather statistics
 * We have to open/close *kc each time :(
 *
 ****************************************************************************/

int get_stat(netdata* data)
{
    kstat_t *ksp;
    kstat_named_t *knp;
    kstat_ctl_t *kc;
    unsigned long rx_o, tx_o;

    if ((kc = kstat_open()) == NULL)
    {   
        perror("kstat_open()");
        return 1;
    }

    rx_o = data->stats.rx_bytes; tx_o = data->stats.tx_bytes;

    ksp = kstat_lookup(kc, NULL, -1, data->ifdata.if_name);
    if (ksp && kstat_read(kc, ksp, NULL) >= 0)
    {   
        knp = (kstat_named_t *)kstat_data_lookup(ksp, "opackets");
        if (knp)
            data->stats.tx_packets = knp->value.ui32;
        knp = (kstat_named_t *)kstat_data_lookup(ksp, "ipackets");
        if (knp)
            data->stats.rx_packets = knp->value.ui32;
        knp = (kstat_named_t *)kstat_data_lookup(ksp, "obytes");
        if (knp)
            data->stats.tx_bytes = knp->value.ui32;
        knp = (kstat_named_t *)kstat_data_lookup(ksp, "rbytes");
        if (knp)
            data->stats.rx_bytes = knp->value.ui32;
        knp = (kstat_named_t *)kstat_data_lookup(ksp, "oerrors");
        if (knp)
            data->stats.tx_errors = knp->value.ui32;
        knp = (kstat_named_t *)kstat_data_lookup(ksp, "ierrors");
        if (knp)
            data->stats.rx_errors = knp->value.ui32;
    }

    kstat_close(kc);

    /* check for overflows */
    if (rx_o > data->stats.rx_bytes)
        data->stats.rx_over++;
    if (tx_o > data->stats.tx_bytes)
        data->stats.tx_over++;

    return 0;
}