pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
pe_health.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2023 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 <crm/pengine/status.h>
13 #include <crm/pengine/internal.h>
14 #include "pe_status_private.h"
15 
22 void
24 {
25  switch (pe__health_strategy(scheduler)) {
27  pcmk__score_red = 0;
30  break;
31 
36  break;
37 
42  break;
43 
44  default: // progressive or custom
45  pcmk__score_red = pe__health_score(PCMK__OPT_NODE_HEALTH_RED,
46  scheduler);
48  scheduler);
50  scheduler);
51  break;
52  }
53 
54  if ((pcmk__score_red != 0) || (pcmk__score_yellow != 0)
55  || (pcmk__score_green != 0)) {
56  crm_debug("Values of node health scores: "
57  PCMK__VALUE_RED "=%d "
58  PCMK__VALUE_YELLOW "=%d "
59  PCMK__VALUE_GREEN "=%d",
61  }
62 }
63 
73 static void
74 add_node_health_value(gpointer key, gpointer value, gpointer user_data)
75 {
76  if (pcmk__starts_with((const char *) key, "#health")) {
77  int score = char2score((const char *) value);
78  int *health = (int *) user_data;
79 
80  *health = pcmk__add_scores(score, *health);
81  crm_trace("Combined '%s' into node health score (now %s)",
82  (const char *) value, pcmk_readable_score(*health));
83  }
84 }
85 
95 int
96 pe__sum_node_health_scores(const pcmk_node_t *node, int base_health)
97 {
98  CRM_ASSERT(node != NULL);
99  g_hash_table_foreach(node->details->attrs, add_node_health_value,
100  &base_health);
101  return base_health;
102 }
103 
113 int
115 {
116  GHashTableIter iter;
117  const char *name = NULL;
118  const char *value = NULL;
119  enum pcmk__health_strategy strategy;
120  int score = 0;
121  int rc = 1;
122 
123  CRM_ASSERT(node != NULL);
124 
125  strategy = pe__health_strategy(node->details->data_set);
126  if (strategy == pcmk__health_strategy_none) {
127  return rc;
128  }
129 
130  g_hash_table_iter_init(&iter, node->details->attrs);
131  while (g_hash_table_iter_next(&iter, (gpointer *) &name,
132  (gpointer *) &value)) {
133  if (pcmk__starts_with(name, "#health")) {
134  /* It's possible that pcmk__score_red equals pcmk__score_yellow,
135  * or pcmk__score_yellow equals pcmk__score_green, so check the
136  * textual value first to be able to distinguish those.
137  */
138  if (pcmk__str_eq(value, PCMK__VALUE_RED, pcmk__str_casei)) {
139  return -1;
140  } else if (pcmk__str_eq(value, PCMK__VALUE_YELLOW,
141  pcmk__str_casei)) {
142  rc = 0;
143  continue;
144  }
145 
146  // The value is an integer, so compare numerically
147  score = char2score(value);
148  if (score <= pcmk__score_red) {
149  return -1;
150  } else if ((score <= pcmk__score_yellow)
152  rc = 0;
153  }
154  }
155  }
156  return rc;
157 }
GHashTable * attrs
Node attributes.
Definition: nodes.h:115
#define INFINITY
Definition: crm.h:98
const char * pcmk_readable_score(int score)
Return a displayable static string for a score value.
Definition: scores.c:86
#define PCMK__VALUE_GREEN
const char * name
Definition: cib.c:26
int char2score(const char *score)
Get the integer value of a score string.
Definition: scores.c:36
int pe__node_health(pcmk_node_t *node)
Definition: pe_health.c:114
pcmk_scheduler_t * data_set
Cluster that node is part of.
Definition: nodes.h:126
Implementation of pcmk_scheduler_t.
Definition: scheduler.h:172
#define PCMK__VALUE_RED
#define crm_debug(fmt, args...)
Definition: logging.h:386
#define PCMK__OPT_NODE_HEALTH_RED
#define crm_trace(fmt, args...)
Definition: logging.h:387
struct pe_node_shared_s * details
Basic node information.
Definition: nodes.h:134
#define PCMK__OPT_NODE_HEALTH_GREEN
Implementation of pcmk_node_t.
Definition: nodes.h:130
int pcmk__score_green
Definition: scores.c:21
Cluster status and scheduling.
#define PCMK__OPT_NODE_HEALTH_YELLOW
int pcmk__score_red
Definition: scores.c:20
void pe__unpack_node_health_scores(pcmk_scheduler_t *scheduler)
Definition: pe_health.c:23
pcmk_scheduler_t * scheduler
#define CRM_ASSERT(expr)
Definition: results.h:42
int pcmk__score_yellow
Definition: scores.c:22
pcmk__health_strategy
#define PCMK__VALUE_YELLOW
int pcmk__add_scores(int score1, int score2)
Definition: scores.c:116
bool pcmk__starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
Definition: strings.c:481
int pe__sum_node_health_scores(const pcmk_node_t *node, int base_health)
Definition: pe_health.c:96