From 78766bfe18a915b20b4a2de490ef4e9a1b805099 Mon Sep 17 00:00:00 2001 From: Jonathan McCrohan Date: Mon, 20 Feb 2012 00:50:14 +0000 Subject: Add PHP to make install --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 7b05f25..a619764 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,8 @@ MODBUSLOGLOGDIR=/var/modbuslog FTPHELPERCFLAGS= FTPHELPERLIBS=-lconfig FTPHELPERDIR=/usr/bin +PHPSDIR=php +WWWDIR=/var/www all: modbuslog ftphelper @@ -29,3 +31,4 @@ install: all test -f $(MODBUSLOGLOGDIR)/interval/interval.txt || $(INSTALL) -m 644 $(SDIR)/interval.txt $(MODBUSLOGLOGDIR)/interval/interval.txt $(INSTALL) $(SDIR)/ftpupload $(FTPHELPERDIR)/ftpupload $(INSTALL) $(SDIR)/ftphelper $(FTPHELPERDIR)/ftphelper + $(INSTALL) $(PHPSDIR)/readfile.php $(WWWDIR)/readfile.php -- cgit v1.2.3 From a5b894ac9c583066da6fbd144b85d24f0ed58568 Mon Sep 17 00:00:00 2001 From: Jonathan McCrohan Date: Mon, 20 Feb 2012 00:50:31 +0000 Subject: Update documentation --- README | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README b/README index 5d855c2..e8618e2 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -modbuslog - 2012/01/10 +modbuslog - 2012/02/20 -------------------- modbuslog is a program designed to read from a modbus device at predefined @@ -7,7 +7,7 @@ intervals and store the output to file. Compilation: -------------------- -modbuslog requires the libconfig (>1.4.8) and libmodbus (>3.0.1) libraries, +modbuslog requires the libconfig (>=1.4.8) and libmodbus (>=3.0.1) libraries, and will not function without these libraries installed. Neither of these libraries are currently available (or if they are, are not of sufficient version) in the current Debian Stable release codenamed "Squeeze". This @@ -28,10 +28,12 @@ libconfig: An older, incompatible version of libconfig currently exists modbuslog uses a Makefile to ease compilation, and as such, can be compiled by simple navigating to the directory and running "make". This will recompile -any packages that need to be compiled. +any files that need to be compiled. These files can then be moved to the required directories on the target system. +Alternatively, if the code is being compiled on the target system itself, +"make install" can be used to both compile and install the software. Required files: -------------------- -- cgit v1.2.3 From a6a6f9c5a4ddc0295bb12fd93ed0ba23ed696122 Mon Sep 17 00:00:00 2001 From: Jonathan McCrohan Date: Mon, 20 Feb 2012 11:25:26 +0000 Subject: Prune archive after 3 months --- Makefile | 1 + README | 1 + src/ftpupload | 5 +++++ 3 files changed, 7 insertions(+) diff --git a/Makefile b/Makefile index a619764..7659c27 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,7 @@ install: all then \ mkdir $(MODBUSLOGLOGDIR); \ mkdir $(MODBUSLOGLOGDIR)/interval; \ + mkdir $(MODBUSLOGLOGDIR)/archive; \ fi test -f $(MODBUSLOGLOGDIR)/interval/interval.txt || $(INSTALL) -m 644 $(SDIR)/interval.txt $(MODBUSLOGLOGDIR)/interval/interval.txt $(INSTALL) $(SDIR)/ftpupload $(FTPHELPERDIR)/ftpupload diff --git a/README b/README index e8618e2..151a255 100644 --- a/README +++ b/README @@ -44,6 +44,7 @@ Required files: /etc/modbuslog.cfg configuration file /var/modbuslog/ output directory where logs are stored /var/modbuslog/interval/interval.txt textfile storing unique interval +/var/modbuslog/archive/ directory containing archived log files /var/www/ webserver directory for php scripts Configuration: diff --git a/src/ftpupload b/src/ftpupload index 2483764..7d46f46 100755 --- a/src/ftpupload +++ b/src/ftpupload @@ -20,4 +20,9 @@ mput $FILE bye EOF +# move file to archive mv $FILE /var/modbuslog/archive/ + +# remove files after 3 months +CLEANUP="/var/modbus/archive/"`/usr/bin/ftphelper --mac`"_"`date +%Y_%m --date='4 months ago'`"*.log" +rm $CLEANUP -- cgit v1.2.3 From 6a055d58464524d68880eecd9de49e68660c013d Mon Sep 17 00:00:00 2001 From: Jonathan McCrohan Date: Sun, 4 Mar 2012 22:33:57 +0000 Subject: Remove unused #define statements --- src/modbuslog.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/modbuslog.c b/src/modbuslog.c index 1e849bb..ece8aca 100644 --- a/src/modbuslog.c +++ b/src/modbuslog.c @@ -38,9 +38,6 @@ #include #include -#define SLAVE_ID 1 -#define START_ADDRESS 0 -#define NUMBER_REGISTERS 14 // handle SIGALRM by resetting it -- cgit v1.2.3 From e7b8d92cc55b0c931df6851064e6d36bfa4978f5 Mon Sep 17 00:00:00 2001 From: Jonathan McCrohan Date: Mon, 5 Mar 2012 01:12:18 +0000 Subject: Add syslog logging --- src/modbuslog.c | 81 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/src/modbuslog.c b/src/modbuslog.c index ece8aca..fd1f00d 100644 --- a/src/modbuslog.c +++ b/src/modbuslog.c @@ -38,7 +38,7 @@ #include #include - +#include // handle SIGALRM by resetting it void minute_check(int signum) { @@ -46,7 +46,8 @@ void minute_check(int signum) { } // get mac address of primary interface eth0 -char *mac_address() { +char * +mac_address() { int s; struct ifreq ifr; @@ -70,18 +71,22 @@ char *mac_address() { int main(int argc, char *argv[]) { - const char *configfile = "/etc/modbuslog.cfg"; - int DEBUG = 0; + int SYSLOG_CONSOLE_OUTPUT = 0; int k; // check the argv array for strings matching -d for (k = 1; k < argc; k++) { if (strcmp(argv[k], "-d") == 0) { DEBUG = 1; + SYSLOG_CONSOLE_OUTPUT = LOG_PERROR; } } + openlog("modbuslog", SYSLOG_CONSOLE_OUTPUT | LOG_PID | LOG_CONS, LOG_USER); + syslog(LOG_INFO, "modbuslog starting"); + + const char *configfile = "/etc/modbuslog.cfg"; config_t cfg; //config_setting_t *setting; @@ -100,8 +105,10 @@ int main(int argc, char *argv[]) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); - fprintf(stderr, "Unable to find configfile.\n"); + syslog(LOG_ERR, "Unable to find configfile"); return -1; + } else { + syslog(LOG_INFO, "configfile found successfully"); } // die if core config file options aren't there @@ -111,16 +118,15 @@ int main(int argc, char *argv[]) { && config_lookup_string(&cfg, "modbus.parity", &modbus_parity) && config_lookup_int(&cfg, "modbus.stop_bits", &modbus_stop_bits) && config_lookup_int(&cfg, "modbus.retry", &modbus_retry))) { - fprintf(stderr, - "Incomplete modbus configuration. Check configuration file.\n"); + syslog(LOG_ERR, + "Incomplete modbus configuration. Check configuration file"); + closelog(); return -1; } - if (DEBUG) { - printf("%s %s %d %c %d %d\n", mac_address(), modbus_device_address, - modbus_baud_rate, modbus_parity[0], modbus_data_bits, - modbus_stop_bits); - } + syslog(LOG_INFO, "%s %s %d %c %d %d", mac_address(), modbus_device_address, + modbus_baud_rate, modbus_parity[0], modbus_data_bits, + modbus_stop_bits); modbus_t *ctx; uint16_t tab_reg[4]; @@ -191,7 +197,7 @@ int main(int argc, char *argv[]) { // if we match the required time for the reading if (unixtime_min % intervalduration == 0) { //printf("specified minute. %d %d\n", unixtime_min, - // intervalduration); + // intervalduration); // attempt to create new modbus connection ctx = modbus_new_rtu(modbus_device_address, modbus_baud_rate, @@ -200,16 +206,15 @@ int main(int argc, char *argv[]) { modbus_set_slave(ctx, slaveid); modbus_set_debug(ctx, DEBUG); - if (ctx == NULL) { - fprintf(stderr, "Unable to create libmodbus object.\n"); - //return -1; + if (ctx == NULL) + { + syslog(LOG_ERR, "Unable to create libmodbus object"); } if (modbus_connect(ctx) == -1) { - fprintf(stderr, "Connection failed: %s\n", + syslog(LOG_ERR, "libmodbus: Connection failed: %s", modbus_strerror(errno)); modbus_free(ctx); - //return -1; } int retry = 0; @@ -234,10 +239,11 @@ int main(int argc, char *argv[]) { break; } if (rc == -1) { - fprintf(stderr, "ERROR: %s\n", modbus_strerror(errno)); + syslog(LOG_ERR, "libmodbus error: %s", + modbus_strerror(errno)); } retry++; - } while ((rc != -1) && (retry < (modbus_retry+1))); + } while ((rc != -1) && (retry < (modbus_retry + 1))); //MODBUS_GET_HIGH_BYTE(data); //MODBUS_GET_LOW_BYTE(data); @@ -264,6 +270,8 @@ int main(int argc, char *argv[]) { fclose(fp); } else { // file doesn't exist. create it. + syslog(LOG_NOTICE, "logfile does not exist"); + syslog(LOG_INFO, "creating file: %s", filename); FILE *fp = fopen(filename, "w"); fprintf( fp, @@ -273,6 +281,8 @@ int main(int argc, char *argv[]) { FILE *filehandle = fopen(filename, "a+"); + syslog(LOG_DEBUG, "opening file for append: %s", filename); + int16_t registervalue = 0; int p; @@ -304,12 +314,26 @@ int main(int argc, char *argv[]) { struct tm lc = *localtime(&unixtime_min); int intervalid; - FILE *intervalfile = fopen("/var/modbuslog/interval/interval.txt", "r+"); + FILE *intervalfile = fopen( + "/var/modbuslog/interval/interval.txt", "r+"); + + if (intervalfile) { + fclose(intervalfile); + } else { + // file doesn't exist. create it. + syslog(LOG_NOTICE, "interval file does not exist"); + syslog(LOG_INFO, "attempting to create file: %s", + intervalfile); + FILE *intervalfile = fopen(filename, "w"); + fprintf(intervalfile, "0\n"); + fclose(intervalfile); + } + fscanf(intervalfile, "%d", &intervalid); //handle 32bit signed overflow - if(intervalid==2147483647){ - intervalid=0; - }else{ + if (intervalid == 2147483647) { + intervalid = 0; + } else { intervalid++; } rewind(intervalfile); @@ -326,6 +350,15 @@ int main(int argc, char *argv[]) { registervalue); fclose(filehandle); + syslog( + LOG_DEBUG, + "%i|%04i%02i%02i|%02i%02i%02i|%04i%02i%02i|%02i%02i%02i|%i|%i|%i\n", + intervalid, utc.tm_year + 1900, utc.tm_mon + 1, + utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec, + lc.tm_year + 1900, lc.tm_mon + 1, lc.tm_mday, + lc.tm_hour, lc.tm_min, lc.tm_sec, slaveid, startaddress, + registervalue); + modbus_close(ctx); modbus_free(ctx); -- cgit v1.2.3 From 947f7d6b88ef8e891f67d7fac44961cd20fde406 Mon Sep 17 00:00:00 2001 From: Jonathan McCrohan Date: Mon, 5 Mar 2012 01:17:37 +0000 Subject: Refactor code No source chances, reindent only. --- src/ftphelper.c | 3 ++- src/modbuslog.c | 7 ++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/ftphelper.c b/src/ftphelper.c index ffa289c..fae3771 100644 --- a/src/ftphelper.c +++ b/src/ftphelper.c @@ -18,9 +18,10 @@ */ // gcc ftphelper.c -o ftphelper `pkg-config --libs --cflags libconfig` + #include #include -#include +#include #include #include #include diff --git a/src/modbuslog.c b/src/modbuslog.c index fd1f00d..198015f 100644 --- a/src/modbuslog.c +++ b/src/modbuslog.c @@ -46,8 +46,7 @@ void minute_check(int signum) { } // get mac address of primary interface eth0 -char * -mac_address() { +char *mac_address() { int s; struct ifreq ifr; @@ -206,8 +205,7 @@ int main(int argc, char *argv[]) { modbus_set_slave(ctx, slaveid); modbus_set_debug(ctx, DEBUG); - if (ctx == NULL) - { + if (ctx == NULL) { syslog(LOG_ERR, "Unable to create libmodbus object"); } @@ -366,7 +364,6 @@ int main(int argc, char *argv[]) { //return 0; } - //printf("%d ", slaveid); } //printf("%d\n", unixtime_min); -- cgit v1.2.3