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