pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pe_health.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2024 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 
23 void
25 {
26  switch (pe__health_strategy(scheduler)) {
28  pcmk__score_red = 0;
31  break;
32 
37  break;
38 
43  break;
44 
45  default: // progressive or custom
46  pcmk__score_red = pe__health_score(PCMK_OPT_NODE_HEALTH_RED,
47  scheduler);
49  scheduler);
51  scheduler);
52  break;
53  }
54 
55  if ((pcmk__score_red != 0) || (pcmk__score_yellow != 0)
56  || (pcmk__score_green != 0)) {
57  crm_debug("Values of node health scores: "
58  PCMK_VALUE_RED "=%d "
59  PCMK_VALUE_YELLOW "=%d "
60  PCMK_VALUE_GREEN "=%d",
62  }
63 }
64 
74 static void
75 add_node_health_value(gpointer key, gpointer value, gpointer user_data)
76 {
77  if (pcmk__starts_with((const char *) key, "#health")) {
78  int score = char2score((const char *) value);
79  int *health = (int *) user_data;
80 
81  *health = pcmk__add_scores(score, *health);
82  crm_trace("Combined '%s' into node health score (now %s)",
83  (const char *) value, pcmk_readable_score(*health));
84  }
85 }
86 
96 int
97 pe__sum_node_health_scores(const pcmk_node_t *node, int base_health)
98 {
99  CRM_ASSERT(node != NULL);
100  g_hash_table_foreach(node->details->attrs, add_node_health_value,
101  &base_health);
102  return base_health;
103 }
104 
114 int
116 {
117  GHashTableIter iter;
118  const char *name = NULL;
119  const char *value = NULL;
120  enum pcmk__health_strategy strategy;
121  int score = 0;
122  int rc = 1;
123 
124  CRM_ASSERT(node != NULL);
125 
126  strategy = pe__health_strategy(node->details->data_set);
127  if (strategy == pcmk__health_strategy_none) {
128  return rc;
129  }
130 
131  g_hash_table_iter_init(&iter, node->details->attrs);
132  while (g_hash_table_iter_next(&iter, (gpointer *) &name,
133  (gpointer *) &value)) {
134  if (pcmk__starts_with(name, "#health")) {
135  /* It's possible that pcmk__score_red equals pcmk__score_yellow,
136  * or pcmk__score_yellow equals pcmk__score_green, so check the
137  * textual value first to be able to distinguish those.
138  */
139  if (pcmk__str_eq(value, PCMK_VALUE_RED, pcmk__str_casei)) {
140  return -1;
141  } else if (pcmk__str_eq(value, PCMK_VALUE_YELLOW,
142  pcmk__str_casei)) {
143  rc = 0;
144  continue;
145  }
146 
147  // The value is an integer, so compare numerically
148  score = char2score(value);
149  if (score <= pcmk__score_red) {
150  return -1;
151  } else if ((score <= pcmk__score_yellow)
153  rc = 0;
154  }
155  }
156  }
157  return rc;
158 }
GHashTable * attrs
Definition: nodes.h:142
#define PCMK_VALUE_GREEN
Definition: options.h:157
#define PCMK_OPT_NODE_HEALTH_GREEN
Definition: options.h:49
const char * name
Definition: cib.c:26
const char * pcmk_readable_score(int score)
Return a displayable static string for a score value.
Definition: scores.c:86
#define PCMK_OPT_NODE_HEALTH_RED
Definition: options.h:50
int pe__node_health(pcmk_node_t *node)
Definition: pe_health.c:115
pcmk_scheduler_t * data_set
Definition: nodes.h:153
#define PCMK_VALUE_RED
Definition: options.h:197
#define crm_debug(fmt, args...)
Definition: logging.h:402
int char2score(const char *score)
Get the integer value of a score string.
Definition: scores.c:36
#define crm_trace(fmt, args...)
Definition: logging.h:404
struct pe_node_shared_s * details
Definition: nodes.h:167
int pcmk__add_scores(int score1, int score2)
Definition: scores.c:116
#define PCMK_OPT_NODE_HEALTH_YELLOW
Definition: options.h:52
int pcmk__score_green
Definition: scores.c:21
Cluster status and scheduling.
int pcmk__score_red
Definition: scores.c:20
void pe__unpack_node_health_scores(pcmk_scheduler_t *scheduler)
Definition: pe_health.c:24
pcmk_scheduler_t * scheduler
#define CRM_ASSERT(expr)
Definition: results.h:42
int pcmk__score_yellow
Definition: scores.c:22
pcmk__health_strategy
bool pcmk__starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
Definition: strings.c:556
#define PCMK_VALUE_YELLOW
Definition: options.h:221
#define PCMK_SCORE_INFINITY
Integer score to use to represent "infinity".
Definition: scores.h:24
int pe__sum_node_health_scores(const pcmk_node_t *node, int base_health)
Definition: pe_health.c:97