OpenVAS Scanner  22.7.9
heartbeat.c
Go to the documentation of this file.
1 /* SPDX-FileCopyrightText: 2023 Greenbone AG
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  */
5 
11 #include "../misc/heartbeat.h"
12 
13 #include "../misc/plugutils.h" /* for kb_item_set_int_with_main_kb_check */
14 
15 #include <gvm/base/prefs.h> /* for prefs_get() */
16 #include <gvm/boreas/cli.h> /* for is_host_alive() */
17 
18 #undef G_LOG_DOMAIN
19 
22 #define G_LOG_DOMAIN "sd main"
23 
32 int
33 check_host_still_alive (kb_t kb, const char *hostname)
34 {
35  int is_alive = 0;
36  boreas_error_t alive_err;
37 
38  /* Heartbeat will work only with boreas enabled. We check if we
39  have all what we need before running a heartbeat check. */
40  if (prefs_get_bool ("test_alive_hosts_only"))
41  {
42  const gchar *alive_test_str = prefs_get ("ALIVE_TEST");
43 
44  /* Don't perform a hearbeat check if the host is always considered
45  alive or the alive test is not valid. */
46  if (!(alive_test_str
47  && atoi (alive_test_str) >= ALIVE_TEST_TCP_ACK_SERVICE
48  && atoi (alive_test_str) < 32 // max value for alive test combi.
49  && !((atoi (alive_test_str)) & ALIVE_TEST_CONSIDER_ALIVE)))
50  return -1;
51  }
52  else
53  {
54  g_warning ("%s: Trying to perform an alive test, but Boreas is not "
55  "enabled. Heartbeat check for %s will not be performed",
56  __func__, hostname);
57  return -1;
58  }
59 
60  alive_err = is_host_alive (hostname, &is_alive);
61  if (alive_err)
62  {
63  g_warning ("%s: Heartbeat check failed for %s with error %d.", __func__,
64  hostname, alive_err);
65  return -1;
66  }
67 
68  if (is_alive == 0)
69  {
70  g_message ("%s: Heartbeat check was not successful. The host %s has"
71  " been set as dead.",
72  __func__, hostname);
73  kb_item_set_int_with_main_kb_check (kb, "Host/dead", 1);
74  return 0;
75  }
76 
77  return 1;
78 }
kb_item_set_int_with_main_kb_check
int kb_item_set_int_with_main_kb_check(kb_t kb, const char *name, int value)
Check if the current kb corresponds to the original scanid, if it matches it call kb_item_set_int....
Definition: plugutils.c:554
check_host_still_alive
int check_host_still_alive(kb_t kb, const char *hostname)
Check if the hosts is still alive and set it as dead if not.
Definition: heartbeat.c:33
hostname
const char * hostname
Definition: pluginlaunch.c:68