2003-08-18 02:55:46 +08:00
|
|
|
/* XFce 4 - Netload Plugin
|
|
|
|
* Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de>
|
|
|
|
*
|
|
|
|
* 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; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2003-08-14 02:14:27 +08:00
|
|
|
*
|
2003-08-18 02:55:46 +08:00
|
|
|
* 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-14 02:14:27 +08:00
|
|
|
*
|
2003-08-18 02:55:46 +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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
2003-08-14 02:14:27 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include "net.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Basic code taken from IceWM
|
|
|
|
* (c) Marko Macek and others, GNU General Public License
|
|
|
|
*
|
|
|
|
* Converted from C++ to C (using glib data types) by Bernhard Walle
|
|
|
|
*/
|
|
|
|
|
2003-08-18 02:55:46 +08:00
|
|
|
|
|
|
|
static guint64 prev_ibytes, cur_ibytes, offset_ibytes;
|
|
|
|
static guint64 prev_obytes, cur_obytes, offset_obytes;
|
|
|
|
static struct timeval prev_time;
|
|
|
|
|
|
|
|
void init_netload()
|
|
|
|
{
|
|
|
|
prev_ibytes = cur_ibytes = offset_ibytes = 0;
|
|
|
|
prev_obytes = cur_obytes = offset_obytes = 0;
|
|
|
|
gettimeofday(&prev_time, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_current_netload(const gchar* device, gint64 *in, gint64 *out, gint64 *tot)
|
2003-08-14 02:14:27 +08:00
|
|
|
{
|
|
|
|
static gboolean first = TRUE;
|
|
|
|
struct timeval curr_time;
|
|
|
|
gdouble delta_t;
|
|
|
|
gint64 ni, no;
|
|
|
|
|
|
|
|
gchar buf[BUFSIZ];
|
|
|
|
gchar *p;
|
|
|
|
gint64 ipackets, opackets, ierrs, oerrs, idrop, odrop, ififo, ofifo,
|
|
|
|
iframe, ocolls, ocarrier, icomp, ocomp, imcast;
|
2003-08-18 02:55:46 +08:00
|
|
|
|
2003-08-14 02:14:27 +08:00
|
|
|
FILE *fp = NULL;
|
2003-08-18 02:55:46 +08:00
|
|
|
|
2003-08-14 02:14:27 +08:00
|
|
|
gettimeofday(&curr_time, NULL);
|
|
|
|
|
|
|
|
delta_t = (gdouble) ((curr_time.tv_sec - prev_time.tv_sec) * 1000000L
|
|
|
|
+ (curr_time.tv_usec - prev_time.tv_usec)) / 1000000.0;
|
|
|
|
|
2003-08-18 02:55:46 +08:00
|
|
|
|
2003-08-14 02:14:27 +08:00
|
|
|
if ( (fp = fopen("/proc/net/dev", "r")) == NULL)
|
|
|
|
{
|
2003-08-18 02:55:46 +08:00
|
|
|
*in = *out = *tot = 0;
|
2003-08-14 02:14:27 +08:00
|
|
|
return;
|
|
|
|
}
|
2003-08-18 02:55:46 +08:00
|
|
|
|
|
|
|
while ( fgets(buf, BUFSIZ, fp) != NULL )
|
|
|
|
{
|
2003-08-14 02:14:27 +08:00
|
|
|
p = buf;
|
|
|
|
while (*p == ' ')
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
}
|
2003-08-18 02:55:46 +08:00
|
|
|
|
2003-08-14 02:14:27 +08:00
|
|
|
if (strncmp(p, device, strlen(device)) == 0 && p[strlen(device)] == ':')
|
|
|
|
{
|
|
|
|
p = strchr(p, ':') + 1;
|
2003-08-18 02:55:46 +08:00
|
|
|
|
|
|
|
if (sscanf(
|
|
|
|
p,
|
|
|
|
"%llu %lld %lld %lld %lld %lld %lld %lld" " %llu %lld %lld %lld %lld %lld %lld %lld",
|
|
|
|
&cur_ibytes, &ipackets, &ierrs, &idrop, &ififo, &iframe, &icomp, &imcast,
|
|
|
|
&cur_obytes, &opackets, &oerrs, &odrop, &ofifo, &ocolls, &ocarrier, &ocomp) != 16 )
|
2003-08-14 02:14:27 +08:00
|
|
|
{
|
|
|
|
ipackets = opackets = 0;
|
|
|
|
sscanf(p, "%lld %lld %lld %lld %lld" " %lld %lld %lld %lld %lld %lld",
|
|
|
|
&ipackets, &ierrs, &idrop, &ififo, &iframe,
|
|
|
|
&opackets, &oerrs, &odrop, &ofifo, &ocolls, &ocarrier);
|
2003-08-18 02:55:46 +08:00
|
|
|
/* for linux<2.0 fake packets as bytes (we only need relative values anyway) */
|
2003-08-14 02:14:27 +08:00
|
|
|
cur_ibytes = ipackets;
|
|
|
|
cur_obytes = opackets;
|
|
|
|
}
|
2003-08-18 02:55:46 +08:00
|
|
|
|
2003-08-14 02:14:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2003-08-18 02:55:46 +08:00
|
|
|
|
2003-08-14 02:14:27 +08:00
|
|
|
cur_ibytes += offset_ibytes;
|
|
|
|
cur_obytes += offset_obytes;
|
2003-08-18 02:55:46 +08:00
|
|
|
|
2003-08-14 02:14:27 +08:00
|
|
|
if (cur_ibytes < prev_ibytes)
|
2003-08-18 02:55:46 +08:00
|
|
|
{
|
|
|
|
/* har, har, overflow. Use the recent prev_ibytes value as offset this time */
|
2003-08-14 02:14:27 +08:00
|
|
|
cur_ibytes = offset_ibytes = prev_ibytes;
|
2003-08-18 02:55:46 +08:00
|
|
|
}
|
|
|
|
|
2003-08-14 02:14:27 +08:00
|
|
|
if (cur_obytes < prev_obytes)
|
2003-08-18 02:55:46 +08:00
|
|
|
{
|
|
|
|
/* har, har, overflow. Use the recent prev_obytes value as offset this time */
|
2003-08-14 02:14:27 +08:00
|
|
|
cur_obytes = offset_obytes = prev_obytes;
|
2003-08-18 02:55:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-14 02:14:27 +08:00
|
|
|
ni = (gint64)((cur_ibytes - prev_ibytes) / delta_t);
|
|
|
|
no = (gint64)((cur_obytes - prev_obytes) / delta_t);
|
2003-08-18 02:55:46 +08:00
|
|
|
|
|
|
|
if (in != NULL && out != NULL && tot != NULL && !first)
|
2003-08-14 02:14:27 +08:00
|
|
|
{
|
|
|
|
*in = ni;
|
|
|
|
*out = no;
|
|
|
|
*tot = *in + *out;
|
|
|
|
}
|
2003-08-18 02:55:46 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*in = *out = *tot = 0;
|
|
|
|
first = FALSE;
|
|
|
|
}
|
2003-08-14 02:14:27 +08:00
|
|
|
|
|
|
|
prev_time.tv_sec = curr_time.tv_sec;
|
|
|
|
prev_time.tv_usec = curr_time.tv_usec;
|
|
|
|
|
|
|
|
prev_ibytes = cur_ibytes;
|
|
|
|
prev_obytes = cur_obytes;
|
|
|
|
|
2003-08-18 02:55:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void close_netload()
|
|
|
|
{
|
|
|
|
/* Do nothing here */
|
2003-08-14 02:14:27 +08:00
|
|
|
}
|
|
|
|
|