new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / monitord / monitor_agents.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right 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 #include "read-agents.h"
13
14
15 void monitor_agents()
16 {
17     char **cr_agents;
18     char **av_agents;
19
20     av_agents = get_agents_with_timeout(GA_ACTIVE, mond.notify_time);
21
22     /* No agent saved */
23     if (!mond.agents) {
24         mond.agents = av_agents;
25         return;
26     }
27
28     /* Check if any of the previously available agents are disconnected */
29     cr_agents = mond.agents;
30     while (*cr_agents) {
31         int available = 0;
32         char **tmp_av;
33
34         tmp_av = av_agents;
35         while (tmp_av && *tmp_av) {
36             if (strcmp(*cr_agents, *tmp_av) == 0) {
37                 available = 1;
38                 break;
39             }
40             tmp_av++;
41         }
42
43         /* Agent disconnected */
44         if (available == 0) {
45             char str[OS_SIZE_1024 + 1];
46
47             /* Send disconnected message */
48             snprintf(str, OS_SIZE_1024 - 1, OS_AG_DISCON, *cr_agents);
49             if (SendMSG(mond.a_queue, str, ARGV0,
50                         LOCALFILE_MQ) < 0) {
51                 merror(QUEUE_SEND, ARGV0);
52             }
53         }
54
55         cr_agents++;
56     }
57
58     /* Remove old agent list and add current one */
59     free_agents(mond.agents);
60     mond.agents = av_agents;
61     return;
62 }