diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | README | 9 | ||||
-rw-r--r-- | src/ftphelper.c | 3 | ||||
-rwxr-xr-x | src/ftpupload | 5 | ||||
-rw-r--r-- | src/modbuslog.c | 79 |
5 files changed, 70 insertions, 30 deletions
@@ -9,6 +9,8 @@ MODBUSLOGLOGDIR=/var/modbuslog FTPHELPERCFLAGS= FTPHELPERLIBS=-lconfig FTPHELPERDIR=/usr/bin +PHPSDIR=php +WWWDIR=/var/www all: modbuslog ftphelper @@ -25,7 +27,9 @@ 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 $(INSTALL) $(SDIR)/ftphelper $(FTPHELPERDIR)/ftphelper + $(INSTALL) $(PHPSDIR)/readfile.php $(WWWDIR)/readfile.php @@ -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: -------------------- @@ -42,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/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 <stdio.h> #include <sys/ioctl.h> -#include <sys/types.h> +#include <sys/types.h> #include <sys/socket.h> #include <net/if.h> #include <string.h> 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 diff --git a/src/modbuslog.c b/src/modbuslog.c index 1e849bb..198015f 100644 --- a/src/modbuslog.c +++ b/src/modbuslog.c @@ -38,10 +38,7 @@ #include <net/if.h> #include <string.h> -#define SLAVE_ID 1 -#define START_ADDRESS 0 -#define NUMBER_REGISTERS 14 - +#include <syslog.h> // handle SIGALRM by resetting it void minute_check(int signum) { @@ -73,18 +70,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; @@ -103,8 +104,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 @@ -114,16 +117,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]; @@ -194,7 +196,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, @@ -204,15 +206,13 @@ int main(int argc, char *argv[]) { modbus_set_debug(ctx, DEBUG); if (ctx == NULL) { - fprintf(stderr, "Unable to create libmodbus object.\n"); - //return -1; + 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; @@ -237,10 +237,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); @@ -267,6 +268,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, @@ -276,6 +279,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; @@ -307,12 +312,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); @@ -329,6 +348,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); @@ -336,7 +364,6 @@ int main(int argc, char *argv[]) { //return 0; } - //printf("%d ", slaveid); } //printf("%d\n", unixtime_min); |