2005-02-05 02:12:01 +08:00
|
|
|
/*
|
2005-08-01 06:54:22 +08:00
|
|
|
* Id: $Id$
|
2005-02-05 02:12:01 +08:00
|
|
|
* -------------------------------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; You may only use
|
|
|
|
* version 2 of the License, you have no option to use any other version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
|
|
|
* the GNU General Public License for more details.
|
2003-08-25 04:05:32 +08:00
|
|
|
*
|
2005-02-05 02:12:01 +08:00
|
|
|
* You should have received a copy of the GNU General Public License along with this program; if
|
|
|
|
* not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2003-08-25 04:05:32 +08:00
|
|
|
*
|
2005-02-05 02:12:01 +08:00
|
|
|
* -------------------------------------------------------------------------------------------------
|
2003-08-25 04:05:32 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is just a wrapper between the netload-plugin and the wormulon source.
|
|
|
|
* Wormulon is a small command-line util which displays the netload. You can find it
|
|
|
|
* at http://raisdorf.net/wormulon. Most sourcecode is taken from wormulon.
|
|
|
|
*
|
|
|
|
* Thanks to Hendrik Scholz. Only his work made it possible to support a large
|
|
|
|
* number of operating systems quickly without a library! Without him only
|
|
|
|
* Linux and FreeBSD (with foreign code from IceWM) would be supported.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* From Wormulon */
|
2003-08-26 05:08:58 +08:00
|
|
|
#include "net.h"
|
2003-08-25 04:05:32 +08:00
|
|
|
#include "os.h"
|
|
|
|
#include "wormulon.h"
|
|
|
|
#include "slurm.h" /* slurm structs */
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
#include <sys/types.h>
|
2005-08-01 06:54:22 +08:00
|
|
|
#include <errno.h>
|
2005-02-05 02:12:01 +08:00
|
|
|
|
2005-08-01 06:54:22 +08:00
|
|
|
#include "global.h"
|
2005-02-05 02:12:01 +08:00
|
|
|
|
2003-08-25 04:05:32 +08:00
|
|
|
#ifdef __HPUX__
|
2005-02-05 02:12:01 +08:00
|
|
|
# include "wormulon/hpux.h"
|
|
|
|
# include "wormulon/hpux.c"
|
2003-09-13 20:30:49 +08:00
|
|
|
#elif __APPLE__
|
2005-02-05 02:12:01 +08:00
|
|
|
# include "src/macos.h"
|
|
|
|
# include "src/macos.c"
|
2003-08-25 04:05:32 +08:00
|
|
|
#elif __FreeBSD__
|
2005-02-05 02:12:01 +08:00
|
|
|
# include "wormulon/freebsd.h"
|
|
|
|
# include "wormulon/freebsd.c"
|
2003-08-25 04:05:32 +08:00
|
|
|
#elif __linux__
|
2005-02-05 02:12:01 +08:00
|
|
|
# include "wormulon/linux.h"
|
|
|
|
# include "wormulon/linux.c"
|
2003-08-25 04:05:32 +08:00
|
|
|
#elif __OpenBSD__ || __MicroBSD__
|
2005-02-05 02:12:01 +08:00
|
|
|
# include "wormulon/openbsd.h"
|
|
|
|
# include "wormulon/openbsd.c"
|
2003-08-25 04:05:32 +08:00
|
|
|
#elif __NetBSD__
|
2005-02-05 02:12:01 +08:00
|
|
|
# include "wormulon/netbsd.h"
|
|
|
|
# include "wormulon/netbsd.c"
|
2003-08-25 04:05:32 +08:00
|
|
|
#elif __Solaris__
|
2005-02-05 02:12:01 +08:00
|
|
|
# include "wormulon/solaris.h"
|
|
|
|
# include "wormulon/solaris.c"
|
2003-08-25 04:05:32 +08:00
|
|
|
#else
|
|
|
|
/* should not get here */
|
2005-02-05 02:12:01 +08:00
|
|
|
# error "OS not supported"
|
2003-08-25 04:05:32 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
2003-09-06 20:37:20 +08:00
|
|
|
int init_netload(netdata* data, const char* device)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
2003-08-26 05:08:58 +08:00
|
|
|
memset( data, 0, sizeof(netdata) );
|
2003-09-06 20:37:20 +08:00
|
|
|
|
|
|
|
if (device == NULL || strlen(device) == 0)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
strncpy( data->ifdata.if_name, device, INTERFACE_NAME_LENGTH);
|
|
|
|
data->ifdata.if_name[INTERFACE_NAME_LENGTH] = '\0';
|
2003-08-25 04:05:32 +08:00
|
|
|
|
2003-08-31 20:54:36 +08:00
|
|
|
init_osspecific( data );
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
data->ip_address[0] = 0;
|
|
|
|
data->ip_update_count = 0;
|
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
if (checkinterface(data) != TRUE)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
2003-08-26 05:08:58 +08:00
|
|
|
data->correct_interface = FALSE;
|
2003-09-06 20:37:20 +08:00
|
|
|
return FALSE;
|
2003-08-25 04:05:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* init in a sane state */
|
2003-08-26 05:08:58 +08:00
|
|
|
get_stat(data);
|
|
|
|
data->backup_in = data->stats.rx_bytes;
|
|
|
|
data->backup_out = data->stats.tx_bytes;
|
2003-08-25 04:05:32 +08:00
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
data->correct_interface = TRUE;
|
2003-08-27 04:34:46 +08:00
|
|
|
|
2005-08-01 06:54:22 +08:00
|
|
|
PRINT_DBG("The netload plugin was initialized for '%s'.", device);
|
2003-09-06 20:37:20 +08:00
|
|
|
|
|
|
|
return TRUE;
|
2003-08-25 04:05:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
2003-08-26 05:08:58 +08:00
|
|
|
void get_current_netload(netdata* data, unsigned long *in, unsigned long *out, unsigned long *tot)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
|
|
|
struct timeval curr_time;
|
|
|
|
double delta_t;
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
if (! data->correct_interface)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
2005-02-05 02:12:01 +08:00
|
|
|
if (in != NULL && out != NULL && tot != NULL)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
|
|
|
*in = *out = *tot = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gettimeofday(&curr_time, NULL);
|
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
delta_t = (double) ((curr_time.tv_sec - data->prev_time.tv_sec) * 1000000L
|
|
|
|
+ (curr_time.tv_usec - data->prev_time.tv_usec)) / 1000000.0;
|
2003-08-25 04:05:32 +08:00
|
|
|
|
|
|
|
/* update */
|
2003-08-26 05:08:58 +08:00
|
|
|
get_stat(data);
|
|
|
|
if (data->backup_in > data->stats.rx_bytes)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
2003-08-31 20:54:36 +08:00
|
|
|
data->cur_in = (int)( data->stats.rx_bytes / delta_t + 0.5);
|
2003-08-25 04:05:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-08-31 20:54:36 +08:00
|
|
|
data->cur_in = (int)( (data->stats.rx_bytes - data->backup_in) / delta_t + 0.5);
|
2003-08-25 04:05:32 +08:00
|
|
|
}
|
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
if (data->backup_out > data->stats.tx_bytes)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
2003-08-31 20:54:36 +08:00
|
|
|
data->cur_out = (int)( data->stats.tx_bytes / delta_t + 0.5);
|
2003-08-25 04:05:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-08-31 20:54:36 +08:00
|
|
|
data->cur_out = (int)( (data->stats.tx_bytes - data->backup_out) / delta_t + 0.5);
|
2003-08-25 04:05:32 +08:00
|
|
|
}
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
if (in != NULL && out != NULL && tot != NULL)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
2003-08-26 05:08:58 +08:00
|
|
|
*in = data->cur_in;
|
|
|
|
*out = data->cur_out;
|
2003-08-25 04:05:32 +08:00
|
|
|
*tot = *in + *out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save 'new old' values */
|
2003-08-26 05:08:58 +08:00
|
|
|
data->backup_in = data->stats.rx_bytes;
|
|
|
|
data->backup_out = data->stats.tx_bytes;
|
2003-08-25 04:05:32 +08:00
|
|
|
|
|
|
|
/* do the same with time */
|
2003-08-26 05:08:58 +08:00
|
|
|
data->prev_time.tv_sec = curr_time.tv_sec;
|
|
|
|
data->prev_time.tv_usec = curr_time.tv_usec;
|
2003-08-25 04:05:32 +08:00
|
|
|
}
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
char* get_name(netdata* data)
|
|
|
|
{
|
|
|
|
return data->ifdata.if_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
char* get_ip_address(netdata* data)
|
|
|
|
{
|
|
|
|
int sockfd;
|
|
|
|
struct ifreq ifr;
|
|
|
|
struct sockaddr_in *p_sa;
|
|
|
|
|
|
|
|
/* use cached value if possible and if the update count is non-zero */
|
|
|
|
if (data->ip_address && data->ip_update_count > 0)
|
|
|
|
{
|
|
|
|
data->ip_update_count--;
|
|
|
|
return data->ip_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the value from the operating system */
|
|
|
|
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
|
|
|
{
|
2005-08-01 06:54:22 +08:00
|
|
|
PRINT_DBG("Error in socket: %s", strerror(errno));
|
2005-02-05 02:12:01 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(ifr.ifr_name, IF_NAMESIZE, data->ifdata.if_name);
|
|
|
|
if (ioctl(sockfd, SIOCGIFADDR, &ifr) != 0)
|
|
|
|
{
|
2005-02-05 02:14:41 +08:00
|
|
|
close(sockfd);
|
2005-08-01 06:54:22 +08:00
|
|
|
PRINT_DBG("Error in ictl(sockfd): %s", strerror(errno));
|
2005-02-05 02:12:01 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-02-05 02:14:41 +08:00
|
|
|
close(sockfd);
|
2005-02-05 02:12:01 +08:00
|
|
|
|
|
|
|
p_sa = (struct sockaddr_in*) &ifr.ifr_addr;
|
|
|
|
|
|
|
|
if (!inet_ntop(AF_INET, &p_sa->sin_addr, data->ip_address, IP_ADDRESS_LENGTH))
|
|
|
|
{
|
2005-08-01 06:54:22 +08:00
|
|
|
PRINT_DBG("Error in inet_ntop: %s", strerror(errno));
|
2005-02-05 02:12:01 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now updated */
|
|
|
|
data->ip_update_count = IP_UPDATE_INTERVAL;
|
|
|
|
|
|
|
|
return data->ip_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
2003-08-26 05:08:58 +08:00
|
|
|
void close_netload(netdata* data)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
|
|
|
/* We need not code here */
|
|
|
|
}
|
|
|
|
|