1 /* @(#) $Id: ./src/monitord/generate_reports.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2010 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
16 int OS_SendCustomEmail(char **to, char *subject, char *smtpserver, char *from, char *idsname, FILE *fp, struct tm *p);
17 char *(monthss[])={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
18 "Sep","Oct","Nov","Dec"};
21 void generate_reports(int cday, int cmon, int cyear,struct tm *p)
34 while(mond.reports[s])
37 if(mond.reports[s]->emailto == NULL)
43 /* We create a new process to run the report and send the email.
44 * To avoid crashing monitord if something goes wrong.
49 merror("%s: ERROR: Fork failed. cause: %d - %s", ARGV0, errno, strerror(errno));
59 snprintf(fname, 255, "/logs/.report-%d.log", getpid());
61 merror("%s: INFO: Starting daily reporting for '%s'", ARGV0, mond.reports[s]->title);
62 mond.reports[s]->r_filter.fp = fopen(fname, "w+");
63 if(!mond.reports[s]->r_filter.fp)
65 merror("%s: ERROR: Unable to open temporary reports file.", ARGV0);
71 /* Opening the log file. */
72 snprintf(aname, 255, "%s/%d/%s/ossec-%s-%02d.log",
73 ALERTS, cyear, monthss[cmon], "alerts", cday);
74 os_strdup(aname, mond.reports[s]->r_filter.filename);
78 os_ReportdStart(&mond.reports[s]->r_filter);
79 fflush(mond.reports[s]->r_filter.fp);
81 if(ftell(mond.reports[s]->r_filter.fp) < 10)
83 merror("%s: INFO: Report '%s' empty.", ARGV0, mond.reports[s]->title);
85 else if(OS_SendCustomEmail(mond.reports[s]->emailto, mond.reports[s]->title,
86 mond.smtpserver, mond.emailfrom, mond.emailidsname, mond.reports[s]->r_filter.fp, p) != 0)
88 merror("%s: WARN: Unable to send report email.", ARGV0);
90 fclose(mond.reports[s]->r_filter.fp);
92 free(mond.reports[s]->r_filter.filename);
93 mond.reports[s]->r_filter.filename = NULL;
99 /* Sleep between each report. Time is not important in here. */
111 wp = waitpid((pid_t) -1, NULL, WNOHANG);
114 merror(WAITPID_ERROR, ARGV0);
118 /* If there is still any report left, sleep 5 and try again.*/
124 merror("%s: WARN: Report taking too long to complete. Waiting for it to finish...", ARGV0);
128 merror("%s: WARN: Report took too long. Moving on...", ARGV0);