From 4f5395938002f624502fd7afba7dfa3b3ee8433d Mon Sep 17 00:00:00 2001 From: nicowallmeier <> Date: Sun, 12 Oct 2003 06:08:28 +0000 Subject: [lcd4linux @ 2003-10-12 06:08:28 by nicowallmeier] imond/telmond support --- Makefile.am | 3 +- imon.c | 442 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ imon.h | 53 ++++++++ parser.c | 20 ++- parser.h | 12 +- processor.c | 109 ++++++++++++++- 6 files changed, 633 insertions(+), 6 deletions(-) create mode 100755 imon.c create mode 100755 imon.h diff --git a/Makefile.am b/Makefile.am index e5b17f2..331b33f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -29,7 +29,8 @@ filter.c filter.h \ widget.c widget.h \ exec.c exec.h \ mail2.c \ -socket.c socket.h +socket.c socket.h \ +imon.c imon.h liblcd4linux_la_DEPENDENCIES = @DRIVERS@ liblcd4linux_la_LDFLAGS = -version-info 9:11:9 diff --git a/imon.c b/imon.c new file mode 100755 index 0000000..4f70ac0 --- /dev/null +++ b/imon.c @@ -0,0 +1,442 @@ +/* $Id: imon.c,v 1.1 2003/10/12 06:08:28 nicowallmeier Exp $ + * + * imond/telmond data processing + * + * This file is part of LCD4Linux. + * + * LCD4Linux 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. + * + * LCD4Linux 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * $Log: imon.c,v $ + * Revision 1.1 2003/10/12 06:08:28 nicowallmeier + * imond/telmond support + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include /* decl of inet_addr() */ +#include + +#include "cfg.h" +#include "debug.h" +#include "parser.h" +#include "imon.h" + +#define TRUE 1 +#define FALSE 0 + +static int fd; + + /*---------------------------------------------------------------------------- + * service_connect (host_name, port) - connect to tcp-service + *---------------------------------------------------------------------------- + */ +static int +service_connect (char * host_name, int port) +{ + struct sockaddr_in addr; + struct hostent * host_p; + int fd; + int opt = 1; + + (void) memset ((char *) &addr, 0, sizeof (addr)); + + if ((addr.sin_addr.s_addr = inet_addr ((char *) host_name)) == INADDR_NONE) + { + host_p = gethostbyname (host_name); + + if (! host_p) + { + error ("%s: host not found\n", host_name); + return (-1); + } + + (void) memcpy ((char *) (&addr.sin_addr), host_p->h_addr, + host_p->h_length); + } + + addr.sin_family = AF_INET; + addr.sin_port = htons ((unsigned short) port); + + if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) + { /* open socket */ + perror ("socket"); + return (-1); + } + + (void) setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, + (char *) &opt, sizeof (opt)); + + if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) != 0) + { + (void) close (fd); + perror (host_name); + return (-1); + } + + return (fd); +} /* service_connect (char * host_name, int port) */ + + +/*---------------------------------------------------------------------------- + * send_command (int fd, char * str) - send command to imond + *---------------------------------------------------------------------------- + */ +static void +send_command (int fd, char * str) +{ + char buf[256]; + int len = strlen (str); + + sprintf (buf, "%s\r\n", str); + write (fd, buf, len + 2); + + return; +} /* send_command (int fd, char * str) */ + + +/*---------------------------------------------------------------------------- + * get_answer (int fd) - get answer from imond + *---------------------------------------------------------------------------- + */ +static char * +get_answer (int fd) +{ + static char buf[8192]; + int len; + + len = read (fd, buf, 8192); + + if (len <= 0) + { + return ((char *) NULL); + } + + while (len > 1 && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) + { + buf[len - 1] = '\0'; + len--; + } + + if (! strncmp (buf, "OK ", 3)) /* OK xxxx */ + { + return (buf + 3); + } + else if (len > 2 && ! strcmp (buf + len - 2, "OK")) + { + *(buf + len - 2) = '\0'; + return (buf); + } + else if (len == 2 && ! strcmp (buf + len - 2, "OK")) + { + return (buf); + } + + return ((char *) NULL); /* ERR xxxx */ +} /* get_answer (int fd) */ + + +/*---------------------------------------------------------------------------- + * get_numerical_value (char * cmd) - send cmd, get numval + *---------------------------------------------------------------------------- + */ +static int +get_numerical_value (char * cmd) +{ + char * answer; + int rtc; + + send_command (fd, cmd); + + answer = get_answer (fd); + + if (answer) + { + rtc = atoi (answer); + } + else + { + rtc = -1; + } + return (rtc); +} /* get_numerical_value (char * cmd, int arg) */ + + +/*---------------------------------------------------------------------------- + * get_value (char * cmd) - send command, get value + *---------------------------------------------------------------------------- + */ +static char * +get_value (char * cmd) +{ + char * answer; + + send_command (fd, cmd); + + answer = get_answer (fd); + + if (answer) + { + return (answer); + } + + return (""); +} /* get_value (char * cmd, int arg) */ + + +int init(){ + char *s, *host; + int port; + int connect; + + host=cfg_get ("Imon_Host","127.0.0.1"); + if (*host=='\0') { + error ("Imon: no 'Imon_Host' entry in %s", cfg_source()); + return -1; + } + + if (cfg_number("Imon_Port",5000,1,65536,&port)<0){ + return -1; + } + + connect=service_connect(host,port); + + s=cfg_get ("Imon_Pass",NULL); + if ((s!=NULL) && (*s!='\0')) { // Passwort senden + char buf[40]; + sprintf(buf,"pass %s",s); + send_command(connect,buf); + s=get_answer(connect); + } + + return connect; +} + +int ImonCh(int index, struct imonchannel *ch, int token_usage[]) { + static int err[CHANNELS+1]; + char *s; + char buf[40]; + int result=0; + + if (err[index]) return -1; + if ((fd==0) && ((fd=init())<0)) return -1; + + if ((*ch).max_in == 0){ // not initializied + sprintf(buf, "Imon_%d_Dev", index); + s=cfg_get(buf,NULL); + if (s==NULL) { + error ("Imon: no 'Imon_%i_Dev' entry in %s", index, cfg_source()); + err[index]=1; + return -1; + } + strcpy((*ch).dev,s); + + sprintf(buf, "Imon_%d_MaxIn", index); + cfg_number(buf,768,1,65536,&(*ch).max_in); + + sprintf(buf, "Imon_%d_MaxOut", index); + cfg_number(buf,128,1,65536,&(*ch).max_out); + } + + sprintf(buf, "status %s", (*ch).dev); + s=get_value(buf); + strcpy((*ch).status,s); + + if ((1<= 0){ + int l = read (telmond_fd, telbuf, 127); + if ((l > 0) && (strcmp(telbuf,oldanswer))){ + char date[11]; + sscanf(telbuf,"%s %s %s %s",date,(*t).time,(*t).number,(*t).msn); + date[4]='\0'; + date[7]='\0'; + sprintf((*t).date,"%s.%s.%s",date+8,date+5,date); + phonebook((*t).number); + phonebook((*t).msn); + } + close (telmond_fd); + strcpy(oldanswer,telbuf); + } + } + return 0; +} + + diff --git a/imon.h b/imon.h new file mode 100755 index 0000000..1252e9d --- /dev/null +++ b/imon.h @@ -0,0 +1,53 @@ +/* $Id: imon.h,v 1.1 2003/10/12 06:08:28 nicowallmeier Exp $ + * + * imond/telmond data processing + * + * This file is part of LCD4Linux. + * + * LCD4Linux 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. + * + * LCD4Linux 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * $Log: imon.h,v $ + * Revision 1.1 2003/10/12 06:08:28 nicowallmeier + * imond/telmond support + * + */ + +#ifndef _IMON_H_ +#define _IMON_H_ + +#define CHANNELS 9 + +struct imonchannel { + int rate_in, rate_out, max_in, max_out; + char status[8], phone[17], ip[16], otime[11], charge[10], dev[6]; +}; + +struct imon { + int cpu; + char date[11], time[9]; +}; + +struct telmon { + char number[256], msn[256], time[9], date[11]; +}; + +int Imon(struct imon *i, int cpu, int datetime); +int ImonCh(int index, struct imonchannel *ch, int token_usage[]); +char* ImonVer(); + +int Telmon(struct telmon *t); + +#endif diff --git a/parser.c b/parser.c index 44252ad..84e5252 100644 --- a/parser.c +++ b/parser.c @@ -1,4 +1,4 @@ -/* $Id: parser.c,v 1.21 2003/10/05 17:58:50 reinelt Exp $ +/* $Id: parser.c,v 1.22 2003/10/12 06:08:28 nicowallmeier Exp $ * * row definition parser * @@ -22,6 +22,9 @@ * * * $Log: parser.c,v $ + * Revision 1.22 2003/10/12 06:08:28 nicowallmeier + * imond/telmond support + * * Revision 1.21 2003/10/05 17:58:50 reinelt * libtool junk; copyright messages cleaned up * @@ -192,6 +195,21 @@ static SYMTAB Symtab[] = {{ "%", T_PERCENT, C_GENERIC, 0 }, { "u*", T_MAIL_UNSEEN,C_MAIL, 0 }, { "s*", T_SENSOR, C_SENSOR, 1 }, { "x*", T_EXEC, C_EXEC, 1 }, + { "jc", T_IMON_CPU, C_IMON, 1 }, + { "jv", T_IMON_VER, C_IMON, 0 }, + { "jd", T_IMON_DATE, C_IMON, 0 }, + { "jm", T_IMON_TIME, C_IMON, 0 }, + { "ji*",T_IMON_RIN, C_IMON, 1 }, + { "jo*",T_IMON_ROUT, C_IMON, 1 }, + { "js*",T_IMON_STATUS,C_IMON, 0 }, + { "jn*",T_IMON_PHONE, C_IMON, 0 }, + { "jp*",T_IMON_IP, C_IMON, 0 }, + { "jt*",T_IMON_OTIME, C_IMON, 0 }, + { "jk*",T_IMON_CHARGE,C_IMON, 0 }, + { "kn", T_TELMON_NUMBER, C_TELMON,0 }, + { "km", T_TELMON_MSN, C_TELMON, 0 }, + { "kd", T_TELMON_DATE, C_TELMON, 0 }, + { "kt", T_TELMON_TIME, C_TELMON, 0 }, { "", -1, 0 }}; static int bar_type (char tag) diff --git a/parser.h b/parser.h index caeb8dd..dd607d6 100644 --- a/parser.h +++ b/parser.h @@ -1,4 +1,4 @@ -/* $Id: parser.h,v 1.14 2003/10/05 17:58:50 reinelt Exp $ +/* $Id: parser.h,v 1.15 2003/10/12 06:08:28 nicowallmeier Exp $ * * row definition parser * @@ -22,6 +22,9 @@ * * * $Log: parser.h,v $ + * Revision 1.15 2003/10/12 06:08:28 nicowallmeier + * imond/telmond support + * * Revision 1.14 2003/10/05 17:58:50 reinelt * libtool junk; copyright messages cleaned up * @@ -101,12 +104,15 @@ typedef enum { T_EXTENDED, T_MAIL, T_MAIL_UNSEEN, T_SENSOR, - T_EXEC + T_EXEC, + T_IMON_CPU, T_IMON_VER, T_IMON_DATE, T_IMON_TIME, T_IMON_RIN, T_IMON_ROUT, + T_IMON_STATUS, T_IMON_PHONE, T_IMON_IP, T_IMON_OTIME, T_IMON_CHARGE, + T_TELMON_NUMBER, T_TELMON_MSN, T_TELMON_TIME, T_TELMON_DATE } TOKEN; typedef enum { C_GENERIC, C_MEM, C_LOAD, C_CPU, C_DISK, C_ETH, C_PPP, C_ISDN, C_SETI, C_BATT, C_DVB, - C_MAIL, C_SENSOR, C_EXEC + C_MAIL, C_SENSOR, C_EXEC, C_IMON, C_TELMON } CLASS; char *parse_row (char *string, int supported_bars, int usage[]); diff --git a/processor.c b/processor.c index 788d900..9ac9786 100644 --- a/processor.c +++ b/processor.c @@ -1,4 +1,4 @@ -/* $Id: processor.c,v 1.48 2003/10/12 04:46:19 reinelt Exp $ +/* $Id: processor.c,v 1.49 2003/10/12 06:08:28 nicowallmeier Exp $ * * main data processing * @@ -22,6 +22,9 @@ * * * $Log: processor.c,v $ + * Revision 1.49 2003/10/12 06:08:28 nicowallmeier + * imond/telmond support + * * Revision 1.48 2003/10/12 04:46:19 reinelt * * @@ -236,6 +239,7 @@ #include "dvb.h" #include "seti.h" #include "exec.h" +#include "imon.h" #define ROWS 64 #define ICONS 8 @@ -259,6 +263,9 @@ static struct { double perc, cput; } seti; static struct { int num, unseen;} mail[MAILBOXES+1]; static struct { double val, min, max; } sensor[SENSORS+1]; static struct { double strength, snr; } dvb; +static struct imonchannel imonch[CHANNELS+1]; +static struct imon imon; +static struct telmon telmon; extern int tick, tack; static int tick_text, tick_bar, tick_icon, tick_gpo; @@ -368,6 +375,14 @@ static double query (int token) case T_EXEC: return exec[(token>>8)-'0'].val; + case T_IMON_CPU: + return imon.cpu; + + case T_IMON_RIN: + return ((double)imonch[(token>>8)-'0'].rate_in)/1024; + + case T_IMON_ROUT: + return ((double)imonch[(token>>8)-'0'].rate_out)/1024; } return 0.0; } @@ -438,7 +453,19 @@ static double query_bar (int token) case T_SENSOR: i=(token>>8)-'0'; return (value-sensor[i].min)/(sensor[i].max-sensor[i].min); + + case T_IMON_CPU: + return value/100; + + case T_IMON_RIN: + i=(token>>8)-'0'; + return value/(imonch[i].max_in/8); + + case T_IMON_ROUT: + i=(token>>8)-'0'; + return value/(imonch[i].max_out/8); } + return value; } @@ -594,6 +621,63 @@ static void print_token (int token, char **p, char *start) *p+=sprintf (*p, "%.*s",cols-(int)(*p-start), exec[i].s); break; + case T_IMON_VER: + *p+=sprintf (*p, "%s", ImonVer()); + break; + + case T_IMON_DATE: + *p+=sprintf (*p, "%s ", imon.date); + break; + + case T_IMON_TIME: + *p+=sprintf (*p, "%s ", imon.time); + break; + + case T_IMON_CPU: + *p+=sprintf (*p, "%3.0f", query(token)); + break; + + case T_IMON_RIN: + case T_IMON_ROUT: + *p+=sprintf(*p, "%4.1f", query(token)); + break; + + case T_IMON_STATUS: + *p+=sprintf(*p, "%s", imonch[(token>>8)-'0'].status); + break; + + case T_IMON_PHONE: + *p+=sprintf(*p, "%s", imonch[(token>>8)-'0'].phone); + break; + + case T_IMON_IP: + *p+=sprintf(*p, "%s", imonch[(token>>8)-'0'].ip); + break; + + case T_IMON_OTIME: + *p+=sprintf(*p, "%s", imonch[(token>>8)-'0'].otime); + break; + + case T_IMON_CHARGE: + *p+=sprintf(*p, "%s", imonch[(token>>8)-'0'].charge); + break; + + case T_TELMON_NUMBER: + *p+=sprintf(*p, "%s", telmon.number); + break; + + case T_TELMON_MSN: + *p+=sprintf(*p, "%s", telmon.msn); + break; + + case T_TELMON_DATE: + *p+=sprintf(*p, "%s", telmon.date); + break; + + case T_TELMON_TIME: + *p+=sprintf(*p, "%s", telmon.time); + break; + default: *p+=sprintf (*p, "%5.0f", query(token)); } @@ -658,6 +742,29 @@ static void collect_data (void) DVB (&dvb.strength, &dvb.snr); } + if (token_usage[C_IMON]) { + if (token_usage[T_IMON_CPU] || + token_usage[T_IMON_DATE] || + token_usage[T_IMON_TIME]) { + Imon (&imon, T_IMON_CPU, T_IMON_DATE+T_IMON_TIME); + } + for (i=0; i<=CHANNELS; i++) { + if (((1<