From 822e8f408de69ba01a7bb8e7789dcfaef30b4a06 Mon Sep 17 00:00:00 2001 From: losinshi Date: Thu, 31 Aug 2006 14:52:33 +0000 Subject: [PATCH] checksensor nearly ready git-svn-id: file:///home/jan/tmp/wetterstation/trunk@57 dd492736-c11a-0410-ad51-8c26713eaf7f --- cronjob/checksensor/checksensor.c | 48 +++++++++++++++++++++- cronjob/checksensor/config.c | 68 +++++++++++++++---------------- 2 files changed, 78 insertions(+), 38 deletions(-) diff --git a/cronjob/checksensor/checksensor.c b/cronjob/checksensor/checksensor.c index dbe3e0a..264e26f 100644 --- a/cronjob/checksensor/checksensor.c +++ b/cronjob/checksensor/checksensor.c @@ -34,12 +34,14 @@ #define BUFFSIZE 512 +#define BUFFSIZE_EXTRA 2048 /* Variablen ---------------------------------------------------------- */ w_opts global_opts; static PGconn *connection = NULL; static char *conn_string = NULL; +static char *mail_buff = NULL; static sens_info_list_ptr failed_sensors = NULL; /* Funktionen ----------------------------------------------------------*/ @@ -227,6 +229,46 @@ static int check_sensors(){ return fail_count; } +static char *get_message(int line, void *arg){ + sens_info_list_ptr info = *((sens_info_list_ptr*) arg); + if (info != NULL){ + if(line == 0){ + return MAIL_HEAD; + } else { + if(mail_buff == NULL) + mail_buff = malloc(sizeof(char)*BUFFSIZE_EXTRA); + snprintf(mail_buff, BUFFSIZE_EXTRA, "\r\nDer Sensor mit der ID %d hat in den letzten %d Stunden nur %d Werte geliefert!\r\nDaten zum Sensor:\r\nTyp:\t\t%s\r\nStandort:\t%s\r\nBeschreibung:\t%s\r\n", info->id, global_opts.interval, info->count, info->type_desc, info->sens_location, info->sens_desc); + *((sens_info_list_ptr*)arg) = info->next; + return mail_buff; + } + } else { + if(mail_buff != NULL) + free(mail_buff); + return NULL; + } +} + +static void mail_failtures(){ + server_vars *servo = get_default_servopts(); + address_all_struct addresses; + int mail_err_nr = 0; + + servo->host = global_opts.mail_host; + servo->port = global_opts.mail_port; + servo->ssl_use = global_opts.mail_ssl; + servo->auth_use = global_opts.mail_auth; + servo->auth_user = global_opts.mail_auth_user; + servo->auth_pass = global_opts.mail_auth_pass; + + addresses.from = NULL; + addresses.to = global_opts.address_list; + addresses.cc = NULL; + addresses.bcc = NULL; + + mail_err_nr = mail_message(& addresses, "Wetterstation", 0, get_message, & failed_sensors, servo); + printf("%s\n",get_mail_status_text(mail_err_nr)); +} + /* Mainfkt. und diverse andere Funktionen zum beenden des Programmes ---*/ /* Main-Funktion */ @@ -273,8 +315,10 @@ int main(int argc, char *argv[]){ DEBUGOUT2(" Datenbank = %s\n",global_opts.pg_database); generate_conn_string(); - get_sensors_from_db(); - //check_sensors(); + if(global_opts.id_from_db) + get_sensors_from_db(); + if(check_sensors()) + mail_failtures(); clean(); return EXIT_SUCCESS; diff --git a/cronjob/checksensor/config.c b/cronjob/checksensor/config.c index 817e3b5..07117d5 100644 --- a/cronjob/checksensor/config.c +++ b/cronjob/checksensor/config.c @@ -97,52 +97,48 @@ static int read_yn(const char *line, void *arg){ } static int add_sens_id(const char *line, void *arg){ - if (line == NULL){ - *((sens_id_list_ptr*)arg) = NULL; - return 1; + sens_id_list_ptr id_new, id_temp; + + id_new = malloc(sizeof(sensor_id)); + id_new->next = NULL; + id_new->id = atoi(line); + + DEBUGOUT2("add ID: %d\n", atoi(line)); + + id_temp = *((sens_id_list_ptr*)arg); + if (id_temp == NULL){ + id_temp = id_new; + *((sens_id_list_ptr*)arg) = id_temp; } else { - sens_id_list_ptr id_new, id_temp; - - id_new = malloc(sizeof(sensor_id)); - id_new->next = NULL; - id_new->id = atoi(line); - - id_temp = *((sens_id_list_ptr*)arg); - if (id_temp == NULL){ - id_temp = id_new; - } else { - while (id_temp->next != NULL){ - id_temp = id_temp->next; - } - id_temp->next = id_new; + while (id_temp->next != NULL){ + id_temp = id_temp->next; } - return 1; + id_temp->next = id_new; } + return 1; } static int add_address(const char *line, void *arg){ - if (line == NULL){ - *((mail_list_ptr*)arg) = NULL; - return 1; + mail_list_ptr adr_new, adr_temp; + + adr_new = malloc(sizeof(address_struct)); + adr_new->next = NULL; + adr_new->mailbox = strdup(line); + + DEBUGOUT2("add addr: %s\n", line); + + adr_temp = *((mail_list_ptr*)arg); + if (adr_temp == NULL){ + adr_temp = adr_new; + *((mail_list_ptr*)arg) = adr_temp; } else { - mail_list_ptr adr_new, adr_temp; - - adr_new = malloc(sizeof(address_struct)); - adr_new->next = NULL; - adr_new->mailbox = strdup(line); - - adr_temp = *((mail_list_ptr*)arg); - if (adr_temp == NULL){ - adr_temp = adr_new; - } else { - while (adr_temp->next != NULL){ - adr_temp = adr_temp->next; - } - adr_temp->next = adr_new; + while (adr_temp->next != NULL){ + adr_temp = adr_temp->next; } - return 1; + adr_temp->next = adr_new; } + return 1; }