aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ftphelper.c3
-rwxr-xr-xsrc/ftpupload5
-rw-r--r--src/modbuslog.c79
3 files changed, 60 insertions, 27 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 <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);