Imported Upstream version 2.7
[ossec-hids.git] / src / monitord / monitor_agents.c
1 /* @(#) $Id: ./src/monitord/monitor_agents.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right 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 #include "shared.h"
15 #include "monitord.h"
16 #include "read-agents.h"
17
18
19
20 void monitor_agents()
21 {
22     char **cr_agents;
23     char **av_agents;
24
25     av_agents = get_agents(GA_ACTIVE);
26
27
28     /* No agent saved */
29     if(!mond.agents)
30     {
31         mond.agents = av_agents;
32         return;
33     }
34
35     /* Checking if any of the previous available agents
36      * are disconnected.
37      */
38     cr_agents = mond.agents;
39     while(*cr_agents)
40     {
41         int available = 0;
42         char **tmp_av;
43
44         tmp_av = av_agents;
45         while(tmp_av && *tmp_av)
46         {
47             if(strcmp(*cr_agents, *tmp_av) == 0)
48             {
49                 available = 1;
50                 break;
51             }
52             tmp_av++;
53         }
54
55         /* Agent disconnected */
56         if(available == 0)
57         {
58             char str[OS_SIZE_1024 +1];
59
60             /* Sending disconnected message */
61             snprintf(str, OS_SIZE_1024 -1, OS_AG_DISCON, *cr_agents);
62             if(SendMSG(mond.a_queue, str, ARGV0,
63                         LOCALFILE_MQ) < 0)
64             {
65                 merror(QUEUE_SEND, ARGV0);
66             }
67         }
68
69         cr_agents++;
70     }
71
72
73     /* Removing old agent list and adding currently one */
74     free_agents(mond.agents);
75     mond.agents = av_agents;
76     return;
77 }
78
79 /* EOF */