new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / monitord / monitord.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
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
7  * Foundation
8  */
9
10 #include "shared.h"
11 #include "monitord.h"
12
13 /* Global variables */
14 monitor_config mond;
15
16
17 void Monitord()
18 {
19     time_t tm;
20     struct tm *p;
21
22     int today = 0;
23     int thismonth = 0;
24     int thisyear = 0;
25
26     char str[OS_SIZE_1024 + 1];
27
28     /* Wait a few seconds to settle */
29     sleep(10);
30
31     memset(str, '\0', OS_SIZE_1024 + 1);
32
33     /* Get current time before starting */
34     tm = time(NULL);
35     p = localtime(&tm);
36
37     today = p->tm_mday;
38     thismonth = p->tm_mon;
39     thisyear = p->tm_year + 1900;
40
41     /* Connect to the message queue or exit */
42     if ((mond.a_queue = StartMQ(DEFAULTQUEUE, WRITE)) < 0) {
43         ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
44     }
45
46     /* Send startup message */
47     snprintf(str, OS_SIZE_1024 - 1, OS_AD_STARTED);
48     if (SendMSG(mond.a_queue, str, ARGV0,
49                 LOCALFILE_MQ) < 0) {
50         merror(QUEUE_SEND, ARGV0);
51     }
52
53     /* Main monitor loop */
54     while (1) {
55         tm = time(NULL);
56         p = localtime(&tm);
57
58         /* Check for unavailable agents */
59         if (mond.monitor_agents) {
60             monitor_agents();
61         }
62
63         /* Day changed, deal with log files */
64         if (today != p->tm_mday) {
65             /* Generate reports */
66             generate_reports(today, thismonth, thisyear);
67
68             manage_files(today, thismonth, thisyear);
69
70             today = p->tm_mday;
71             thismonth = p->tm_mon;
72             thisyear = p->tm_year + 1900;
73         }
74
75         /* We only check every two minutes */
76         sleep(120);
77     }
78 }
79