pacemaker  2.0.4-2deceaa
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rules_alerts.c
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2020 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/msg_xml.h>
13 #include <crm/pengine/rules.h>
16 
17 static void
18 get_meta_attrs_from_cib(xmlNode *basenode, pcmk__alert_t *entry,
19  guint *max_timeout)
20 {
21  GHashTable *config_hash = crm_str_table_new();
22  crm_time_t *now = crm_time_new(NULL);
23  const char *value = NULL;
24 
25  pe_unpack_nvpairs(basenode, basenode, XML_TAG_META_SETS, NULL, config_hash,
26  NULL, FALSE, now, NULL);
27  crm_time_free(now);
28 
29  value = g_hash_table_lookup(config_hash, XML_ALERT_ATTR_TIMEOUT);
30  if (value) {
31  entry->timeout = crm_get_msec(value);
32  if (entry->timeout <= 0) {
33  if (entry->timeout == 0) {
34  crm_trace("Alert %s uses default timeout of %dmsec",
36  } else {
37  crm_warn("Alert %s has invalid timeout value '%s', using default %dmsec",
38  entry->id, (char*)value, PCMK__ALERT_DEFAULT_TIMEOUT_MS);
39  }
41  } else {
42  crm_trace("Alert %s uses timeout of %dmsec",
43  entry->id, entry->timeout);
44  }
45  if (entry->timeout > *max_timeout) {
46  *max_timeout = entry->timeout;
47  }
48  }
49  value = g_hash_table_lookup(config_hash, XML_ALERT_ATTR_TSTAMP_FORMAT);
50  if (value) {
51  /* hard to do any checks here as merely anything can
52  * can be a valid time-format-string
53  */
54  entry->tstamp_format = strdup(value);
55  crm_trace("Alert %s uses timestamp format '%s'",
56  entry->id, entry->tstamp_format);
57  }
58 
59  g_hash_table_destroy(config_hash);
60 }
61 
62 static void
63 get_envvars_from_cib(xmlNode *basenode, pcmk__alert_t *entry)
64 {
65  xmlNode *child;
66 
67  if ((basenode == NULL) || (entry == NULL)) {
68  return;
69  }
70 
71  child = first_named_child(basenode, XML_TAG_ATTR_SETS);
72  if (child == NULL) {
73  return;
74  }
75 
76  if (entry->envvars == NULL) {
77  entry->envvars = crm_str_table_new();
78  }
79 
80  for (child = first_named_child(child, XML_CIB_TAG_NVPAIR); child != NULL;
81  child = crm_next_same_xml(child)) {
82 
83  const char *name = crm_element_value(child, XML_NVPAIR_ATTR_NAME);
84  const char *value = crm_element_value(child, XML_NVPAIR_ATTR_VALUE);
85 
86  if (value == NULL) {
87  value = "";
88  }
89  g_hash_table_insert(entry->envvars, strdup(name), strdup(value));
90  crm_trace("Alert %s: added environment variable %s='%s'",
91  entry->id, name, value);
92  }
93 }
94 
95 static void
96 unpack_alert_filter(xmlNode *basenode, pcmk__alert_t *entry)
97 {
98  xmlNode *select = first_named_child(basenode, XML_CIB_TAG_ALERT_SELECT);
99  xmlNode *event_type = NULL;
100  uint32_t flags = pcmk__alert_none;
101 
102  for (event_type = __xml_first_child_element(select); event_type != NULL;
103  event_type = __xml_next_element(event_type)) {
104 
105  const char *tagname = crm_element_name(event_type);
106 
107  if (tagname == NULL) {
108  continue;
109 
110  } else if (!strcmp(tagname, XML_CIB_TAG_ALERT_FENCING)) {
111  flags |= pcmk__alert_fencing;
112 
113  } else if (!strcmp(tagname, XML_CIB_TAG_ALERT_NODES)) {
114  flags |= pcmk__alert_node;
115 
116  } else if (!strcmp(tagname, XML_CIB_TAG_ALERT_RESOURCES)) {
117  flags |= pcmk__alert_resource;
118 
119  } else if (!strcmp(tagname, XML_CIB_TAG_ALERT_ATTRIBUTES)) {
120  xmlNode *attr;
121  const char *attr_name;
122  int nattrs = 0;
123 
124  flags |= pcmk__alert_attribute;
125  for (attr = first_named_child(event_type, XML_CIB_TAG_ALERT_ATTR);
126  attr != NULL;
127  attr = crm_next_same_xml(attr)) {
128 
129  attr_name = crm_element_value(attr, XML_NVPAIR_ATTR_NAME);
130  if (attr_name) {
131  if (nattrs == 0) {
132  g_strfreev(entry->select_attribute_name);
133  entry->select_attribute_name = NULL;
134  }
135  ++nattrs;
136  entry->select_attribute_name = realloc_safe(entry->select_attribute_name,
137  (nattrs + 1) * sizeof(char*));
138  entry->select_attribute_name[nattrs - 1] = strdup(attr_name);
139  entry->select_attribute_name[nattrs] = NULL;
140  }
141  }
142  }
143  }
144 
145  if (flags != pcmk__alert_none) {
146  entry->flags = flags;
147  crm_debug("Alert %s receives events: attributes:%s%s%s%s",
148  entry->id,
149  (is_set(flags, pcmk__alert_attribute)?
150  (entry->select_attribute_name? "some" : "all") : "none"),
151  (is_set(flags, pcmk__alert_fencing)? " fencing" : ""),
152  (is_set(flags, pcmk__alert_node)? " nodes" : ""),
153  (is_set(flags, pcmk__alert_resource)? " resources" : ""));
154  }
155 }
156 
157 static void
158 unpack_alert(xmlNode *alert, pcmk__alert_t *entry, guint *max_timeout)
159 {
160  get_envvars_from_cib(alert, entry);
161  get_meta_attrs_from_cib(alert, entry, max_timeout);
162  unpack_alert_filter(alert, entry);
163 }
164 
176 GListPtr
177 pe_unpack_alerts(xmlNode *alerts)
178 {
179  xmlNode *alert;
180  pcmk__alert_t *entry;
181  guint max_timeout = 0;
182  GListPtr alert_list = NULL;
183 
184  if (alerts == NULL) {
185  return alert_list;
186  }
187 
188  for (alert = first_named_child(alerts, XML_CIB_TAG_ALERT);
189  alert != NULL; alert = crm_next_same_xml(alert)) {
190 
191  xmlNode *recipient;
192  int recipients = 0;
193  const char *alert_id = ID(alert);
194  const char *alert_path = crm_element_value(alert, XML_ALERT_ATTR_PATH);
195 
196  /* The schema should enforce this, but to be safe ... */
197  if ((alert_id == NULL) || (alert_path == NULL)) {
198  crm_warn("Ignoring invalid alert without id and path");
199  continue;
200  }
201 
202  entry = pcmk__alert_new(alert_id, alert_path);
203 
204  unpack_alert(alert, entry, &max_timeout);
205 
206  if (entry->tstamp_format == NULL) {
208  }
209 
210  crm_debug("Alert %s: path=%s timeout=%dms tstamp-format='%s' %u vars",
211  entry->id, entry->path, entry->timeout, entry->tstamp_format,
212  (entry->envvars? g_hash_table_size(entry->envvars) : 0));
213 
214  for (recipient = first_named_child(alert, XML_CIB_TAG_ALERT_RECIPIENT);
215  recipient != NULL; recipient = crm_next_same_xml(recipient)) {
216 
217  pcmk__alert_t *recipient_entry = pcmk__dup_alert(entry);
218 
219  recipients++;
220  recipient_entry->recipient = strdup(crm_element_value(recipient,
222  unpack_alert(recipient, recipient_entry, &max_timeout);
223  alert_list = g_list_prepend(alert_list, recipient_entry);
224  crm_debug("Alert %s has recipient %s with value %s and %d envvars",
225  entry->id, ID(recipient), recipient_entry->recipient,
226  (recipient_entry->envvars?
227  g_hash_table_size(recipient_entry->envvars) : 0));
228  }
229 
230  if (recipients == 0) {
231  alert_list = g_list_prepend(alert_list, entry);
232  } else {
233  pcmk__free_alert(entry);
234  }
235  }
236  return alert_list;
237 }
238 
245 void
247 {
248  if (alert_list) {
249  g_list_free_full(alert_list, (GDestroyNotify) pcmk__free_alert);
250  }
251 }
A dumping ground.
void pe_unpack_nvpairs(xmlNode *top, xmlNode *xml_obj, const char *set_name, GHashTable *node_hash, GHashTable *hash, const char *always_first, gboolean overwrite, crm_time_t *now, crm_time_t *next_change)
Extract nvpair blocks contained by an XML element into a hash table.
Definition: rules.c:1005
void pe_free_alert_list(GListPtr alert_list)
Definition: rules_alerts.c:246
struct crm_time_s crm_time_t
Definition: iso8601.h:32
xmlNode * first_named_child(const xmlNode *parent, const char *name)
Definition: xml.c:4399
char ** select_attribute_name
#define XML_CIB_TAG_ALERT_RESOURCES
Definition: msg_xml.h:155
long long crm_get_msec(const char *input)
Parse a time+units string and return milliseconds equivalent.
Definition: strings.c:211
char * tstamp_format
#define XML_NVPAIR_ATTR_NAME
Definition: msg_xml.h:339
#define XML_CIB_TAG_ALERT_ATTRIBUTES
Definition: msg_xml.h:152
pcmk__alert_t * pcmk__dup_alert(pcmk__alert_t *entry)
Definition: alerts.c:132
#define XML_CIB_TAG_NVPAIR
Definition: msg_xml.h:160
#define XML_ALERT_ATTR_REC_VALUE
Definition: msg_xml.h:356
void pcmk__free_alert(pcmk__alert_t *entry)
Definition: alerts.c:107
GListPtr pe_unpack_alerts(xmlNode *alerts)
Definition: rules_alerts.c:177
#define XML_TAG_ATTR_SETS
Definition: msg_xml.h:163
#define crm_warn(fmt, args...)
Definition: logging.h:364
#define crm_debug(fmt, args...)
Definition: logging.h:368
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:522
#define XML_CIB_TAG_ALERT_NODES
Definition: msg_xml.h:154
#define crm_trace(fmt, args...)
Definition: logging.h:369
#define XML_TAG_META_SETS
Definition: msg_xml.h:164
GHashTable * envvars
#define XML_ALERT_ATTR_TIMEOUT
Definition: msg_xml.h:354
#define XML_ALERT_ATTR_TSTAMP_FORMAT
Definition: msg_xml.h:355
#define XML_CIB_TAG_ALERT
Definition: msg_xml.h:149
crm_time_t * crm_time_new(const char *string)
Definition: iso8601.c:93
#define XML_CIB_TAG_ALERT_FENCING
Definition: msg_xml.h:153
#define XML_NVPAIR_ATTR_VALUE
Definition: msg_xml.h:340
pcmk__alert_t * pcmk__alert_new(const char *id, const char *path)
Create a new alert entry structure.
Definition: alerts.c:94
#define XML_CIB_TAG_ALERT_SELECT
Definition: msg_xml.h:151
#define PCMK__ALERT_DEFAULT_TSTAMP_FORMAT
#define XML_ALERT_ATTR_PATH
Definition: msg_xml.h:353
#define XML_CIB_TAG_ALERT_ATTR
Definition: msg_xml.h:156
#define ID(x)
Definition: msg_xml.h:418
char * name
Definition: pcmk_fence.c:30
#define XML_CIB_TAG_ALERT_RECIPIENT
Definition: msg_xml.h:150
GList * GListPtr
Definition: crm.h:214
#define PCMK__ALERT_DEFAULT_TIMEOUT_MS
uint64_t flags
Definition: remote.c:149
xmlNode * crm_next_same_xml(const xmlNode *sibling)
Get next instance of same XML tag.
Definition: xml.c:4425
void crm_time_free(crm_time_t *dt)
Definition: iso8601.c:141