diff --git a/cronjob/checksensor/checksensor.c b/cronjob/checksensor/checksensor.c new file mode 100644 index 0000000..0011517 --- /dev/null +++ b/cronjob/checksensor/checksensor.c @@ -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 /* EXIT_SUCCESS */ +#include +#include /* STDOUT_FILENO */ +#include +#include +#include +#include +#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); +} + + diff --git a/cronjob/checksensor/checksensor.h b/cronjob/checksensor/checksensor.h new file mode 100644 index 0000000..1a70e31 --- /dev/null +++ b/cronjob/checksensor/checksensor.h @@ -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; diff --git a/cronjob/checksensor/config.c b/cronjob/checksensor/config.c index e0cf0d4..cb4a30a 100644 --- a/cronjob/checksensor/config.c +++ b/cronjob/checksensor/config.c @@ -31,7 +31,7 @@ #include "config.h" #include "definitions.h" -#include "main.h" +#include "checksensor.h" //#include "mailer.h" @@ -49,15 +49,15 @@ static const struct config_keyword keywords[] = { /* keyword handler variable address default */ {"checkinterval", read_int, &(global_opts.interval), DEFAULT_CHECK_INTERVAL}, {"sensorid_from_db", read_yn, &(global_opts.id_from_db), "yes"}, - {"sensorid", add_sens_id, &(global_opts.sens_id_list), NULL}, - {"adminaddress", add_address, &(global_opts.address_list), NULL}, + {"sensorid", add_sens_id, &(global_opts.sens_id_list), ""}, + {"adminaddress", add_address, &(global_opts.address_list), ""}, - {"mail_host", read_str, &(global_opts.mail_host), NULL}, - {"mail_port", read_int, &(global_opts.mail_port), NULL}, - {"mail_use_ssl", read_yn, &(global_opts.mail_ssl), NULL}, - {"mail_use_auth", read_yn, &(global_opts.mail_auth), NULL}, - {"mail_auth_user", read_str, &(global_opts.mail_auth_user), NULL}, - {"mail_auth_pass", read_str, &(global_opts.mail_auth_pass), NULL}, + {"mail_host", read_str, &(global_opts.mail_host), ""}, + {"mail_port", read_int, &(global_opts.mail_port), ""}, + {"mail_use_ssl", read_yn, &(global_opts.mail_ssl), ""}, + {"mail_use_auth", read_yn, &(global_opts.mail_auth), ""}, + {"mail_auth_user", read_str, &(global_opts.mail_auth_user), ""}, + {"mail_auth_pass", read_str, &(global_opts.mail_auth_pass), ""}, {"pg_host", read_str, &(global_opts.pg_host), DEFAULT_PG_HOST}, {"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 */ if(reset){ for (i = 0; keywords[i].keyword[0]; i++) + //printf("keyword: %s \n",keywords[i].keyword); if (keywords[i].def[0]) keywords[i].handler(keywords[i].def, keywords[i].var); }