pacemaker  2.0.4-2deceaa
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lrmd_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 
12 #include <glib.h>
13 #include <unistd.h>
14 
15 #include <crm/crm.h>
16 #include <crm/msg_xml.h>
17 #include <crm/services.h>
18 #include <crm/common/mainloop.h>
21 #include <crm/lrmd_internal.h>
22 
23 #include <crm/pengine/status.h>
24 #include <crm/cib.h>
25 #include <crm/lrmd.h>
26 
27 static lrmd_key_value_t *
28 alert_key2param(lrmd_key_value_t *head, enum pcmk__alert_keys_e name,
29  const char *value)
30 {
31  const char **key;
32 
33  if (value == NULL) {
34  value = "";
35  }
36  for (key = pcmk__alert_keys[name]; *key; key++) {
37  crm_trace("Setting alert key %s = '%s'", *key, value);
38  head = lrmd_key_value_add(head, *key, value);
39  }
40  return head;
41 }
42 
43 static lrmd_key_value_t *
44 alert_key2param_int(lrmd_key_value_t *head, enum pcmk__alert_keys_e name,
45  int value)
46 {
47  char *value_s = crm_itoa(value);
48 
49  head = alert_key2param(head, name, value_s);
50  free(value_s);
51  return head;
52 }
53 
54 static lrmd_key_value_t *
55 alert_key2param_ms(lrmd_key_value_t *head, enum pcmk__alert_keys_e name,
56  guint value)
57 {
58  char *value_s = crm_strdup_printf("%u", value);
59 
60  head = alert_key2param(head, name, value_s);
61  free(value_s);
62  return head;
63 }
64 
65 static void
66 set_ev_kv(gpointer key, gpointer value, gpointer user_data)
67 {
68  lrmd_key_value_t **head = (lrmd_key_value_t **) user_data;
69 
70  if (value) {
71  crm_trace("Setting environment variable %s='%s'",
72  (char*)key, (char*)value);
73  *head = lrmd_key_value_add(*head, key, value);
74  }
75 }
76 
77 static lrmd_key_value_t *
78 alert_envvar2params(lrmd_key_value_t *head, pcmk__alert_t *entry)
79 {
80  if (entry->envvars) {
81  g_hash_table_foreach(entry->envvars, set_ev_kv, &head);
82  }
83  return head;
84 }
85 
86 /*
87  * We could use g_strv_contains() instead of this function,
88  * but that has only been available since glib 2.43.2.
89  */
90 static gboolean
91 is_target_alert(char **list, const char *value)
92 {
93  int target_list_num = 0;
94  gboolean rc = FALSE;
95 
96  CRM_CHECK(value != NULL, return FALSE);
97 
98  if (list == NULL) {
99  return TRUE;
100  }
101 
102  target_list_num = g_strv_length(list);
103 
104  for (int cnt = 0; cnt < target_list_num; cnt++) {
105  if (strcmp(list[cnt], value) == 0) {
106  rc = TRUE;
107  break;
108  }
109  }
110  return rc;
111 }
112 
127 static int
128 exec_alert_list(lrmd_t *lrmd, GList *alert_list, enum pcmk__alert_flags kind,
129  const char *attr_name, lrmd_key_value_t *params)
130 {
131  bool any_success = FALSE, any_failure = FALSE;
132  const char *kind_s = pcmk__alert_flag2text(kind);
133  pcmk__time_hr_t *now = NULL;
134  struct timeval tv_now;
135  char timestamp_epoch[20];
136  char timestamp_usec[7];
137 
138  params = alert_key2param(params, PCMK__alert_key_kind, kind_s);
139  params = alert_key2param(params, PCMK__alert_key_version, VERSION);
140 
141  for (GList *iter = g_list_first(alert_list); iter; iter = g_list_next(iter)) {
142  pcmk__alert_t *entry = (pcmk__alert_t *)(iter->data);
143  lrmd_key_value_t *copy_params = NULL;
144  lrmd_key_value_t *head = NULL;
145  int rc;
146 
147  if (is_not_set(entry->flags, kind)) {
148  crm_trace("Filtering unwanted %s alert to %s via %s",
149  kind_s, entry->recipient, entry->id);
150  continue;
151  }
152 
153  if ((kind == pcmk__alert_attribute)
154  && !is_target_alert(entry->select_attribute_name, attr_name)) {
155 
156  crm_trace("Filtering unwanted attribute '%s' alert to %s via %s",
157  attr_name, entry->recipient, entry->id);
158  continue;
159  }
160 
161  if (now == NULL) {
162  if (gettimeofday(&tv_now, NULL) == 0) {
163  now = pcmk__time_timeval_hr_convert(NULL, &tv_now);
164  }
165  }
166  crm_info("Sending %s alert via %s to %s",
167  kind_s, entry->id, entry->recipient);
168 
169  /* Make a copy of the parameters, because each alert will be unique */
170  for (head = params; head != NULL; head = head->next) {
171  copy_params = lrmd_key_value_add(copy_params, head->key, head->value);
172  }
173 
174  copy_params = alert_key2param(copy_params, PCMK__alert_key_recipient,
175  entry->recipient);
176 
177  if (now) {
178  char *timestamp = pcmk__time_format_hr(entry->tstamp_format, now);
179 
180  if (timestamp) {
181  copy_params = alert_key2param(copy_params,
183  timestamp);
184  free(timestamp);
185  }
186 
187  snprintf(timestamp_epoch, sizeof(timestamp_epoch), "%lld",
188  (long long) tv_now.tv_sec);
189  copy_params = alert_key2param(copy_params,
191  timestamp_epoch);
192  snprintf(timestamp_usec, sizeof(timestamp_usec), "%06d", now->useconds);
193  copy_params = alert_key2param(copy_params,
195  timestamp_usec);
196  }
197 
198  copy_params = alert_envvar2params(copy_params, entry);
199 
200  rc = lrmd->cmds->exec_alert(lrmd, entry->id, entry->path,
201  entry->timeout, copy_params);
202  if (rc < 0) {
203  crm_err("Could not execute alert %s: %s " CRM_XS " rc=%d",
204  entry->id, pcmk_strerror(rc), rc);
205  any_failure = TRUE;
206  } else {
207  any_success = TRUE;
208  }
209  }
210 
211  if (now) {
212  free(now);
213  }
214 
215  if (any_failure) {
216  return (any_success? -1 : -2);
217  }
218  return pcmk_ok;
219 }
220 
236 int
237 lrmd_send_attribute_alert(lrmd_t *lrmd, GList *alert_list,
238  const char *node, uint32_t nodeid,
239  const char *attr_name, const char *attr_value)
240 {
241  int rc = pcmk_ok;
242  lrmd_key_value_t *params = NULL;
243 
244  if (lrmd == NULL) {
245  return -2;
246  }
247 
248  params = alert_key2param(params, PCMK__alert_key_node, node);
249  params = alert_key2param_int(params, PCMK__alert_key_nodeid, nodeid);
250  params = alert_key2param(params, PCMK__alert_key_attribute_name, attr_name);
251  params = alert_key2param(params, PCMK__alert_key_attribute_value,
252  attr_value);
253 
254  rc = exec_alert_list(lrmd, alert_list, pcmk__alert_attribute, attr_name,
255  params);
256  lrmd_key_value_freeall(params);
257  return rc;
258 }
259 
274 int
275 lrmd_send_node_alert(lrmd_t *lrmd, GList *alert_list,
276  const char *node, uint32_t nodeid, const char *state)
277 {
278  int rc = pcmk_ok;
279  lrmd_key_value_t *params = NULL;
280 
281  if (lrmd == NULL) {
282  return -2;
283  }
284 
285  params = alert_key2param(params, PCMK__alert_key_node, node);
286  params = alert_key2param(params, PCMK__alert_key_desc, state);
287  params = alert_key2param_int(params, PCMK__alert_key_nodeid, nodeid);
288 
289  rc = exec_alert_list(lrmd, alert_list, pcmk__alert_node, NULL, params);
290  lrmd_key_value_freeall(params);
291  return rc;
292 }
293 
309 int
310 lrmd_send_fencing_alert(lrmd_t *lrmd, GList *alert_list,
311  const char *target, const char *task, const char *desc,
312  int op_rc)
313 {
314  int rc = pcmk_ok;
315  lrmd_key_value_t *params = NULL;
316 
317  if (lrmd == NULL) {
318  return -2;
319  }
320 
321  params = alert_key2param(params, PCMK__alert_key_node, target);
322  params = alert_key2param(params, PCMK__alert_key_task, task);
323  params = alert_key2param(params, PCMK__alert_key_desc, desc);
324  params = alert_key2param_int(params, PCMK__alert_key_rc, op_rc);
325 
326  rc = exec_alert_list(lrmd, alert_list, pcmk__alert_fencing, NULL, params);
327  lrmd_key_value_freeall(params);
328  return rc;
329 }
330 
344 int
345 lrmd_send_resource_alert(lrmd_t *lrmd, GList *alert_list,
346  const char *node, lrmd_event_data_t *op)
347 {
348  int rc = pcmk_ok;
349  int target_rc = pcmk_ok;
350  lrmd_key_value_t *params = NULL;
351 
352  if (lrmd == NULL) {
353  return -2;
354  }
355 
356  target_rc = rsc_op_expected_rc(op);
357  if ((op->interval_ms == 0) && (target_rc == op->rc)
358  && safe_str_eq(op->op_type, RSC_STATUS)) {
359 
360  /* Don't send alerts for probes with the expected result. Leave it up to
361  * the agent whether to alert for 'failed' probes. (Even if we find a
362  * resource running, it was probably because someone did a clean-up of
363  * the status section.)
364  */
365  return pcmk_ok;
366  }
367 
368  params = alert_key2param(params, PCMK__alert_key_node, node);
369  params = alert_key2param(params, PCMK__alert_key_rsc, op->rsc_id);
370  params = alert_key2param(params, PCMK__alert_key_task, op->op_type);
371  params = alert_key2param_ms(params, PCMK__alert_key_interval,
372  op->interval_ms);
373  params = alert_key2param_int(params, PCMK__alert_key_target_rc, target_rc);
374  params = alert_key2param_int(params, PCMK__alert_key_status, op->op_status);
375  params = alert_key2param_int(params, PCMK__alert_key_rc, op->rc);
376 
377  /* Reoccurring operations do not set exec_time, so on timeout, set it
378  * to the operation timeout since that's closer to the actual value.
379  */
380  if (op->op_status == PCMK_LRM_OP_TIMEOUT && op->exec_time == 0) {
381  params = alert_key2param_int(params, PCMK__alert_key_exec_time,
382  op->timeout);
383  } else {
384  params = alert_key2param_int(params, PCMK__alert_key_exec_time,
385  op->exec_time);
386  }
387 
388  if (op->op_status == PCMK_LRM_OP_DONE) {
389  params = alert_key2param(params, PCMK__alert_key_desc,
390  services_ocf_exitcode_str(op->rc));
391  } else {
392  params = alert_key2param(params, PCMK__alert_key_desc,
393  services_lrm_status_str(op->op_status));
394  }
395 
396  rc = exec_alert_list(lrmd, alert_list, pcmk__alert_resource, NULL, params);
397  lrmd_key_value_freeall(params);
398  return rc;
399 }
Services API.
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:233
A dumping ground.
const char * pcmk_strerror(int rc)
Definition: results.c:55
int lrmd_send_resource_alert(lrmd_t *lrmd, GList *alert_list, const char *node, lrmd_event_data_t *op)
Definition: lrmd_alerts.c:345
const char * rsc_id
Definition: lrmd.h:203
pcmk__time_hr_t * pcmk__time_timeval_hr_convert(pcmk__time_hr_t *target, struct timeval *tv)
Definition: iso8601.c:1591
int rsc_op_expected_rc(lrmd_event_data_t *event)
Definition: operations.c:329
char ** select_attribute_name
#define VERSION
Definition: config.h:619
char * tstamp_format
Resource agent executor.
enum ocf_exitcode rc
Definition: lrmd.h:221
pcmk__alert_keys_e
char * pcmk__time_format_hr(const char *format, pcmk__time_hr_t *hr_dt)
Definition: iso8601.c:1631
Wrappers for and extensions to glib mainloop.
int(* exec_alert)(lrmd_t *lrmd, const char *alert_id, const char *alert_path, int timeout, lrmd_key_value_t *params)
Execute an alert agent.
Definition: lrmd.h:504
unsigned int exec_time
Definition: lrmd.h:231
const char * pcmk__alert_keys[PCMK__ALERT_INTERNAL_KEY_MAX][3]
Definition: alerts.c:21
int lrmd_send_node_alert(lrmd_t *lrmd, GList *alert_list, const char *node, uint32_t nodeid, const char *state)
Definition: lrmd_alerts.c:275
int rc
Definition: pcmk_fence.c:34
char * key
Definition: lrmd.h:29
int lrmd_send_attribute_alert(lrmd_t *lrmd, GList *alert_list, const char *node, uint32_t nodeid, const char *attr_name, const char *attr_value)
Definition: lrmd_alerts.c:237
#define crm_trace(fmt, args...)
Definition: logging.h:369
lrmd_key_value_t * lrmd_key_value_add(lrmd_key_value_t *kvp, const char *key, const char *value)
Definition: lrmd_client.c:144
struct lrmd_key_value_s * next
Definition: lrmd.h:31
GHashTable * envvars
const char * op_type
Definition: lrmd.h:205
const char * target
Definition: pcmk_fence.c:28
void lrmd_key_value_freeall(lrmd_key_value_t *head)
Definition: lrmd_client.c:167
#define CRM_XS
Definition: logging.h:54
Cluster status and scheduling.
lrmd_api_operations_t * cmds
Definition: lrmd.h:533
#define crm_err(fmt, args...)
Definition: logging.h:363
#define RSC_STATUS
Definition: crm.h:210
Cluster Configuration.
Definition: lrmd.h:532
#define pcmk_ok
Definition: results.h:67
#define safe_str_eq(a, b)
Definition: util.h:65
char * name
Definition: pcmk_fence.c:30
int lrmd_send_fencing_alert(lrmd_t *lrmd, GList *alert_list, const char *target, const char *task, const char *desc, int op_rc)
Definition: lrmd_alerts.c:310
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
pcmk__alert_flags
char * value
Definition: lrmd.h:30
#define crm_info(fmt, args...)
Definition: logging.h:366
guint interval_ms
Definition: lrmd.h:214