1 /* Copyright (C) 2010 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
13 static const char *(monthss[]) = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
14 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
18 void generate_reports(int cday, int cmon, int cyear)
22 if (!mond.smtpserver) {
30 while (mond.reports[s]) {
32 if (mond.reports[s]->emailto == NULL) {
37 /* We create a new process to run the report and send the email.
38 * To avoid crashing monitord if something goes wrong.
42 merror("%s: ERROR: Fork failed. cause: %d - %s", ARGV0, errno, strerror(errno));
45 } else if (pid == 0) {
50 snprintf(fname, 255, "/logs/.report-%d.log", getpid());
52 merror("%s: INFO: Starting daily reporting for '%s'", ARGV0, mond.reports[s]->title);
53 mond.reports[s]->r_filter.fp = fopen(fname, "w+");
54 if (!mond.reports[s]->r_filter.fp) {
55 merror("%s: ERROR: Unable to open temporary reports file.", ARGV0);
60 /* Open the log file */
61 snprintf(aname, 255, "%s/%d/%s/ossec-%s-%02d.log",
62 ALERTS, cyear, monthss[cmon], "alerts", cday);
63 os_strdup(aname, mond.reports[s]->r_filter.filename);
66 os_ReportdStart(&mond.reports[s]->r_filter);
67 fflush(mond.reports[s]->r_filter.fp);
69 fclose(mond.reports[s]->r_filter.fp);
73 if((sr = stat(fname, &sb)) < 0) {
74 merror("Cannot stat %s: %s", fname, strerror(errno));
77 if (sb.st_size == 0) {
78 merror("%s: INFO: Report '%s' empty.", ARGV0, mond.reports[s]->title);
79 } else if (OS_SendCustomEmail2(mond.reports[s]->emailto,
80 mond.reports[s]->title,
86 merror("%s: WARN: Unable to send report email.", ARGV0);
89 if(unlink(fname) < 0) {
90 merror("%s: ERROR: Cannot unlink file %s: %s", ARGV0, fname, strerror(errno));
93 free(mond.reports[s]->r_filter.filename);
94 mond.reports[s]->r_filter.filename = NULL;
98 /* Sleep between each report. Time is not important in here. */
108 wp = waitpid((pid_t) - 1, NULL, WNOHANG);
110 merror(WAITPID_ERROR, ARGV0, errno, strerror(errno));
111 } else if (wp == 0) {
112 /* If there is still any report left, sleep 5 and try again */
117 merror("%s: WARN: Report taking too long to complete. Waiting for it to finish...", ARGV0);
120 merror("%s: WARN: Report took too long. Moving on...", ARGV0);