re-factor iface screen code (M. Dolze)
This commit is contained in:
@@ -16,11 +16,12 @@ v.0.5dev (ongoing development)
|
||||
+ hd44780
|
||||
+ IOWarrior
|
||||
+ serialVFD
|
||||
+ add iface screen(s) to lcdproc client (Andrew Foss)
|
||||
+ add iface screen(s) to lcdproc client (Andrew Foss/Markus Dolze)
|
||||
* update sample Perl clients
|
||||
* fix LCDd crash on shutdown of clients using "menu_set_main" (Andrew Foss)
|
||||
* fix sock_connect() to allow 0 as legal socket (Frederick Nacino)
|
||||
* improve serialVFD driver (Stefan Herdler)
|
||||
+ add MD8800 driver for LCDs in Medion MD8800 PCs (Stefan Herdler/Martin Møller)
|
||||
|
||||
v0.5.0
|
||||
This version has split off from unstable-0.4.3. Includes major changes.
|
||||
|
||||
+3
-2
@@ -21,6 +21,7 @@ drivers=`echo $drivers | sed -e 's/,/ /g'`
|
||||
|
||||
dnl replace special keyword "all" in a secure manner
|
||||
drivers=[" $drivers "]
|
||||
drivers=`echo " $drivers " | sed -e "s/ all,/ ${allDrivers} /"`
|
||||
drivers=`echo " $drivers " | sed -e "s/ all / ${allDrivers} /"`
|
||||
drivers=`echo $drivers | sed -e 's/,/ /g'`
|
||||
|
||||
@@ -29,8 +30,8 @@ selectdrivers=" "
|
||||
for driver in $drivers ; do
|
||||
case $driver in
|
||||
!*)
|
||||
driver=`echo $driver | sed -e 's/^.//'`
|
||||
selectdrivers=[`echo $selectdrivers | sed -r -e "s/ $driver / /g"`]
|
||||
driver=`echo "$driver" | sed -e 's/^.//'`
|
||||
selectdrivers=[`echo " $selectdrivers " | sed -r -e "s/ $driver / /g"`]
|
||||
;;
|
||||
*)
|
||||
selectdrivers=["$selectdrivers $driver "]
|
||||
|
||||
+1
-133
@@ -161,7 +161,7 @@ iface_screen(int rep, int display, int *flags_ptr)
|
||||
/* for each interface do */
|
||||
for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
|
||||
/*read iface_parameter stats */
|
||||
if (!get_iface_stats(&iface[iface_nmbr])) {
|
||||
if (!machine_get_iface_stats(&iface[iface_nmbr])) {
|
||||
/* there was an error, so we exit the loop */
|
||||
break;
|
||||
}
|
||||
@@ -184,138 +184,6 @@ iface_screen(int rep, int display, int *flags_ptr)
|
||||
} // End iface_screen()
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
* Read interface statistics from system and store in the struct
|
||||
* passed as a pointer. If there are no errors, it returns 1. If errors,
|
||||
* returns 0.
|
||||
*************************************************************************
|
||||
*/
|
||||
|
||||
int
|
||||
get_iface_stats (IfaceInfo *interface)
|
||||
{
|
||||
#ifdef linux
|
||||
FILE *file; /* file handler */
|
||||
char buffer[1024]; /* buffer to work with the file */
|
||||
static int first_time = 1; /* is it first time we call this function? */
|
||||
char *ch_pointer = NULL; /* pointer to where interface values are in file */
|
||||
|
||||
/* Open the file in read-only mode and parse */
|
||||
|
||||
if ((file = fopen(DEVFILE, "r")) != NULL) {
|
||||
/* Skip first 2 header lines of file */
|
||||
fgets(buffer, sizeof(buffer), file);
|
||||
fgets(buffer, sizeof(buffer), file);
|
||||
|
||||
/* By default, treat interface as down */
|
||||
interface->status = down;
|
||||
|
||||
/* Search iface_name and scan values */
|
||||
while ((fgets(buffer, sizeof(buffer), file) != NULL)) {
|
||||
if (strstr(buffer, interface->name)) {
|
||||
/* interface exists */
|
||||
interface->status = up; /* is up */
|
||||
interface->last_online = time(NULL); /* save actual time */
|
||||
|
||||
/* search ':' and skip over it */
|
||||
ch_pointer = strchr(buffer, ':');
|
||||
ch_pointer++;
|
||||
|
||||
/* Now ch_pointer points to values of iface_name */
|
||||
/* Scan values from here */
|
||||
sscanf(ch_pointer, "%lf %lf %*s %*s %*s %*s %*s %*s %lf %lf",
|
||||
&interface->rc_byte,
|
||||
&interface->rc_pkt,
|
||||
&interface->tr_byte,
|
||||
&interface->tr_pkt);
|
||||
|
||||
/* if is the first time we call this function,
|
||||
* old values are the same as new so we don't
|
||||
* get big speeds when calculating
|
||||
*/
|
||||
if (first_time) {
|
||||
interface->rc_byte_old = interface->rc_byte;
|
||||
interface->tr_byte_old = interface->tr_byte;
|
||||
interface->rc_pkt_old = interface->rc_pkt;
|
||||
interface->tr_pkt_old = interface->tr_pkt;
|
||||
first_time = 0; /* now it isn't first time */
|
||||
}
|
||||
}
|
||||
} /* while */
|
||||
|
||||
fclose(file); /* close file */
|
||||
return 1; /* everything went OK */
|
||||
|
||||
}
|
||||
else { /* error when opening the file */
|
||||
report(RPT_CRIT, "Error: Could not open %s\n", DEVFILE);
|
||||
return 0; /* something went wrong */
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_mib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static int first_time = 1; /* is it first time we call this function? */
|
||||
int rows;
|
||||
int name[6] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, 0, IFDATA_GENERAL};
|
||||
size_t len;
|
||||
struct ifmibdata ifmd; /* ifmibdata contains the network statistics */
|
||||
|
||||
len = sizeof(rows);
|
||||
/* get number of interfaces */
|
||||
if (sysctlbyname("net.link.generic.system.ifcount", &rows, &len, NULL, 0) == 0) {
|
||||
interface->status = down; /* set status down by default */
|
||||
|
||||
len = sizeof(ifmd);
|
||||
/* walk through all interfaces in the ifmib table from last to first */
|
||||
for ( ; rows > 0; rows--) {
|
||||
name[4] = rows; /* set the interface index */
|
||||
/* retrive the ifmibdata for the current index */
|
||||
if (sysctl(name, 6, &ifmd, &len, NULL, 0) == -1) {
|
||||
report(RPT_ERR, "Reading ifmibdata failed");
|
||||
break;
|
||||
}
|
||||
/* check if its interface name matches */
|
||||
if (strcmp(ifmd.ifmd_name, interface->name) == 0) {
|
||||
interface->last_online = time(NULL); /* save actual time */
|
||||
|
||||
if ((ifmd.ifmd_flags & IFF_UP) == IFF_UP)
|
||||
interface->status = up; /* is up */
|
||||
|
||||
interface->rc_byte = ifmd.ifmd_data.ifi_ibytes;
|
||||
interface->tr_byte = ifmd.ifmd_data.ifi_obytes;
|
||||
interface->rc_pkt = ifmd.ifmd_data.ifi_ipackets;
|
||||
interface->tr_pkt = ifmd.ifmd_data.ifi_opackets;
|
||||
|
||||
if (first_time) {
|
||||
interface->rc_byte_old = interface->rc_byte;
|
||||
interface->tr_byte_old = interface->tr_byte;
|
||||
interface->rc_pkt_old = interface->rc_pkt;
|
||||
interface->tr_pkt_old = interface->tr_pkt;
|
||||
first_time = 0; /* now it isn't first time */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* if we are here there is no interface with the given name */
|
||||
report(RPT_CRIT, "There is no interface named %s", interface->name);
|
||||
return 0;
|
||||
} else {
|
||||
report(RPT_CRIT, "Reading interface count via sysctl failed");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* __FreeBSD__ */
|
||||
|
||||
} /* get_iface_stats() */
|
||||
|
||||
/*************************************************************************
|
||||
* Send commands to server to add speed screen with all required widgets
|
||||
|
||||
+1
-39
@@ -12,48 +12,10 @@
|
||||
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define DEVFILE "/proc/net/dev" /* file to read statistics from */
|
||||
|
||||
/* status definitions */
|
||||
|
||||
typedef enum {
|
||||
down = 0,
|
||||
up = 1,
|
||||
} IfaceStatus;
|
||||
#include "machine.h"
|
||||
|
||||
#define MAX_INTERFACES 3 /* max number of interfaces in multi-interface mode */
|
||||
|
||||
/* Struct for interface values (transmision, reception, etc..) */
|
||||
|
||||
typedef struct iface_info
|
||||
{
|
||||
/* interface name and alias (=display name) */
|
||||
char *name;
|
||||
char *alias;
|
||||
|
||||
IfaceStatus status;
|
||||
|
||||
time_t last_online;
|
||||
|
||||
/* received bytes */
|
||||
double rc_byte;
|
||||
double rc_byte_old;
|
||||
|
||||
/* transmited bytes */
|
||||
double tr_byte;
|
||||
double tr_byte_old;
|
||||
|
||||
/* received packets */
|
||||
double rc_pkt;
|
||||
double rc_pkt_old;
|
||||
|
||||
/* transmited packets */
|
||||
double tr_pkt;
|
||||
double tr_pkt_old;
|
||||
} IfaceInfo;
|
||||
|
||||
|
||||
int iface_screen (int rep, int display, int *flags_ptr);
|
||||
IfaceInfo iface[MAX_INTERFACES]; /* interface info */
|
||||
|
||||
|
||||
@@ -27,6 +27,40 @@ typedef struct
|
||||
int number;
|
||||
} procinfo_type;
|
||||
|
||||
/* status definitions for network interfaces */
|
||||
typedef enum {
|
||||
down = 0,
|
||||
up = 1,
|
||||
} IfaceStatus;
|
||||
|
||||
/* Struct for network interface values (transmision, reception, etc..) */
|
||||
typedef struct iface_info
|
||||
{
|
||||
/* interface name and alias (=display name) */
|
||||
char *name;
|
||||
char *alias;
|
||||
|
||||
IfaceStatus status;
|
||||
|
||||
time_t last_online;
|
||||
|
||||
/* received bytes */
|
||||
double rc_byte;
|
||||
double rc_byte_old;
|
||||
|
||||
/* transmited bytes */
|
||||
double tr_byte;
|
||||
double tr_byte_old;
|
||||
|
||||
/* received packets */
|
||||
double rc_pkt;
|
||||
double rc_pkt_old;
|
||||
|
||||
/* transmited packets */
|
||||
double tr_pkt;
|
||||
double tr_pkt_old;
|
||||
} IfaceInfo;
|
||||
|
||||
int machine_init();
|
||||
int machine_close();
|
||||
|
||||
@@ -38,5 +72,7 @@ int machine_get_meminfo(meminfo_type *result);
|
||||
int machine_get_procs(LinkedList *procs);
|
||||
int machine_get_smpload(load_type *result, int *numcpus);
|
||||
int machine_get_uptime(double *up, double *idle);
|
||||
int machine_get_iface_stats (IfaceInfo *interface);
|
||||
|
||||
|
||||
#endif /* _lcdproc_machine_h_ */
|
||||
|
||||
@@ -428,4 +428,11 @@ static int swapmode(int *rettotal, int *retfree)
|
||||
return(FALSE);
|
||||
}
|
||||
|
||||
/* Get network statistics */
|
||||
int machine_get_iface_stats (IfaceInfo *interface)
|
||||
{
|
||||
/* Implementation missing */
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* __APPLE__ */
|
||||
|
||||
@@ -48,6 +48,9 @@
|
||||
#include <machine/apm_bios.h>
|
||||
#include <kvm.h>
|
||||
#include <errno.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_mib.h>
|
||||
|
||||
#include "main.h"
|
||||
#include "machine.h"
|
||||
@@ -416,4 +419,63 @@ static int swapmode(int *retavail, int *retfree)
|
||||
return(n);
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
* Read interface statistics from system and store in the struct
|
||||
* passed as a pointer. If there are no errors, it returns 1. If errors,
|
||||
* returns 0.
|
||||
*************************************************************************
|
||||
*/
|
||||
int machine_get_iface_stats (IfaceInfo *interface)
|
||||
{
|
||||
static int first_time = 1; /* is it first time we call this function? */
|
||||
int rows;
|
||||
int name[6] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, 0, IFDATA_GENERAL};
|
||||
size_t len;
|
||||
struct ifmibdata ifmd; /* ifmibdata contains the network statistics */
|
||||
|
||||
len = sizeof(rows);
|
||||
/* get number of interfaces */
|
||||
if (sysctlbyname("net.link.generic.system.ifcount", &rows, &len, NULL, 0) == 0) {
|
||||
interface->status = down; /* set status down by default */
|
||||
|
||||
len = sizeof(ifmd);
|
||||
/* walk through all interfaces in the ifmib table from last to first */
|
||||
for ( ; rows > 0; rows--) {
|
||||
name[4] = rows; /* set the interface index */
|
||||
/* retrive the ifmibdata for the current index */
|
||||
if (sysctl(name, 6, &ifmd, &len, NULL, 0) == -1) {
|
||||
perror("read sysctl");
|
||||
break;
|
||||
}
|
||||
/* check if its interface name matches */
|
||||
if (strcmp(ifmd.ifmd_name, interface->name) == 0) {
|
||||
interface->last_online = time(NULL); /* save actual time */
|
||||
|
||||
if ((ifmd.ifmd_flags & IFF_UP) == IFF_UP)
|
||||
interface->status = up; /* is up */
|
||||
|
||||
interface->rc_byte = ifmd.ifmd_data.ifi_ibytes;
|
||||
interface->tr_byte = ifmd.ifmd_data.ifi_obytes;
|
||||
interface->rc_pkt = ifmd.ifmd_data.ifi_ipackets;
|
||||
interface->tr_pkt = ifmd.ifmd_data.ifi_opackets;
|
||||
|
||||
if (first_time) {
|
||||
interface->rc_byte_old = interface->rc_byte;
|
||||
interface->tr_byte_old = interface->tr_byte;
|
||||
interface->rc_pkt_old = interface->rc_pkt;
|
||||
interface->tr_pkt_old = interface->tr_pkt;
|
||||
first_time = 0; /* now it isn't first time */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* if we are here there is no interface with the given name */
|
||||
return 0;
|
||||
} else {
|
||||
perror("read sysctlbyname");
|
||||
return 0;
|
||||
}
|
||||
} /* get_iface_stats() */
|
||||
|
||||
|
||||
#endif /* __FreeBSD__ */
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include "shared/LL.h"
|
||||
|
||||
#define MAX_CPUS 8
|
||||
#define DEVFILE "/proc/net/dev" /* file to read network statistics from */
|
||||
|
||||
static int batt_fd;
|
||||
static int load_fd;
|
||||
@@ -512,4 +513,71 @@ int machine_get_uptime(double *up, double *idle)
|
||||
return(TRUE);
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
* Read interface statistics from system and store in the struct
|
||||
* passed as a pointer. If there are no errors, it returns 1. If errors,
|
||||
* returns 0.
|
||||
*************************************************************************
|
||||
*/
|
||||
int machine_get_iface_stats (IfaceInfo *interface)
|
||||
{
|
||||
FILE *file; /* file handler */
|
||||
char buffer[1024]; /* buffer to work with the file */
|
||||
static int first_time = 1; /* is it first time we call this function? */
|
||||
char *ch_pointer = NULL; /* pointer to where interface values are in file */
|
||||
|
||||
/* Open the file in read-only mode and parse */
|
||||
|
||||
if ((file = fopen(DEVFILE, "r")) != NULL) {
|
||||
/* Skip first 2 header lines of file */
|
||||
fgets(buffer, sizeof(buffer), file);
|
||||
fgets(buffer, sizeof(buffer), file);
|
||||
|
||||
/* By default, treat interface as down */
|
||||
interface->status = down;
|
||||
|
||||
/* Search iface_name and scan values */
|
||||
while ((fgets(buffer, sizeof(buffer), file) != NULL)) {
|
||||
if (strstr(buffer, interface->name)) {
|
||||
/* interface exists */
|
||||
interface->status = up; /* is up */
|
||||
interface->last_online = time(NULL); /* save actual time */
|
||||
|
||||
/* search ':' and skip over it */
|
||||
ch_pointer = strchr(buffer, ':');
|
||||
ch_pointer++;
|
||||
|
||||
/* Now ch_pointer points to values of iface_name */
|
||||
/* Scan values from here */
|
||||
sscanf(ch_pointer, "%lf %lf %*s %*s %*s %*s %*s %*s %lf %lf",
|
||||
&interface->rc_byte,
|
||||
&interface->rc_pkt,
|
||||
&interface->tr_byte,
|
||||
&interface->tr_pkt);
|
||||
|
||||
/* if is the first time we call this function,
|
||||
* old values are the same as new so we don't
|
||||
* get big speeds when calculating
|
||||
*/
|
||||
if (first_time) {
|
||||
interface->rc_byte_old = interface->rc_byte;
|
||||
interface->tr_byte_old = interface->tr_byte;
|
||||
interface->rc_pkt_old = interface->rc_pkt;
|
||||
interface->tr_pkt_old = interface->tr_pkt;
|
||||
first_time = 0; /* now it isn't first time */
|
||||
}
|
||||
}
|
||||
} /* while */
|
||||
|
||||
fclose(file); /* close file */
|
||||
return 1; /* everything went OK */
|
||||
|
||||
}
|
||||
else { /* error when opening the file */
|
||||
perror("Error: Could not open DEVFILE");
|
||||
return 0; /* something went wrong */
|
||||
}
|
||||
} /* get_iface_stats() */
|
||||
|
||||
#endif /* linux */
|
||||
|
||||
@@ -52,6 +52,9 @@
|
||||
#include <uvm/uvm_extern.h>
|
||||
#include <machine/apmvar.h>
|
||||
#include <errno.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <ifaddrs.h>
|
||||
|
||||
#if (__NetBSD_Version__ >= 300000000)
|
||||
#include <sys/statvfs.h>
|
||||
@@ -137,7 +140,7 @@ int machine_get_fs(mounts_type fs[], int *cnt)
|
||||
#if (__NetBSD_Version__ >= 300000000)
|
||||
struct statvfs *mntbuf;
|
||||
struct statvfs *pp;
|
||||
#else
|
||||
#else
|
||||
struct statfs *mntbuf;
|
||||
struct statfs *pp;
|
||||
#endif
|
||||
@@ -370,5 +373,51 @@ int machine_get_uptime(double *up, double *idle)
|
||||
return(TRUE);
|
||||
}
|
||||
|
||||
/* Get network statistics */
|
||||
int machine_get_iface_stats (IfaceInfo *interface)
|
||||
{
|
||||
static int first_time = 1; /* is it first time we call this function? */
|
||||
struct ifaddrs *ifa, *ifa_ptr;
|
||||
struct if_data *ifd;
|
||||
|
||||
/* get first interface */
|
||||
if (getifaddrs(&ifa) == -1)
|
||||
perror("getifaddr failed");
|
||||
|
||||
/* loop through all interfaces */
|
||||
for (ifa_ptr = ifa; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next) {
|
||||
interface->status = down; /* set status down by default */
|
||||
|
||||
/* check if we got the right interface and if it is Link type */
|
||||
if ((strcmp(ifa_ptr->ifa_name, interface->name) == 0) &&
|
||||
(ifa_ptr->ifa_addr->sa_family == AF_LINK)) {
|
||||
|
||||
ifd = (struct if_data *)ifa_ptr->ifa_data;
|
||||
interface->last_online = time(NULL); /* save actual time */
|
||||
|
||||
if ((ifa_ptr->ifa_flags & IFF_UP) == IFF_UP)
|
||||
interface->status = up; /* is up */
|
||||
|
||||
interface->rc_byte = ifd->ifi_ibytes;
|
||||
interface->tr_byte = ifd->ifi_obytes;
|
||||
interface->rc_pkt = ifd->ifi_ipackets;
|
||||
interface->tr_pkt = ifd->ifi_opackets;
|
||||
|
||||
if (first_time) {
|
||||
interface->rc_byte_old = interface->rc_byte;
|
||||
interface->tr_byte_old = interface->tr_byte;
|
||||
interface->rc_pkt_old = interface->rc_pkt;
|
||||
interface->tr_pkt_old = interface->tr_pkt;
|
||||
first_time = 0; /* now it isn't first time */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
freeifaddrs(ifa);
|
||||
|
||||
/* if we are here there is no interface with the given name */
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* __NetBSD__ */
|
||||
|
||||
|
||||
@@ -378,4 +378,11 @@ int machine_get_uptime(double *up, double *idle)
|
||||
return(TRUE);
|
||||
}
|
||||
|
||||
/* Get network statistics */
|
||||
int machine_get_iface_stats (IfaceInfo *interface)
|
||||
{
|
||||
/* Implementation missing */
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* __OpenBSD__ */
|
||||
|
||||
@@ -439,5 +439,12 @@ int machine_get_uptime(double *up, double *idle)
|
||||
return(TRUE);
|
||||
}
|
||||
|
||||
/* Get network statistics */
|
||||
int machine_get_iface_stats (IfaceInfo *interface)
|
||||
{
|
||||
/* Implementation missing */
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* sun */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user