pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
pcmk_status.c
Go to the documentation of this file.
1/*
2 * Copyright 2004-2025 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 General Public License version 2
7 * or later (GPLv2+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11
12#include <stdbool.h>
13#include <stdint.h>
14
15#include <crm/cib/internal.h>
16#include <crm/common/output.h>
17#include <crm/common/results.h>
20#include <crm/stonith-ng.h> // stonith__register_messages()
21#include <pacemaker.h>
22#include <pacemaker-internal.h>
23
24static stonith_t *
25fencing_connect(void)
26{
28 int rc = pcmk_rc_ok;
29
30 if (st == NULL) {
31 return NULL;
32 }
33
34 rc = st->cmds->connect(st, crm_system_name, NULL);
35 if (rc == pcmk_rc_ok) {
36 return st;
37 } else {
39 return NULL;
40 }
41}
42
67int
69 cib_t *cib, xmlNode *current_cib,
70 enum pcmk_pacemakerd_state pcmkd_state,
71 enum pcmk__fence_history fence_history,
72 uint32_t show, uint32_t show_opts,
73 const char *only_node, const char *only_rsc,
74 const char *neg_location_prefix)
75{
76 xmlNode *cib_copy = pcmk__xml_copy(NULL, current_cib);
77 stonith_history_t *stonith_history = NULL;
78 int history_rc = 0;
79 GList *unames = NULL;
80 GList *resources = NULL;
81 pcmk__output_t *out = NULL;
82
83 int rc = pcmk_rc_ok;
84
85 if ((scheduler == NULL) || (scheduler->priv->out == NULL)) {
86 return EINVAL;
87 }
88 out = scheduler->priv->out;
89
90 rc = pcmk__update_configured_schema(&cib_copy, false);
91 if (rc != pcmk_rc_ok) {
93 pcmk__xml_free(cib_copy);
94 out->err(out, "Upgrade failed: %s", pcmk_rc_str(rc));
95 return rc;
96 }
97
98 /* get the stonith-history if there is evidence we need it */
99 if (fence_history != pcmk__fence_history_none) {
100 history_rc = pcmk__get_fencing_history(stonith, &stonith_history,
101 fence_history);
102 }
103
105 scheduler->input = cib_copy;
107
108 /* Unpack constraints if any section will need them
109 * (tickets may be referenced in constraints but not granted yet,
110 * and bans need negative location constraints) */
114 }
115
116 unames = pe__build_node_name_list(scheduler, only_node);
117 resources = pe__build_rsc_list(scheduler, only_rsc);
118
119 /* Always print DC if NULL. */
120 if (scheduler->dc_node == NULL) {
121 show |= pcmk_section_dc;
122 }
123
124 out->message(out, "cluster-status",
125 scheduler, pcmkd_state, pcmk_rc2exitc(history_rc),
126 stonith_history, fence_history, show, show_opts,
127 neg_location_prefix, unames, resources);
128
129 g_list_free_full(unames, free);
130 g_list_free_full(resources, free);
131
132 stonith__history_free(stonith_history);
133 stonith_history = NULL;
134 return rc;
135}
136
137int
138pcmk_status(xmlNodePtr *xml)
139{
140 cib_t *cib = NULL;
141 pcmk__output_t *out = NULL;
142 int rc = pcmk_rc_ok;
143
144 uint32_t show_opts = pcmk_show_pending
147
148 cib = cib_new();
149
150 if (cib == NULL) {
151 return pcmk_rc_cib_corrupt;
152 }
153
154 rc = pcmk__xml_output_new(&out, xml);
155 if (rc != pcmk_rc_ok) {
156 cib_delete(cib);
157 return rc;
158 }
159
163
165 show_opts, NULL, NULL, NULL, 0);
167
168 cib_delete(cib);
169 return rc;
170}
171
204int
206 enum pcmk__fence_history fence_history, uint32_t show,
207 uint32_t show_opts, const char *only_node, const char *only_rsc,
208 const char *neg_location_prefix, unsigned int timeout_ms)
209{
210 xmlNode *current_cib = NULL;
211 int rc = pcmk_rc_ok;
212 stonith_t *stonith = NULL;
214 time_t last_updated = 0;
216
217 if (cib == NULL) {
218 return ENOTCONN;
219 }
220
221 if (cib->variant == cib_native) {
222 rc = pcmk__pacemakerd_status(out, crm_system_name, timeout_ms, false,
223 &pcmkd_state);
224 if (rc != pcmk_rc_ok) {
225 return rc;
226 }
227
228 last_updated = time(NULL);
229
230 switch (pcmkd_state) {
234 /* Fencer and CIB may still be available while shutting down or
235 * running on a Pacemaker Remote node
236 */
237 break;
238 default:
239 // Fencer and CIB are definitely unavailable
240 out->message(out, "pacemakerd-health",
241 NULL, pcmkd_state, NULL, last_updated);
242 return rc;
243 }
244
245 if (fence_history != pcmk__fence_history_none) {
246 stonith = fencing_connect();
247 }
248 }
249
250 rc = cib__signon_query(out, &cib, &current_cib);
251 if (rc != pcmk_rc_ok) {
252 if (pcmkd_state != pcmk_pacemakerd_state_invalid) {
253 // Invalid at this point means we didn't query the pcmkd state
254 out->message(out, "pacemakerd-health",
255 NULL, pcmkd_state, NULL, last_updated);
256 }
257 goto done;
258 }
259
262 scheduler->priv->out = out;
263
264 if ((cib->variant == cib_native) && pcmk_is_set(show, pcmk_section_times)) {
265 // Currently used only in the times section
266 pcmk__query_node_name(out, 0, &(scheduler->priv->local_node_name), 0);
267 }
268
269 rc = pcmk__output_cluster_status(scheduler, stonith, cib, current_cib,
270 pcmkd_state, fence_history, show,
271 show_opts, only_node, only_rsc,
272 neg_location_prefix);
273 if (rc != pcmk_rc_ok) {
274 out->err(out, "Error outputting status info from the fencer or CIB");
275 }
276
277done:
279 stonith__api_free(stonith);
280 pcmk__xml_free(current_cib);
281 return pcmk_rc_ok;
282}
int cib__clean_up_connection(cib_t **cib)
Definition cib_utils.c:942
int cib__signon_query(pcmk__output_t *out, cib_t **cib, xmlNode **cib_object)
Definition cib_utils.c:863
void cib_delete(cib_t *cib)
Free all memory used by CIB connection.
Definition cib_client.c:721
cib_t * cib_new(void)
Create a new CIB connection object.
Definition cib_client.c:562
@ cib_native
Definition cib_types.h:30
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
Definition util.h:80
char * crm_system_name
Definition utils.c:45
void stonith__api_free(stonith_t *stonith_api)
Definition st_client.c:1939
void stonith__history_free(stonith_history_t *head)
Definition st_client.c:776
stonith_t * stonith__api_new(void)
Definition st_client.c:1866
void stonith__register_messages(pcmk__output_t *out)
Definition st_output.c:623
pcmk_pacemakerd_state
@ pcmk_pacemakerd_state_invalid
@ pcmk_pacemakerd_state_running
@ pcmk_pacemakerd_state_shutting_down
@ pcmk_pacemakerd_state_remote
pcmk_scheduler_t * scheduler
Control output from tools.
@ pcmk_show_timing
Definition output.h:62
@ pcmk_show_pending
Definition output.h:65
@ pcmk_show_inactive_rscs
Definition output.h:63
#define pcmk_section_all
Definition output.h:49
@ pcmk_section_times
Definition output.h:29
@ pcmk_section_bans
Definition output.h:41
@ pcmk_section_tickets
Definition output.h:40
@ pcmk_section_dc
Definition output.h:28
void pcmk__xml_output_finish(pcmk__output_t *out, crm_exit_t exit_status, xmlNodePtr *xml)
Definition output.c:273
int pcmk__xml_output_new(pcmk__output_t **out, xmlNodePtr *xml)
Definition output.c:246
High Level API.
stonith_t * st
Definition pcmk_fence.c:30
int pcmk__status(pcmk__output_t *out, cib_t *cib, enum pcmk__fence_history fence_history, uint32_t show, uint32_t show_opts, const char *only_node, const char *only_rsc, const char *neg_location_prefix, unsigned int timeout_ms)
int pcmk_status(xmlNodePtr *xml)
Output cluster status formatted like crm_mon --output-as=xml
int pcmk__output_cluster_status(pcmk_scheduler_t *scheduler, stonith_t *stonith, cib_t *cib, xmlNode *current_cib, enum pcmk_pacemakerd_state pcmkd_state, enum pcmk__fence_history fence_history, uint32_t show, uint32_t show_opts, const char *only_node, const char *only_rsc, const char *neg_location_prefix)
Definition pcmk_status.c:68
int pcmk__pacemakerd_status(pcmk__output_t *out, const char *ipc_name, unsigned int message_timeout_ms, bool show_output, enum pcmk_pacemakerd_state *state)
int pcmk__get_fencing_history(stonith_t *st, stonith_history_t **stonith_history, enum pcmk__fence_history fence_history)
Fetch fencing history, optionally reducing it.
Definition pcmk_fence.c:653
pcmk__fence_history
Control how much of the fencing history is output.
Definition pcmki_fence.h:22
@ pcmk__fence_history_none
Definition pcmki_fence.h:23
@ pcmk__fence_history_full
Definition pcmki_fence.h:25
void pcmk__register_lib_messages(pcmk__output_t *out)
void pcmk__unpack_constraints(pcmk_scheduler_t *scheduler)
GList * pe__build_rsc_list(pcmk_scheduler_t *scheduler, const char *s)
Definition utils.c:844
void pe__register_messages(pcmk__output_t *out)
Definition pe_output.c:3482
GList * pe__build_node_name_list(pcmk_scheduler_t *scheduler, const char *s)
Definition utils.c:811
Function and executable result codes.
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
Definition results.c:617
@ pcmk_rc_ok
Definition results.h:159
@ pcmk_rc_cib_corrupt
Definition results.h:147
crm_exit_t pcmk_rc2exitc(int rc)
Map a function return code to the most similar exit code.
Definition results.c:820
#define pcmk__mem_assert(ptr)
void pcmk_free_scheduler(pcmk_scheduler_t *scheduler)
Free scheduler data.
Definition scheduler.c:193
pcmk_scheduler_t * pcmk_new_scheduler(void)
Create a new object to hold scheduler data.
Definition scheduler.c:32
void pcmk_reset_scheduler(pcmk_scheduler_t *scheduler)
Reset scheduler data to defaults.
Definition scheduler.c:87
int pcmk__update_configured_schema(xmlNode **xml, bool to_logs)
Update XML from its configured schema to the latest major series.
Definition schemas.c:1257
gboolean cluster_status(pcmk_scheduler_t *scheduler)
Definition status.c:73
Fencing aka. STONITH.
enum cib_variant variant
Definition cib_types.h:315
This structure contains everything that makes up a single output formatter.
int(* message)(pcmk__output_t *out, const char *message_id,...)
int int void(* err)(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
pcmk_node_t * dc_node
Definition scheduler.h:85
pcmk__scheduler_private_t * priv
Definition scheduler.h:99
xmlNode * input
Definition scheduler.h:81
int(* connect)(stonith_t *st, const char *name, int *stonith_fd)
Connect to the local fencer.
Definition stonith-ng.h:250
Fencing history entry.
Definition stonith-ng.h:173
Fencer API connection object.
Definition stonith-ng.h:657
stonith_api_operations_t * cmds
Definition stonith-ng.h:661
xmlNode * pcmk__xml_copy(xmlNode *parent, xmlNode *src)
Definition xml.c:832
void pcmk__xml_free(xmlNode *xml)
Definition xml.c:816