checksensor finished

git-svn-id: file:///home/jan/tmp/wetterstation/trunk@59 dd492736-c11a-0410-ad51-8c26713eaf7f
This commit is contained in:
losinshi 2006-08-31 17:04:18 +00:00
parent 6af8bd3138
commit 3b0cd019a2
4 changed files with 128 additions and 17 deletions

View File

@ -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)

View File

@ -1,6 +1,6 @@
/* /*
checksensor.c -- Part of the weatherdeamon checksensor.c -- Part of checksensor
Copyright (C) 2006 Jan Losinski Copyright (C) 2006 Jan Losinski
@ -20,6 +20,10 @@
*/ */
/*
* Wenn mehr kommentare benoetigt werden:
* losinski@wh2.tu-dresden.de
* */
#include <stdlib.h> /* EXIT_SUCCESS */ #include <stdlib.h> /* EXIT_SUCCESS */
#include <errno.h> #include <errno.h>
@ -46,11 +50,22 @@ static sens_info_list_ptr failed_sensors = NULL;
/* Funktionen ----------------------------------------------------------*/ /* 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 clean();
static void exit_sig_handler(int); static void exit_sig_handler(int);
/* Implementierungen ---------------------------------------------------*/ /* Implementierungen ---------------------------------------------------*/
/* baut den String fuer die Postgres-Verbindung zusammen */
static void generate_conn_string(){ static void generate_conn_string(){
if(conn_string == NULL){ if(conn_string == NULL){
conn_string = malloc(sizeof(char)*BUFFSIZE); 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){ static PGconn *pg_check_connect(char *conn_string){
PGconn *conn = PQconnectdb(conn_string); /* Connection aufbauen */ PGconn *conn = PQconnectdb(conn_string); /* Connection aufbauen */
if(PQstatus(conn) != CONNECTION_OK){ if(PQstatus(conn) != CONNECTION_OK){
@ -68,6 +84,7 @@ static PGconn *pg_check_connect(char *conn_string){
return conn; return conn;
} }
/* Fuehrt ein SQL-Statement aus. Bricht mit fehler ab wenn nicht moeglich */
static PGresult *pg_check_exec(PGconn *conn, char *query){ static PGresult *pg_check_exec(PGconn *conn, char *query){
PGresult *res; PGresult *res;
res = PQexec(conn, query); res = PQexec(conn, query);
@ -80,6 +97,7 @@ static PGresult *pg_check_exec(PGconn *conn, char *query){
return res; return res;
} }
/* Holt sich die ID's der Sensoren aus der Datenbank */
static void get_sensors_from_db(){ static void get_sensors_from_db(){
int id_field; int id_field;
int i; int i;
@ -110,6 +128,7 @@ static void get_sensors_from_db(){
connection = NULL; connection = NULL;
} }
/* Tabellenname des typs aus der Datenbank holen */
static char *get_type_table_by_id(PGconn *connection, int sens_id){ static char *get_type_table_by_id(PGconn *connection, int sens_id){
char *table; char *table;
int table_field; int table_field;
@ -133,6 +152,7 @@ static char *get_type_table_by_id(PGconn *connection, int sens_id){
return table; return table;
} }
/* Datensätze im gegebenem Interval zaehlen */
static int count_data_by_sensor_id(PGconn *connection, int sens_id){ static int count_data_by_sensor_id(PGconn *connection, int sens_id){
int count_field; int count_field;
char *table; char *table;
@ -162,6 +182,7 @@ static int count_data_by_sensor_id(PGconn *connection, int sens_id){
return count; return count;
} }
/* Informationen ueber einen Sensor holen */
static sens_info_list_ptr get_sensor_info(PGconn *conn, int id, int count){ static sens_info_list_ptr get_sensor_info(PGconn *conn, int id, int count){
sens_info_list_ptr new_info; sens_info_list_ptr new_info;
PGresult *res; 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_desc_field = PQfnumber(res, "sens_desc");
sens_loc_field = PQfnumber(res, "sens_location"); sens_loc_field = PQfnumber(res, "sens_location");
new_info = malloc(sizeof(sensor_info)); new_info = malloc(sizeof(sensor_info));
new_info->id = id; new_info->id = id;
new_info->count = count; 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; return new_info;
} }
/* Sensoren der Reihe nach pruefen */
static int check_sensors(){ static int check_sensors(){
sens_id_list_ptr temp_id_ptr = global_opts.sens_id_list; sens_id_list_ptr temp_id_ptr = global_opts.sens_id_list;
sens_info_list_ptr temp_inf_ptr = NULL; sens_info_list_ptr temp_inf_ptr = NULL;
@ -229,6 +249,8 @@ static int check_sensors(){
return fail_count; return fail_count;
} }
/* Callback-Funktion für das Versenden der Mail,
* baut die nachicht zusammen */
static char *get_message(int line, void *arg){ static char *get_message(int line, void *arg){
sens_info_list_ptr info = *((sens_info_list_ptr*) arg); sens_info_list_ptr info = *((sens_info_list_ptr*) arg);
if (info != NULL){ if (info != NULL){
@ -248,6 +270,7 @@ static char *get_message(int line, void *arg){
} }
} }
/* Schickt die Mail los */
static void mail_failtures(){ static void mail_failtures(){
server_vars *servo = get_default_servopts(); server_vars *servo = get_default_servopts();
address_all_struct addresses; address_all_struct addresses;

View File

@ -1,6 +1,6 @@
/* /*
main.h -- Part of the weatherdeamon main.h -- Part of checksensor
Copyright (C) 2006 Jan Losinski Copyright (C) 2006 Jan Losinski
@ -19,24 +19,30 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
/*
* Wenn mehr kommentare benoetigt werden:
* losinski@wh2.tu-dresden.de
* */
#include "mailer.h" #include "mailer.h"
/* Datenstrukturen --------------------------------------------------------*/ /* 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;
typedef struct sens_id_list { typedef struct sens_id_list {
sens_id_list_ptr next; sens_id_list_ptr next;
int id; int id;
} sensor_id; } sensor_id;
/* Pointer auf die Liste der Addresen an die die mail versendet wird */
typedef struct address_t *mail_list_ptr; typedef struct address_t *mail_list_ptr;
/* Optionen */ /* Optionen */
typedef struct { typedef struct {
int interval; /* Das Interval, in dem ein Sensor zuletzt gesendet haben sollte */ 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 */ 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 */ 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 */ 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_user; /* Postgres-Username */
char *pg_pass; /* Postgres-Password */ char *pg_pass; /* Postgres-Password */
char *pg_database; /* Postgres-Datenbank */ char *pg_database; /* Postgres-Datenbank */
char *mail_host; char *mail_host; /* Hostname (oder ip) des Mailservers */
int mail_port; int mail_port; /* Port des Mailservers */
char mail_ssl; char mail_ssl; /* Flag ob SSL (TLS) genutzt werden soll */
char mail_auth; char mail_auth; /* Flag ob authentifiziert werden soll */
char *mail_auth_user; char *mail_auth_user; /* User für die authentifizierung */
char *mail_auth_pass; char *mail_auth_pass; /* Passwort für die Authentifizierung */
} w_opts; } w_opts;
/* Struktur, die Infos ueber den Sensor enthaelt */
typedef struct sens_info_list *sens_info_list_ptr; typedef struct sens_info_list *sens_info_list_ptr;
typedef struct sens_info_list { typedef struct sens_info_list {
int id; int id; /* ID des Sensors */
int count; int count; /* Anzahl der datensaetze im Interval */
char *type_desc; char *type_desc; /* Typenbeschreibung */
char *sens_location; char *sens_location; /* Standort */
char *sens_desc; char *sens_desc; /* Beschreibung des Sensors */
sens_info_list_ptr next; sens_info_list_ptr next; /* naecstes Element */
} sensor_info; } sensor_info;
/* Funktionen -------------------------------------------------------------*/ /* Funktionen -------------------------------------------------------------*/

View File

@ -20,6 +20,10 @@
*/ */
/*
* Wenn mehr kommentare benoetigt werden:
* losinski@wh2.tu-dresden.de
* */
#include <string.h> #include <string.h>
@ -30,6 +34,7 @@
#include "definitions.h" #include "definitions.h"
#include "checksensor.h" #include "checksensor.h"
#define CONFIG_BUFFERSIZE 2048
/* Funktionsdefinitionen */ /* Funktionsdefinitionen */
static int read_int(const char *, void *); static int read_int(const char *, void *);