2
* Copyright 2003,2005,2006 Bernhard Walle <bernhard@bwalle.de>
3
* Copyright 2010 Florian Rivoal <frivoal@gmail.com>
4
* -------------------------------------------------------------------------------------------------
6
* This program is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License as published by the Free
8
* Software Foundation; either version 2 of the License, or (at your option)
11
* This program is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16
* You should have received a copy of the GNU General Public License along with
17
* this program; if not, write to the Free Software Foundation, Inc., 675 Mass
18
* Ave, Cambridge, MA 02139, USA.
20
* -------------------------------------------------------------------------------------------------
24
* This is just a wrapper between the netload-plugin and the wormulon source.
25
* Wormulon is a small command-line util which displays the netload. You can find it
26
* at http://raisdorf.net/wormulon. Most sourcecode is taken from wormulon.
28
* Thanks to Hendrik Scholz. Only his work made it possible to support a large
29
* number of operating systems quickly without a library! Without him only
30
* Linux and FreeBSD (with foreign code from IceWM) would be supported.
40
#include "slurm.h" /* slurm structs */
42
#include <sys/types.h>
48
# include "wormulon/hpux.h"
49
# include "wormulon/hpux.c"
51
# include "src/macos.h"
52
# include "src/macos.c"
53
#elif __FreeBSD__ || __DragonFly__ || __FreeBSD_kernel__
54
# include "wormulon/freebsd.h"
55
# include "wormulon/freebsd.c"
57
# include "wormulon/linux.h"
58
# include "wormulon/linux.c"
59
#elif __OpenBSD__ || __MicroBSD__
60
# include "wormulon/openbsd.h"
61
# include "wormulon/openbsd.c"
63
# include "wormulon/netbsd.h"
64
# include "wormulon/netbsd.c"
66
# include "wormulon/solaris.h"
67
# include "wormulon/solaris.c"
69
/* should not get here */
70
# error "OS not supported"
74
/* ---------------------------------------------------------------------------------------------- */
75
int init_netload(netdata* data, const char* device)
77
memset( data, 0, sizeof(netdata) );
79
if (device == NULL || strlen(device) == 0)
84
strncpy( data->ifdata.if_name, device, INTERFACE_NAME_LENGTH);
85
data->ifdata.if_name[INTERFACE_NAME_LENGTH] = '\0';
87
init_osspecific( data );
89
data->ip_address[0] = 0;
90
data->ip_update_count = 0;
92
data->up_update_count = 0;
94
if (checkinterface(data) != TRUE)
96
data->correct_interface = FALSE;
100
/* init in a sane state */
102
data->backup_in = data->stats.rx_bytes;
103
data->backup_out = data->stats.tx_bytes;
105
data->correct_interface = TRUE;
107
PRINT_DBG("The netload plugin was initialized for '%s'.", device);
113
/* ---------------------------------------------------------------------------------------------- */
114
void get_current_netload(netdata* data, unsigned long *in, unsigned long *out, unsigned long *tot)
116
struct timeval curr_time;
119
if (! data->correct_interface)
121
if (in != NULL && out != NULL && tot != NULL)
123
*in = *out = *tot = 0;
127
gettimeofday(&curr_time, NULL);
129
delta_t = (double) ((curr_time.tv_sec - data->prev_time.tv_sec) * 1000000L
130
+ (curr_time.tv_usec - data->prev_time.tv_usec)) / 1000000.0;
134
if (data->backup_in > data->stats.rx_bytes)
136
data->cur_in = (int)( data->stats.rx_bytes / delta_t + 0.5);
140
data->cur_in = (int)( (data->stats.rx_bytes - data->backup_in) / delta_t + 0.5);
143
if (data->backup_out > data->stats.tx_bytes)
145
data->cur_out = (int)( data->stats.tx_bytes / delta_t + 0.5);
149
data->cur_out = (int)( (data->stats.tx_bytes - data->backup_out) / delta_t + 0.5);
152
if (in != NULL && out != NULL && tot != NULL)
155
*out = data->cur_out;
159
/* save 'new old' values */
160
data->backup_in = data->stats.rx_bytes;
161
data->backup_out = data->stats.tx_bytes;
163
/* do the same with time */
164
data->prev_time.tv_sec = curr_time.tv_sec;
165
data->prev_time.tv_usec = curr_time.tv_usec;
169
/* ---------------------------------------------------------------------------------------------- */
170
char* get_name(netdata* data)
172
return data->ifdata.if_name;
176
/* ---------------------------------------------------------------------------------------------- */
177
int get_interface_up(netdata* data)
181
struct sockaddr_in *p_sa;
183
/* if the update count is non-zero */
184
if (data->up_update_count > 0)
186
data->up_update_count--;
190
/* get the value from the operating system */
191
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
193
PRINT_DBG("Error in socket: %s", strerror(errno));
197
snprintf(ifr.ifr_name, IF_NAMESIZE, data->ifdata.if_name);
198
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) != 0)
200
PRINT_DBG("Error in ioctl(sockfd): %s", strerror(errno));
206
data->up = ((ifr.ifr_flags & IFF_UP) == IFF_UP) ? TRUE : FALSE;
207
data->up_update_count = UP_UPDATE_INTERVAL;
213
/* ---------------------------------------------------------------------------------------------- */
214
char* get_ip_address(netdata* data)
218
struct sockaddr_in *p_sa;
220
/* use cached value if possible and if the update count is non-zero */
221
if (data->ip_address && data->ip_update_count > 0)
223
data->ip_update_count--;
224
return data->ip_address;
227
/* get the value from the operating system */
228
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
230
PRINT_DBG("Error in socket: %s", strerror(errno));
234
snprintf(ifr.ifr_name, IF_NAMESIZE, data->ifdata.if_name);
235
if (ioctl(sockfd, SIOCGIFADDR, &ifr) != 0)
237
if (errno != EADDRNOTAVAIL)
239
PRINT_DBG("Error in ioctl(sockfd): %s", strerror(errno));
246
p_sa = (struct sockaddr_in*) &ifr.ifr_addr;
248
if (!inet_ntop(AF_INET, &p_sa->sin_addr, data->ip_address, IP_ADDRESS_LENGTH))
250
PRINT_DBG("Error in inet_ntop: %s", strerror(errno));
255
data->ip_update_count = IP_UPDATE_INTERVAL;
257
return data->ip_address;
261
/* ---------------------------------------------------------------------------------------------- */
262
void close_netload(netdata* data)
264
/* We need not code here */