root/lib/common/alerts.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pcmk__alert_new
  2. pcmk__free_alert
  3. pcmk__dup_alert
  4. pcmk__add_alert_key
  5. pcmk__add_alert_key_int

   1 /*
   2  * Copyright 2015-2024 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 #include <crm/crm.h>
  12 #include <crm/lrmd.h>
  13 #include <crm/common/xml.h>
  14 #include <crm/common/alerts_internal.h>
  15 #include <crm/common/cib_internal.h>
  16 #include <crm/common/xml_internal.h>
  17 
  18 const char *pcmk__alert_keys[PCMK__ALERT_INTERNAL_KEY_MAX] = {
  19     [PCMK__alert_key_recipient] = "CRM_alert_recipient",
  20     [PCMK__alert_key_node] = "CRM_alert_node",
  21     [PCMK__alert_key_nodeid] = "CRM_alert_nodeid",
  22     [PCMK__alert_key_rsc] = "CRM_alert_rsc",
  23     [PCMK__alert_key_task] = "CRM_alert_task",
  24     [PCMK__alert_key_interval] = "CRM_alert_interval",
  25     [PCMK__alert_key_desc] = "CRM_alert_desc",
  26     [PCMK__alert_key_status] = "CRM_alert_status",
  27     [PCMK__alert_key_target_rc] = "CRM_alert_target_rc",
  28     [PCMK__alert_key_rc] = "CRM_alert_rc",
  29     [PCMK__alert_key_kind] = "CRM_alert_kind",
  30     [PCMK__alert_key_version] = "CRM_alert_version",
  31     [PCMK__alert_key_node_sequence] = PCMK__ALERT_NODE_SEQUENCE,
  32     [PCMK__alert_key_timestamp] = "CRM_alert_timestamp",
  33     [PCMK__alert_key_attribute_name] = "CRM_alert_attribute_name",
  34     [PCMK__alert_key_attribute_value] = "CRM_alert_attribute_value",
  35     [PCMK__alert_key_timestamp_epoch] = "CRM_alert_timestamp_epoch",
  36     [PCMK__alert_key_timestamp_usec] = "CRM_alert_timestamp_usec",
  37     [PCMK__alert_key_exec_time] = "CRM_alert_exec_time",
  38 };
  39 
  40 /*!
  41  * \brief Create a new alert entry structure
  42  *
  43  * \param[in] id  ID to use
  44  * \param[in] path  Path to alert agent executable
  45  *
  46  * \return Pointer to newly allocated alert entry
  47  * \note Non-string fields will be filled in with defaults.
  48  *       It is the caller's responsibility to free the result,
  49  *       using pcmk__free_alert().
  50  */
  51 pcmk__alert_t *
  52 pcmk__alert_new(const char *id, const char *path)
     /* [previous][next][first][last][top][bottom][index][help] */
  53 {
  54     pcmk__alert_t *entry = pcmk__assert_alloc(1, sizeof(pcmk__alert_t));
  55 
  56     pcmk__assert((id != NULL) && (path != NULL));
  57     entry->id = pcmk__str_copy(id);
  58     entry->path = pcmk__str_copy(path);
  59     entry->timeout = PCMK__ALERT_DEFAULT_TIMEOUT_MS;
  60     entry->flags = pcmk__alert_default;
  61     return entry;
  62 }
  63 
  64 void
  65 pcmk__free_alert(pcmk__alert_t *entry)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67     if (entry) {
  68         free(entry->id);
  69         free(entry->path);
  70         free(entry->tstamp_format);
  71         free(entry->recipient);
  72 
  73         g_strfreev(entry->select_attribute_name);
  74         if (entry->envvars) {
  75             g_hash_table_destroy(entry->envvars);
  76         }
  77         free(entry);
  78     }
  79 }
  80 
  81 /*!
  82  * \internal
  83  * \brief Duplicate an alert entry
  84  *
  85  * \param[in] entry  Alert entry to duplicate
  86  *
  87  * \return Duplicate of alert entry
  88  */
  89 pcmk__alert_t *
  90 pcmk__dup_alert(const pcmk__alert_t *entry)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92     pcmk__alert_t *new_entry = pcmk__alert_new(entry->id, entry->path);
  93 
  94     new_entry->timeout = entry->timeout;
  95     new_entry->flags = entry->flags;
  96     new_entry->envvars = pcmk__str_table_dup(entry->envvars);
  97     new_entry->tstamp_format = pcmk__str_copy(entry->tstamp_format);
  98     new_entry->recipient = pcmk__str_copy(entry->recipient);
  99     if (entry->select_attribute_name) {
 100         new_entry->select_attribute_name = g_strdupv(entry->select_attribute_name);
 101     }
 102     return new_entry;
 103 }
 104 
 105 void
 106 pcmk__add_alert_key(GHashTable *table, enum pcmk__alert_keys_e name,
     /* [previous][next][first][last][top][bottom][index][help] */
 107                     const char *value)
 108 {
 109     pcmk__assert((table != NULL) && (name >= 0)
 110                  && (name < PCMK__ALERT_INTERNAL_KEY_MAX));
 111     if (value == NULL) {
 112         crm_trace("Removing alert key %s", pcmk__alert_keys[name]);
 113         g_hash_table_remove(table, pcmk__alert_keys[name]);
 114     } else {
 115         crm_trace("Inserting alert key %s = '%s'",
 116                   pcmk__alert_keys[name], value);
 117         pcmk__insert_dup(table, pcmk__alert_keys[name], value);
 118     }
 119 }
 120 
 121 void
 122 pcmk__add_alert_key_int(GHashTable *table, enum pcmk__alert_keys_e name,
     /* [previous][next][first][last][top][bottom][index][help] */
 123                         int value)
 124 {
 125     pcmk__assert((table != NULL) && (name >= 0)
 126                  && (name < PCMK__ALERT_INTERNAL_KEY_MAX));
 127     crm_trace("Inserting alert key %s = %d", pcmk__alert_keys[name], value);
 128     g_hash_table_insert(table, pcmk__str_copy(pcmk__alert_keys[name]),
 129                         pcmk__itoa(value));
 130 }

/* [previous][next][first][last][top][bottom][index][help] */