pacemaker  1.1.24-3850484742
Scalable High-Availability cluster resource manager
alerts.c
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This software is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <crm_internal.h>
20 #include <crm/crm.h>
21 #include <crm/lrmd.h>
22 #include <crm/msg_xml.h>
24 #include <crm/cib/internal.h> /* for F_CIB_UPDATE_RESULT */
25 
26 /*
27  * to allow script compatibility we can have more than one
28  * set of environment variables
29  */
31 {
32  [CRM_alert_recipient] = {"CRM_notify_recipient", "CRM_alert_recipient", NULL},
33  [CRM_alert_node] = {"CRM_notify_node", "CRM_alert_node", NULL},
34  [CRM_alert_nodeid] = {"CRM_notify_nodeid", "CRM_alert_nodeid", NULL},
35  [CRM_alert_rsc] = {"CRM_notify_rsc", "CRM_alert_rsc", NULL},
36  [CRM_alert_task] = {"CRM_notify_task", "CRM_alert_task", NULL},
37  [CRM_alert_interval] = {"CRM_notify_interval", "CRM_alert_interval", NULL},
38  [CRM_alert_desc] = {"CRM_notify_desc", "CRM_alert_desc", NULL},
39  [CRM_alert_status] = {"CRM_notify_status", "CRM_alert_status", NULL},
40  [CRM_alert_target_rc] = {"CRM_notify_target_rc", "CRM_alert_target_rc", NULL},
41  [CRM_alert_rc] = {"CRM_notify_rc", "CRM_alert_rc", NULL},
42  [CRM_alert_kind] = {"CRM_notify_kind", "CRM_alert_kind", NULL},
43  [CRM_alert_version] = {"CRM_notify_version", "CRM_alert_version", NULL},
44  [CRM_alert_node_sequence] = {"CRM_notify_node_sequence", CRM_ALERT_NODE_SEQUENCE, NULL},
45  [CRM_alert_timestamp] = {"CRM_notify_timestamp", "CRM_alert_timestamp", NULL},
46  [CRM_alert_attribute_name] = {"CRM_notify_attribute_name", "CRM_alert_attribute_name", NULL},
47  [CRM_alert_attribute_value] = {"CRM_notify_attribute_value", "CRM_alert_attribute_value", NULL}
48 };
49 
62 crm_alert_entry_new(const char *id, const char *path)
63 {
64  crm_alert_entry_t *entry = calloc(1, sizeof(crm_alert_entry_t));
65 
66  CRM_ASSERT(entry && id && path);
67  entry->id = strdup(id);
68  entry->path = strdup(path);
70  entry->flags = crm_alert_default;
71  return entry;
72 }
73 
74 void
76 {
77  if (entry) {
78  free(entry->id);
79  free(entry->path);
80  free(entry->tstamp_format);
81  free(entry->recipient);
82 
83  g_strfreev(entry->select_attribute_name);
84  if (entry->envvars) {
85  g_hash_table_destroy(entry->envvars);
86  }
87  free(entry);
88  }
89 }
90 
101 {
102  crm_alert_entry_t *new_entry = crm_alert_entry_new(entry->id, entry->path);
103 
104  new_entry->timeout = entry->timeout;
105  new_entry->flags = entry->flags;
106  new_entry->envvars = crm_str_table_dup(entry->envvars);
107  if (entry->tstamp_format) {
108  new_entry->tstamp_format = strdup(entry->tstamp_format);
109  }
110  if (entry->recipient) {
111  new_entry->recipient = strdup(entry->recipient);
112  }
113  if (entry->select_attribute_name) {
114  new_entry->select_attribute_name = g_strdupv(entry->select_attribute_name);
115  }
116  return new_entry;
117 }
118 
119 void
121 {
122  const char **key;
123  enum crm_alert_keys_e name;
124 
125  for(name = 0; name < DIMOF(crm_alert_keys); name++) {
126  for(key = crm_alert_keys[name]; *key; key++) {
127  crm_trace("Unsetting alert key %s", *key);
128  unsetenv(*key);
129  }
130  }
131 }
132 
133 void
134 crm_insert_alert_key(GHashTable *table, enum crm_alert_keys_e name,
135  const char *value)
136 {
137  for (const char **key = crm_alert_keys[name]; *key; key++) {
138  crm_trace("Inserting alert key %s = '%s'", *key, value);
139  if (value) {
140  g_hash_table_insert(table, strdup(*key), strdup(value));
141  } else {
142  g_hash_table_remove(table, *key);
143  }
144  }
145 }
146 
147 void
148 crm_insert_alert_key_int(GHashTable *table, enum crm_alert_keys_e name,
149  int value)
150 {
151  for (const char **key = crm_alert_keys[name]; *key; key++) {
152  crm_trace("Inserting alert key %s = %d", *key, value);
153  g_hash_table_insert(table, strdup(*key), crm_itoa(value));
154  }
155 }
156 
157 static void
158 set_envvar(gpointer key, gpointer value, gpointer user_data)
159 {
160  gboolean always_unset = GPOINTER_TO_INT(user_data);
161 
162  crm_trace("%s environment variable %s='%s'",
163  (value? "Setting" : "Unsetting"),
164  (char*)key, (value? (char*)value : ""));
165  if (value && !always_unset) {
166  setenv(key, value, 1);
167  } else {
168  unsetenv(key);
169  }
170 }
171 
172 void
174 {
175  if (entry->envvars) {
176  g_hash_table_foreach(entry->envvars, set_envvar, GINT_TO_POINTER(FALSE));
177  }
178 }
179 
180 /*
181  * \note We have no way of restoring a previous value if one was set.
182  */
183 void
185 {
186  if (entry->envvars) {
187  g_hash_table_foreach(entry->envvars, set_envvar, GINT_TO_POINTER(TRUE));
188  }
189 }
190 
191 #define XPATH_PATCHSET1_DIFF "//" F_CIB_UPDATE_RESULT "//" XML_TAG_DIFF_ADDED
192 
193 #define XPATH_PATCHSET1_CRMCONFIG XPATH_PATCHSET1_DIFF "//" XML_CIB_TAG_CRMCONFIG
194 #define XPATH_PATCHSET1_ALERTS XPATH_PATCHSET1_DIFF "//" XML_CIB_TAG_ALERTS
195 
196 #define XPATH_PATCHSET1_EITHER \
197  XPATH_PATCHSET1_CRMCONFIG " | " XPATH_PATCHSET1_ALERTS
198 
199 #define XPATH_CONFIG "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION
200 
201 #define XPATH_CRMCONFIG XPATH_CONFIG "/" XML_CIB_TAG_CRMCONFIG "/"
202 #define XPATH_ALERTS XPATH_CONFIG "/" XML_CIB_TAG_ALERTS
203 
213 bool
214 crm_patchset_contains_alert(xmlNode *msg, bool config)
215 {
216  int rc = -1;
217  int format= 1;
218  xmlNode *patchset = get_message_xml(msg, F_CIB_UPDATE_RESULT);
219  xmlNode *change = NULL;
220  xmlXPathObject *xpathObj = NULL;
221 
222  CRM_CHECK(msg != NULL, return FALSE);
223 
224  crm_element_value_int(msg, F_CIB_RC, &rc);
225  if (rc < pcmk_ok) {
226  crm_trace("Ignore failed CIB update: %s (%d)", pcmk_strerror(rc), rc);
227  return FALSE;
228  }
229 
230  crm_element_value_int(patchset, "format", &format);
231  if (format == 1) {
232  const char *diff = (config? XPATH_PATCHSET1_EITHER : XPATH_PATCHSET1_ALERTS);
233 
234  if ((xpathObj = xpath_search(msg, diff)) != NULL) {
235  freeXpathObject(xpathObj);
236  return TRUE;
237  }
238  } else if (format == 2) {
239  for (change = __xml_first_child(patchset); change != NULL; change = __xml_next(change)) {
240  const char *xpath = crm_element_value(change, XML_DIFF_PATH);
241 
242  if (xpath == NULL) {
243  continue;
244  }
245 
246  if ((!config || !strstr(xpath, XPATH_CRMCONFIG))
247  && !strstr(xpath, XPATH_ALERTS)) {
248 
249  /* this is not a change to an existing section ... */
250 
251  xmlNode *section = NULL;
252  const char *name = NULL;
253 
254  if ((strcmp(xpath, XPATH_CONFIG) != 0) ||
255  ((section = __xml_first_child(change)) == NULL) ||
256  ((name = crm_element_name(section)) == NULL) ||
257  (strcmp(name, XML_CIB_TAG_ALERTS) != 0)) {
258 
259  /* ... nor is it a newly added alerts section */
260  continue;
261  }
262  }
263 
264  return TRUE;
265  }
266 
267  } else {
268  crm_warn("Unknown patch format: %d", format);
269  }
270  return FALSE;
271 }
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:190
void crm_unset_envvar_list(crm_alert_entry_t *entry)
Definition: alerts.c:184
A dumping ground.
xmlNode * get_message_xml(xmlNode *msg, const char *field)
Definition: xml.c:2558
crm_alert_entry_t * crm_alert_entry_new(const char *id, const char *path)
Create a new alert entry structure.
Definition: alerts.c:62
#define XPATH_ALERTS
Definition: alerts.c:202
#define CRM_ALERT_DEFAULT_TIMEOUT_MS
const char * pcmk_strerror(int rc)
Definition: logging.c:1017
GHashTable * envvars
void crm_insert_alert_key_int(GHashTable *table, enum crm_alert_keys_e name, int value)
Definition: alerts.c:148
char * recipient
#define pcmk_ok
Definition: error.h:45
Local Resource Manager.
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Definition: nvpair.c:428
#define XPATH_CRMCONFIG
Definition: alerts.c:201
uint32_t flags
char * tstamp_format
#define XPATH_PATCHSET1_EITHER
Definition: alerts.c:196
#define crm_warn(fmt, args...)
Definition: logging.h:275
char * id
#define XPATH_PATCHSET1_ALERTS
Definition: alerts.c:194
void crm_unset_alert_keys()
Definition: alerts.c:120
#define F_CIB_RC
Definition: internal.h:41
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:393
#define XPATH_CONFIG
Definition: alerts.c:199
#define crm_trace(fmt, args...)
Definition: logging.h:280
int setenv(const char *name, const char *value, int why)
const char * crm_alert_keys[CRM_ALERT_INTERNAL_KEY_MAX][3]
Definition: alerts.c:30
#define CRM_ALERT_NODE_SEQUENCE
#define F_CIB_UPDATE_RESULT
Definition: internal.h:52
crm_alert_entry_t * crm_dup_alert_entry(crm_alert_entry_t *entry)
Definition: alerts.c:100
#define XML_DIFF_PATH
Definition: msg_xml.h:444
void crm_insert_alert_key(GHashTable *table, enum crm_alert_keys_e name, const char *value)
Definition: alerts.c:134
bool crm_patchset_contains_alert(xmlNode *msg, bool config)
Definition: alerts.c:214
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
Definition: xpath.c:145
GHashTable * crm_str_table_dup(GHashTable *old_table)
Definition: strings.c:414
char ** select_attribute_name
int timeout
#define DIMOF(a)
Definition: crm.h:29
crm_alert_keys_e
char * path
#define CRM_ASSERT(expr)
Definition: error.h:20
#define XML_CIB_TAG_ALERTS
Definition: msg_xml.h:168
char * crm_itoa(int an_int)
Definition: strings.c:61
void crm_set_envvar_list(crm_alert_entry_t *entry)
Definition: alerts.c:173
void freeXpathObject(xmlXPathObjectPtr xpathObj)
Definition: xpath.c:45
void crm_free_alert_entry(crm_alert_entry_t *entry)
Definition: alerts.c:75
#define CRM_ALERT_INTERNAL_KEY_MAX