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