Working on sensor-checking
git-svn-id: file:///home/jan/tmp/wetterstation/trunk@51 dd492736-c11a-0410-ad51-8c26713eaf7f
This commit is contained in:
parent
5f44a08280
commit
f308f7b6ba
|
|
@ -0,0 +1,182 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
checksensor.c -- Part of the weatherdeamon
|
||||||
|
|
||||||
|
Copyright (C) 2006 Jan Losinski
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
|
||||||
|
$Id: process.c v 1.00 11 Aug 2006 losinski $
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h> /* EXIT_SUCCESS */
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h> /* STDOUT_FILENO */
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <postgresql/libpq-fe.h>
|
||||||
|
#include "definitions.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "checksensor.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define BUFFSIZE 1024
|
||||||
|
|
||||||
|
|
||||||
|
/* Variablen ---------------------------------------------------------- */
|
||||||
|
w_opts global_opts;
|
||||||
|
static PGconn *connection = NULL;
|
||||||
|
static char *conn_string = NULL;
|
||||||
|
|
||||||
|
/* Funktionen ----------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void clean();
|
||||||
|
static void exit_sig_handler(int);
|
||||||
|
|
||||||
|
/* Implementierungen ---------------------------------------------------*/
|
||||||
|
|
||||||
|
static void generate_conn_string(){
|
||||||
|
if(conn_string == NULL){
|
||||||
|
conn_string = malloc(sizeof(char)*BUFFSIZE);
|
||||||
|
snprintf(conn_string, BUFFSIZE, "host=%s dbname=%s user=%s password=%s", global_opts.pg_host, global_opts.pg_database, global_opts.pg_user, global_opts.pg_pass);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static PGconn *pg_check_connect(char *conn_string){
|
||||||
|
PGconn *conn = PQconnectdb(conn_string); /* Connection aufbauen */
|
||||||
|
if(PQstatus(conn) != CONNECTION_OK){
|
||||||
|
DEBUGOUT2("\nFehler beim Aufbau der Datenbankverbindung\n%s\n", PQerrorMessage(conn));
|
||||||
|
exit_error(ERROR_MAKECONN);
|
||||||
|
}
|
||||||
|
DEBUGOUT1("Verbindung hergestellt \n");
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PGresult *pg_check_exec(PGconn *conn, char *query){
|
||||||
|
PGresult *res;
|
||||||
|
res = PQexec(conn, query);
|
||||||
|
if(!res || PQresultStatus(res) != PGRES_TUPLES_OK){
|
||||||
|
DEBUGOUT2("Fehler beim exec: %s\n", query);
|
||||||
|
exit_error(ERROR_QUERY);
|
||||||
|
} else {
|
||||||
|
DEBUGOUT2("Query: '%s' ausgeführt\n", query);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void get_sensors_from_db(){
|
||||||
|
int id_field;
|
||||||
|
int i;
|
||||||
|
sens_id_list_ptr new_id, temp_id = NULL;
|
||||||
|
connection = pg_check_connect(conn_string);
|
||||||
|
PGresult *res = pg_check_exec(connection, "select id from sensoren");
|
||||||
|
id_field = PQfnumber(res, "id");
|
||||||
|
for (i = 0; i < PQntuples(res); i++){
|
||||||
|
new_id = malloc(sizeof(sensor_id));
|
||||||
|
new_id->id = atoi(PQgetvalue(res, i, id_field));
|
||||||
|
if (temp_id == NULL){
|
||||||
|
temp_id = new_id;
|
||||||
|
global_opts.sens_id_list = temp_id;
|
||||||
|
} else {
|
||||||
|
temp_id->next = new_id;
|
||||||
|
temp_id = temp_id->next;
|
||||||
|
}
|
||||||
|
DEBUGOUT2("add id: (%d)\n", new_id->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Mainfkt. und diverse andere Funktionen zum beenden des Programmes ---*/
|
||||||
|
|
||||||
|
/* Main-Funktion */
|
||||||
|
int main(int argc, char *argv[]){
|
||||||
|
/* errno 0 setzen */
|
||||||
|
errno = 0;
|
||||||
|
|
||||||
|
DEBUGOUT1("Programm gestartet\n");
|
||||||
|
|
||||||
|
/* Signal-Handling zum Abbrechen ses Progammes */
|
||||||
|
if(signal(SIGABRT, exit_sig_handler) == SIG_ERR)
|
||||||
|
exit_error(ERROR_SEIINST);
|
||||||
|
DEBUGOUT1("Signalhandler zum beenden per SIGABRT installiert\n");
|
||||||
|
if(signal(SIGINT, exit_sig_handler) == SIG_ERR)
|
||||||
|
exit_error(ERROR_SEIINST);
|
||||||
|
DEBUGOUT1("Signalhandler zum beenden per SIGINT installiert\n");
|
||||||
|
if(signal(SIGQUIT, exit_sig_handler) == SIG_ERR)
|
||||||
|
exit_error(ERROR_SEIINST);
|
||||||
|
DEBUGOUT1("Signalhandler zum beenden per SIGQUIT installiert\n");
|
||||||
|
if(signal(SIGTERM, exit_sig_handler) == SIG_ERR)
|
||||||
|
exit_error(ERROR_SEIINST);
|
||||||
|
DEBUGOUT1("Signalhandler zum beenden per SIGTERM installiert\n");
|
||||||
|
|
||||||
|
read_config(DEFAULT_CONFIG_FILE,1);
|
||||||
|
|
||||||
|
/* Debug-Ausgaben, um zu sehen, ob die Optionen richtig gesetzt werden */
|
||||||
|
DEBUGOUT1("\nPostgres:\n");
|
||||||
|
DEBUGOUT2(" Host: = %s\n",global_opts.pg_host);
|
||||||
|
DEBUGOUT2(" User: = %s\n",global_opts.pg_user);
|
||||||
|
DEBUGOUT2(" Pass: = %s\n",global_opts.pg_pass);
|
||||||
|
DEBUGOUT2(" Datenbank = %s\n",global_opts.pg_database);
|
||||||
|
|
||||||
|
generate_conn_string();
|
||||||
|
get_sensors_from_db();
|
||||||
|
|
||||||
|
clean();
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Diese Funktion beendet das Programm mit einer Fehlermeldung. */
|
||||||
|
void exit_error(char* err){
|
||||||
|
DEBUGOUT1("\nEtwas unschoenes ist passiert\n");
|
||||||
|
clean();
|
||||||
|
if(errno != 0){
|
||||||
|
perror("Fehler");
|
||||||
|
}
|
||||||
|
write(STDOUT_FILENO, err, strlen(err));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Wird bei Beendigungssignalen ausgefuehrt */
|
||||||
|
static void exit_sig_handler(int signr){
|
||||||
|
#ifdef DEBUG
|
||||||
|
DEBUGOUT1("\n");
|
||||||
|
switch (signr){
|
||||||
|
case SIGABRT:
|
||||||
|
DEBUGOUT1("SIGABRT Interupt erhalten!\n");
|
||||||
|
case SIGTERM:
|
||||||
|
DEBUGOUT1("SIGTERM Interupt erhalten!\n");
|
||||||
|
case SIGQUIT:
|
||||||
|
DEBUGOUT1("SIGQUIT Interupt erhalten!\n");
|
||||||
|
case SIGINT:
|
||||||
|
DEBUGOUT1("SIGINT Interupt erhalten!\n");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
clean();
|
||||||
|
DEBUGOUT1("Beende Programm...\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Aufraumen ausfuehren */
|
||||||
|
static void clean(){
|
||||||
|
DEBUGOUT1("\nRaeume auf...\n");
|
||||||
|
if(connection != NULL)
|
||||||
|
PQfinish(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
main.h -- Part of the weatherdeamon
|
||||||
|
|
||||||
|
Copyright (C) 2006 Jan Losinski
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "mailer.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* Datenstrukturen --------------------------------------------------------*/
|
||||||
|
|
||||||
|
typedef struct sens_id_list *sens_id_list_ptr;
|
||||||
|
typedef struct sens_id_list {
|
||||||
|
sens_id_list_ptr next;
|
||||||
|
int id;
|
||||||
|
} sensor_id;
|
||||||
|
|
||||||
|
typedef struct address_t *mail_list_ptr;
|
||||||
|
|
||||||
|
/* Optionen */
|
||||||
|
typedef struct {
|
||||||
|
int *interval; /* Das Interval, in dem ein Sensor zuletzt gesendet haben sollte */
|
||||||
|
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 */
|
||||||
|
char *pg_host; /* Postgres-Host */
|
||||||
|
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;
|
||||||
|
} w_opts;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Funktionen -------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/* Programm mit einem Fatalem Fehler beenden.
|
||||||
|
* Argument: Fehlermeldung */
|
||||||
|
void exit_error(char*);
|
||||||
|
|
||||||
|
/* Variablen --------------------------------------------------------------*/
|
||||||
|
extern w_opts global_opts;
|
||||||
|
|
@ -31,7 +31,7 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "definitions.h"
|
#include "definitions.h"
|
||||||
#include "main.h"
|
#include "checksensor.h"
|
||||||
//#include "mailer.h"
|
//#include "mailer.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -49,15 +49,15 @@ static const struct config_keyword keywords[] = {
|
||||||
/* keyword handler variable address default */
|
/* keyword handler variable address default */
|
||||||
{"checkinterval", read_int, &(global_opts.interval), DEFAULT_CHECK_INTERVAL},
|
{"checkinterval", read_int, &(global_opts.interval), DEFAULT_CHECK_INTERVAL},
|
||||||
{"sensorid_from_db", read_yn, &(global_opts.id_from_db), "yes"},
|
{"sensorid_from_db", read_yn, &(global_opts.id_from_db), "yes"},
|
||||||
{"sensorid", add_sens_id, &(global_opts.sens_id_list), NULL},
|
{"sensorid", add_sens_id, &(global_opts.sens_id_list), ""},
|
||||||
{"adminaddress", add_address, &(global_opts.address_list), NULL},
|
{"adminaddress", add_address, &(global_opts.address_list), ""},
|
||||||
|
|
||||||
{"mail_host", read_str, &(global_opts.mail_host), NULL},
|
{"mail_host", read_str, &(global_opts.mail_host), ""},
|
||||||
{"mail_port", read_int, &(global_opts.mail_port), NULL},
|
{"mail_port", read_int, &(global_opts.mail_port), ""},
|
||||||
{"mail_use_ssl", read_yn, &(global_opts.mail_ssl), NULL},
|
{"mail_use_ssl", read_yn, &(global_opts.mail_ssl), ""},
|
||||||
{"mail_use_auth", read_yn, &(global_opts.mail_auth), NULL},
|
{"mail_use_auth", read_yn, &(global_opts.mail_auth), ""},
|
||||||
{"mail_auth_user", read_str, &(global_opts.mail_auth_user), NULL},
|
{"mail_auth_user", read_str, &(global_opts.mail_auth_user), ""},
|
||||||
{"mail_auth_pass", read_str, &(global_opts.mail_auth_pass), NULL},
|
{"mail_auth_pass", read_str, &(global_opts.mail_auth_pass), ""},
|
||||||
|
|
||||||
{"pg_host", read_str, &(global_opts.pg_host), DEFAULT_PG_HOST},
|
{"pg_host", read_str, &(global_opts.pg_host), DEFAULT_PG_HOST},
|
||||||
{"pg_user", read_str, &(global_opts.pg_user), DEFAULT_PG_USER},
|
{"pg_user", read_str, &(global_opts.pg_user), DEFAULT_PG_USER},
|
||||||
|
|
@ -159,6 +159,7 @@ int read_config(const char *file, int reset){
|
||||||
/* Optionen mit default-werten füllen */
|
/* Optionen mit default-werten füllen */
|
||||||
if(reset){
|
if(reset){
|
||||||
for (i = 0; keywords[i].keyword[0]; i++)
|
for (i = 0; keywords[i].keyword[0]; i++)
|
||||||
|
//printf("keyword: %s \n",keywords[i].keyword);
|
||||||
if (keywords[i].def[0])
|
if (keywords[i].def[0])
|
||||||
keywords[i].handler(keywords[i].def, keywords[i].var);
|
keywords[i].handler(keywords[i].def, keywords[i].var);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue