Imported Upstream version 2.7
[ossec-hids.git] / src / monitord / monitord.c
1 /* @(#) $Id: ./src/monitord/monitord.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
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
10  * Foundation
11  */
12
13
14
15 #include "shared.h"
16 #include "monitord.h"
17
18
19 /* Real monitord global */
20 void Monitord()
21 {
22     time_t tm;
23     struct tm *p;
24
25     int today = 0;              
26     int thismonth = 0;
27     int thisyear = 0;
28
29     char str[OS_SIZE_1024 +1];
30
31     /* Waiting a few seconds to settle */
32     sleep(10);
33
34     memset(str, '\0', OS_SIZE_1024 +1);
35
36
37     /* Getting currently time before starting */
38     tm = time(NULL);
39     p = localtime(&tm); 
40
41     today = p->tm_mday;
42     thismonth = p->tm_mon;
43     thisyear = p->tm_year+1900;
44
45
46
47     /* Connecting to the message queue
48      * Exit if it fails.
49      */
50     if((mond.a_queue = StartMQ(DEFAULTQUEUE,WRITE)) < 0)
51     {
52         ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
53     }
54
55
56     /* Sending startup message */
57     snprintf(str, OS_SIZE_1024 -1, OS_AD_STARTED);
58     if(SendMSG(mond.a_queue, str, ARGV0,
59                        LOCALFILE_MQ) < 0)
60     {
61         merror(QUEUE_SEND, ARGV0);
62     }
63
64
65     /* Main monitor loop */
66     while(1)
67     {
68         tm = time(NULL);
69         p = localtime(&tm);
70
71
72         /* Checking unavailable agents */
73         if(mond.monitor_agents)
74         {
75             monitor_agents();
76         }
77
78         /* Day changed, deal with log files */
79         if(today != p->tm_mday)
80         {
81             /* Generate reports. */
82             generate_reports(today, thismonth, thisyear, p);
83
84             manage_files(today, thismonth, thisyear);
85
86             today = p->tm_mday;
87             thismonth = p->tm_mon;
88             thisyear = p->tm_year+1900;
89         }
90
91         /* We only check every two minutes */
92         sleep(120);
93     }
94 }
95
96 /* EOF */