From 3b0cd019a250fe54c198a54ac1e2d13be8eaf8ed Mon Sep 17 00:00:00 2001 From: losinshi Date: Thu, 31 Aug 2006 17:04:18 +0000 Subject: [PATCH] checksensor finished git-svn-id: file:///home/jan/tmp/wetterstation/trunk@59 dd492736-c11a-0410-ad51-8c26713eaf7f --- cronjob/checksensor/Makefile | 76 +++++++++++++++++++++++++++++++ cronjob/checksensor/checksensor.c | 29 ++++++++++-- cronjob/checksensor/checksensor.h | 35 ++++++++------ cronjob/checksensor/config.c | 5 ++ 4 files changed, 128 insertions(+), 17 deletions(-) create mode 100644 cronjob/checksensor/Makefile diff --git a/cronjob/checksensor/Makefile b/cronjob/checksensor/Makefile new file mode 100644 index 0000000..49ff604 --- /dev/null +++ b/cronjob/checksensor/Makefile @@ -0,0 +1,76 @@ +CC = gcc +LD = gcc +RM = rm +CFLAGS = -c +LDFLAS = -o +INCL = -I$$(pg_config --includedir) + +BIN_NAME = checksensor +OBJS = checksensor.o config.o mailer.o + +CONF_NAME = checksensor.conf + +INSTDIR = /usr/bin/ +CONFDIR = /etc/ +INSTGRP = losinski +INSTUSR = losinski + +DESTDIR = /home/losinski + +# Alles bauen +all: $(BIN_NAME) +# $(MAKE) cleanup + + +# Binary Linken +$(BIN_NAME): $(OBJS) + @ echo Linke: $(LD) $(DEBUG) $(NOLOG) -L$$(pg_config --libdir)/pgsql -lesmtp -lssl -lpq $(LDFLAS) $(BIN_NAME) $(OBJS) + @ $(LD) $(DEBUG) $(NOLOG) -L$$(pg_config --libdir)/pgsql -lesmtp -lssl -lpq $(LDFLAS) $(BIN_NAME) $(OBJS) + @ echo Binary $(BIN_NAME) ist fertig! + +# Abhängigkeiten +checksensor.o: checksensor.c definitions.h config.h checksensor.h mailer.h +config.o: config.c config.h definitions.h checksensor.h mailer.h +mailer.o: mailer.c mailer.h + +# Compillieren +$(OBJS): + @ echo "Kompilliere: "$(CC) $(DEBUG) $(NOLOG) $(INCL) $(CFLAGS) $*.c + @ $(CC) $(DEBUG) $(NOLOG) $(INCL) $(CFLAGS) $*.c + +# Programm mit debug-ausgabe bauen +debug: + @ echo "baue Version mit Debugoutput ..." + @ $(MAKE) all DEBUG=-DDEBUG + +# Installieren +install: + @ echo "kopiere $(BIN_NAME) nach $(DESTDIR)$(INSTDIR)" + @ mkdir -p $(DESTDIR)$(INSTDIR); \ + cp $(BIN_NAME) $(DESTDIR)$(INSTDIR) + @ echo "setze Rechte auf $(BIN_NAME)" + @ cd $(DESTDIR)$(INSTDIR); \ + chmod 755 $(BIN_NAME); \ + chgrp $(INSTGRP) $(BIN_NAME); \ + chown $(INSTUSR) $(BIN_NAME) + @ echo "kopiere $(CONF_NAME) nach $(DESTDIR)$(CONFDIR)" + @ mkdir -p $(DESTDIR)$(CONFDIR); \ + cp $(CONF_NAME) $(DESTDIR)$(CONFDIR) + @ echo "setze Rechte auf $(CONF_NAME)" + @ cd $(DESTDIR)$(CONFDIR); \ + chmod 755 $(CONF_NAME); \ + chgrp $(CONFGRP) $(CONF_NAME); \ + chown $(CONFUSR) $(CONF_NAME) + + +# Aufräumnen (alle Object-Files löschen) +cleanup: + @ echo "Räume auf..." + @ echo "...entferne Object-Files:" + @ echo " " $(OBJS) + @ $(RM) -f $(OBJS) + +clean: cleanup + @ echo "...lösche binary:" + @ echo " " $(BIN_NAME) + @ rm -f $(BIN_NAME) diff --git a/cronjob/checksensor/checksensor.c b/cronjob/checksensor/checksensor.c index 264e26f..568fd01 100644 --- a/cronjob/checksensor/checksensor.c +++ b/cronjob/checksensor/checksensor.c @@ -1,6 +1,6 @@ /* - checksensor.c -- Part of the weatherdeamon + checksensor.c -- Part of checksensor Copyright (C) 2006 Jan Losinski @@ -20,6 +20,10 @@ */ +/* + * Wenn mehr kommentare benoetigt werden: + * losinski@wh2.tu-dresden.de + * */ #include /* EXIT_SUCCESS */ #include @@ -46,11 +50,22 @@ static sens_info_list_ptr failed_sensors = NULL; /* Funktionen ----------------------------------------------------------*/ +static void generate_conn_string(); +static PGconn *pg_check_connect(char *); +static PGresult *pg_check_exec(PGconn *, char *); +static void get_sensors_from_db(); +static char *get_type_table_by_id(PGconn *, int ); +static int count_data_by_sensor_id(PGconn *, int ); +static sens_info_list_ptr get_sensor_info(PGconn *, int , int ); +static int check_sensors(); +static char *get_message(int , void *); +static void mail_failtures(); static void clean(); static void exit_sig_handler(int); /* Implementierungen ---------------------------------------------------*/ +/* baut den String fuer die Postgres-Verbindung zusammen */ static void generate_conn_string(){ if(conn_string == NULL){ conn_string = malloc(sizeof(char)*BUFFSIZE); @@ -58,6 +73,7 @@ static void generate_conn_string(){ } } +/* Baut eine Vebindung zur postgres auf, bricht mit fehler ab wenn nicht moeglich */ static PGconn *pg_check_connect(char *conn_string){ PGconn *conn = PQconnectdb(conn_string); /* Connection aufbauen */ if(PQstatus(conn) != CONNECTION_OK){ @@ -68,6 +84,7 @@ static PGconn *pg_check_connect(char *conn_string){ return conn; } +/* Fuehrt ein SQL-Statement aus. Bricht mit fehler ab wenn nicht moeglich */ static PGresult *pg_check_exec(PGconn *conn, char *query){ PGresult *res; res = PQexec(conn, query); @@ -80,6 +97,7 @@ static PGresult *pg_check_exec(PGconn *conn, char *query){ return res; } +/* Holt sich die ID's der Sensoren aus der Datenbank */ static void get_sensors_from_db(){ int id_field; int i; @@ -110,6 +128,7 @@ static void get_sensors_from_db(){ connection = NULL; } +/* Tabellenname des typs aus der Datenbank holen */ static char *get_type_table_by_id(PGconn *connection, int sens_id){ char *table; int table_field; @@ -133,6 +152,7 @@ static char *get_type_table_by_id(PGconn *connection, int sens_id){ return table; } +/* Datensätze im gegebenem Interval zaehlen */ static int count_data_by_sensor_id(PGconn *connection, int sens_id){ int count_field; char *table; @@ -162,6 +182,7 @@ static int count_data_by_sensor_id(PGconn *connection, int sens_id){ return count; } +/* Informationen ueber einen Sensor holen */ static sens_info_list_ptr get_sensor_info(PGconn *conn, int id, int count){ sens_info_list_ptr new_info; PGresult *res; @@ -183,9 +204,7 @@ static sens_info_list_ptr get_sensor_info(PGconn *conn, int id, int count){ sens_desc_field = PQfnumber(res, "sens_desc"); sens_loc_field = PQfnumber(res, "sens_location"); - new_info = malloc(sizeof(sensor_info)); - new_info->id = id; new_info->count = count; @@ -200,6 +219,7 @@ static sens_info_list_ptr get_sensor_info(PGconn *conn, int id, int count){ return new_info; } +/* Sensoren der Reihe nach pruefen */ static int check_sensors(){ sens_id_list_ptr temp_id_ptr = global_opts.sens_id_list; sens_info_list_ptr temp_inf_ptr = NULL; @@ -229,6 +249,8 @@ static int check_sensors(){ return fail_count; } +/* Callback-Funktion für das Versenden der Mail, + * baut die nachicht zusammen */ static char *get_message(int line, void *arg){ sens_info_list_ptr info = *((sens_info_list_ptr*) arg); if (info != NULL){ @@ -248,6 +270,7 @@ static char *get_message(int line, void *arg){ } } +/* Schickt die Mail los */ static void mail_failtures(){ server_vars *servo = get_default_servopts(); address_all_struct addresses; diff --git a/cronjob/checksensor/checksensor.h b/cronjob/checksensor/checksensor.h index 03b58af..489acea 100644 --- a/cronjob/checksensor/checksensor.h +++ b/cronjob/checksensor/checksensor.h @@ -1,6 +1,6 @@ /* - main.h -- Part of the weatherdeamon + main.h -- Part of checksensor Copyright (C) 2006 Jan Losinski @@ -19,24 +19,30 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +/* + * Wenn mehr kommentare benoetigt werden: + * losinski@wh2.tu-dresden.de + * */ #include "mailer.h" /* Datenstrukturen --------------------------------------------------------*/ +/* Liste der IDs der zu ueberpruefenden sensoren */ typedef struct sens_id_list *sens_id_list_ptr; typedef struct sens_id_list { sens_id_list_ptr next; int id; } sensor_id; +/* Pointer auf die Liste der Addresen an die die mail versendet wird */ typedef struct address_t *mail_list_ptr; /* Optionen */ typedef struct { int interval; /* Das Interval, in dem ein Sensor zuletzt gesendet haben sollte */ - int sendings; + int sendings; /* Wie oft im gegebenen interval daten angekommen sein sollten */ mail_list_ptr address_list; /* Liste der Ids der Sensoren, die ueberprueft werden sollen */ sens_id_list_ptr sens_id_list; /* Liste der Mail-Addressen, die benachichtigt werden sollen wenn was ist */ char id_from_db; /* Flag, ob die Id's aus der Datenbank gelesen werden sollen */ @@ -44,22 +50,23 @@ typedef struct { char *pg_user; /* Postgres-Username */ char *pg_pass; /* Postgres-Password */ char *pg_database; /* Postgres-Datenbank */ - char *mail_host; - int mail_port; - char mail_ssl; - char mail_auth; - char *mail_auth_user; - char *mail_auth_pass; + char *mail_host; /* Hostname (oder ip) des Mailservers */ + int mail_port; /* Port des Mailservers */ + char mail_ssl; /* Flag ob SSL (TLS) genutzt werden soll */ + char mail_auth; /* Flag ob authentifiziert werden soll */ + char *mail_auth_user; /* User für die authentifizierung */ + char *mail_auth_pass; /* Passwort für die Authentifizierung */ } w_opts; +/* Struktur, die Infos ueber den Sensor enthaelt */ typedef struct sens_info_list *sens_info_list_ptr; typedef struct sens_info_list { - int id; - int count; - char *type_desc; - char *sens_location; - char *sens_desc; - sens_info_list_ptr next; + int id; /* ID des Sensors */ + int count; /* Anzahl der datensaetze im Interval */ + char *type_desc; /* Typenbeschreibung */ + char *sens_location; /* Standort */ + char *sens_desc; /* Beschreibung des Sensors */ + sens_info_list_ptr next; /* naecstes Element */ } sensor_info; /* Funktionen -------------------------------------------------------------*/ diff --git a/cronjob/checksensor/config.c b/cronjob/checksensor/config.c index 747bf96..d105d42 100644 --- a/cronjob/checksensor/config.c +++ b/cronjob/checksensor/config.c @@ -20,6 +20,10 @@ */ +/* + * Wenn mehr kommentare benoetigt werden: + * losinski@wh2.tu-dresden.de + * */ #include @@ -30,6 +34,7 @@ #include "definitions.h" #include "checksensor.h" +#define CONFIG_BUFFERSIZE 2048 /* Funktionsdefinitionen */ static int read_int(const char *, void *);