3 /* Copyright (C) 2010 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 2) as published by the FSF - Free Software
15 int OS_SendCustomEmail(char **to, char *subject, char *smtpserver, char *from, FILE *fp, struct tm *p);
16 char *(monthss[])={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
17 "Sep","Oct","Nov","Dec"};
20 void generate_reports(int cday, int cmon, int cyear,struct tm *p)
33 while(mond.reports[s])
36 if(mond.reports[s]->emailto == NULL)
42 /* We create a new process to run the report and send the email.
43 * To avoid crashing monitord if something goes wrong.
48 merror("%s: ERROR: Fork failed. cause: %d - %s", ARGV0, errno, strerror(errno));
58 snprintf(fname, 255, "/logs/.report-%d.log", getpid());
60 merror("%s: INFO: Starting daily reporting for '%s'", ARGV0, mond.reports[s]->title);
61 mond.reports[s]->r_filter.fp = fopen(fname, "w+");
62 if(!mond.reports[s]->r_filter.fp)
64 merror("%s: ERROR: Unable to open temporary reports file.", ARGV0);
70 /* Opening the log file. */
71 snprintf(aname, 255, "%s/%d/%s/ossec-%s-%02d.log",
72 ALERTS, cyear, monthss[cmon], "alerts", cday);
73 os_strdup(aname, mond.reports[s]->r_filter.filename);
77 os_ReportdStart(&mond.reports[s]->r_filter);
78 fflush(mond.reports[s]->r_filter.fp);
80 if(ftell(mond.reports[s]->r_filter.fp) < 10)
82 merror("%s: INFO: Report '%s' empty.", ARGV0, mond.reports[s]->title);
84 else if(OS_SendCustomEmail(mond.reports[s]->emailto, mond.reports[s]->title,
85 mond.smtpserver, mond.emailfrom, mond.reports[s]->r_filter.fp, p) != 0)
87 merror("%s: WARN: Unable to send report email.", ARGV0);
89 fclose(mond.reports[s]->r_filter.fp);
91 free(mond.reports[s]->r_filter.filename);
92 mond.reports[s]->r_filter.filename = NULL;
98 /* Sleep between each report. Time is not important in here. */
110 wp = waitpid((pid_t) -1, NULL, WNOHANG);
113 merror(WAITPID_ERROR, ARGV0);
117 /* If there is still any report left, sleep 5 and try again.*/
123 merror("%s: WARN: Report taking too long to complete. Waiting for it to finish...", ARGV0);
127 merror("%s: WARN: Report took too long. Moving on...", ARGV0);