LCDproc development and user support list

Text archives Help


[Lcdproc] Volunteers to try netlcd in lcdproc...


Chronological Thread 
  • From: a.foss AT f5.com (Andrew Foss)
  • Subject: [Lcdproc] Volunteers to try netlcd in lcdproc...
  • Date: Mon Mar 27 17:49:01 2006

sorry, one minor bug fixed enabling/disabling the iface screen from the lcd keypad menus in the attached diff.

andrew

Andrew Foss wrote:
lcdproc folks,

would anyone have any interest or time to try the attached patch? It applies to the current CVS tree.

It integrates the netlcd display of interface stats as one of the lcdproc screen options, it also allows a /usr/local/etc/lcdproc.conf to configure the lcdproc screens.

I'd like to confirm that this gives the same results as netlcd when the new "iface" screen is enabled, before I move on. I only have 2 line lcd's, so I can't actually see the whole iface screen properly, if it's working as expected, next step will be to add a 2 line lcd display option for us poor folks w/ little lcd's...

thanks,
andrew
------------------------------------------------------------------------

diff --exclude='*.wanjet' --exclude='.??*' --exclude=config.h
--exclude='*.orig' --exclude=Makefile --exclude='*.o' --exclude=Doxyfile
--exclude='*.a' --exclude=config.log --exclude=config.status
--exclude='init-*' --exclude=lcdproc -Nuarp lcdproc/iface.c
lcdproc-cfgfile/iface.c
--- lcdproc/iface.c 1969-12-31 16:00:00.000000000 -0800
+++ lcdproc-cfgfile/iface.c 2006-03-27 08:52:10.000000000 -0800
@@ -0,0 +1,1008 @@
+/* + netlcdclient - Client for LCDproc which shows networks statistics
+
+ Copyright (C) 2002 Luis Llorente Campo <luisllorente AT luisllorente.com> + Multiinterface Extension by Stephan Skrodzki <skrodzki AT stevekist.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, or (at your option)
+ any later 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.
+
+ 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. +
+*/
+
+#include <stdio.h>
+#include <getopt.h>
+#include <signal.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "iface.h"
+#ifdef NETLCDCLIENT
+#include "sockets.h"
+#else
+#include "shared/sockets.h"
+#include "shared/debug.h"
+#include "shared/report.h"
+#include "shared/configfile.h"
+#include "main.h"
+#define UNSET_INT -1 +#define UNSET_STR "\01"
+#endif //NETLCDCLIENT
+
+
+#ifdef NETLCDCLIENT
+/* Option flags and global variables */
+
+char *program_name; /* the name the program was run with */
+int port = 13666; /* default port */
+char server[100] = "localhost"; /* default server */
+int daemon_mode = 0; /* by default, no daemon mode */
+#endif //NETLCDCLIENT
+/* interface name passed as parameter */
+char iface_parameter[MAX_INTERFACES][MAX_PARAM_LEN] = { "\0" , "\0" , "\0" };
+/* alias name for the interface */
+char iface_alias[MAX_INTERFACES][MAX_PARAM_LEN] = { "\0" , "\0" , "\0" };
+int iface_count = 0; /* number of interfaces */
+char unit_label[MAX_INTERFACES] = "B"; /* default unit label is Bytes */
+int transfer_screen = 0; /* by default, transfer screen is not shown */
+int sock = 0; /* socket handler */
+
+/* command line parameters, used by getopt_long() */
+static struct option const long_options[] =
+{
+ {"interface", required_argument, 0, 'i'},
+ {"alias", required_argument, 0, 'a'},
+ {"server", required_argument, 0, 's'},
+ {"port", required_argument, 0, 'p'},
+ {"unit", required_argument, 0, 'u'},
+ {"transfer", no_argument, 0, 't'},
+ {"daemon", no_argument, 0, 'd'},
+ {"help", no_argument, 0, 'h'},
+ {"version", no_argument, 0, 'V'},
+ {NULL, 0, NULL, 0}
+};
+
+#ifdef NETLCDCLIENT
+/*************************************************************************
+ * MAIN PROGRAM FUNCTION
+ *************************************************************************
+ */
+
+int
+main (int argc, char **argv)
+{
+
+ struct iface_info iface[MAX_INTERFACES]; /* interface info */
+
+ int len; /* bytes read from server */
+
+ char readbuff[MAXMSG]; /* read buffer from server */
+
+ unsigned int sleep_time = 1; /* interval between updates (1 second)
*/
+
+ int pid; /* child process identificator for daemon mode */
+
+ int iface_nmbr; /* loop variable for the interface */
+
+ /* Capture signals to exit program cleanly */
+ /* Ctrl-C */
+ signal(SIGINT, exit_program);
+ /* kill */
+ signal(SIGTERM, exit_program);
+ /* kill -HUP */
+ signal(SIGHUP, exit_program);
+ /* kill -KILL */
+ signal(SIGKILL, exit_program);
+
+ /* get program name from command line */
+ program_name = argv[0];
+
+ /* parse command line parameters */
+ decode_switches (argc, argv);
+
+ /* check if any interface name has been introduced */
+
+ if (iface_count == 0) {
+ /* no one has been introduced, so exit the program showing
help */
+ printf("No interface selected, please, use --interface
option\n");
+ usage (1);
+ }
+
+ /* check if we go to daemon mode */
+ if (daemon_mode) {
+ signal(SIGTTOU, SIG_IGN);
+ signal(SIGTTIN, SIG_IGN);
+ signal(SIGTSTP, SIG_IGN);
+ /* create child process */
+ pid = fork();
+ /* create new session */
+ setsid();
+ signal(SIGHUP, SIG_IGN);
+ if (pid != 0) { /* we are the parent process and exit */
+ exit(0);
+ }
+ /* from here, we are the child process */
+ }
+
+ /* Try to connect to server */
+ sock = sock_connect(server, port);
+
+ if (sock <= 0) { /* there was some error and exit */
+ fprintf(stderr, "Error: Could not connect to %s:%d\n", server, port);
+ exit(0);
+ }
+
+ /* Say "hello" to server */
+ sock_send_string(sock, "hello\n");
+
+ /* Wait the server to be prepared */
+ usleep(500000);
+
+ /* Read server response */
+ len = sock_recv(sock, readbuff, MAXMSG);
+
+ /* Now we are ready to send commands to server */
+
+ /* set initial speed screen with widgets */
+ initialize_speed_screen();
+
+ /* set initial transfer screen if needed */
+ if (transfer_screen) {
+ initialize_transfer_screen();
+ }
+
+ /* initialize all interface structs */
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ iface[iface_nmbr].last_online = 0;
+ iface[iface_nmbr].status = down;
+ }
+
+
+ /* main loop */
+ while (1) {
+ /* read server responses */
+ len = sock_recv(sock, readbuff, MAXMSG);
+ + /* for each interface do */
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ /*read iface_parameter stats */
+ if (!get_iface_stats(iface_parameter[iface_nmbr], \
+
&iface[iface_nmbr])) {
+ /* there was an error, so we exit the loop */
+ break;
+ }
+
+ /* actualize speed values in display */
+ actualize_speed_screen(&iface[iface_nmbr], sleep_time, \
+
(iface_count == 1) ? 0 : iface_nmbr + 1);
+ + /* if needed, actualize transfer values in display */
+ if (transfer_screen) {
+ actualize_transfer_screen(&iface[iface_nmbr], \
+
(iface_count == 1) ? 0 : iface_nmbr + 1);
+ }
+ + /* Actual values are the old ones in the next loop */
+ iface[iface_nmbr].rc_byte_old = iface[iface_nmbr].rc_byte;
+ iface[iface_nmbr].tr_byte_old = iface[iface_nmbr].tr_byte;
+ iface[iface_nmbr].rc_pkt_old = iface[iface_nmbr].rc_pkt;
+ iface[iface_nmbr].tr_pkt_old = iface[iface_nmbr].tr_pkt;
+
+ } /* for */
+
+ /* Wait some time to do the main loop again */
+ sleep(sleep_time);
+
+ } /* while (1) */
+
+ /* Before exit the program, close the socket */
+ sock_close(sock);
+
+ exit (0);
+
+} /* main() */
+
+/*************************************************************************
+ * END MAIN PROGRAM FUNCTION
+ **************************************************************************/
+
+/*************************************************************************
+ * Set all the option flags according to the switches specified.
+ * Return the index of the first non-option argument.
+ *************************************************************************
+ */
+
+static int
+decode_switches (int argc, char **argv)
+{
+ int c;
+
+ while ((c = getopt_long (argc, argv, "i:a:s:p:u:tdhV", \
+ long_options,
(int *) 0)) != EOF) {
+ + switch (c) {
+
+ case 'i':
+ /* Check number of interfaces introduced */
+ if (iface_count >= MAX_INTERFACES) {
+ fprintf(stderr, "Too many interfaces
introduced. " \
+ "Only %d are
supported.\n", MAX_INTERFACES);
+ exit(0);
+ }
+ strncpy(iface_parameter[iface_count], optarg,
MAX_PARAM_LEN);
+ strncpy(iface_alias[iface_count], optarg, MAX_PARAM_LEN); + iface_count++;
+ break;
+
+ case 'a':
+ strncpy(iface_alias[iface_count-1], optarg,
MAX_PARAM_LEN);
+ break;
+
+ case 's':
+ strncpy(server, optarg, sizeof(server));
+ break;
+
+ case 'p':
+ port = atoi(optarg);
+ break;
+
+ case 'u':
+ /* check for valid values */
+ if (strstr(optarg, "byte")) strncpy(unit_label,
"B", \
+
sizeof(unit_label));
+ else if (strstr(optarg, "bit")) strncpy(unit_label,
"b", \
+
sizeof(unit_label));
+ else if (strstr(optarg, "packet")) strncpy(unit_label,
"pkt", \
+
sizeof(unit_label));
+ else {
+ fprintf(stderr, "netlcdclient: argument
'%s' for -u parameter is not valid\n", optarg);
+ usage(0);
+ }
+ break;
+
+ case 't':
+ transfer_screen = 1; /* show transfer screen
*/
+ break; +
+ case 'd':
+ daemon_mode = 1; /* go to daemon mode */
+ break;
+
+ case 'V': /* show version */
+ printf("netlcdclient v%s\n", VERSION);
+ exit (0);
+
+ case 'h': /* show help */
+ usage (0);
+ exit(0);
+ + } /* switch */
+ } /* while */
+
+ return optind;
+
+} /* decode_switches() */
+
+
+/************************************************************************* + * Show program help and exit.
+ ************************************************************************* + */
+
+static void
+usage (int status)
+{
+ printf ("\n"
+"netlcdclient v%s for LCDproc, by Luis Llorente\n", VERSION);
+ printf ("\n"
+"Usage: %s -i <interface> [-a alias] [ -tdhV ] [ -s server ] [ -p port ] [-u
unit]\n", program_name);
+ printf ("\n"
+"Options in []'s are optional.\n"
+" -i ... -i , --interface=INTERFACE show INTERFACE statistics\n"
+" -a , --alias=ALIAS alias name for the interface (-a has to
\n"
+" follow -i)\n"
+" -s, --server=SERVER connect to SERVER (default is
localhost)\n"
+" -p, --port=NUMBER connect to specified port number
(default\n"
+" is 13666)\n"
+" -u, --unit=TYPE speed measure unit. Available units
are:\n"
+" byte (default)\n"
+" bit\n"
+" packet\n"
+" -t, --transfer add screen with transferred traffic\n"
+" -d, --daemon run in the background\n"
+" -h, --help display this help and exit\n"
+" -V, --version output version information and exit\n"
+);
+ printf("\n"
+"Example:\n"
+" %s -s my.server.org -p 2300 -u bit -i eth0 -a LAN\n", program_name);
+
+ exit (status);
+
+} /* usage() */
+
+#else
+/* reads and parses configuration file */
+static int
+iface_process_configfile(char *configfile)
+{
+ char iface_label[12];
+
+ debug( RPT_DEBUG, "%s()", __FUNCTION__ );
+
+ /* Read server settings*/
+
+ if( strcmp( configfile, UNSET_STR ) == 0 || config_read_file(
configfile ) != 0 ) {
+ report( RPT_CRIT, "Could not read config file: %s",
configfile );
+ return -1;
+ printf( "Could not read config file: %s", configfile );
+ //report( RPT_WARNING, "Could not read config file: %s",
configfile );
+ }
+
+ for (iface_count = 0; iface_count < MAX_INTERFACES; iface_count++) {
+ sprintf(iface_label, "interface%i", iface_count);
+ printf("Label %s count %i\n", iface_label, iface_count);
+ strncpy(iface_parameter[iface_count], config_get_string( "Iface",
iface_label, 0, "" ),sizeof(iface_parameter[iface_count]));
+ sprintf(iface_label, "alias%i", iface_count);
+ strncpy(iface_alias[iface_count], config_get_string( "Iface",
iface_label, 0, "" ),sizeof(iface_alias[iface_count]));
+ printf("Interface %s alias %s count %i\n",
iface_parameter[iface_count], iface_alias[iface_count], iface_count);
+ if ( !strlen(iface_parameter[iface_count] )) {
+ iface_count++;
+ break;
+ }
+ }
+ strncpy(unit_label, config_get_string( "Iface", "unit", 0, "bit"),
sizeof(unit_label));
+ transfer_screen = config_get_bool( "Iface", "transfer", 0, 0 );
+
+ return 0;
+}
+//////////////////////////////////////////////////////////////////////////
+// IFace screen shows info about percentage of the CPU being used
+//
+// +--------------------+ +--------------------+
+// |## CPU 51.9%: myh #@| |CPU [---- ]48.1%@|
+// |Usr 46.0% Nice 0.0%| |U-- S- N I--- |
+// |Sys 5.9% Idle 48.1%| +--------------------+
+// |0%-------- 100%|
+// +--------------------+
+//
+int
+iface_screen(int rep, int display, int *flags_ptr)
+{
+ //ALF TODO need to make this actual time since last run
+ unsigned int interval = difftime(time(NULL),iface[0].last_online);
/* interval since last update */
+ if (!interval) return 0; /* need at least 1 second, no divide by 0 */
+
+ if ((*flags_ptr & INITIALIZED) == 0) {
+ *flags_ptr |= INITIALIZED;
+
+ /* get configuration options */
+ iface_process_configfile(configfile);
+
+ /* set initial speed screen with widgets */
+ initialize_speed_screen();
+
+ /* set initial transfer screen if needed */
+ if (transfer_screen) {
+ initialize_transfer_screen();
+ }
+
+ /* initialize all interface structs */
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ iface[iface_nmbr].last_online = 0;
+ iface[iface_nmbr].status = down;
+ }
+ return(0);
+ }
+ /* for each interface do */
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ /*read iface_parameter stats */
+ if (!get_iface_stats(iface_parameter[iface_nmbr], \
+
&iface[iface_nmbr])) {
+ /* there was an error, so we exit the loop */
+ break;
+ }
+
+ /* actualize speed values in display */
+ actualize_speed_screen(&iface[iface_nmbr], interval, \
+ (iface_count
== 1) ? 0 : iface_nmbr + 1);
+ + /* if needed, actualize transfer values in display */
+ if (transfer_screen) {
+ actualize_transfer_screen(&iface[iface_nmbr], \
+ (iface_count
== 1) ? 0 : iface_nmbr + 1);
+ }
+ + /* Actual values are the old ones in the next loop */
+ iface[iface_nmbr].rc_byte_old = iface[iface_nmbr].rc_byte;
+ iface[iface_nmbr].tr_byte_old = iface[iface_nmbr].tr_byte;
+ iface[iface_nmbr].rc_pkt_old = iface[iface_nmbr].rc_pkt;
+ iface[iface_nmbr].tr_pkt_old = iface[iface_nmbr].tr_pkt;
+
+ } /* for */
+
+ return(0);
+} // End iface_screen()
+#endif //NETLCDCLIENT
+
+/*************************************************************************
+ * Read iface_name values from /proc/net/dev 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 (char *iface_name, struct iface_info *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, think interface is down */
+ interface->status = down;
+ /* Search iface_name and scan values */
+ while ((fgets(buffer, sizeof(buffer), file) != NULL)) {
+ if (strstr(buffer, iface_name)) {
+ /* interface exists */
+ interface->status = up; /* is up */
+ interface->last_online = time(NULL); /* save
actual time */
+ /* copy the name in the interface struct */
+ strcpy(interface->name, iface_name);
+ /* search ':' */
+ ch_pointer = strchr(buffer, ':');
+ /* skip ':' */
+ 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 */
+ fprintf(stderr,"Error: Could not open %s\n", DEVFILE);
+ return(0); /* something went wrong */
+ }
+
+} /* get_iface_stats() */
+
+/*************************************************************************
+ * Send commands to server to add speed screen with all required widgets
+ *************************************************************************
+ */
+void
+initialize_speed_screen(void)
+{
+
+ char buffer[45];
+ int iface_nmbr; /* interface number */
+
+ /* Add screen */
+ sock_send_string(sock, "screen_add NS\n");
+
+ /* Set screen name */
+ sock_send_string(sock, "screen_set NS name {Load}\n");
+
+ /* Add title to screen */
+ sock_send_string(sock, "widget_add NS title title\n");
+
+ if (iface_count == 1) { /* Single interface mode */
+ /* Set title */
+ sprintf(buffer, "widget_set NS title {Load: %s}\n",
iface_alias[0]);
+ sock_send_string(sock, buffer);
+ + /* Add and set download, upload and total string widgets */
+ sock_send_string(sock, "widget_add NS dl string\n");
+ sock_send_string(sock, "widget_set NS dl 1 2 {DL:}\n");
+ sock_send_string(sock, "widget_add NS ul string\n");
+ sock_send_string(sock, "widget_set NS ul 1 3 {UL:}\n");
+ sock_send_string(sock, "widget_add NS total string\n");
+ sock_send_string(sock, "widget_set NS total 1 4 {Total:}\n");
+ }
+ else { /* multi-interfaces mode: one line per interface */
+ /* Set title */
+ if (strstr(unit_label, "B")) {
+ sprintf(buffer, "widget_set NS title {Net Load
(bytes)}\n");
+ }
+ else {
+ if (strstr(unit_label, "b")) {
+ sprintf(buffer, "widget_set NS title {Net Load
(bits)}\n");
+ }
+ else {
+ sprintf(buffer, "widget_set NS title {Net Load
(packets)}\n");
+ }
+ }
+
+ sock_send_string(sock, buffer);
+
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ sprintf(buffer, "widget_add NS i%1d string\n",
iface_nmbr);
+ sock_send_string(sock, buffer);
+ sprintf(buffer, "widget_set NS i%1d 1 %1d {%5.5s NA
(never)}\n",\
+ iface_nmbr,
iface_nmbr+1, iface_alias[iface_nmbr]);
+ sock_send_string(sock, buffer);
+ }
+ } /* else */
+ +} /* initialize_speed_screen() */
+
+/*************************************************************************
+ * Format the value (in bytes) passed as parameter according to its scale,
+ * adding the proper suffix. Store the formatted value string in the
+ * variable 'buff' passed as pointer.
+ *************************************************************************
+ */
+void +format_value (char *buff, double value, char *unit)
+{
+
+ float formated_value;
+
+ /* if the measure unit is in 'b' (bits), the value passed must
+ * be converted to bits (from bytes)
+ */
+ if (strstr(unit, "b")) {
+ value *= 8;
+ }
+
+ /* bytes, bits or packets */
+ if (value < 1024) {
+ sprintf(buff, "%8ld %s",(long)value, unit);
+ return;
+ }
+
+ /* Kilobytes, Kilobits of Kilopackets */
+ if (value < 1000000.0f) {
+ if (strstr(unit, "B")) { /* 1 KB = 1024 */
+ formated_value = (float) value / 1024.0f;
+ }
+ else { /* 1 K = 1000 */
+ formated_value = (float) value / 1000.0f;
+ }
+
+ sprintf(buff, "%8.3f K%s", formated_value, unit);
+ return;
+ }
+
+ /* Megabytes, Megabits or Megapackets */
+ if (value < 1000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 MB = 1024 KB */
+ formated_value = (float) value / 1048576.0f; /* 1024
^ 2 */
+ }
+ else { /* 1 M = 1000 K */
+ formated_value = (float) value / 1000000.0f; /* 1000
^ 2 */
+ }
+
+ sprintf(buff, "%8.3f M%s", formated_value, unit);
+ return;
+ }
+
+ /* Gigabytes, Gigabits or Gigapackets */
+ if (value < 1000000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 GB = 1024 MB */
+ formated_value = (float) value / 1073741824.0f; /*
1024 ^ 3 */
+ }
+ else { /* 1 G = 1000 M */
+ formated_value = (float) value / 1000000000.0f; /*
1000 ^ 3 */
+ }
+
+ sprintf(buff, "%8.3f G%s", formated_value, unit);
+ return;
+ }
+
+ /* Terabytes, Terabits or Terapackets */
+ if (value >= 1000000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 TB = 1024 GB */
+ formated_value = (float) value / 1099511627776.0f; /*
1024 ^ 4 */
+ }
+ else { /* 1 T = 1000 G */
+ formated_value = (float) value / 1000000000000.0f; /*
1000 ^ 4 */
+ }
+
+ sprintf(buff, "%8.3f T%s", formated_value, unit);
+ return;
+ }
+
+} /* format_value() */
+
+/*************************************************************************
+ * Format the value (in bytes) passed as parameter according to its scale,
+ * adding the proper suffix. Store the formatted value string in the
+ * variable 'buff' passed as pointer. Version for multi-interfaces mode
+ *************************************************************************
+ */
+void +format_value_multi_interface (char *buff, double value, char *unit)
+{
+
+ char mybuff[20]; /* temp buffer */
+ float formated_value;
+
+ /* if the measure unit is in 'b' (bits), the value passed must
+ * be converted to bits (from bytes)
+ */
+ if (strstr(unit, "b")) {
+ value *= 8;
+ }
+
+ /* bytes, bits or packets */
+ if (value < 1000) {
+ sprintf(buff, "%3ld ", (long)value);
+ return;
+ }
+
+ /* Kilobytes, Kilobits of Kilopackets */
+ if (value < 1000000) {
+ if (strstr(unit, "B")) { /* 1 KB = 1024 */
+ formated_value = (float) value / 1024.0f;
+ }
+ else { /* 1 K = 1000 */
+ formated_value = (float) value / 1000.0f;
+ }
+
+ sprintf(mybuff, "%3.1f", formated_value);
+ if (mybuff[2] != '.') {
+ sprintf(buff, "%.3sK", mybuff);
+ }
+ else {
+ sprintf(buff, " %.2sK", mybuff);
+ }
+ return;
+ } /* if */
+
+ /* Megabytes, Megabits or Megapackets */
+ if (value < 1000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 MB = 1024 KB */
+ formated_value = (float) value / 1048576.0f; /* 1024
^ 2 */
+ }
+ else { /* 1 M = 1000 K */
+ formated_value = (float) value / 1000000.0f; /* 1000
^ 2 */
+ }
+
+ sprintf(mybuff, "%3.1f", formated_value);
+ if (mybuff[2] != '.') {
+ sprintf(buff, "%.3sM", mybuff);
+ }
+ else {
+ sprintf(buff, " %.2sM", mybuff);
+ }
+ return;
+ } /* if */
+
+ /* Gigabytes, Gigabits or Gigapackets */
+ if (value < 1000000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 GB = 1024 MB */
+ formated_value = (float) value / 1073741824.0f; /*
1024 ^ 3 */
+ }
+ else { /* 1 G = 1000 M */
+ formated_value = (float) value / 1000000000.0f; /*
1000 ^ 3 */
+ }
+
+ sprintf(mybuff, "%3.1f", formated_value);
+ if (mybuff[2] != '.') {
+ sprintf(buff, "%.3sG", mybuff);
+ }
+ else {
+ sprintf(buff, " %.2sG", mybuff);
+ }
+ return;
+ } /* if */
+
+ /* Terabytes, Terabits or Terapackets */
+ if (value >= 1000000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 TB = 1024 GB */
+ formated_value = (float) value / 1099511627776.0f; /*
1024 ^ 4 */
+ }
+ else { /* 1 T = 1000 G */
+ formated_value = (float) value / 1000000000000.0f; /*
1000 ^ 4 */
+ }
+
+ sprintf(mybuff, "%3.1f", formated_value);
+ if (mybuff[2] != '.') {
+ sprintf(buff, "%.3sT", mybuff);
+ }
+ else {
+ sprintf(buff, " %.2sT", mybuff);
+ }
+ return;
+ } /* if */
+
+} /* format_value_multi_interface() */
+
+/*************************************************************************
+ * Format the time in ASCII, depending on the elapsed time
+ *************************************************************************
+ */
+void +get_time_string (char *buff, time_t last_online)
+{
+
+ time_t act_time; /* actual time */
+ char timebuff[30];
+
+ act_time = time(NULL);
+
+ if (last_online == 0) { /* never was online */
+ sprintf(buff, "never");
+ return;
+ }
+
+ /* Transform Unix time format to UTC time format */
+ strcpy(timebuff, ctime(&last_online));
+
+ if ((act_time - last_online) > 86400 ) { /* 86400 = 24 * 60 * 60. Is it + more than 24 hours? */
+ timebuff[10] = '\0';
+ sprintf(buff, "%s", &timebuff[4]);
+ return;
+ }
+ else {
+ timebuff[19] = '\0';
+ sprintf(buff, "%s", &timebuff[11]);
+ return;
+ }
+
+} /* get_time_string() */
+
+/*************************************************************************
+ * Actualize values in display, calculating speeds in the defined interval
+ * of time and sending proper commands to server. If measure unit is + * 'pkt', we don't format this speed. Is always XXXX pkt/s
+ *************************************************************************
+ */
+void
+actualize_speed_screen(struct iface_info *iface, unsigned int interval, int
line)
+{
+ char speed[20]; /* buffer to store the formated speed string in single + interface mode, and download speed string in + multi-interfaces mode) */
+ char speed1[20]; /* buffer to store the formated upload speed string in + multi-interfaces mode*/
+ char send_buff[50]; /* buffer to send to server */
+
+ double rc_speed;
+ double tr_speed;
+
+ if (line == 0) { /* single interface mode */
+ if (iface->status == up) {
+ /* Calculate Download speed */
+ if (strstr(unit_label, "pkt")) { /* don't format this
value */
+ rc_speed = (iface->rc_pkt -
iface->rc_pkt_old) / interval;
+ sprintf(speed, "%8ld %s", (long)rc_speed,
unit_label);
+ }
+ else {
+ rc_speed = (iface->rc_byte -
iface->rc_byte_old) / interval;
+ format_value(speed, rc_speed, unit_label);
+ }
+
+ /* Actualize Download speed */
+ sprintf(send_buff, "widget_set NS dl 1 2 {DL:
%s/s}\n", speed);
+ sock_send_string(sock, send_buff);
+
+ /* Calculate Upload speed */
+ if (strstr(unit_label, "pkt")) { /* don't format this
value */
+ tr_speed = (iface->tr_pkt -
iface->tr_pkt_old) / interval;
+ sprintf(speed, "%8ld %s", (long)tr_speed,
unit_label);
+ }
+ else {
+ tr_speed = (iface->tr_byte -
iface->tr_byte_old) / interval;
+ format_value(speed, tr_speed, unit_label);
+ }
+
+ /* Actualize Upload speed */
+ sprintf(send_buff, "widget_set NS ul 1 3 {UL:
%s/s}\n", speed);
+ sock_send_string(sock, send_buff);
+
+ /* Calculate Total speed */
+ if (strstr(unit_label, "pkt")) { /* don't format this
value */
+ sprintf(speed, "%7ld %s", (long)(rc_speed +
tr_speed), unit_label);
+ }
+ else {
+ format_value(speed, rc_speed + tr_speed,
unit_label);
+ }
+
+ /* Actualize Total speed */
+ sprintf(send_buff, "widget_set NS total 1 4 {Total:
%s/s}\n", speed);
+ sock_send_string(sock, send_buff);
+ }
+ else { /* Interface is down */
+ get_time_string(speed, iface->last_online);
+ sprintf(send_buff, "widget_set NS dl 1 2 {NA
(%s)}\n", speed);
+ sock_send_string(sock, send_buff);
+ sock_send_string(sock, "widget_set NS ul 1 3 {}\n");
+ sock_send_string(sock, "widget_set NS total 1 4
{}\n");
+ }
+ }
+ else { /* multi-interfaces mode: 1 line per interface */
+ if (iface->status == up) {
+ if (strstr(unit_label, "pkt")) {
+ rc_speed = (iface->rc_pkt -
iface->rc_pkt_old) / interval;
+ tr_speed = (iface->tr_pkt -
iface->tr_pkt_old) / interval;
+ }
+ else {
+ rc_speed = (iface->rc_byte -
iface->rc_byte_old) / interval;
+ tr_speed = (iface->tr_byte -
iface->tr_byte_old) / interval;
+ }
+ format_value_multi_interface(speed, rc_speed,
unit_label);
+ format_value_multi_interface(speed1, tr_speed,
unit_label);
+ sprintf(send_buff, "widget_set NS i%1d 1 %1d {%5.5s U:%.4s
D:%.4s}\n", line-1, line+1, iface_alias[line-1], speed1, speed);
+ }
+ else { /* Interface is down */
+ get_time_string(speed, iface->last_online);
+ sprintf(send_buff, "widget_set NS i%1d 1 %1d {%5.5s NA
(%s)}\n", line-1, line+1, iface_alias[line-1], speed);
+ }
+ sock_send_string(sock, send_buff);
+ }
+
+} /* actualize_speed_screen() */
+
+/*************************************************************************
+ * Send commands to server to add transfer screen with all required widgets
+ *************************************************************************
+ */
+void
+initialize_transfer_screen(void)
+{
+
+ char buffer[50];
+ int iface_nmbr; /* interface number */
+
+ /* Add screen */
+ sock_send_string(sock, "screen_add NT\n");
+
+ /* Set screen name */
+ sock_send_string(sock, "screen_setN NT name {Transfer}\n");
+
+ /* Add title to screen */
+ sock_send_string(sock, "widget_add NT title title\n");
+
+ if (iface_count == 1) { /* single interface mode */
+ /* Set title */
+ sprintf(buffer, "widget_set NT title {Transfer: %s}\n",
iface_alias[0]);
+ sock_send_string(sock, buffer);
+
+ /* Add and set download, upload and total string widgets */
+ sock_send_string(sock, "widget_add NT dl string\n");
+ sock_send_string(sock, "widget_set NT dl 1 2 {DL:}\n");
+ sock_send_string(sock, "widget_add NT ul string\n");
+ sock_send_string(sock, "widget_set NT ul 1 3 {UL:}\n");
+ sock_send_string(sock, "widget_add NT total string\n");
+ sock_send_string(sock, "widget_set NT total 1 4 {Total:}\n");
+
+ }
+ else { /* multi-interfaces mode: one line per interface */
+ /* Set title (transfer screen is always in "bytes") */
+ sock_send_string(sock, "widget_set NT title {Net Transfer
(bytes)}\n");
+ + for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ sprintf(buffer, "widget_add NT i%1d string\n",
iface_nmbr);
+ sock_send_string(sock, buffer);
+ sprintf(buffer, "widget_set NT i%1d 1 %1d {%5.5s NA
(never)]\n", iface_nmbr, iface_nmbr+1, iface_alias[iface_nmbr]);
+ sock_send_string(sock, buffer);
+ }
+ }
+
+} /* initialize_transfer_screen() */
+
+/*************************************************************************
+ * Actualize values in display, formatting transfer measures and sending + * proper commands to server. Traffic is shown in "bytes" unit
+ *************************************************************************
+ */
+void
+actualize_transfer_screen(struct iface_info *iface, int line)
+{
+ char transfer[20]; /* buffer to store the formated traffic string in + single interface mode, and download transfer + string in multi-interfaces mode */
+ char transfer1[20]; /* buffer to store the formated upload traffic + string in multi-interfaces mode*/
+ char send_buff[50]; /* buffer to send to server */
+
+ if (line == 0) { /* single interface mode */
+ if (iface->status == up) {
+ /* Format download traffic */
+ format_value(transfer, iface->rc_byte, "B");
+ /* Prepare command to send */
+ sprintf(send_buff, "widget_set NT dl 1 2 {DL:
%s}\n", transfer);
+ /* Send command to server */
+ sock_send_string(sock, send_buff);
+
+ /* Format upload traffic */
+ format_value(transfer, iface->tr_byte, "B");
+ /* Prepare command to send */
+ sprintf(send_buff, "widget_set NT ul 1 3 {UL:
%s}\n", transfer);
+ /* Send command to server */
+ sock_send_string(sock, send_buff);
+
+ /* Format total traffic */
+ format_value(transfer, iface->rc_byte + iface->tr_byte,
"B");
+ /* Prepare command to send */
+ sprintf(send_buff, "widget_set NT total 1 4 {Total:
%s}\n", transfer);
+ /* Send command to server */
+ sock_send_string(sock, send_buff);
+ }
+ else { /* Interface is down */
+ get_time_string(transfer, iface->last_online);
+ sprintf(send_buff, "widget_set NT dl 1 2 {NA
(%s)}\n", transfer);
+ sock_send_string(sock, send_buff);
+ sock_send_string(sock, "widget_set NT ul 1 3 {}\n");
+ sock_send_string(sock, "widget_set NT total 1 4
{}\n");
+ }
+ }
+ else { /* multi-interfaces mode: one line per interface */
+ if (iface->status == up) {
+ format_value_multi_interface(transfer, iface->rc_byte,
"B");
+ format_value_multi_interface(transfer1, iface->tr_byte,
"B");
+ sprintf(send_buff, "widget_set NT i%1d 1 %1d {%5.5s U:%.4s
D:%.4s}\n", line-1, line+1, iface_alias[line-1], transfer1, transfer);
+ }
+ else { /* Interface is down */
+ get_time_string(transfer, iface->last_online);
+ sprintf(send_buff, "widget_set NT i%1d 1 %1d {%5.5s NA (%s)}\n", line-1, line+1, iface_alias[line-1], transfer); + }
+
+ /* Send command to server */
+ sock_send_string(sock, send_buff);
+ }
+
+} /* actualize_transfer_screen() */
+
+#ifdef NETLCDCLIENT
+/*************************************************************************
+ * Exit the program in a clean way
+ *************************************************************************
+ */
+void
+exit_program (int val)
+{
+ /* check if socket is open and close it */
+ if (sock != 0) {
+ sock_close(sock);
+ }
+
+ fprintf(stderr, "\nExiting netlcdclient....\n");
+
+ exit(0);
+
+} /* exit_program() */
+#endif //NETLCDCLIENT
diff --exclude='*.wanjet' --exclude='.??*' --exclude=config.h
--exclude='*.orig' --exclude=Makefile --exclude='*.o' --exclude=Doxyfile
--exclude='*.a' --exclude=config.log --exclude=config.status
--exclude='init-*' --exclude=lcdproc -Nuarp lcdproc/iface.h
lcdproc-cfgfile/iface.h
--- lcdproc/iface.h 1969-12-31 16:00:00.000000000 -0800
+++ lcdproc-cfgfile/iface.h 2006-03-26 12:10:20.000000000 -0800
@@ -0,0 +1,100 @@
+#ifndef IFACE_H
+#define IFACE_H
+/* netlcdclient.h - definitions and function prototipes
+ * + * Copyright (C) 2002 Luis Llorente Campo
+ * + * 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, or (at your option)
+ * any later version.
+ */
+ +
+#include <time.h>
+
+#define DEVFILE "/proc/net/dev" /* file to read statistics from */
+
+/* status definitions */
+
+#define up 1
+#define down 0
+#define MAX_PARAM_LEN 10 /* max interface_parameter or alias length */
+
+#define MAX_INTERFACES 3 /* max number of interfaces in multi-interface
mode */
+
+#ifndef ALF
+int iface_screen (int rep, int display, int *flags_ptr);
+struct iface_info iface[MAX_INTERFACES]; /* interface info */
+int iface_nmbr; /* loop variable for the interface */
+#endif //ALF
+
+/* Struct for interface values (transmision, reception, etc..) */
+
+struct iface_info
+{
+ /* interface name */
+ char name[20];
+
+ int 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;
+ +}; /* struct iface_info */
+
+/************************/
+/* Functions prototipes */
+/************************/
+
+/* show usage options */
+static void usage (int status);
+
+/* parse command line parameters */
+static int decode_switches (int argc, char **argv);
+
+/* read interface stats from /proc/net/dev */
+int get_iface_stats (char *iface_name, struct iface_info *interface);
+
+/* send initial commands to server to add the speed screen */
+void initialize_speed_screen (void);
+
+/* send initial commands to server to add the transfer screen */
+void initialize_transfer_screen(void);
+
+/* format the time in ASCII */
+void get_time_string (char *buff, time_t last_online);
+
+/* format value, scaling value and adding proper suffixes */
+void format_value (char *buff, double value, char *unit);
+
+/* format value, scaling value and adding proper suffixes (for multi-interface + * mode) */
+void format_value_multi_interface(char *buff, double value, char *unit);
+
+/* actualize widgets values in speed screen */
+void actualize_speed_screen (struct iface_info *iface, unsigned int
interval, int line);
+
+/* actualize widgets values in transfer screen */
+void actualize_transfer_screen (struct iface_info *iface, int line);
+
+#ifdef NETLCDCLIENT
+/* exit the program cleanly */
+void exit_program (int val);
+#endif //NETLCDCLIENT
+#endif
diff --exclude='*.wanjet' --exclude='.??*' --exclude=config.h
--exclude='*.orig' --exclude=Makefile --exclude='*.o' --exclude=Doxyfile
--exclude='*.a' --exclude=config.log --exclude=config.status
--exclude='init-*' --exclude=lcdproc -Nuarp lcdproc/lcdproc.conf
lcdproc-cfgfile/lcdproc.conf
--- lcdproc/lcdproc.conf 1969-12-31 16:00:00.000000000 -0800
+++ lcdproc-cfgfile/lcdproc.conf 2006-03-27 09:13:44.000000000 -0800
@@ -0,0 +1,93 @@
+# LCDproc client configuration file
+
+## Options for lcdproc ##
+[lcdproc]
+# address of the LCDd server to connect to
+Server=localhost
+
+# Port of the server to connect to
+Port=13666
+
+# set reporting level
+ReportLevel=2
+
+# report to to syslog ?
+ReportToSyslog=false
+
+# stay in foreground ?
+Foreground=false
+
+# slow down initial announcement of modes (in 1/100s)
+#delay=2
+
+# screen specific configuration options
+[CPU]
+# Show screen
+Active=True
+OnTime=1
+OffTime=2
+ShowInvisible=false
+[Iface]
+# Show screen
+Active=True
+# Show stats for Interface0
+#interface0=eth0
+# Interface0 alias name to display
+#alias0=LAN
+
+# Show stats for Interface1
+#interface1=eth1
+# Interface1 alias name to display
+#alias1=WAN
+
+# Show stats for Interface2 /* more interfaces change iface.h and rebuild */
+#interface2=eth2
+# Interface2 alias name to display
+#alias2=MGMT
+
+# Units to display byte, bit or packet
+unit=bit
+
+# add screen with transferred traffic\n"
+#transfer=TRUE
+
+[Memory]
+# Show screen
+Active=True
+[Load]
+# Show screen
+Active=True
+[TimeDate]
+# Show screen
+Active=True
+[About]
+# Show screen
+Active=false
+[SMP-CPU]
+# Show screen
+Active=false
+[OldTime]
+# Show screen
+Active=false
+[BigClock]
+# Show screen
+Active=false
+[Uptime]
+# Show screen
+Active=false
+[Battery]
+# Show screen
+Active=false
+[CPUGraph]
+# Show screen
+Active=false
+[ProcSize]
+# Show screen
+Active=false
+[Disk]
+# Show screen
+Active=false
+[MiniClock]
+# Show screen
+Active=True
+# EOF
diff --exclude='*.wanjet' --exclude='.??*' --exclude=config.h
--exclude='*.orig' --exclude=Makefile --exclude='*.o' --exclude=Doxyfile
--exclude='*.a' --exclude=config.log --exclude=config.status
--exclude='init-*' --exclude=lcdproc -Nuarp lcdproc/main.c
lcdproc-cfgfile/main.c
--- lcdproc/main.c 2006-03-04 00:00:09.000000000 -0800
+++ lcdproc-cfgfile/main.c 2006-03-26 16:52:51.000000000 -0800
@@ -22,6 +22,7 @@
#include "shared/sockets.h"
#include "shared/debug.h"
#include "shared/report.h"
+#include "shared/configfile.h"
#include "batt.h"
#include "chrono.h"
@@ -31,6 +32,7 @@
#include "load.h"
#include "mem.h"
#include "machine.h"
+#include "iface.h"
// TODO: Commenting... Everything!
@@ -54,6 +56,10 @@ static struct utsname unamebuf;
static void HelpScreen(int exit_state);
static void exit_program(int val);
static void main_loop();
+static int process_command_line(int argc, char **argv);
+static int process_configfile(char *cfgfile);
+#define CHAIN(e,f) { if( e>=0 ) { e=(f); }}
+#define CHAIN_END(e,msg) { if( e<0 ) { report( RPT_CRIT,(msg)); exit(e); }}
static int islow = -1;
@@ -65,26 +71,38 @@ mode sequence[] =
{
// flags default ACTIVE will run by default
// longname which on off inv timer flags - { "CPU", 'C', 1, 2, 0, 0xffff, ACTIVE, cpu_screen }, // [C]PU
- { "Memory", 'M', 4, 16, 0, 0xffff, ACTIVE, mem_screen }, //
[M]emory
- { "Load", 'L', 64, 128, 1, 0xffff, ACTIVE, xload_screen }, //
[L]oad (load histogram)
- { "TimeDate", 'T', 4, 64, 0, 0xffff, ACTIVE, time_screen }, //
[T]ime/Date
- { "About", 'A', 999, 9999, 0, 0xffff, ACTIVE, credit_screen }, //
[A]bout (credits)
- { "SMP-CPU", 'P', 1, 2, 0, 0xffff, 0, cpu_smp_screen }, //
CPU_SM[P]
- { "OldTime", 'O', 4, 64, 0, 0xffff, 0, clock_screen }, //
[O]ld Timescreen
- { "BigClock", 'K', 4, 64, 0, 0xffff, 0, big_clock_screen }, // big cloc[K] - { "Uptime", 'U', 4, 128, 0, 0xffff, 0, uptime_screen }, // Old [U]ptime Screen
- { "Battery", 'B', 32, 256, 1, 0xffff, 0, battery_screen }, //
[B]attery Status
- { "CPUGraph", 'G', 1, 2, 0, 0xffff, 0, cpu_graph_screen }, //
CPU histogram [G]raph
- { "ProcSize", 'S', 16, 256, 1, 0xffff, 0, mem_top_screen }, //
[S]ize of biggest processes
- { "Disk", 'D', 256, 256, 1, 0xffff, 0, disk_screen }, //
[D]isk stats
- { "MiniClock", 'N', 4, 64, 0, 0xffff, 0, mini_clock_screen }, // Mi[n]i clock - { NULL, 0, 0, 0, 0, 0,}, // No more.. all done.
+ { "CPU", 'C', 1, 2, 0, 0xffff, ACTIVE, cpu_screen },
// [C]PU
+ { "Iface", 'I', 1, 2, 0, 0xffff, 0, iface_screen },
// [I]face
+ { "Memory", 'M', 4, 16, 0, 0xffff, ACTIVE, mem_screen },
// [M]emory
+ { "Load", 'L', 64, 128, 1, 0xffff, ACTIVE, xload_screen },
// [L]oad (load histogram)
+ { "TimeDate", 'T', 4, 64, 0, 0xffff, ACTIVE, time_screen },
// [T]ime/Date
+ { "About", 'A', 999, 9999, 0, 0xffff, ACTIVE, credit_screen
}, // [A]bout (credits)
+ { "SMP-CPU", 'P', 1, 2, 0, 0xffff, 0, cpu_smp_screen
}, // CPU_SM[P]
+ { "OldTime", 'O', 4, 64, 0, 0xffff, 0, clock_screen },
// [O]ld Timescreen
+ { "BigClock", 'K', 4, 64, 0, 0xffff, 0, big_clock_screen }, // big cloc[K] + { "Uptime", 'U', 4, 128, 0, 0xffff, 0, uptime_screen }, // Old [U]ptime Screen
+ { "Battery", 'B', 32, 256, 1, 0xffff, 0, battery_screen
}, // [B]attery Status
+ { "CPUGraph", 'G', 1, 2, 0, 0xffff, 0,
cpu_graph_screen }, // CPU histogram [G]raph
+ { "ProcSize", 'S', 16, 256, 1, 0xffff, 0, mem_top_screen
}, // [S]ize of biggest processes
+ { "Disk", 'D', 256, 256, 1, 0xffff, 0, disk_screen },
// [D]isk stats
+ { "MiniClock", 'N', 4, 64, 0, 0xffff, 0, mini_clock_screen },// Mi[n]i clock + { NULL, 0, 0, 0, 0, 0, 0, 0}, // No more.. all done.
};
// TODO: Config file; not just command line
+/* All variables are set to 'unset' values*/
+#define UNSET_INT -1 +#define UNSET_STR "\01"
+#define DEFAULT_SERVER "127.0.0.1"
+char * progname = "lcdproc";
+char * server = NULL;
+int port = LCDPORT;
+int daemonize = UNSET_INT;
+static int report_level = UNSET_INT;
+static int report_dest = UNSET_INT;
+
const char *get_hostname()
{
@@ -135,11 +153,9 @@ void clear_modes()
int
main(int argc, char **argv)
{
- int i;
- int c;
char *server = NULL;
int port = LCDPORT;
- int daemonize = FALSE;
+ int error=0;
/* get uname information */
if (uname(&unamebuf) == -1) {
@@ -153,12 +169,77 @@ main(int argc, char **argv)
signal(SIGHUP, exit_program); // kill -HUP
signal(SIGKILL, exit_program); // kill -9 [cannot be trapped; but
...]
+ /* Set default config file, command line may overwrite */
+ strncpy(configfile, DEFAULT_CONFIGFILE, sizeof(configfile));
+
+ /* Read command line*/
+ CHAIN( error, process_command_line(argc, argv) );
+
+ /* Read config file*/
+ if (strcmp(configfile, UNSET_STR)!=0)
+ CHAIN( error, process_configfile(configfile) );
+
+ CHAIN_END( error, "Error in config file or command line\n" );
+
+ if (server == NULL)
+ server = DEFAULT_SERVER;
+
+ // Connect to the server...
+ usleep(500000); // wait for the server to start up
+ sock = sock_connect(server, port);
+ if (sock <= 0) {
+ fprintf(stderr, "Error connecting to LCD server %s on port
%d.\n"
+ "Check to see that the server is running and
operating normally.\n",
+ server, port);
+ return(EXIT_FAILURE);
+ }
+
+ sock_send_string(sock, "hello\n");
+ usleep(500000); // wait for the server to say hi.
+
+ // We grab the real values below, from the "connect" line.
+ lcd_wid = 20;
+ lcd_hgt = 4;
+ lcd_cellwid = 5;
+ lcd_cellhgt = 8;
+
+ if (daemonize) {
+ if (daemon(1,0) != 0) {
+ fprintf(stderr, "Error: daemonize failed");
+ return(EXIT_FAILURE);
+ }
+ }
+
+ // Init the status gatherers...
+ mode_init();
+
+ // And spew stuff!
+ main_loop();
+
+ // Clean up
+ exit_program(EXIT_SUCCESS);
+
+ return(0);
+}
+
+/* parses arguments given on command line */
+static int
+process_command_line(int argc, char **argv)
+{
+ int i;
+ int c;
+
/* No error output from getopt */
opterr = 0;
/* get options */
- while ((c = getopt( argc, argv, "s:p:e:dhv")) > 0) {
+ while ((c = getopt( argc, argv, "s:p:e:c:dhv")) > 0) {
switch (c) {
+ // c is for config file
+ case 'c':
+ strncpy(configfile, optarg,
sizeof(configfile));
+ configfile[sizeof(configfile)-1] = 0; /*
Terminate string */
+ break;
// s is for server
case 's':
if (server == NULL)
@@ -204,50 +285,75 @@ main(int argc, char **argv)
int found = set_mode(shortname, argv[i], 1);

if (!found) {
- fprintf(stderr, "Invalid Mode: %c, ignoring\n",
argv[i][0]);
+ fprintf(stderr, "Invalid Mode: %c\n", argv[i][0]);
return(EXIT_FAILURE);
}
}
+ return 0;
+}
- if (server == NULL)
- server = "localhost";
+/* reads and parses configuration file */
+static int
+process_configfile(char *configfile)
+{
+ int k;
- // Connect to the server...
- usleep(500000); // wait for the server to start up
- sock = sock_connect(server, port);
- if (sock <= 0) {
- fprintf(stderr, "Error connecting to LCD server %s on port
%d.\n"
- "Check to see that the server is running and
operating normally.\n",
- server, port);
- return(EXIT_FAILURE);
- }
+ debug( RPT_DEBUG, "%s()", __FUNCTION__ );
- sock_send_string(sock, "hello\n");
- usleep(500000); // wait for the server to say hi.
+ /* Read server settings*/
- // We grab the real values below, from the "connect" line.
- lcd_wid = 20;
- lcd_hgt = 4;
- lcd_cellwid = 5;
- lcd_cellhgt = 8;
+ if( strcmp( configfile, UNSET_STR ) == 0 ) {
+ configfile = DEFAULT_CONFIGFILE;
+ }
+ if( config_read_file( configfile ) != 0 ) {
+ report( RPT_CRIT, "Could not read config file: %s",
configfile );
+ return -1;
+ //report( RPT_WARNING, "Could not read config file: %s",
configfile );
+ }
- if (daemonize) {
- if (daemon(1,0) != 0) {
- fprintf(stderr, "Error: daemonize failed");
- return(EXIT_FAILURE);
+ if( server == NULL ) {
+ server = strdup( config_get_string( progname, "Server", 0,
DEFAULT_SERVER ));
+ printf("Server %s\n",server);
+ }
+ if( port == UNSET_INT ) {
+ port = config_get_int( progname, "Port", 0, LCDPORT );
+ }
+ if( report_level == UNSET_INT ) {
+ report_level = config_get_int( progname, "ReportLevel", 0,
RPT_WARNING );
+ }
+ if( report_dest == UNSET_INT ) {
+ if( config_get_bool( progname, "ReportToSyslog", 0, 0 )) {
+ report_dest = RPT_DEST_SYSLOG;
+ } else {
+ report_dest = RPT_DEST_STDERR;
}
- }
-
- // Init the status gatherers...
- mode_init();
-
- // And spew stuff!
- main_loop();
+ }
+ if( daemonize == UNSET_INT ) {
+ daemonize = ~config_get_bool( progname, "Foreground", 0, 1 );
+ }
+ if( islow < 0 ) {
+ islow = config_get_int( progname, "delay", 0, -1 );
+ }
- // Clean up
- exit_program(EXIT_SUCCESS);
+ /*
+ * check for config file variables to override all the sequence
defaults
+ */
+ for ( k = 0; sequence[k].which ; k++) {
+ if (sequence[k].longname) {
+ sequence[k].on_time = config_get_int( sequence[k].longname,
"OnTime", 0, sequence[k].on_time );
+ sequence[k].off_time = config_get_int( sequence[k].longname,
"OffTime", 0, sequence[k].off_time );
+ sequence[k].show_invisible = config_get_bool(
sequence[k].longname, "ShowInvisible", 0, sequence[k].show_invisible );
+ if ( config_get_bool( sequence[k].longname, "Active", 0,
sequence[k].flags & ACTIVE ) ) {
+ sequence[k].flags |= ACTIVE;
+ printf("Active %s\n", sequence[k].longname);
+ } else {
+ sequence[k].flags &= (~ACTIVE);
+ printf("Inactive %s\n", sequence[k].longname);
+ }
+ }
+ }
- return(0);
+ return 0;
}
void
@@ -260,6 +366,7 @@ HelpScreen (int exit_state)
" -p <port> connect to LCDd daemon using
<port>\n"
" -d daemonize\n"
" -e <delay> slow down initial announcement of
modes (in 1/100s)\n"
+ " -c <config> Use a configuration file other than
%s\n"
" -h show this help screen\n"
" -v display program version\n"
" and <modes> are\n"
@@ -273,14 +380,14 @@ HelpScreen (int exit_state)
" B Battery battery status\n"
" T TimeDate time & date information\n"
" O OldTime old time screen\n"
- " U Uptime old uptime screen\n"
+ " U Uptime uptime screen\n"
" K BigClock big clock\n"
" N MiniClock minimal clock\n"
" A About credits page\n"
"\n"
"Example:\n"
" lcdproc -s my.lcdproc.server.com p 13666 C M X\n"
- );
+ , DEFAULT_CONFIGFILE);
exit(exit_state);
}
@@ -298,6 +405,7 @@ exit_program(int val)
exit(val);
}
+#define LCDPROC_MENUS
#ifdef LCDPROC_MENUS
int
menus_init ()
diff --exclude='*.wanjet' --exclude='.??*' --exclude=config.h
--exclude='*.orig' --exclude=Makefile --exclude='*.o' --exclude=Doxyfile
--exclude='*.a' --exclude=config.log --exclude=config.status
--exclude='init-*' --exclude=lcdproc -Nuarp lcdproc/main.h
lcdproc-cfgfile/main.h
--- lcdproc/main.h 2006-02-28 00:00:09.000000000 -0800
+++ lcdproc-cfgfile/main.h 2006-03-26 13:01:36.000000000 -0800
@@ -20,6 +20,9 @@
#define LCDP_AC_BACKUP 0x02
#define LCDP_AC_UNKNOWN 0xFF
+#define DEFAULT_CONFIGFILE "/usr/local/etc/lcdproc.conf"
+char configfile[256]; /* a lot of space in the executable. */
+
extern int Quit;
extern int sock;
extern char *version;
diff --exclude='*.wanjet' --exclude='.??*' --exclude=config.h
--exclude='*.orig' --exclude=Makefile --exclude='*.o' --exclude=Doxyfile
--exclude='*.a' --exclude=config.log --exclude=config.status
--exclude='init-*' --exclude=lcdproc -Nuarp lcdproc/iface.c
lcdproc-cfgfile/iface.c
--- lcdproc/iface.c 1969-12-31 16:00:00.000000000 -0800
+++ lcdproc-cfgfile/iface.c 2006-03-27 09:37:43.000000000 -0800
@@ -0,0 +1,1008 @@
+/*
+ netlcdclient - Client for LCDproc which shows networks statistics
+
+ Copyright (C) 2002 Luis Llorente Campo
<luisllorente AT luisllorente.com>

+ Multiinterface Extension by Stephan Skrodzki
<skrodzki AT stevekist.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, or (at your option)
+ any later 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.
+
+ 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.
+
+*/
+
+#include <stdio.h>
+#include <getopt.h>
+#include <signal.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "iface.h"
+#ifdef NETLCDCLIENT
+#include "sockets.h"
+#else
+#include "shared/sockets.h"
+#include "shared/debug.h"
+#include "shared/report.h"
+#include "shared/configfile.h"
+#include "main.h"
+#define UNSET_INT -1
+#define UNSET_STR "\01"
+#endif //NETLCDCLIENT
+
+
+#ifdef NETLCDCLIENT
+/* Option flags and global variables */
+
+char *program_name; /* the name the program was run with */
+int port = 13666; /* default port */
+char server[100] = "localhost"; /* default server */
+int daemon_mode = 0; /* by default, no daemon mode */
+#endif //NETLCDCLIENT
+/* interface name passed as parameter */
+char iface_parameter[MAX_INTERFACES][MAX_PARAM_LEN] = { "\0" , "\0" , "\0" };
+/* alias name for the interface */
+char iface_alias[MAX_INTERFACES][MAX_PARAM_LEN] = { "\0" , "\0" , "\0" };
+int iface_count = 0; /* number of interfaces */
+char unit_label[MAX_INTERFACES] = "B"; /* default unit label is Bytes */
+int transfer_screen = 0; /* by default, transfer screen is not shown */
+int sock = 0; /* socket handler */
+
+/* command line parameters, used by getopt_long() */
+static struct option const long_options[] =
+{
+ {"interface", required_argument, 0, 'i'},
+ {"alias", required_argument, 0, 'a'},
+ {"server", required_argument, 0, 's'},
+ {"port", required_argument, 0, 'p'},
+ {"unit", required_argument, 0, 'u'},
+ {"transfer", no_argument, 0, 't'},
+ {"daemon", no_argument, 0, 'd'},
+ {"help", no_argument, 0, 'h'},
+ {"version", no_argument, 0, 'V'},
+ {NULL, 0, NULL, 0}
+};
+
+#ifdef NETLCDCLIENT
+/*************************************************************************
+ * MAIN PROGRAM FUNCTION
+ *************************************************************************
+ */
+
+int
+main (int argc, char **argv)
+{
+
+ struct iface_info iface[MAX_INTERFACES]; /* interface info */
+
+ int len; /* bytes read from server */
+
+ char readbuff[MAXMSG]; /* read buffer from server */
+
+ unsigned int sleep_time = 1; /* interval between updates (1 second)
*/
+
+ int pid; /* child process identificator for daemon mode */
+
+ int iface_nmbr; /* loop variable for the interface */
+
+ /* Capture signals to exit program cleanly */
+ /* Ctrl-C */
+ signal(SIGINT, exit_program);
+ /* kill */
+ signal(SIGTERM, exit_program);
+ /* kill -HUP */
+ signal(SIGHUP, exit_program);
+ /* kill -KILL */
+ signal(SIGKILL, exit_program);
+
+ /* get program name from command line */
+ program_name = argv[0];
+
+ /* parse command line parameters */
+ decode_switches (argc, argv);
+
+ /* check if any interface name has been introduced */
+
+ if (iface_count == 0) {
+ /* no one has been introduced, so exit the program showing
help */
+ printf("No interface selected, please, use --interface
option\n");
+ usage (1);
+ }
+
+ /* check if we go to daemon mode */
+ if (daemon_mode) {
+ signal(SIGTTOU, SIG_IGN);
+ signal(SIGTTIN, SIG_IGN);
+ signal(SIGTSTP, SIG_IGN);
+ /* create child process */
+ pid = fork();
+ /* create new session */
+ setsid();
+ signal(SIGHUP, SIG_IGN);
+ if (pid != 0) { /* we are the parent process and exit */
+ exit(0);
+ }
+ /* from here, we are the child process */
+ }
+
+ /* Try to connect to server */
+ sock = sock_connect(server, port);
+
+ if (sock <= 0) { /* there was some error and exit */
+ fprintf(stderr, "Error: Could not connect to %s:%d\n", server, port);
+ exit(0);
+ }
+
+ /* Say "hello" to server */
+ sock_send_string(sock, "hello\n");
+
+ /* Wait the server to be prepared */
+ usleep(500000);
+
+ /* Read server response */
+ len = sock_recv(sock, readbuff, MAXMSG);
+
+ /* Now we are ready to send commands to server */
+
+ /* set initial speed screen with widgets */
+ initialize_speed_screen();
+
+ /* set initial transfer screen if needed */
+ if (transfer_screen) {
+ initialize_transfer_screen();
+ }
+
+ /* initialize all interface structs */
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ iface[iface_nmbr].last_online = 0;
+ iface[iface_nmbr].status = down;
+ }
+
+
+ /* main loop */
+ while (1) {
+ /* read server responses */
+ len = sock_recv(sock, readbuff, MAXMSG);
+
+ /* for each interface do */
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ /*read iface_parameter stats */
+ if (!get_iface_stats(iface_parameter[iface_nmbr], \
+
&iface[iface_nmbr])) {
+ /* there was an error, so we exit the loop */
+ break;
+ }
+
+ /* actualize speed values in display */
+ actualize_speed_screen(&iface[iface_nmbr], sleep_time, \
+
(iface_count == 1) ? 0 : iface_nmbr + 1);
+
+ /* if needed, actualize transfer values in display */
+ if (transfer_screen) {
+ actualize_transfer_screen(&iface[iface_nmbr], \
+
(iface_count == 1) ? 0 : iface_nmbr + 1);
+ }
+
+ /* Actual values are the old ones in the next loop */
+ iface[iface_nmbr].rc_byte_old = iface[iface_nmbr].rc_byte;
+ iface[iface_nmbr].tr_byte_old = iface[iface_nmbr].tr_byte;
+ iface[iface_nmbr].rc_pkt_old = iface[iface_nmbr].rc_pkt;
+ iface[iface_nmbr].tr_pkt_old = iface[iface_nmbr].tr_pkt;
+
+ } /* for */
+
+ /* Wait some time to do the main loop again */
+ sleep(sleep_time);
+
+ } /* while (1) */
+
+ /* Before exit the program, close the socket */
+ sock_close(sock);
+
+ exit (0);
+
+} /* main() */
+
+/*************************************************************************
+ * END MAIN PROGRAM FUNCTION
+ **************************************************************************/
+
+/*************************************************************************
+ * Set all the option flags according to the switches specified.
+ * Return the index of the first non-option argument.
+ *************************************************************************
+ */
+
+static int
+decode_switches (int argc, char **argv)
+{
+ int c;
+
+ while ((c = getopt_long (argc, argv, "i:a:s:p:u:tdhV", \
+ long_options,
(int *) 0)) != EOF) {
+
+ switch (c) {
+
+ case 'i':
+ /* Check number of interfaces introduced */
+ if (iface_count >= MAX_INTERFACES) {
+ fprintf(stderr, "Too many interfaces
introduced. " \
+ "Only %d are
supported.\n", MAX_INTERFACES);
+ exit(0);
+ }
+ strncpy(iface_parameter[iface_count], optarg,
MAX_PARAM_LEN);
+ strncpy(iface_alias[iface_count], optarg,
MAX_PARAM_LEN);
+ iface_count++;
+ break;
+
+ case 'a':
+ strncpy(iface_alias[iface_count-1], optarg,
MAX_PARAM_LEN);
+ break;
+
+ case 's':
+ strncpy(server, optarg, sizeof(server));
+ break;
+
+ case 'p':
+ port = atoi(optarg);
+ break;
+
+ case 'u':
+ /* check for valid values */
+ if (strstr(optarg, "byte"))
strncpy(unit_label, "B", \
+
sizeof(unit_label));
+ else if (strstr(optarg, "bit"))
strncpy(unit_label, "b", \
+
sizeof(unit_label));
+ else if (strstr(optarg, "packet"))
strncpy(unit_label, "pkt", \
+
sizeof(unit_label));
+ else {
+ fprintf(stderr, "netlcdclient:
argument '%s' for -u parameter is not valid\n", optarg);
+ usage(0);
+ }
+ break;
+
+ case 't':
+ transfer_screen = 1; /* show transfer screen
*/
+ break;
+
+ case 'd':
+ daemon_mode = 1; /* go to daemon mode */
+ break;
+
+ case 'V': /* show version */
+ printf("netlcdclient v%s\n", VERSION);
+ exit (0);
+
+ case 'h': /* show help */
+ usage (0);
+ exit(0);
+
+ } /* switch */
+ } /* while */
+
+ return optind;
+
+} /* decode_switches() */
+
+
+/*************************************************************************
+ * Show program help and exit.
+ *************************************************************************
+ */
+
+static void
+usage (int status)
+{
+ printf ("\n"
+"netlcdclient v%s for LCDproc, by Luis Llorente\n", VERSION);
+ printf ("\n"
+"Usage: %s -i <interface> [-a alias] [ -tdhV ] [ -s server ] [ -p port ] [-u
unit]\n", program_name);
+ printf ("\n"
+"Options in []'s are optional.\n"
+" -i ... -i , --interface=INTERFACE show INTERFACE statistics\n"
+" -a , --alias=ALIAS alias name for the interface (-a has
to \n"
+" follow -i)\n"
+" -s, --server=SERVER connect to SERVER (default is
localhost)\n"
+" -p, --port=NUMBER connect to specified port number
(default\n"
+" is 13666)\n"
+" -u, --unit=TYPE speed measure unit. Available units
are:\n"
+" byte (default)\n"
+" bit\n"
+" packet\n"
+" -t, --transfer add screen with transferred traffic\n"
+" -d, --daemon run in the background\n"
+" -h, --help display this help and exit\n"
+" -V, --version output version information and exit\n"
+);
+ printf("\n"
+"Example:\n"
+" %s -s my.server.org -p 2300 -u bit -i eth0 -a LAN\n", program_name);
+
+ exit (status);
+
+} /* usage() */
+
+#else
+/* reads and parses configuration file */
+static int
+iface_process_configfile(char *configfile)
+{
+ char iface_label[12];
+
+ debug( RPT_DEBUG, "%s()", __FUNCTION__ );
+
+ /* Read server settings*/
+
+ if( strcmp( configfile, UNSET_STR ) == 0 || config_read_file(
configfile ) != 0 ) {
+ report( RPT_CRIT, "Could not read config file: %s",
configfile );
+ return -1;
+ printf( "Could not read config file: %s", configfile );
+ //report( RPT_WARNING, "Could not read config file: %s",
configfile );
+ }
+
+ for (iface_count = 0; iface_count < MAX_INTERFACES; iface_count++) {
+ sprintf(iface_label, "interface%i", iface_count);
+ printf("Label %s count %i\n", iface_label, iface_count);
+ strncpy(iface_parameter[iface_count], config_get_string(
"Iface", iface_label, 0, "" ),sizeof(iface_parameter[iface_count]));
+ sprintf(iface_label, "alias%i", iface_count);
+ strncpy(iface_alias[iface_count], config_get_string( "Iface",
iface_label, 0, "" ),sizeof(iface_alias[iface_count]));
+ printf("Interface %s alias %s count %i\n",
iface_parameter[iface_count], iface_alias[iface_count], iface_count);
+ if ( !strlen(iface_parameter[iface_count] )) {
+ iface_count++;
+ break;
+ }
+ }
+ strncpy(unit_label, config_get_string( "Iface", "unit", 0, "bit"),
sizeof(unit_label));
+ transfer_screen = config_get_bool( "Iface", "transfer", 0, 0 );
+
+ return 0;
+}
+//////////////////////////////////////////////////////////////////////////
+// IFace screen shows info about percentage of the CPU being used
+//
+// +--------------------+ +--------------------+
+// |## CPU 51.9%: myh #@| |CPU [---- ]48.1%@|
+// |Usr 46.0% Nice 0.0%| |U-- S- N I--- |
+// |Sys 5.9% Idle 48.1%| +--------------------+
+// |0%-------- 100%|
+// +--------------------+
+//
+int
+iface_screen(int rep, int display, int *flags_ptr)
+{
+ //ALF TODO need to make this actual time since last run
+ unsigned int interval = difftime(time(NULL),iface[0].last_online);
/* interval since last update */
+ if (!interval) return 0; /* need at least 1 second, no divide by 0 */
+
+ if ((*flags_ptr & INITIALIZED) == 0) {
+ *flags_ptr |= INITIALIZED;
+
+ /* get configuration options */
+ iface_process_configfile(configfile);
+
+ /* set initial speed screen with widgets */
+ initialize_speed_screen();
+
+ /* set initial transfer screen if needed */
+ if (transfer_screen) {
+ initialize_transfer_screen();
+ }
+
+ /* initialize all interface structs */
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ iface[iface_nmbr].last_online = 0;
+ iface[iface_nmbr].status = down;
+ }
+ return(0);
+ }
+ /* for each interface do */
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ /*read iface_parameter stats */
+ if (!get_iface_stats(iface_parameter[iface_nmbr], \
+
&iface[iface_nmbr])) {
+ /* there was an error, so we exit the loop */
+ break;
+ }
+
+ /* actualize speed values in display */
+ actualize_speed_screen(&iface[iface_nmbr], interval, \
+ (iface_count
== 1) ? 0 : iface_nmbr + 1);
+
+ /* if needed, actualize transfer values in display */
+ if (transfer_screen) {
+ actualize_transfer_screen(&iface[iface_nmbr], \
+ (iface_count
== 1) ? 0 : iface_nmbr + 1);
+ }
+
+ /* Actual values are the old ones in the next loop */
+ iface[iface_nmbr].rc_byte_old = iface[iface_nmbr].rc_byte;
+ iface[iface_nmbr].tr_byte_old = iface[iface_nmbr].tr_byte;
+ iface[iface_nmbr].rc_pkt_old = iface[iface_nmbr].rc_pkt;
+ iface[iface_nmbr].tr_pkt_old = iface[iface_nmbr].tr_pkt;
+
+ } /* for */
+
+ return(0);
+} // End iface_screen()
+#endif //NETLCDCLIENT
+
+/*************************************************************************
+ * Read iface_name values from /proc/net/dev 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 (char *iface_name, struct iface_info *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, think interface is down */
+ interface->status = down;
+ /* Search iface_name and scan values */
+ while ((fgets(buffer, sizeof(buffer), file) != NULL)) {
+ if (strstr(buffer, iface_name)) {
+ /* interface exists */
+ interface->status = up; /* is up */
+ interface->last_online = time(NULL); /* save
actual time */
+ /* copy the name in the interface struct */
+ strcpy(interface->name, iface_name);
+ /* search ':' */
+ ch_pointer = strchr(buffer, ':');
+ /* skip ':' */
+ 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 */
+ fprintf(stderr,"Error: Could not open %s\n", DEVFILE);
+ return(0); /* something went wrong */
+ }
+
+} /* get_iface_stats() */
+
+/*************************************************************************
+ * Send commands to server to add speed screen with all required widgets
+ *************************************************************************
+ */
+void
+initialize_speed_screen(void)
+{
+
+ char buffer[45];
+ int iface_nmbr; /* interface number */
+
+ /* Add screen */
+ sock_send_string(sock, "screen_add I\n");
+
+ /* Set screen name */
+ sock_send_string(sock, "screen_set I name {Load}\n");
+
+ /* Add title to screen */
+ sock_send_string(sock, "widget_add I title title\n");
+
+ if (iface_count == 1) { /* Single interface mode */
+ /* Set title */
+ sprintf(buffer, "widget_set I title {Load: %s}\n",
iface_alias[0]);
+ sock_send_string(sock, buffer);
+
+ /* Add and set download, upload and total string widgets */
+ sock_send_string(sock, "widget_add I dl string\n");
+ sock_send_string(sock, "widget_set I dl 1 2 {DL:}\n");
+ sock_send_string(sock, "widget_add I ul string\n");
+ sock_send_string(sock, "widget_set I ul 1 3 {UL:}\n");
+ sock_send_string(sock, "widget_add I total string\n");
+ sock_send_string(sock, "widget_set I total 1 4 {Total:}\n");
+ }
+ else { /* multi-interfaces mode: one line per interface */
+ /* Set title */
+ if (strstr(unit_label, "B")) {
+ sprintf(buffer, "widget_set I title {Net Load
(bytes)}\n");
+ }
+ else {
+ if (strstr(unit_label, "b")) {
+ sprintf(buffer, "widget_set I title {Net Load
(bits)}\n");
+ }
+ else {
+ sprintf(buffer, "widget_set I title {Net Load
(packets)}\n");
+ }
+ }
+
+ sock_send_string(sock, buffer);
+
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ sprintf(buffer, "widget_add I i%1d string\n",
iface_nmbr);
+ sock_send_string(sock, buffer);
+ sprintf(buffer, "widget_set I i%1d 1 %1d {%5.5s NA
(never)}\n",\
+ iface_nmbr,
iface_nmbr+1, iface_alias[iface_nmbr]);
+ sock_send_string(sock, buffer);
+ }
+ } /* else */
+
+} /* initialize_speed_screen() */
+
+/*************************************************************************
+ * Format the value (in bytes) passed as parameter according to its scale,
+ * adding the proper suffix. Store the formatted value string in the
+ * variable 'buff' passed as pointer.
+ *************************************************************************
+ */
+void
+format_value (char *buff, double value, char *unit)
+{
+
+ float formated_value;
+
+ /* if the measure unit is in 'b' (bits), the value passed must
+ * be converted to bits (from bytes)
+ */
+ if (strstr(unit, "b")) {
+ value *= 8;
+ }
+
+ /* bytes, bits or packets */
+ if (value < 1024) {
+ sprintf(buff, "%8ld %s",(long)value, unit);
+ return;
+ }
+
+ /* Kilobytes, Kilobits of Kilopackets */
+ if (value < 1000000.0f) {
+ if (strstr(unit, "B")) { /* 1 KB = 1024 */
+ formated_value = (float) value / 1024.0f;
+ }
+ else { /* 1 K = 1000 */
+ formated_value = (float) value / 1000.0f;
+ }
+
+ sprintf(buff, "%8.3f K%s", formated_value, unit);
+ return;
+ }
+
+ /* Megabytes, Megabits or Megapackets */
+ if (value < 1000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 MB = 1024 KB */
+ formated_value = (float) value / 1048576.0f; /* 1024
^ 2 */
+ }
+ else { /* 1 M = 1000 K */
+ formated_value = (float) value / 1000000.0f; /* 1000
^ 2 */
+ }
+
+ sprintf(buff, "%8.3f M%s", formated_value, unit);
+ return;
+ }
+
+ /* Gigabytes, Gigabits or Gigapackets */
+ if (value < 1000000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 GB = 1024 MB */
+ formated_value = (float) value / 1073741824.0f; /*
1024 ^ 3 */
+ }
+ else { /* 1 G = 1000 M */
+ formated_value = (float) value / 1000000000.0f; /*
1000 ^ 3 */
+ }
+
+ sprintf(buff, "%8.3f G%s", formated_value, unit);
+ return;
+ }
+
+ /* Terabytes, Terabits or Terapackets */
+ if (value >= 1000000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 TB = 1024 GB */
+ formated_value = (float) value / 1099511627776.0f; /*
1024 ^ 4 */
+ }
+ else { /* 1 T = 1000 G */
+ formated_value = (float) value / 1000000000000.0f; /*
1000 ^ 4 */
+ }
+
+ sprintf(buff, "%8.3f T%s", formated_value, unit);
+ return;
+ }
+
+} /* format_value() */
+
+/*************************************************************************
+ * Format the value (in bytes) passed as parameter according to its scale,
+ * adding the proper suffix. Store the formatted value string in the
+ * variable 'buff' passed as pointer. Version for multi-interfaces mode
+ *************************************************************************
+ */
+void
+format_value_multi_interface (char *buff, double value, char *unit)
+{
+
+ char mybuff[20]; /* temp buffer */
+ float formated_value;
+
+ /* if the measure unit is in 'b' (bits), the value passed must
+ * be converted to bits (from bytes)
+ */
+ if (strstr(unit, "b")) {
+ value *= 8;
+ }
+
+ /* bytes, bits or packets */
+ if (value < 1000) {
+ sprintf(buff, "%3ld ", (long)value);
+ return;
+ }
+
+ /* Kilobytes, Kilobits of Kilopackets */
+ if (value < 1000000) {
+ if (strstr(unit, "B")) { /* 1 KB = 1024 */
+ formated_value = (float) value / 1024.0f;
+ }
+ else { /* 1 K = 1000 */
+ formated_value = (float) value / 1000.0f;
+ }
+
+ sprintf(mybuff, "%3.1f", formated_value);
+ if (mybuff[2] != '.') {
+ sprintf(buff, "%.3sK", mybuff);
+ }
+ else {
+ sprintf(buff, " %.2sK", mybuff);
+ }
+ return;
+ } /* if */
+
+ /* Megabytes, Megabits or Megapackets */
+ if (value < 1000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 MB = 1024 KB */
+ formated_value = (float) value / 1048576.0f; /* 1024
^ 2 */
+ }
+ else { /* 1 M = 1000 K */
+ formated_value = (float) value / 1000000.0f; /* 1000
^ 2 */
+ }
+
+ sprintf(mybuff, "%3.1f", formated_value);
+ if (mybuff[2] != '.') {
+ sprintf(buff, "%.3sM", mybuff);
+ }
+ else {
+ sprintf(buff, " %.2sM", mybuff);
+ }
+ return;
+ } /* if */
+
+ /* Gigabytes, Gigabits or Gigapackets */
+ if (value < 1000000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 GB = 1024 MB */
+ formated_value = (float) value / 1073741824.0f; /*
1024 ^ 3 */
+ }
+ else { /* 1 G = 1000 M */
+ formated_value = (float) value / 1000000000.0f; /*
1000 ^ 3 */
+ }
+
+ sprintf(mybuff, "%3.1f", formated_value);
+ if (mybuff[2] != '.') {
+ sprintf(buff, "%.3sG", mybuff);
+ }
+ else {
+ sprintf(buff, " %.2sG", mybuff);
+ }
+ return;
+ } /* if */
+
+ /* Terabytes, Terabits or Terapackets */
+ if (value >= 1000000000000.0f) {
+ if (strstr(unit, "B")) { /* 1 TB = 1024 GB */
+ formated_value = (float) value / 1099511627776.0f; /*
1024 ^ 4 */
+ }
+ else { /* 1 T = 1000 G */
+ formated_value = (float) value / 1000000000000.0f; /*
1000 ^ 4 */
+ }
+
+ sprintf(mybuff, "%3.1f", formated_value);
+ if (mybuff[2] != '.') {
+ sprintf(buff, "%.3sT", mybuff);
+ }
+ else {
+ sprintf(buff, " %.2sT", mybuff);
+ }
+ return;
+ } /* if */
+
+} /* format_value_multi_interface() */
+
+/*************************************************************************
+ * Format the time in ASCII, depending on the elapsed time
+ *************************************************************************
+ */
+void
+get_time_string (char *buff, time_t last_online)
+{
+
+ time_t act_time; /* actual time */
+ char timebuff[30];
+
+ act_time = time(NULL);
+
+ if (last_online == 0) { /* never was online */
+ sprintf(buff, "never");
+ return;
+ }
+
+ /* Transform Unix time format to UTC time format */
+ strcpy(timebuff, ctime(&last_online));
+
+ if ((act_time - last_online) > 86400 ) { /* 86400 = 24 * 60 * 60. Is
it
+
more than 24 hours? */
+ timebuff[10] = '\0';
+ sprintf(buff, "%s", &timebuff[4]);
+ return;
+ }
+ else {
+ timebuff[19] = '\0';
+ sprintf(buff, "%s", &timebuff[11]);
+ return;
+ }
+
+} /* get_time_string() */
+
+/*************************************************************************
+ * Actualize values in display, calculating speeds in the defined interval
+ * of time and sending proper commands to server. If measure unit is
+ * 'pkt', we don't format this speed. Is always XXXX pkt/s
+ *************************************************************************
+ */
+void
+actualize_speed_screen(struct iface_info *iface, unsigned int interval, int
line)
+{
+ char speed[20]; /* buffer to store the formated speed string in
single
+ interface mode, and
download speed string in
+ multi-interfaces
mode) */
+ char speed1[20]; /* buffer to store the formated upload speed
string in
+ multi-interfaces
mode*/
+ char send_buff[50]; /* buffer to send to server */
+
+ double rc_speed;
+ double tr_speed;
+
+ if (line == 0) { /* single interface mode */
+ if (iface->status == up) {
+ /* Calculate Download speed */
+ if (strstr(unit_label, "pkt")) { /* don't format this
value */
+ rc_speed = (iface->rc_pkt -
iface->rc_pkt_old) / interval;
+ sprintf(speed, "%8ld %s", (long)rc_speed,
unit_label);
+ }
+ else {
+ rc_speed = (iface->rc_byte -
iface->rc_byte_old) / interval;
+ format_value(speed, rc_speed, unit_label);
+ }
+
+ /* Actualize Download speed */
+ sprintf(send_buff, "widget_set I dl 1 2 {DL:
%s/s}\n", speed);
+ sock_send_string(sock, send_buff);
+
+ /* Calculate Upload speed */
+ if (strstr(unit_label, "pkt")) { /* don't format this
value */
+ tr_speed = (iface->tr_pkt -
iface->tr_pkt_old) / interval;
+ sprintf(speed, "%8ld %s", (long)tr_speed,
unit_label);
+ }
+ else {
+ tr_speed = (iface->tr_byte -
iface->tr_byte_old) / interval;
+ format_value(speed, tr_speed, unit_label);
+ }
+
+ /* Actualize Upload speed */
+ sprintf(send_buff, "widget_set I ul 1 3 {UL:
%s/s}\n", speed);
+ sock_send_string(sock, send_buff);
+
+ /* Calculate Total speed */
+ if (strstr(unit_label, "pkt")) { /* don't format this
value */
+ sprintf(speed, "%7ld %s", (long)(rc_speed +
tr_speed), unit_label);
+ }
+ else {
+ format_value(speed, rc_speed + tr_speed,
unit_label);
+ }
+
+ /* Actualize Total speed */
+ sprintf(send_buff, "widget_set I total 1 4 {Total:
%s/s}\n", speed);
+ sock_send_string(sock, send_buff);
+ }
+ else { /* Interface is down */
+ get_time_string(speed, iface->last_online);
+ sprintf(send_buff, "widget_set I dl 1 2 {NA (%s)}\n",
speed);
+ sock_send_string(sock, send_buff);
+ sock_send_string(sock, "widget_set I ul 1 3 {}\n");
+ sock_send_string(sock, "widget_set I total 1 4 {}\n");
+ }
+ }
+ else { /* multi-interfaces mode: 1 line per interface */
+ if (iface->status == up) {
+ if (strstr(unit_label, "pkt")) {
+ rc_speed = (iface->rc_pkt -
iface->rc_pkt_old) / interval;
+ tr_speed = (iface->tr_pkt -
iface->tr_pkt_old) / interval;
+ }
+ else {
+ rc_speed = (iface->rc_byte -
iface->rc_byte_old) / interval;
+ tr_speed = (iface->tr_byte -
iface->tr_byte_old) / interval;
+ }
+ format_value_multi_interface(speed, rc_speed,
unit_label);
+ format_value_multi_interface(speed1, tr_speed,
unit_label);
+ sprintf(send_buff, "widget_set I i%1d 1 %1d {%5.5s
U:%.4s D:%.4s}\n", line-1, line+1, iface_alias[line-1], speed1, speed);
+ }
+ else { /* Interface is down */
+ get_time_string(speed, iface->last_online);
+ sprintf(send_buff, "widget_set I i%1d 1 %1d {%5.5s NA
(%s)}\n", line-1, line+1, iface_alias[line-1], speed);
+ }
+ sock_send_string(sock, send_buff);
+ }
+
+} /* actualize_speed_screen() */
+
+/*************************************************************************
+ * Send commands to server to add transfer screen with all required widgets
+ *************************************************************************
+ */
+void
+initialize_transfer_screen(void)
+{
+
+ char buffer[50];
+ int iface_nmbr; /* interface number */
+
+ /* Add screen */
+ sock_send_string(sock, "screen_add NT\n");
+
+ /* Set screen name */
+ sock_send_string(sock, "screen_setN NT name {Transfer}\n");
+
+ /* Add title to screen */
+ sock_send_string(sock, "widget_add NT title title\n");
+
+ if (iface_count == 1) { /* single interface mode */
+ /* Set title */
+ sprintf(buffer, "widget_set NT title {Transfer: %s}\n",
iface_alias[0]);
+ sock_send_string(sock, buffer);
+
+ /* Add and set download, upload and total string widgets */
+ sock_send_string(sock, "widget_add NT dl string\n");
+ sock_send_string(sock, "widget_set NT dl 1 2 {DL:}\n");
+ sock_send_string(sock, "widget_add NT ul string\n");
+ sock_send_string(sock, "widget_set NT ul 1 3 {UL:}\n");
+ sock_send_string(sock, "widget_add NT total string\n");
+ sock_send_string(sock, "widget_set NT total 1 4 {Total:}\n");
+
+ }
+ else { /* multi-interfaces mode: one line per interface */
+ /* Set title (transfer screen is always in "bytes") */
+ sock_send_string(sock, "widget_set NT title {Net Transfer
(bytes)}\n");
+
+ for (iface_nmbr = 0; iface_nmbr < iface_count; iface_nmbr++) {
+ sprintf(buffer, "widget_add NT i%1d string\n",
iface_nmbr);
+ sock_send_string(sock, buffer);
+ sprintf(buffer, "widget_set NT i%1d 1 %1d {%5.5s NA
(never)]\n", iface_nmbr, iface_nmbr+1, iface_alias[iface_nmbr]);
+ sock_send_string(sock, buffer);
+ }
+ }
+
+} /* initialize_transfer_screen() */
+
+/*************************************************************************
+ * Actualize values in display, formatting transfer measures and sending
+ * proper commands to server. Traffic is shown in "bytes" unit
+ *************************************************************************
+ */
+void
+actualize_transfer_screen(struct iface_info *iface, int line)
+{
+ char transfer[20]; /* buffer to store the formated traffic string
in
+ single interface
mode, and download transfer
+ string in
multi-interfaces mode */
+ char transfer1[20]; /* buffer to store the formated upload traffic
+ string in
multi-interfaces mode*/
+ char send_buff[50]; /* buffer to send to server */
+
+ if (line == 0) { /* single interface mode */
+ if (iface->status == up) {
+ /* Format download traffic */
+ format_value(transfer, iface->rc_byte, "B");
+ /* Prepare command to send */
+ sprintf(send_buff, "widget_set NT dl 1 2 {DL:
%s}\n", transfer);
+ /* Send command to server */
+ sock_send_string(sock, send_buff);
+
+ /* Format upload traffic */
+ format_value(transfer, iface->tr_byte, "B");
+ /* Prepare command to send */
+ sprintf(send_buff, "widget_set NT ul 1 3 {UL:
%s}\n", transfer);
+ /* Send command to server */
+ sock_send_string(sock, send_buff);
+
+ /* Format total traffic */
+ format_value(transfer, iface->rc_byte +
iface->tr_byte, "B");
+ /* Prepare command to send */
+ sprintf(send_buff, "widget_set NT total 1 4 {Total:
%s}\n", transfer);
+ /* Send command to server */
+ sock_send_string(sock, send_buff);
+ }
+ else { /* Interface is down */
+ get_time_string(transfer, iface->last_online);
+ sprintf(send_buff, "widget_set NT dl 1 2 {NA
(%s)}\n", transfer);
+ sock_send_string(sock, send_buff);
+ sock_send_string(sock, "widget_set NT ul 1 3 {}\n");
+ sock_send_string(sock, "widget_set NT total 1 4
{}\n");
+ }
+ }
+ else { /* multi-interfaces mode: one line per interface */
+ if (iface->status == up) {
+ format_value_multi_interface(transfer,
iface->rc_byte, "B");
+ format_value_multi_interface(transfer1,
iface->tr_byte, "B");
+ sprintf(send_buff, "widget_set NT i%1d 1 %1d {%5.5s
U:%.4s D:%.4s}\n", line-1, line+1, iface_alias[line-1], transfer1, transfer);
+ }
+ else { /* Interface is down */
+ get_time_string(transfer, iface->last_online);
+ sprintf(send_buff, "widget_set NT i%1d 1 %1d {%5.5s
NA (%s)}\n", line-1, line+1, iface_alias[line-1], transfer);
+ }
+
+ /* Send command to server */
+ sock_send_string(sock, send_buff);
+ }
+
+} /* actualize_transfer_screen() */
+
+#ifdef NETLCDCLIENT
+/*************************************************************************
+ * Exit the program in a clean way
+ *************************************************************************
+ */
+void
+exit_program (int val)
+{
+ /* check if socket is open and close it */
+ if (sock != 0) {
+ sock_close(sock);
+ }
+
+ fprintf(stderr, "\nExiting netlcdclient....\n");
+
+ exit(0);
+
+} /* exit_program() */
+#endif //NETLCDCLIENT
diff --exclude='*.wanjet' --exclude='.??*' --exclude=config.h
--exclude='*.orig' --exclude=Makefile --exclude='*.o' --exclude=Doxyfile
--exclude='*.a' --exclude=config.log --exclude=config.status
--exclude='init-*' --exclude=lcdproc -Nuarp lcdproc/iface.h
lcdproc-cfgfile/iface.h
--- lcdproc/iface.h 1969-12-31 16:00:00.000000000 -0800
+++ lcdproc-cfgfile/iface.h 2006-03-26 12:10:20.000000000 -0800
@@ -0,0 +1,100 @@
+#ifndef IFACE_H
+#define IFACE_H
+/* netlcdclient.h - definitions and function prototipes
+ *
+ * Copyright (C) 2002 Luis Llorente Campo
+ *
+ * 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, or (at your option)
+ * any later version.
+ */
+
+
+#include <time.h>
+
+#define DEVFILE "/proc/net/dev" /* file to read statistics from */
+
+/* status definitions */
+
+#define up 1
+#define down 0
+#define MAX_PARAM_LEN 10 /* max interface_parameter or alias length */
+
+#define MAX_INTERFACES 3 /* max number of interfaces in multi-interface
mode */
+
+#ifndef ALF
+int iface_screen (int rep, int display, int *flags_ptr);
+struct iface_info iface[MAX_INTERFACES]; /* interface info */
+int iface_nmbr; /* loop variable for the interface */
+#endif //ALF
+
+/* Struct for interface values (transmision, reception, etc..) */
+
+struct iface_info
+{
+ /* interface name */
+ char name[20];
+
+ int 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;
+
+}; /* struct iface_info */
+
+/************************/
+/* Functions prototipes */
+/************************/
+
+/* show usage options */
+static void usage (int status);
+
+/* parse command line parameters */
+static int decode_switches (int argc, char **argv);
+
+/* read interface stats from /proc/net/dev */
+int get_iface_stats (char *iface_name, struct iface_info *interface);
+
+/* send initial commands to server to add the speed screen */
+void initialize_speed_screen (void);
+
+/* send initial commands to server to add the transfer screen */
+void initialize_transfer_screen(void);
+
+/* format the time in ASCII */
+void get_time_string (char *buff, time_t last_online);
+
+/* format value, scaling value and adding proper suffixes */
+void format_value (char *buff, double value, char *unit);
+
+/* format value, scaling value and adding proper suffixes (for
multi-interface
+ * mode) */
+void format_value_multi_interface(char *buff, double value, char *unit);
+
+/* actualize widgets values in speed screen */
+void actualize_speed_screen (struct iface_info *iface, unsigned int
interval, int line);
+
+/* actualize widgets values in transfer screen */
+void actualize_transfer_screen (struct iface_info *iface, int line);
+
+#ifdef NETLCDCLIENT
+/* exit the program cleanly */
+void exit_program (int val);
+#endif //NETLCDCLIENT
+#endif
diff --exclude='*.wanjet' --exclude='.??*' --exclude=config.h
--exclude='*.orig' --exclude=Makefile --exclude='*.o' --exclude=Doxyfile
--exclude='*.a' --exclude=config.log --exclude=config.status
--exclude='init-*' --exclude=lcdproc -Nuarp lcdproc/lcdproc.conf
lcdproc-cfgfile/lcdproc.conf
--- lcdproc/lcdproc.conf 1969-12-31 16:00:00.000000000 -0800
+++ lcdproc-cfgfile/lcdproc.conf 2006-03-27 09:13:44.000000000 -0800
@@ -0,0 +1,93 @@
+# LCDproc client configuration file
+
+## Options for lcdproc ##
+[lcdproc]
+# address of the LCDd server to connect to
+Server=localhost
+
+# Port of the server to connect to
+Port=13666
+
+# set reporting level
+ReportLevel=2
+
+# report to to syslog ?
+ReportToSyslog=false
+
+# stay in foreground ?
+Foreground=false
+
+# slow down initial announcement of modes (in 1/100s)
+#delay=2
+
+# screen specific configuration options
+[CPU]
+# Show screen
+Active=True
+OnTime=1
+OffTime=2
+ShowInvisible=false
+[Iface]
+# Show screen
+Active=True
+# Show stats for Interface0
+#interface0=eth0
+# Interface0 alias name to display
+#alias0=LAN
+
+# Show stats for Interface1
+#interface1=eth1
+# Interface1 alias name to display
+#alias1=WAN
+
+# Show stats for Interface2 /* more interfaces change iface.h and rebuild */
+#interface2=eth2
+# Interface2 alias name to display
+#alias2=MGMT
+
+# Units to display byte, bit or packet
+unit=bit
+
+# add screen with transferred traffic\n"
+#transfer=TRUE
+
+[Memory]
+# Show screen
+Active=True
+[Load]
+# Show screen
+Active=True
+[TimeDate]
+# Show screen
+Active=True
+[About]
+# Show screen
+Active=false
+[SMP-CPU]
+# Show screen
+Active=false
+[OldTime]
+# Show screen
+Active=false
+[BigClock]
+# Show screen
+Active=false
+[Uptime]
+# Show screen
+Active=false
+[Battery]
+# Show screen
+Active=false
+[CPUGraph]
+# Show screen
+Active=false
+[ProcSize]
+# Show screen
+Active=false
+[Disk]
+# Show screen
+Active=false
+[MiniClock]
+# Show screen
+Active=True
+# EOF
diff --exclude='*.wanjet' --exclude='.??*' --exclude=config.h
--exclude='*.orig' --exclude=Makefile --exclude='*.o' --exclude=Doxyfile
--exclude='*.a' --exclude=config.log --exclude=config.status
--exclude='init-*' --exclude=lcdproc -Nuarp lcdproc/main.c
lcdproc-cfgfile/main.c
--- lcdproc/main.c 2006-03-04 00:00:09.000000000 -0800
+++ lcdproc-cfgfile/main.c 2006-03-26 16:52:51.000000000 -0800
@@ -22,6 +22,7 @@
#include "shared/sockets.h"
#include "shared/debug.h"
#include "shared/report.h"
+#include "shared/configfile.h"

#include "batt.h"
#include "chrono.h"
@@ -31,6 +32,7 @@
#include "load.h"
#include "mem.h"
#include "machine.h"
+#include "iface.h"

// TODO: Commenting... Everything!

@@ -54,6 +56,10 @@ static struct utsname unamebuf;
static void HelpScreen(int exit_state);
static void exit_program(int val);
static void main_loop();
+static int process_command_line(int argc, char **argv);
+static int process_configfile(char *cfgfile);
+#define CHAIN(e,f) { if( e>=0 ) { e=(f); }}
+#define CHAIN_END(e,msg) { if( e<0 ) { report( RPT_CRIT,(msg)); exit(e); }}

static int islow = -1;

@@ -65,26 +71,38 @@ mode sequence[] =
{
// flags default ACTIVE will run by default
// longname which on off inv timer flags
- { "CPU", 'C', 1, 2, 0, 0xffff, ACTIVE, cpu_screen }, //
[C]PU
- { "Memory", 'M', 4, 16, 0, 0xffff, ACTIVE, mem_screen }, //
[M]emory
- { "Load", 'L', 64, 128, 1, 0xffff, ACTIVE, xload_screen }, //
[L]oad (load histogram)
- { "TimeDate", 'T', 4, 64, 0, 0xffff, ACTIVE, time_screen }, //
[T]ime/Date
- { "About", 'A', 999, 9999, 0, 0xffff, ACTIVE, credit_screen }, //
[A]bout (credits)
- { "SMP-CPU", 'P', 1, 2, 0, 0xffff, 0, cpu_smp_screen }, //
CPU_SM[P]
- { "OldTime", 'O', 4, 64, 0, 0xffff, 0, clock_screen }, //
[O]ld Timescreen
- { "BigClock", 'K', 4, 64, 0, 0xffff, 0, big_clock_screen }, //
big cloc[K]
- { "Uptime", 'U', 4, 128, 0, 0xffff, 0, uptime_screen }, //
Old [U]ptime Screen
- { "Battery", 'B', 32, 256, 1, 0xffff, 0, battery_screen }, //
[B]attery Status
- { "CPUGraph", 'G', 1, 2, 0, 0xffff, 0, cpu_graph_screen }, //
CPU histogram [G]raph
- { "ProcSize", 'S', 16, 256, 1, 0xffff, 0, mem_top_screen }, //
[S]ize of biggest processes
- { "Disk", 'D', 256, 256, 1, 0xffff, 0, disk_screen }, //
[D]isk stats
- { "MiniClock", 'N', 4, 64, 0, 0xffff, 0, mini_clock_screen }, //
Mi[n]i clock
- { NULL, 0, 0, 0, 0, 0,}, // No more.. all done.
+ { "CPU", 'C', 1, 2, 0, 0xffff, ACTIVE, cpu_screen },
// [C]PU
+ { "Iface", 'I', 1, 2, 0, 0xffff, 0, iface_screen
}, // [I]face
+ { "Memory", 'M', 4, 16, 0, 0xffff, ACTIVE, mem_screen },
// [M]emory
+ { "Load", 'L', 64, 128, 1, 0xffff, ACTIVE, xload_screen
}, // [L]oad (load histogram)
+ { "TimeDate", 'T', 4, 64, 0, 0xffff, ACTIVE, time_screen
}, // [T]ime/Date
+ { "About", 'A', 999, 9999, 0, 0xffff, ACTIVE, credit_screen
}, // [A]bout (credits)
+ { "SMP-CPU", 'P', 1, 2, 0, 0xffff, 0,
cpu_smp_screen }, // CPU_SM[P]
+ { "OldTime", 'O', 4, 64, 0, 0xffff, 0, clock_screen
}, // [O]ld Timescreen
+ { "BigClock", 'K', 4, 64, 0, 0xffff, 0,
big_clock_screen }, // big cloc[K]
+ { "Uptime", 'U', 4, 128, 0, 0xffff, 0, uptime_screen
}, // Old [U]ptime Screen
+ { "Battery", 'B', 32, 256, 1, 0xffff, 0,
battery_screen }, // [B]attery Status
+ { "CPUGraph", 'G', 1, 2, 0, 0xffff, 0,
cpu_graph_screen }, // CPU histogram [G]raph
+ { "ProcSize", 'S', 16, 256, 1, 0xffff, 0,
mem_top_screen }, // [S]ize of biggest processes
+ { "Disk", 'D', 256, 256, 1, 0xffff, 0, disk_screen
}, // [D]isk stats
+ { "MiniClock", 'N', 4, 64, 0, 0xffff, 0,
mini_clock_screen },// Mi[n]i clock
+ { NULL, 0, 0, 0, 0, 0, 0, 0},
// No more.. all done.
};



// TODO: Config file; not just command line
+/* All variables are set to 'unset' values*/
+#define UNSET_INT -1
+#define UNSET_STR "\01"
+#define DEFAULT_SERVER "127.0.0.1"
+char * progname = "lcdproc";
+char * server = NULL;
+int port = LCDPORT;
+int daemonize = UNSET_INT;
+static int report_level = UNSET_INT;
+static int report_dest = UNSET_INT;
+

const char *get_hostname()
{
@@ -135,11 +153,9 @@ void clear_modes()
int
main(int argc, char **argv)
{
- int i;
- int c;
char *server = NULL;
int port = LCDPORT;
- int daemonize = FALSE;
+ int error=0;

/* get uname information */
if (uname(&unamebuf) == -1) {
@@ -153,12 +169,77 @@ main(int argc, char **argv)
signal(SIGHUP, exit_program); // kill -HUP
signal(SIGKILL, exit_program); // kill -9 [cannot be trapped; but
...]

+ /* Set default config file, command line may overwrite */
+ strncpy(configfile, DEFAULT_CONFIGFILE, sizeof(configfile));
+
+ /* Read command line*/
+ CHAIN( error, process_command_line(argc, argv) );
+
+ /* Read config file*/
+ if (strcmp(configfile, UNSET_STR)!=0)
+ CHAIN( error, process_configfile(configfile) );
+
+ CHAIN_END( error, "Error in config file or command line\n" );
+
+ if (server == NULL)
+ server = DEFAULT_SERVER;
+
+ // Connect to the server...
+ usleep(500000); // wait for the server to start up
+ sock = sock_connect(server, port);
+ if (sock <= 0) {
+ fprintf(stderr, "Error connecting to LCD server %s on port
%d.\n"
+ "Check to see that the server is running and
operating normally.\n",
+ server, port);
+ return(EXIT_FAILURE);
+ }
+
+ sock_send_string(sock, "hello\n");
+ usleep(500000); // wait for the server to say hi.
+
+ // We grab the real values below, from the "connect" line.
+ lcd_wid = 20;
+ lcd_hgt = 4;
+ lcd_cellwid = 5;
+ lcd_cellhgt = 8;
+
+ if (daemonize) {
+ if (daemon(1,0) != 0) {
+ fprintf(stderr, "Error: daemonize failed");
+ return(EXIT_FAILURE);
+ }
+ }
+
+ // Init the status gatherers...
+ mode_init();
+
+ // And spew stuff!
+ main_loop();
+
+ // Clean up
+ exit_program(EXIT_SUCCESS);
+
+ return(0);
+}
+
+/* parses arguments given on command line */
+static int
+process_command_line(int argc, char **argv)
+{
+ int i;
+ int c;
+
/* No error output from getopt */
opterr = 0;

/* get options */
- while ((c = getopt( argc, argv, "s:p:e:dhv")) > 0) {
+ while ((c = getopt( argc, argv, "s:p:e:c:dhv")) > 0) {
switch (c) {
+ // c is for config file
+ case 'c':
+ strncpy(configfile, optarg,
sizeof(configfile));
+ configfile[sizeof(configfile)-1] = 0; /*
Terminate string */
+ break;
// s is for server
case 's':
if (server == NULL)
@@ -204,50 +285,75 @@ main(int argc, char **argv)
int found = set_mode(shortname, argv[i], 1);

if (!found) {
- fprintf(stderr, "Invalid Mode: %c, ignoring\n",
argv[i][0]);
+ fprintf(stderr, "Invalid Mode: %c\n", argv[i][0]);
return(EXIT_FAILURE);
}
}
+ return 0;
+}

- if (server == NULL)
- server = "localhost";
+/* reads and parses configuration file */
+static int
+process_configfile(char *configfile)
+{
+ int k;

- // Connect to the server...
- usleep(500000); // wait for the server to start up
- sock = sock_connect(server, port);
- if (sock <= 0) {
- fprintf(stderr, "Error connecting to LCD server %s on port
%d.\n"
- "Check to see that the server is running and
operating normally.\n",
- server, port);
- return(EXIT_FAILURE);
- }
+ debug( RPT_DEBUG, "%s()", __FUNCTION__ );

- sock_send_string(sock, "hello\n");
- usleep(500000); // wait for the server to say hi.
+ /* Read server settings*/

- // We grab the real values below, from the "connect" line.
- lcd_wid = 20;
- lcd_hgt = 4;
- lcd_cellwid = 5;
- lcd_cellhgt = 8;
+ if( strcmp( configfile, UNSET_STR ) == 0 ) {
+ configfile = DEFAULT_CONFIGFILE;
+ }
+ if( config_read_file( configfile ) != 0 ) {
+ report( RPT_CRIT, "Could not read config file: %s",
configfile );
+ return -1;
+ //report( RPT_WARNING, "Could not read config file: %s",
configfile );
+ }

- if (daemonize) {
- if (daemon(1,0) != 0) {
- fprintf(stderr, "Error: daemonize failed");
- return(EXIT_FAILURE);
+ if( server == NULL ) {
+ server = strdup( config_get_string( progname, "Server", 0,
DEFAULT_SERVER ));
+ printf("Server %s\n",server);
+ }
+ if( port == UNSET_INT ) {
+ port = config_get_int( progname, "Port", 0, LCDPORT );
+ }
+ if( report_level == UNSET_INT ) {
+ report_level = config_get_int( progname, "ReportLevel", 0,
RPT_WARNING );
+ }
+ if( report_dest == UNSET_INT ) {
+ if( config_get_bool( progname, "ReportToSyslog", 0, 0 )) {
+ report_dest = RPT_DEST_SYSLOG;
+ } else {
+ report_dest = RPT_DEST_STDERR;
}
- }
-
- // Init the status gatherers...
- mode_init();
-
- // And spew stuff!
- main_loop();
+ }
+ if( daemonize == UNSET_INT ) {
+ daemonize = ~config_get_bool( progname, "Foreground", 0, 1 );
+ }
+ if( islow < 0 ) {
+ islow = config_get_int( progname, "delay", 0, -1 );
+ }

- // Clean up
- exit_program(EXIT_SUCCESS);
+ /*
+ * check for config file variables to override all the sequence
defaults
+ */
+ for ( k = 0; sequence[k].which ; k++) {
+ if (sequence[k].longname) {
+ sequence[k].on_time = config_get_int(
sequence[k].longname, "OnTime", 0, sequence[k].on_time );
+ sequence[k].off_time = config_get_int(
sequence[k].longname, "OffTime", 0, sequence[k].off_time );
+ sequence[k].show_invisible = config_get_bool(
sequence[k].longname, "ShowInvisible", 0, sequence[k].show_invisible );
+ if ( config_get_bool( sequence[k].longname, "Active",
0, sequence[k].flags & ACTIVE ) ) {
+ sequence[k].flags |= ACTIVE;
+ printf("Active %s\n", sequence[k].longname);
+ } else {
+ sequence[k].flags &= (~ACTIVE);
+ printf("Inactive %s\n", sequence[k].longname);
+ }
+ }
+ }

- return(0);
+ return 0;
}

void
@@ -260,6 +366,7 @@ HelpScreen (int exit_state)
" -p <port> connect to LCDd daemon using
<port>\n"
" -d daemonize\n"
" -e <delay> slow down initial announcement
of modes (in 1/100s)\n"
+ " -c <config> Use a configuration file
other than %s\n"
" -h show this help screen\n"
" -v display program version\n"
" and <modes> are\n"
@@ -273,14 +380,14 @@ HelpScreen (int exit_state)
" B Battery battery status\n"
" T TimeDate time & date information\n"
" O OldTime old time screen\n"
- " U Uptime old uptime screen\n"
+ " U Uptime uptime screen\n"
" K BigClock big clock\n"
" N MiniClock minimal clock\n"
" A About credits page\n"
"\n"
"Example:\n"
" lcdproc -s my.lcdproc.server.com p 13666 C M X\n"
- );
+ , DEFAULT_CONFIGFILE);

exit(exit_state);
}
@@ -298,6 +405,7 @@ exit_program(int val)
exit(val);
}

+#define LCDPROC_MENUS
#ifdef LCDPROC_MENUS
int
menus_init ()
diff --exclude='*.wanjet' --exclude='.??*' --exclude=config.h
--exclude='*.orig' --exclude=Makefile --exclude='*.o' --exclude=Doxyfile
--exclude='*.a' --exclude=config.log --exclude=config.status
--exclude='init-*' --exclude=lcdproc -Nuarp lcdproc/main.h
lcdproc-cfgfile/main.h
--- lcdproc/main.h 2006-02-28 00:00:09.000000000 -0800
+++ lcdproc-cfgfile/main.h 2006-03-26 13:01:36.000000000 -0800
@@ -20,6 +20,9 @@
#define LCDP_AC_BACKUP 0x02
#define LCDP_AC_UNKNOWN 0xFF

+#define DEFAULT_CONFIGFILE "/usr/local/etc/lcdproc.conf"
+char configfile[256]; /* a lot of space in the executable. */
+
extern int Quit;
extern int sock;
extern char *version;



Archive powered by MHonArc 2.6.18.

Top of page