pacemaker  2.0.4-2deceaa
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/lrmd.h>
13 #include <crm/msg_xml.h>
15 #include <crm/cib/internal.h> /* for F_CIB_UPDATE_RESULT */
16 
17 /*
18  * to allow script compatibility we can have more than one
19  * set of environment variables
20  */
22 {
24  "CRM_notify_recipient", "CRM_alert_recipient", NULL
25  },
27  "CRM_notify_node", "CRM_alert_node", NULL
28  },
30  "CRM_notify_nodeid", "CRM_alert_nodeid", NULL
31  },
33  "CRM_notify_rsc", "CRM_alert_rsc", NULL
34  },
36  "CRM_notify_task", "CRM_alert_task", NULL
37  },
39  "CRM_notify_interval", "CRM_alert_interval", NULL
40  },
42  "CRM_notify_desc", "CRM_alert_desc", NULL
43  },
45  "CRM_notify_status", "CRM_alert_status", NULL
46  },
48  "CRM_notify_target_rc", "CRM_alert_target_rc", NULL
49  },
50  [PCMK__alert_key_rc] = {
51  "CRM_notify_rc", "CRM_alert_rc", NULL
52  },
54  "CRM_notify_kind", "CRM_alert_kind", NULL
55  },
57  "CRM_notify_version", "CRM_alert_version", NULL
58  },
60  "CRM_notify_node_sequence", PCMK__ALERT_NODE_SEQUENCE, NULL
61  },
63  "CRM_notify_timestamp", "CRM_alert_timestamp", NULL
64  },
66  "CRM_notify_attribute_name", "CRM_alert_attribute_name", NULL
67  },
69  "CRM_notify_attribute_value", "CRM_alert_attribute_value", NULL
70  },
72  "CRM_notify_timestamp_epoch", "CRM_alert_timestamp_epoch", NULL
73  },
75  "CRM_notify_timestamp_usec", "CRM_alert_timestamp_usec", NULL
76  },
78  "CRM_notify_exec_time", "CRM_alert_exec_time", NULL
79  }
80 };
81 
94 pcmk__alert_new(const char *id, const char *path)
95 {
96  pcmk__alert_t *entry = calloc(1, sizeof(pcmk__alert_t));
97 
98  CRM_ASSERT(entry && id && path);
99  entry->id = strdup(id);
100  entry->path = strdup(path);
102  entry->flags = pcmk__alert_default;
103  return entry;
104 }
105 
106 void
108 {
109  if (entry) {
110  free(entry->id);
111  free(entry->path);
112  free(entry->tstamp_format);
113  free(entry->recipient);
114 
115  g_strfreev(entry->select_attribute_name);
116  if (entry->envvars) {
117  g_hash_table_destroy(entry->envvars);
118  }
119  free(entry);
120  }
121 }
122 
133 {
134  pcmk__alert_t *new_entry = pcmk__alert_new(entry->id, entry->path);
135 
136  new_entry->timeout = entry->timeout;
137  new_entry->flags = entry->flags;
138  new_entry->envvars = crm_str_table_dup(entry->envvars);
139  if (entry->tstamp_format) {
140  new_entry->tstamp_format = strdup(entry->tstamp_format);
141  }
142  if (entry->recipient) {
143  new_entry->recipient = strdup(entry->recipient);
144  }
145  if (entry->select_attribute_name) {
146  new_entry->select_attribute_name = g_strdupv(entry->select_attribute_name);
147  }
148  return new_entry;
149 }
150 
151 void
153  const char *value)
154 {
155  for (const char **key = pcmk__alert_keys[name]; *key; key++) {
156  crm_trace("Inserting alert key %s = '%s'", *key, value);
157  if (value) {
158  g_hash_table_insert(table, strdup(*key), strdup(value));
159  } else {
160  g_hash_table_remove(table, *key);
161  }
162  }
163 }
164 
165 void
167  int value)
168 {
169  for (const char **key = pcmk__alert_keys[name]; *key; key++) {
170  crm_trace("Inserting alert key %s = %d", *key, value);
171  g_hash_table_insert(table, strdup(*key), crm_itoa(value));
172  }
173 }
174 
175 #define XPATH_PATCHSET1_DIFF "//" F_CIB_UPDATE_RESULT "//" XML_TAG_DIFF_ADDED
176 
177 #define XPATH_PATCHSET1_CRMCONFIG XPATH_PATCHSET1_DIFF "//" XML_CIB_TAG_CRMCONFIG
178 #define XPATH_PATCHSET1_ALERTS XPATH_PATCHSET1_DIFF "//" XML_CIB_TAG_ALERTS
179 
180 #define XPATH_PATCHSET1_EITHER \
181  XPATH_PATCHSET1_CRMCONFIG " | " XPATH_PATCHSET1_ALERTS
182 
183 #define XPATH_CONFIG "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION
184 
185 #define XPATH_CRMCONFIG XPATH_CONFIG "/" XML_CIB_TAG_CRMCONFIG "/"
186 #define XPATH_ALERTS XPATH_CONFIG "/" XML_CIB_TAG_ALERTS
187 
197 bool
198 pcmk__alert_in_patchset(xmlNode *msg, bool config)
199 {
200  int rc = -1;
201  int format= 1;
202  xmlNode *patchset = get_message_xml(msg, F_CIB_UPDATE_RESULT);
203  xmlNode *change = NULL;
204  xmlXPathObject *xpathObj = NULL;
205 
206  CRM_CHECK(msg != NULL, return FALSE);
207 
208  crm_element_value_int(msg, F_CIB_RC, &rc);
209  if (rc < pcmk_ok) {
210  crm_trace("Ignore failed CIB update: %s (%d)", pcmk_strerror(rc), rc);
211  return FALSE;
212  }
213 
214  crm_element_value_int(patchset, "format", &format);
215  if (format == 1) {
216  const char *diff = (config? XPATH_PATCHSET1_EITHER : XPATH_PATCHSET1_ALERTS);
217 
218  if ((xpathObj = xpath_search(msg, diff)) != NULL) {
219  freeXpathObject(xpathObj);
220  return TRUE;
221  }
222  } else if (format == 2) {
223  for (change = __xml_first_child(patchset); change != NULL; change = __xml_next(change)) {
224  const char *xpath = crm_element_value(change, XML_DIFF_PATH);
225 
226  if (xpath == NULL) {
227  continue;
228  }
229 
230  if ((!config || !strstr(xpath, XPATH_CRMCONFIG))
231  && !strstr(xpath, XPATH_ALERTS)) {
232 
233  /* this is not a change to an existing section ... */
234 
235  xmlNode *section = NULL;
236  const char *name = NULL;
237 
238  if ((strcmp(xpath, XPATH_CONFIG) != 0) ||
239  ((section = __xml_first_child(change)) == NULL) ||
240  ((name = crm_element_name(section)) == NULL) ||
241  (strcmp(name, XML_CIB_TAG_ALERTS) != 0)) {
242 
243  /* ... nor is it a newly added alerts section */
244  continue;
245  }
246  }
247 
248  return TRUE;
249  }
250 
251  } else {
252  crm_warn("Unknown patch format: %d", format);
253  }
254  return FALSE;
255 }
void pcmk__add_alert_key_int(GHashTable *table, enum pcmk__alert_keys_e name, int value)
Definition: alerts.c:166
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:233
A dumping ground.
xmlNode * get_message_xml(xmlNode *msg, const char *field)
Definition: xml.c:2619
const char * pcmk_strerror(int rc)
Definition: results.c:55
#define XPATH_ALERTS
Definition: alerts.c:186
#define PCMK__ALERT_INTERNAL_KEY_MAX
char ** select_attribute_name
char * tstamp_format
Resource agent executor.
pcmk__alert_t * pcmk__dup_alert(pcmk__alert_t *entry)
Definition: alerts.c:132
bool pcmk__alert_in_patchset(xmlNode *msg, bool config)
Definition: alerts.c:198
#define PCMK__ALERT_NODE_SEQUENCE
pcmk__alert_keys_e
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Definition: nvpair.c:558
#define XPATH_CRMCONFIG
Definition: alerts.c:185
void pcmk__free_alert(pcmk__alert_t *entry)
Definition: alerts.c:107
const char * pcmk__alert_keys[PCMK__ALERT_INTERNAL_KEY_MAX][3]
Definition: alerts.c:21
#define XPATH_PATCHSET1_EITHER
Definition: alerts.c:180
#define crm_warn(fmt, args...)
Definition: logging.h:364
#define XPATH_PATCHSET1_ALERTS
Definition: alerts.c:178
int rc
Definition: pcmk_fence.c:34
#define F_CIB_RC
Definition: internal.h:40
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:522
#define XPATH_CONFIG
Definition: alerts.c:183
#define crm_trace(fmt, args...)
Definition: logging.h:369
#define F_CIB_UPDATE_RESULT
Definition: internal.h:51
GHashTable * envvars
#define XML_DIFF_PATH
Definition: msg_xml.h:413
#define CRM_ASSERT(expr)
Definition: results.h:42
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
Definition: xpath.c:136
GHashTable * crm_str_table_dup(GHashTable *old_table)
Definition: strings.c:495
#define pcmk_ok
Definition: results.h:67
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_ALERTS
Definition: msg_xml.h:148
char * name
Definition: pcmk_fence.c:30
void freeXpathObject(xmlXPathObjectPtr xpathObj)
Definition: xpath.c:36
#define PCMK__ALERT_DEFAULT_TIMEOUT_MS
void pcmk__add_alert_key(GHashTable *table, enum pcmk__alert_keys_e name, const char *value)
Definition: alerts.c:152