diff --git a/fencing/commands.c b/fencing/commands.c
index b592ae661c..00969c8189 100644
--- a/fencing/commands.c
+++ b/fencing/commands.c
@@ -1,2283 +1,2283 @@
/*
* Copyright (C) 2009 Andrew Beekhof
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#if SUPPORT_CIBSECRETS
# include
#endif
#include
GHashTable *device_list = NULL;
GHashTable *topology = NULL;
GList *cmd_list = NULL;
static int active_children = 0;
struct device_search_s {
char *host;
char *action;
int per_device_timeout;
int replies_needed;
int replies_received;
bool allow_suicide;
void *user_data;
void (*callback) (GList * devices, void *user_data);
GListPtr capable;
};
static gboolean stonith_device_dispatch(gpointer user_data);
static void st_child_done(GPid pid, int rc, const char *output, gpointer user_data);
static void stonith_send_reply(xmlNode * reply, int call_options, const char *remote_peer,
const char *client_id);
static void search_devices_record_result(struct device_search_s *search, const char *device,
gboolean can_fence);
typedef struct async_command_s {
int id;
int pid;
int fd_stdout;
int options;
int default_timeout; /* seconds */
int timeout; /* seconds */
int start_delay; /* milliseconds */
int delay_id;
char *op;
char *origin;
char *client;
char *client_name;
char *remote_op_id;
char *victim;
uint32_t victim_nodeid;
char *action;
char *device;
char *mode;
GListPtr device_list;
GListPtr device_next;
void *internal_user_data;
void (*done_cb) (GPid pid, int rc, const char *output, gpointer user_data);
guint timer_sigterm;
guint timer_sigkill;
/*! If the operation timed out, this is the last signal
* we sent to the process to get it to terminate */
int last_timeout_signo;
} async_command_t;
static xmlNode *stonith_construct_async_reply(async_command_t * cmd, const char *output,
xmlNode * data, int rc);
static gboolean
is_action_required(const char *action, stonith_device_t *device)
{
if(device == NULL) {
return FALSE;
} else if (device->required_actions == NULL) {
return FALSE;
} else if (strstr(device->required_actions, action)) {
return TRUE;
}
return FALSE;
}
static int
get_action_delay_max(stonith_device_t * device, const char * action)
{
const char *value = NULL;
int delay_max_ms = 0;
if (safe_str_neq(action, "off") && safe_str_neq(action, "reboot")) {
return 0;
}
value = g_hash_table_lookup(device->params, STONITH_ATTR_DELAY_MAX);
if (value) {
delay_max_ms = crm_get_msec(value);
}
return delay_max_ms;
}
static int
get_action_timeout(stonith_device_t * device, const char *action, int default_timeout)
{
char buffer[512] = { 0, };
char *value = NULL;
CRM_CHECK(action != NULL, return default_timeout);
if (!device->params) {
return default_timeout;
}
snprintf(buffer, sizeof(buffer) - 1, "pcmk_%s_timeout", action);
value = g_hash_table_lookup(device->params, buffer);
if (!value) {
return default_timeout;
}
return atoi(value);
}
static void
free_async_command(async_command_t * cmd)
{
if (!cmd) {
return;
}
if (cmd->delay_id) {
g_source_remove(cmd->delay_id);
}
cmd_list = g_list_remove(cmd_list, cmd);
g_list_free_full(cmd->device_list, free);
free(cmd->device);
free(cmd->action);
free(cmd->victim);
free(cmd->remote_op_id);
free(cmd->client);
free(cmd->client_name);
free(cmd->origin);
free(cmd->mode);
free(cmd->op);
free(cmd);
}
static async_command_t *
create_async_command(xmlNode * msg)
{
async_command_t *cmd = NULL;
xmlNode *op = get_xpath_object("//@" F_STONITH_ACTION, msg, LOG_ERR);
const char *action = crm_element_value(op, F_STONITH_ACTION);
CRM_CHECK(action != NULL, crm_log_xml_warn(msg, "NoAction"); return NULL);
crm_log_xml_trace(msg, "Command");
cmd = calloc(1, sizeof(async_command_t));
crm_element_value_int(msg, F_STONITH_CALLID, &(cmd->id));
crm_element_value_int(msg, F_STONITH_CALLOPTS, &(cmd->options));
crm_element_value_int(msg, F_STONITH_TIMEOUT, &(cmd->default_timeout));
cmd->timeout = cmd->default_timeout;
cmd->origin = crm_element_value_copy(msg, F_ORIG);
cmd->remote_op_id = crm_element_value_copy(msg, F_STONITH_REMOTE_OP_ID);
cmd->client = crm_element_value_copy(msg, F_STONITH_CLIENTID);
cmd->client_name = crm_element_value_copy(msg, F_STONITH_CLIENTNAME);
cmd->op = crm_element_value_copy(msg, F_STONITH_OPERATION);
cmd->action = strdup(action);
cmd->victim = crm_element_value_copy(op, F_STONITH_TARGET);
cmd->mode = crm_element_value_copy(op, F_STONITH_MODE);
cmd->device = crm_element_value_copy(op, F_STONITH_DEVICE);
CRM_CHECK(cmd->op != NULL, crm_log_xml_warn(msg, "NoOp"); free_async_command(cmd); return NULL);
CRM_CHECK(cmd->client != NULL, crm_log_xml_warn(msg, "NoClient"));
cmd->done_cb = st_child_done;
cmd_list = g_list_append(cmd_list, cmd);
return cmd;
}
static gboolean
stonith_device_execute(stonith_device_t * device)
{
int exec_rc = 0;
const char *action_str = NULL;
async_command_t *cmd = NULL;
stonith_action_t *action = NULL;
CRM_CHECK(device != NULL, return FALSE);
if (device->active_pid) {
crm_trace("%s is still active with pid %u", device->id, device->active_pid);
return TRUE;
}
if (device->pending_ops) {
GList *first = device->pending_ops;
cmd = first->data;
if (cmd && cmd->delay_id) {
crm_trace
("Operation %s%s%s on %s was asked to run too early, waiting for start_delay timeout of %dms",
cmd->action, cmd->victim ? " for node " : "", cmd->victim ? cmd->victim : "",
device->id, cmd->start_delay);
return TRUE;
}
device->pending_ops = g_list_remove_link(device->pending_ops, first);
g_list_free_1(first);
}
if (cmd == NULL) {
crm_trace("Nothing further to do for %s", device->id);
return TRUE;
}
if(safe_str_eq(device->agent, STONITH_WATCHDOG_AGENT)) {
if(safe_str_eq(cmd->action, "reboot")) {
pcmk_panic(__FUNCTION__);
return TRUE;
} else if(safe_str_eq(cmd->action, "off")) {
pcmk_panic(__FUNCTION__);
return TRUE;
} else {
crm_info("Faking success for %s watchdog operation", cmd->action);
cmd->done_cb(0, 0, NULL, cmd);
return TRUE;
}
}
#if SUPPORT_CIBSECRETS
if (replace_secret_params(device->id, device->params) < 0) {
/* replacing secrets failed! */
if (safe_str_eq(cmd->action,"stop")) {
/* don't fail on stop! */
crm_info("proceeding with the stop operation for %s", device->id);
} else {
crm_err("failed to get secrets for %s, "
"considering resource not configured", device->id);
exec_rc = PCMK_OCF_NOT_CONFIGURED;
cmd->done_cb(0, exec_rc, NULL, cmd);
return TRUE;
}
}
#endif
action_str = cmd->action;
if (safe_str_eq(cmd->action, "reboot") && is_not_set(device->flags, st_device_supports_reboot)) {
crm_warn("Agent '%s' does not advertise support for 'reboot', performing 'off' action instead", device->agent);
action_str = "off";
}
action = stonith_action_create(device->agent,
action_str,
cmd->victim,
cmd->victim_nodeid,
cmd->timeout, device->params, device->aliases);
/* for async exec, exec_rc is pid if positive and error code if negative/zero */
exec_rc = stonith_action_execute_async(action, (void *)cmd, cmd->done_cb);
if (exec_rc > 0) {
crm_debug("Operation %s%s%s on %s now running with pid=%d, timeout=%ds",
cmd->action, cmd->victim ? " for node " : "", cmd->victim ? cmd->victim : "",
device->id, exec_rc, cmd->timeout);
device->active_pid = exec_rc;
} else {
crm_warn("Operation %s%s%s on %s failed: %s (%d)",
cmd->action, cmd->victim ? " for node " : "", cmd->victim ? cmd->victim : "",
device->id, pcmk_strerror(exec_rc), exec_rc);
cmd->done_cb(0, exec_rc, NULL, cmd);
}
return TRUE;
}
static gboolean
stonith_device_dispatch(gpointer user_data)
{
return stonith_device_execute(user_data);
}
static gboolean
start_delay_helper(gpointer data)
{
async_command_t *cmd = data;
stonith_device_t *device = NULL;
cmd->delay_id = 0;
device = cmd->device ? g_hash_table_lookup(device_list, cmd->device) : NULL;
if (device) {
mainloop_set_trigger(device->work);
}
return FALSE;
}
static void
schedule_stonith_command(async_command_t * cmd, stonith_device_t * device)
{
int delay_max = 0;
CRM_CHECK(cmd != NULL, return);
CRM_CHECK(device != NULL, return);
if (cmd->device) {
free(cmd->device);
}
if (device->include_nodeid && cmd->victim) {
crm_node_t *node = crm_get_peer(0, cmd->victim);
cmd->victim_nodeid = node->id;
}
cmd->device = strdup(device->id);
cmd->timeout = get_action_timeout(device, cmd->action, cmd->default_timeout);
if (cmd->remote_op_id) {
crm_debug("Scheduling %s on %s for remote peer %s with op id (%s) (timeout=%ds)",
cmd->action, device->id, cmd->origin, cmd->remote_op_id, cmd->timeout);
} else {
crm_debug("Scheduling %s on %s for %s (timeout=%ds)",
cmd->action, device->id, cmd->client, cmd->timeout);
}
device->pending_ops = g_list_append(device->pending_ops, cmd);
mainloop_set_trigger(device->work);
delay_max = get_action_delay_max(device, cmd->action);
if (delay_max > 0) {
cmd->start_delay = rand() % delay_max;
crm_notice("Delaying %s on %s for %lldms (timeout=%ds)",
cmd->action, device->id, cmd->start_delay, cmd->timeout);
cmd->delay_id = g_timeout_add(cmd->start_delay, start_delay_helper, cmd);
}
}
void
free_device(gpointer data)
{
GListPtr gIter = NULL;
stonith_device_t *device = data;
g_hash_table_destroy(device->params);
g_hash_table_destroy(device->aliases);
for (gIter = device->pending_ops; gIter != NULL; gIter = gIter->next) {
async_command_t *cmd = gIter->data;
crm_warn("Removal of device '%s' purged operation %s", device->id, cmd->action);
cmd->done_cb(0, -ENODEV, NULL, cmd);
free_async_command(cmd);
}
g_list_free(device->pending_ops);
g_list_free_full(device->targets, free);
mainloop_destroy_trigger(device->work);
free_xml(device->agent_metadata);
free(device->namespace);
free(device->on_target_actions);
free(device->required_actions);
free(device->agent);
free(device->id);
free(device);
}
static GHashTable *
build_port_aliases(const char *hostmap, GListPtr * targets)
{
char *name = NULL;
int last = 0, lpc = 0, max = 0, added = 0;
GHashTable *aliases =
g_hash_table_new_full(crm_strcase_hash, crm_strcase_equal, g_hash_destroy_str, g_hash_destroy_str);
if (hostmap == NULL) {
return aliases;
}
max = strlen(hostmap);
for (; lpc <= max; lpc++) {
switch (hostmap[lpc]) {
/* Assignment chars */
case '=':
case ':':
if (lpc > last) {
free(name);
name = calloc(1, 1 + lpc - last);
memcpy(name, hostmap + last, lpc - last);
}
last = lpc + 1;
break;
/* Delimeter chars */
/* case ',': Potentially used to specify multiple ports */
case 0:
case ';':
case ' ':
case '\t':
if (name) {
char *value = NULL;
value = calloc(1, 1 + lpc - last);
memcpy(value, hostmap + last, lpc - last);
crm_debug("Adding alias '%s'='%s'", name, value);
g_hash_table_replace(aliases, name, value);
if (targets) {
*targets = g_list_append(*targets, strdup(value));
}
value = NULL;
name = NULL;
added++;
} else if (lpc > last) {
crm_debug("Parse error at offset %d near '%s'", lpc - last, hostmap + last);
}
last = lpc + 1;
break;
}
if (hostmap[lpc] == 0) {
break;
}
}
if (added == 0) {
crm_info("No host mappings detected in '%s'", hostmap);
}
free(name);
return aliases;
}
static void
parse_host_line(const char *line, int max, GListPtr * output)
{
int lpc = 0;
int last = 0;
if (max <= 0) {
return;
}
/* Check for any complaints about additional parameters that the device doesn't understand */
if (strstr(line, "invalid") || strstr(line, "variable")) {
crm_debug("Skipping: %s", line);
return;
}
crm_trace("Processing %d bytes: [%s]", max, line);
/* Skip initial whitespace */
for (lpc = 0; lpc <= max && isspace(line[lpc]); lpc++) {
last = lpc + 1;
}
/* Now the actual content */
for (lpc = 0; lpc <= max; lpc++) {
gboolean a_space = isspace(line[lpc]);
if (a_space && lpc < max && isspace(line[lpc + 1])) {
/* fast-forward to the end of the spaces */
- } else if (a_space || line[lpc] == ',' || line[lpc] == 0) {
+ } else if (a_space || line[lpc] == ',' || line[lpc] == ';' || line[lpc] == 0) {
int rc = 1;
char *entry = NULL;
if (lpc != last) {
entry = calloc(1, 1 + lpc - last);
rc = sscanf(line + last, "%[a-zA-Z0-9_-.]", entry);
}
if (entry == NULL) {
/* Skip */
} else if (rc != 1) {
crm_warn("Could not parse (%d %d): %s", last, lpc, line + last);
} else if (safe_str_neq(entry, "on") && safe_str_neq(entry, "off")) {
crm_trace("Adding '%s'", entry);
*output = g_list_append(*output, entry);
entry = NULL;
}
free(entry);
last = lpc + 1;
}
}
}
static GListPtr
parse_host_list(const char *hosts)
{
int lpc = 0;
int max = 0;
int last = 0;
GListPtr output = NULL;
if (hosts == NULL) {
return output;
}
max = strlen(hosts);
for (lpc = 0; lpc <= max; lpc++) {
if (hosts[lpc] == '\n' || hosts[lpc] == 0) {
char *line = NULL;
int len = lpc - last;
if(len > 1) {
line = malloc(1 + len);
}
if(line) {
snprintf(line, 1 + len, "%s", hosts + last);
line[len] = 0; /* Because it might be '\n' */
parse_host_line(line, len, &output);
free(line);
}
last = lpc + 1;
}
}
crm_trace("Parsed %d entries from '%s'", g_list_length(output), hosts);
return output;
}
GHashTable *metadata_cache = NULL;
static xmlNode *
get_agent_metadata(const char *agent)
{
xmlNode *xml = NULL;
char *buffer = NULL;
if(metadata_cache == NULL) {
metadata_cache = g_hash_table_new_full(
crm_str_hash, g_str_equal, g_hash_destroy_str, g_hash_destroy_str);
}
buffer = g_hash_table_lookup(metadata_cache, agent);
if(safe_str_eq(agent, STONITH_WATCHDOG_AGENT)) {
return NULL;
} else if(buffer == NULL) {
stonith_t *st = stonith_api_new();
int rc = st->cmds->metadata(st, st_opt_sync_call, agent, NULL, &buffer, 10);
stonith_api_delete(st);
if (rc || !buffer) {
crm_err("Could not retrieve metadata for fencing agent %s", agent);
return NULL;
}
g_hash_table_replace(metadata_cache, strdup(agent), buffer);
}
xml = string2xml(buffer);
return xml;
}
static gboolean
is_nodeid_required(xmlNode * xml)
{
xmlXPathObjectPtr xpath = NULL;
if (stand_alone) {
return FALSE;
}
if (!xml) {
return FALSE;
}
xpath = xpath_search(xml, "//parameter[@name='nodeid']");
if (numXpathResults(xpath) <= 0) {
freeXpathObject(xpath);
return FALSE;
}
freeXpathObject(xpath);
return TRUE;
}
static char *
add_action(char *actions, const char *action)
{
static size_t len = 256;
int offset = 0;
if (actions == NULL) {
actions = calloc(1, len);
} else {
offset = strlen(actions);
}
if (offset > 0) {
offset += snprintf(actions+offset, len-offset, " ");
}
offset += snprintf(actions+offset, len-offset, "%s", action);
return actions;
}
static void
read_action_metadata(stonith_device_t *device)
{
xmlXPathObjectPtr xpath = NULL;
int max = 0;
int lpc = 0;
if (device->agent_metadata == NULL) {
return;
}
xpath = xpath_search(device->agent_metadata, "//action");
max = numXpathResults(xpath);
if (max <= 0) {
freeXpathObject(xpath);
return;
}
for (lpc = 0; lpc < max; lpc++) {
const char *on_target = NULL;
const char *action = NULL;
const char *automatic = NULL;
const char *required = NULL;
xmlNode *match = getXpathResult(xpath, lpc);
CRM_LOG_ASSERT(match != NULL);
if(match == NULL) { continue; };
on_target = crm_element_value(match, "on_target");
action = crm_element_value(match, "name");
automatic = crm_element_value(match, "automatic");
required = crm_element_value(match, "required");
if(safe_str_eq(action, "list")) {
set_bit(device->flags, st_device_supports_list);
} else if(safe_str_eq(action, "status")) {
set_bit(device->flags, st_device_supports_status);
} else if(safe_str_eq(action, "reboot")) {
set_bit(device->flags, st_device_supports_reboot);
} else if(safe_str_eq(action, "on") && (crm_is_true(automatic))) {
/* this setting implies required=true for unfencing */
required = "true";
}
if (action && crm_is_true(on_target)) {
device->on_target_actions = add_action(device->on_target_actions, action);
}
if (action && crm_is_true(required)) {
device->required_actions = add_action(device->required_actions, action);
}
}
freeXpathObject(xpath);
}
static stonith_device_t *
build_device_from_xml(xmlNode * msg)
{
const char *value = NULL;
xmlNode *dev = get_xpath_object("//" F_STONITH_DEVICE, msg, LOG_ERR);
stonith_device_t *device = NULL;
device = calloc(1, sizeof(stonith_device_t));
device->id = crm_element_value_copy(dev, XML_ATTR_ID);
device->agent = crm_element_value_copy(dev, "agent");
device->namespace = crm_element_value_copy(dev, "namespace");
device->params = xml2list(dev);
value = g_hash_table_lookup(device->params, STONITH_ATTR_HOSTLIST);
if (value) {
device->targets = parse_host_list(value);
}
value = g_hash_table_lookup(device->params, STONITH_ATTR_HOSTMAP);
device->aliases = build_port_aliases(value, &(device->targets));
device->agent_metadata = get_agent_metadata(device->agent);
read_action_metadata(device);
value = g_hash_table_lookup(device->params, "nodeid");
if (!value) {
device->include_nodeid = is_nodeid_required(device->agent_metadata);
}
value = crm_element_value(dev, "rsc_provides");
if (safe_str_eq(value, "unfencing")) {
/* if this agent requires unfencing, 'on' is considered a required action */
device->required_actions = add_action(device->required_actions, "on");
}
if (is_action_required("on", device)) {
crm_info("The fencing device '%s' requires unfencing", device->id);
}
if (device->on_target_actions) {
crm_info("The fencing device '%s' requires actions (%s) to be executed on the target node",
device->id, device->on_target_actions);
}
device->work = mainloop_add_trigger(G_PRIORITY_HIGH, stonith_device_dispatch, device);
/* TODO: Hook up priority */
return device;
}
static const char *
target_list_type(stonith_device_t * dev)
{
const char *check_type = NULL;
check_type = g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTCHECK);
if (check_type == NULL) {
if (g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTLIST)) {
check_type = "static-list";
} else if (g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTMAP)) {
check_type = "static-list";
} else if(is_set(dev->flags, st_device_supports_list)){
check_type = "dynamic-list";
} else if(is_set(dev->flags, st_device_supports_status)){
check_type = "status";
} else {
check_type = "none";
}
}
return check_type;
}
void
schedule_internal_command(const char *origin,
stonith_device_t * device,
const char *action,
const char *victim,
int timeout,
void *internal_user_data,
void (*done_cb) (GPid pid, int rc, const char *output,
gpointer user_data))
{
async_command_t *cmd = NULL;
cmd = calloc(1, sizeof(async_command_t));
cmd->id = -1;
cmd->default_timeout = timeout ? timeout : 60;
cmd->timeout = cmd->default_timeout;
cmd->action = strdup(action);
cmd->victim = victim ? strdup(victim) : NULL;
cmd->device = strdup(device->id);
cmd->origin = strdup(origin);
cmd->client = strdup(crm_system_name);
cmd->client_name = strdup(crm_system_name);
cmd->internal_user_data = internal_user_data;
cmd->done_cb = done_cb; /* cmd, not internal_user_data, is passed to 'done_cb' as the userdata */
schedule_stonith_command(cmd, device);
}
gboolean
string_in_list(GListPtr list, const char *item)
{
int lpc = 0;
int max = g_list_length(list);
for (lpc = 0; lpc < max; lpc++) {
const char *value = g_list_nth_data(list, lpc);
if (safe_str_eq(item, value)) {
return TRUE;
} else {
crm_trace("%d: '%s' != '%s'", lpc, item, value);
}
}
return FALSE;
}
static void
status_search_cb(GPid pid, int rc, const char *output, gpointer user_data)
{
async_command_t *cmd = user_data;
struct device_search_s *search = cmd->internal_user_data;
stonith_device_t *dev = cmd->device ? g_hash_table_lookup(device_list, cmd->device) : NULL;
gboolean can = FALSE;
free_async_command(cmd);
if (!dev) {
search_devices_record_result(search, NULL, FALSE);
return;
}
dev->active_pid = 0;
mainloop_set_trigger(dev->work);
if (rc == 1 /* unkown */ ) {
crm_trace("Host %s is not known by %s", search->host, dev->id);
} else if (rc == 0 /* active */ || rc == 2 /* inactive */ ) {
crm_trace("Host %s is known by %s", search->host, dev->id);
can = TRUE;
} else {
crm_notice("Unkown result when testing if %s can fence %s: rc=%d", dev->id, search->host,
rc);
}
search_devices_record_result(search, dev->id, can);
}
static void
dynamic_list_search_cb(GPid pid, int rc, const char *output, gpointer user_data)
{
async_command_t *cmd = user_data;
struct device_search_s *search = cmd->internal_user_data;
stonith_device_t *dev = cmd->device ? g_hash_table_lookup(device_list, cmd->device) : NULL;
gboolean can_fence = FALSE;
free_async_command(cmd);
/* Host/alias must be in the list output to be eligable to be fenced
*
* Will cause problems if down'd nodes aren't listed or (for virtual nodes)
* if the guest is still listed despite being moved to another machine
*/
if (!dev) {
search_devices_record_result(search, NULL, FALSE);
return;
}
dev->active_pid = 0;
mainloop_set_trigger(dev->work);
/* If we successfully got the targets earlier, don't disable. */
if (rc != 0 && !dev->targets) {
crm_notice("Disabling port list queries for %s (%d): %s", dev->id, rc, output);
/* Fall back to status */
g_hash_table_replace(dev->params, strdup(STONITH_ATTR_HOSTCHECK), strdup("status"));
g_list_free_full(dev->targets, free);
dev->targets = NULL;
} else if (!rc) {
crm_info("Refreshing port list for %s", dev->id);
g_list_free_full(dev->targets, free);
dev->targets = parse_host_list(output);
dev->targets_age = time(NULL);
}
if (dev->targets) {
const char *alias = g_hash_table_lookup(dev->aliases, search->host);
if (!alias) {
alias = search->host;
}
if (string_in_list(dev->targets, alias)) {
can_fence = TRUE;
}
}
search_devices_record_result(search, dev->id, can_fence);
}
/*!
* \internal
* \brief Checks to see if an identical device already exists in the device_list
*/
static stonith_device_t *
device_has_duplicate(stonith_device_t * device)
{
char *key = NULL;
char *value = NULL;
GHashTableIter gIter;
stonith_device_t *dup = g_hash_table_lookup(device_list, device->id);
if (!dup) {
crm_trace("No match for %s", device->id);
return NULL;
} else if (safe_str_neq(dup->agent, device->agent)) {
crm_trace("Different agent: %s != %s", dup->agent, device->agent);
return NULL;
}
/* Use calculate_operation_digest() here? */
g_hash_table_iter_init(&gIter, device->params);
while (g_hash_table_iter_next(&gIter, (void **)&key, (void **)&value)) {
if(strstr(key, "CRM_meta") == key) {
continue;
} else if(strcmp(key, "crm_feature_set") == 0) {
continue;
} else {
char *other_value = g_hash_table_lookup(dup->params, key);
if (!other_value || safe_str_neq(other_value, value)) {
crm_trace("Different value for %s: %s != %s", key, other_value, value);
return NULL;
}
}
}
crm_trace("Match");
return dup;
}
int
stonith_device_register(xmlNode * msg, const char **desc, gboolean from_cib)
{
stonith_device_t *dup = NULL;
stonith_device_t *device = build_device_from_xml(msg);
dup = device_has_duplicate(device);
if (dup) {
crm_debug("Device '%s' already existed in device list (%d active devices)", device->id,
g_hash_table_size(device_list));
free_device(device);
device = dup;
} else {
stonith_device_t *old = g_hash_table_lookup(device_list, device->id);
if (from_cib && old && old->api_registered) {
/* If the cib is writing over an entry that is shared with a stonith client,
* copy any pending ops that currently exist on the old entry to the new one.
* Otherwise the pending ops will be reported as failures
*/
crm_info("Overwriting an existing entry for %s from the cib", device->id);
device->pending_ops = old->pending_ops;
device->api_registered = TRUE;
old->pending_ops = NULL;
if (device->pending_ops) {
mainloop_set_trigger(device->work);
}
}
g_hash_table_replace(device_list, device->id, device);
crm_notice("Added '%s' to the device list (%d active devices)", device->id,
g_hash_table_size(device_list));
}
if (desc) {
*desc = device->id;
}
if (from_cib) {
device->cib_registered = TRUE;
} else {
device->api_registered = TRUE;
}
return pcmk_ok;
}
int
stonith_device_remove(const char *id, gboolean from_cib)
{
stonith_device_t *device = g_hash_table_lookup(device_list, id);
if (!device) {
crm_info("Device '%s' not found (%d active devices)", id, g_hash_table_size(device_list));
return pcmk_ok;
}
if (from_cib) {
device->cib_registered = FALSE;
} else {
device->verified = FALSE;
device->api_registered = FALSE;
}
if (!device->cib_registered && !device->api_registered) {
g_hash_table_remove(device_list, id);
crm_info("Removed '%s' from the device list (%d active devices)",
id, g_hash_table_size(device_list));
}
return pcmk_ok;
}
static int
count_active_levels(stonith_topology_t * tp)
{
int lpc = 0;
int count = 0;
for (lpc = 0; lpc < ST_LEVEL_MAX; lpc++) {
if (tp->levels[lpc] != NULL) {
count++;
}
}
return count;
}
void
free_topology_entry(gpointer data)
{
stonith_topology_t *tp = data;
int lpc = 0;
for (lpc = 0; lpc < ST_LEVEL_MAX; lpc++) {
if (tp->levels[lpc] != NULL) {
g_list_free_full(tp->levels[lpc], free);
}
}
free(tp->node);
free(tp);
}
int
stonith_level_register(xmlNode * msg, char **desc)
{
int id = 0;
int rc = pcmk_ok;
xmlNode *child = NULL;
xmlNode *level = get_xpath_object("//" F_STONITH_LEVEL, msg, LOG_ERR);
const char *node = crm_element_value(level, F_STONITH_TARGET);
stonith_topology_t *tp = g_hash_table_lookup(topology, node);
crm_element_value_int(level, XML_ATTR_ID, &id);
if (desc) {
*desc = crm_strdup_printf("%s[%d]", node, id);
}
if (id <= 0 || id >= ST_LEVEL_MAX) {
return -EINVAL;
}
if (tp == NULL) {
tp = calloc(1, sizeof(stonith_topology_t));
tp->node = strdup(node);
g_hash_table_replace(topology, tp->node, tp);
crm_trace("Added %s to the topology (%d active entries)", node,
g_hash_table_size(topology));
}
if (tp->levels[id] != NULL) {
crm_info("Adding to the existing %s[%d] topology entry (%d active entries)", node, id,
count_active_levels(tp));
}
for (child = __xml_first_child(level); child != NULL; child = __xml_next(child)) {
const char *device = ID(child);
crm_trace("Adding device '%s' for %s (%d)", device, node, id);
tp->levels[id] = g_list_append(tp->levels[id], strdup(device));
}
crm_info("Node %s has %d active fencing levels", node, count_active_levels(tp));
return rc;
}
int
stonith_level_remove(xmlNode * msg, char **desc)
{
int id = 0;
xmlNode *level = get_xpath_object("//" F_STONITH_LEVEL, msg, LOG_ERR);
const char *node = crm_element_value(level, F_STONITH_TARGET);
stonith_topology_t *tp = g_hash_table_lookup(topology, node);
if (desc) {
*desc = crm_strdup_printf("%s[%d]", node, id);
}
crm_element_value_int(level, XML_ATTR_ID, &id);
if (tp == NULL) {
crm_info("Node %s not found (%d active entries)", node, g_hash_table_size(topology));
return pcmk_ok;
} else if (id < 0 || id >= ST_LEVEL_MAX) {
return -EINVAL;
}
if (id == 0 && g_hash_table_remove(topology, node)) {
crm_info("Removed all %s related entries from the topology (%d active entries)",
node, g_hash_table_size(topology));
} else if (id > 0 && tp->levels[id] != NULL) {
g_list_free_full(tp->levels[id], free);
tp->levels[id] = NULL;
crm_info("Removed entry '%d' from %s's topology (%d active entries remaining)",
id, node, count_active_levels(tp));
}
return pcmk_ok;
}
static int
stonith_device_action(xmlNode * msg, char **output)
{
int rc = pcmk_ok;
xmlNode *dev = get_xpath_object("//" F_STONITH_DEVICE, msg, LOG_ERR);
const char *id = crm_element_value(dev, F_STONITH_DEVICE);
async_command_t *cmd = NULL;
stonith_device_t *device = NULL;
if (id) {
crm_trace("Looking for '%s'", id);
device = g_hash_table_lookup(device_list, id);
}
if (device && device->api_registered == FALSE) {
rc = -ENODEV;
} else if (device) {
cmd = create_async_command(msg);
if (cmd == NULL) {
free_device(device);
return -EPROTO;
}
schedule_stonith_command(cmd, device);
rc = -EINPROGRESS;
} else {
crm_info("Device %s not found", id ? id : "");
rc = -ENODEV;
}
return rc;
}
static void
search_devices_record_result(struct device_search_s *search, const char *device, gboolean can_fence)
{
search->replies_received++;
if (can_fence && device) {
search->capable = g_list_append(search->capable, strdup(device));
}
if (search->replies_needed == search->replies_received) {
crm_debug("Finished Search. %d devices can perform action (%s) on node %s",
g_list_length(search->capable),
search->action ? search->action : "",
search->host ? search->host : "");
search->callback(search->capable, search->user_data);
free(search->host);
free(search->action);
free(search);
}
}
static void
can_fence_host_with_device(stonith_device_t * dev, struct device_search_s *search)
{
gboolean can = FALSE;
const char *check_type = NULL;
const char *host = search->host;
const char *alias = NULL;
CRM_LOG_ASSERT(dev != NULL);
if (dev == NULL) {
goto search_report_results;
} else if (host == NULL) {
can = TRUE;
goto search_report_results;
}
if (dev->on_target_actions &&
search->action &&
strstr(dev->on_target_actions, search->action)) {
/* this device can only execute this action on the target node */
if(safe_str_neq(host, stonith_our_uname)) {
crm_trace("%s operation with %s can only be executed for localhost not %s",
search->action, dev->id, host);
goto search_report_results;
}
} else if(safe_str_eq(host, stonith_our_uname) && search->allow_suicide == FALSE) {
crm_trace("%s operation does not support self-fencing", search->action);
goto search_report_results;
}
alias = g_hash_table_lookup(dev->aliases, host);
if (alias == NULL) {
alias = host;
}
check_type = target_list_type(dev);
if (safe_str_eq(check_type, "none")) {
can = TRUE;
} else if (safe_str_eq(check_type, "static-list")) {
/* Presence in the hostmap is sufficient
* Only use if all hosts on which the device can be active can always fence all listed hosts
*/
if (string_in_list(dev->targets, host)) {
can = TRUE;
} else if (g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTMAP)
&& g_hash_table_lookup(dev->aliases, host)) {
can = TRUE;
}
} else if (safe_str_eq(check_type, "dynamic-list")) {
time_t now = time(NULL);
if (dev->targets == NULL || dev->targets_age + 60 < now) {
crm_trace("Running %s command to see if %s can fence %s (%s)",
check_type, dev?dev->id:"N/A", search->host, search->action);
schedule_internal_command(__FUNCTION__, dev, "list", NULL,
search->per_device_timeout, search, dynamic_list_search_cb);
/* we'll respond to this search request async in the cb */
return;
}
if (string_in_list(dev->targets, alias)) {
can = TRUE;
}
} else if (safe_str_eq(check_type, "status")) {
crm_trace("Running %s command to see if %s can fence %s (%s)",
check_type, dev?dev->id:"N/A", search->host, search->action);
schedule_internal_command(__FUNCTION__, dev, "status", search->host,
search->per_device_timeout, search, status_search_cb);
/* we'll respond to this search request async in the cb */
return;
} else {
crm_err("Unknown check type: %s", check_type);
}
if (safe_str_eq(host, alias)) {
crm_notice("%s can%s fence (%s) %s: %s", dev->id, can ? "" : " not", search->action, host, check_type);
} else {
crm_notice("%s can%s fence (%s) %s (aka. '%s'): %s", dev->id, can ? "" : " not", search->action, host, alias,
check_type);
}
search_report_results:
search_devices_record_result(search, dev ? dev->id : NULL, can);
}
static void
search_devices(gpointer key, gpointer value, gpointer user_data)
{
stonith_device_t *dev = value;
struct device_search_s *search = user_data;
can_fence_host_with_device(dev, search);
}
#define DEFAULT_QUERY_TIMEOUT 20
static void
get_capable_devices(const char *host, const char *action, int timeout, bool suicide, void *user_data,
void (*callback) (GList * devices, void *user_data))
{
struct device_search_s *search;
int per_device_timeout = DEFAULT_QUERY_TIMEOUT;
int devices_needing_async_query = 0;
char *key = NULL;
const char *check_type = NULL;
GHashTableIter gIter;
stonith_device_t *device = NULL;
if (!g_hash_table_size(device_list)) {
callback(NULL, user_data);
return;
}
search = calloc(1, sizeof(struct device_search_s));
if (!search) {
callback(NULL, user_data);
return;
}
g_hash_table_iter_init(&gIter, device_list);
while (g_hash_table_iter_next(&gIter, (void **)&key, (void **)&device)) {
check_type = target_list_type(device);
if (safe_str_eq(check_type, "status") || safe_str_eq(check_type, "dynamic-list")) {
devices_needing_async_query++;
}
}
/* If we have devices that require an async event in order to know what
* nodes they can fence, we have to give the events a timeout. The total
* query timeout is divided among those events. */
if (devices_needing_async_query) {
per_device_timeout = timeout / devices_needing_async_query;
if (!per_device_timeout) {
crm_err("stonith-timeout duration %d is too low, raise the duration to %d seconds",
timeout, DEFAULT_QUERY_TIMEOUT * devices_needing_async_query);
per_device_timeout = DEFAULT_QUERY_TIMEOUT;
} else if (per_device_timeout < DEFAULT_QUERY_TIMEOUT) {
crm_notice
("stonith-timeout duration %d is low for the current configuration. Consider raising it to %d seconds",
timeout, DEFAULT_QUERY_TIMEOUT * devices_needing_async_query);
}
}
search->host = host ? strdup(host) : NULL;
search->action = action ? strdup(action) : NULL;
search->per_device_timeout = per_device_timeout;
/* We are guaranteed this many replies. Even if a device gets
* unregistered some how during the async search, we will get
* the correct number of replies. */
search->replies_needed = g_hash_table_size(device_list);
search->allow_suicide = suicide;
search->callback = callback;
search->user_data = user_data;
/* kick off the search */
crm_debug("Searching through %d devices to see what is capable of action (%s) for target %s",
search->replies_needed,
search->action ? search->action : "",
search->host ? search->host : "");
g_hash_table_foreach(device_list, search_devices, search);
}
struct st_query_data {
xmlNode *reply;
char *remote_peer;
char *client_id;
char *target;
char *action;
int call_options;
};
static void
stonith_query_capable_device_cb(GList * devices, void *user_data)
{
struct st_query_data *query = user_data;
int available_devices = 0;
xmlNode *dev = NULL;
xmlNode *list = NULL;
GListPtr lpc = NULL;
/* Pack the results into data */
list = create_xml_node(NULL, __FUNCTION__);
crm_xml_add(list, F_STONITH_TARGET, query->target);
for (lpc = devices; lpc != NULL; lpc = lpc->next) {
stonith_device_t *device = g_hash_table_lookup(device_list, lpc->data);
int action_specific_timeout;
int delay_max;
if (!device) {
/* It is possible the device got unregistered while
* determining who can fence the target */
continue;
}
available_devices++;
action_specific_timeout = get_action_timeout(device, query->action, 0);
dev = create_xml_node(list, F_STONITH_DEVICE);
crm_xml_add(dev, XML_ATTR_ID, device->id);
crm_xml_add(dev, "namespace", device->namespace);
crm_xml_add(dev, "agent", device->agent);
crm_xml_add_int(dev, F_STONITH_DEVICE_VERIFIED, device->verified);
if (is_action_required(query->action, device)) {
crm_xml_add_int(dev, F_STONITH_DEVICE_REQUIRED, 1);
}
if (action_specific_timeout) {
crm_xml_add_int(dev, F_STONITH_ACTION_TIMEOUT, action_specific_timeout);
}
delay_max = get_action_delay_max(device, query->action);
if (delay_max > 0) {
crm_xml_add_int(dev, F_STONITH_DELAY_MAX, delay_max / 1000);
}
if (query->target == NULL) {
xmlNode *attrs = create_xml_node(dev, XML_TAG_ATTRS);
g_hash_table_foreach(device->params, hash2field, attrs);
}
}
crm_xml_add_int(list, "st-available-devices", available_devices);
if (query->target) {
crm_debug("Found %d matching devices for '%s'", available_devices, query->target);
} else {
crm_debug("%d devices installed", available_devices);
}
if (list != NULL) {
crm_trace("Attaching query list output");
add_message_xml(query->reply, F_STONITH_CALLDATA, list);
}
stonith_send_reply(query->reply, query->call_options, query->remote_peer, query->client_id);
free_xml(query->reply);
free(query->remote_peer);
free(query->client_id);
free(query->target);
free(query->action);
free(query);
free_xml(list);
g_list_free_full(devices, free);
}
static void
stonith_query(xmlNode * msg, const char *remote_peer, const char *client_id, int call_options)
{
struct st_query_data *query = NULL;
const char *action = NULL;
const char *target = NULL;
int timeout = 0;
xmlNode *dev = get_xpath_object("//@" F_STONITH_ACTION, msg, LOG_DEBUG_3);
crm_element_value_int(msg, F_STONITH_TIMEOUT, &timeout);
if (dev) {
const char *device = crm_element_value(dev, F_STONITH_DEVICE);
target = crm_element_value(dev, F_STONITH_TARGET);
action = crm_element_value(dev, F_STONITH_ACTION);
if (device && safe_str_eq(device, "manual_ack")) {
/* No query or reply necessary */
return;
}
}
crm_log_xml_debug(msg, "Query");
query = calloc(1, sizeof(struct st_query_data));
query->reply = stonith_construct_reply(msg, NULL, NULL, pcmk_ok);
query->remote_peer = remote_peer ? strdup(remote_peer) : NULL;
query->client_id = client_id ? strdup(client_id) : NULL;
query->target = target ? strdup(target) : NULL;
query->action = action ? strdup(action) : NULL;
query->call_options = call_options;
get_capable_devices(target, action, timeout,
is_set(call_options, st_opt_allow_suicide),
query, stonith_query_capable_device_cb);
}
#define ST_LOG_OUTPUT_MAX 512
static void
log_operation(async_command_t * cmd, int rc, int pid, const char *next, const char *output)
{
if (rc == 0) {
next = NULL;
}
if (cmd->victim != NULL) {
do_crm_log(rc == 0 ? LOG_NOTICE : LOG_ERR,
"Operation '%s' [%d] (call %d from %s) for host '%s' with device '%s' returned: %d (%s)%s%s",
cmd->action, pid, cmd->id, cmd->client_name, cmd->victim, cmd->device, rc,
pcmk_strerror(rc), next ? ". Trying: " : "", next ? next : "");
} else {
do_crm_log_unlikely(rc == 0 ? LOG_DEBUG : LOG_NOTICE,
"Operation '%s' [%d] for device '%s' returned: %d (%s)%s%s",
cmd->action, pid, cmd->device, rc, pcmk_strerror(rc),
next ? ". Trying: " : "", next ? next : "");
}
if (output) {
/* Logging the whole string confuses syslog when the string is xml */
char *prefix = crm_strdup_printf("%s:%d", cmd->device, pid);
crm_log_output(rc == 0 ? LOG_DEBUG : LOG_WARNING, prefix, output);
free(prefix);
}
}
static void
stonith_send_async_reply(async_command_t * cmd, const char *output, int rc, GPid pid)
{
xmlNode *reply = NULL;
gboolean bcast = FALSE;
reply = stonith_construct_async_reply(cmd, output, NULL, rc);
if (safe_str_eq(cmd->action, "metadata")) {
/* Too verbose to log */
crm_trace("Metadata query for %s", cmd->device);
output = NULL;
} else if (crm_str_eq(cmd->action, "monitor", TRUE) ||
crm_str_eq(cmd->action, "list", TRUE) || crm_str_eq(cmd->action, "status", TRUE)) {
crm_trace("Never broadcast %s replies", cmd->action);
} else if (!stand_alone && safe_str_eq(cmd->origin, cmd->victim) && safe_str_neq(cmd->action, "on")) {
crm_trace("Broadcast %s reply for %s", cmd->action, cmd->victim);
crm_xml_add(reply, F_SUBTYPE, "broadcast");
bcast = TRUE;
}
log_operation(cmd, rc, pid, NULL, output);
crm_log_xml_trace(reply, "Reply");
if (bcast) {
crm_xml_add(reply, F_STONITH_OPERATION, T_STONITH_NOTIFY);
send_cluster_message(NULL, crm_msg_stonith_ng, reply, FALSE);
} else if (cmd->origin) {
crm_trace("Directed reply to %s", cmd->origin);
send_cluster_message(crm_get_peer(0, cmd->origin), crm_msg_stonith_ng, reply, FALSE);
} else {
crm_trace("Directed local %ssync reply to %s",
(cmd->options & st_opt_sync_call) ? "" : "a-", cmd->client_name);
do_local_reply(reply, cmd->client, cmd->options & st_opt_sync_call, FALSE);
}
if (stand_alone) {
/* Do notification with a clean data object */
xmlNode *notify_data = create_xml_node(NULL, T_STONITH_NOTIFY_FENCE);
crm_xml_add_int(notify_data, F_STONITH_RC, rc);
crm_xml_add(notify_data, F_STONITH_TARGET, cmd->victim);
crm_xml_add(notify_data, F_STONITH_OPERATION, cmd->op);
crm_xml_add(notify_data, F_STONITH_DELEGATE, "localhost");
crm_xml_add(notify_data, F_STONITH_DEVICE, cmd->device);
crm_xml_add(notify_data, F_STONITH_REMOTE_OP_ID, cmd->remote_op_id);
crm_xml_add(notify_data, F_STONITH_ORIGIN, cmd->client);
do_stonith_notify(0, T_STONITH_NOTIFY_FENCE, rc, notify_data);
}
free_xml(reply);
}
void
unfence_cb(GPid pid, int rc, const char *output, gpointer user_data)
{
async_command_t * cmd = user_data;
stonith_device_t *dev = g_hash_table_lookup(device_list, cmd->device);
log_operation(cmd, rc, pid, NULL, output);
if(dev) {
dev->active_pid = 0;
mainloop_set_trigger(dev->work);
} else {
crm_trace("Device %s does not exist", cmd->device);
}
if(rc != 0) {
crm_exit(DAEMON_RESPAWN_STOP);
}
}
static void
cancel_stonith_command(async_command_t * cmd)
{
stonith_device_t *device;
CRM_CHECK(cmd != NULL, return);
if (!cmd->device) {
return;
}
device = g_hash_table_lookup(device_list, cmd->device);
if (device) {
crm_trace("Cancel scheduled %s on %s", cmd->action, device->id);
device->pending_ops = g_list_remove(device->pending_ops, cmd);
}
}
#define READ_MAX 500
static void
st_child_done(GPid pid, int rc, const char *output, gpointer user_data)
{
stonith_device_t *device = NULL;
stonith_device_t *next_device = NULL;
async_command_t *cmd = user_data;
GListPtr gIter = NULL;
GListPtr gIterNext = NULL;
CRM_CHECK(cmd != NULL, return);
active_children--;
/* The device is ready to do something else now */
device = g_hash_table_lookup(device_list, cmd->device);
if (device) {
device->active_pid = 0;
if (rc == pcmk_ok &&
(safe_str_eq(cmd->action, "list") ||
safe_str_eq(cmd->action, "monitor") || safe_str_eq(cmd->action, "status"))) {
device->verified = TRUE;
}
mainloop_set_trigger(device->work);
}
crm_debug("Operation '%s' on '%s' completed with rc=%d (%d remaining)",
cmd->action, cmd->device, rc, g_list_length(cmd->device_next));
if (rc == 0) {
GListPtr iter;
/* see if there are any required devices left to execute for this op */
for (iter = cmd->device_next; iter != NULL; iter = iter->next) {
next_device = g_hash_table_lookup(device_list, iter->data);
if (next_device != NULL && is_action_required(cmd->action, next_device)) {
cmd->device_next = iter->next;
break;
}
next_device = NULL;
}
} else if (rc != 0 && cmd->device_next && (is_action_required(cmd->action, device) == FALSE)) {
/* if this device didn't work out, see if there are any others we can try.
* if the failed device was 'required', we can't pick another device. */
next_device = g_hash_table_lookup(device_list, cmd->device_next->data);
cmd->device_next = cmd->device_next->next;
}
/* this operation requires more fencing, hooray! */
if (next_device) {
log_operation(cmd, rc, pid, cmd->device, output);
schedule_stonith_command(cmd, next_device);
/* Prevent cmd from being freed */
cmd = NULL;
goto done;
}
if (rc > 0) {
/* Try to provide _something_ useful */
if(output == NULL) {
rc = -ENODATA;
} else if(strstr(output, "imed out")) {
rc = -ETIMEDOUT;
} else if(strstr(output, "Unrecognised action")) {
rc = -EOPNOTSUPP;
} else {
rc = -pcmk_err_generic;
}
}
stonith_send_async_reply(cmd, output, rc, pid);
if (rc != 0) {
goto done;
}
/* Check to see if any operations are scheduled to do the exact
* same thing that just completed. If so, rather than
* performing the same fencing operation twice, return the result
* of this operation for all pending commands it matches. */
for (gIter = cmd_list; gIter != NULL; gIter = gIterNext) {
async_command_t *cmd_other = gIter->data;
gIterNext = gIter->next;
if (cmd == cmd_other) {
continue;
}
/* A pending scheduled command matches the command that just finished if.
* 1. The client connections are different.
* 2. The node victim is the same.
* 3. The fencing action is the same.
* 4. The device scheduled to execute the action is the same.
*/
if (safe_str_eq(cmd->client, cmd_other->client) ||
safe_str_neq(cmd->victim, cmd_other->victim) ||
safe_str_neq(cmd->action, cmd_other->action) ||
safe_str_neq(cmd->device, cmd_other->device)) {
continue;
}
crm_notice
("Merging stonith action %s for node %s originating from client %s with identical stonith request from client %s",
cmd_other->action, cmd_other->victim, cmd_other->client_name, cmd->client_name);
cmd_list = g_list_remove_link(cmd_list, gIter);
stonith_send_async_reply(cmd_other, output, rc, pid);
cancel_stonith_command(cmd_other);
free_async_command(cmd_other);
g_list_free_1(gIter);
}
done:
free_async_command(cmd);
}
static gint
sort_device_priority(gconstpointer a, gconstpointer b)
{
const stonith_device_t *dev_a = a;
const stonith_device_t *dev_b = b;
if (dev_a->priority > dev_b->priority) {
return -1;
} else if (dev_a->priority < dev_b->priority) {
return 1;
}
return 0;
}
static void
stonith_fence_get_devices_cb(GList * devices, void *user_data)
{
async_command_t *cmd = user_data;
stonith_device_t *device = NULL;
crm_info("Found %d matching devices for '%s'", g_list_length(devices), cmd->victim);
if (g_list_length(devices) > 0) {
/* Order based on priority */
devices = g_list_sort(devices, sort_device_priority);
device = g_hash_table_lookup(device_list, devices->data);
if (device) {
cmd->device_list = devices;
cmd->device_next = devices->next;
devices = NULL; /* list owned by cmd now */
}
}
/* we have a device, schedule it for fencing. */
if (device) {
schedule_stonith_command(cmd, device);
/* in progress */
return;
}
/* no device found! */
stonith_send_async_reply(cmd, NULL, -ENODEV, 0);
free_async_command(cmd);
g_list_free_full(devices, free);
}
static int
stonith_fence(xmlNode * msg)
{
const char *device_id = NULL;
stonith_device_t *device = NULL;
async_command_t *cmd = create_async_command(msg);
xmlNode *dev = get_xpath_object("//@" F_STONITH_TARGET, msg, LOG_ERR);
if (cmd == NULL) {
return -EPROTO;
}
device_id = crm_element_value(dev, F_STONITH_DEVICE);
if (device_id) {
device = g_hash_table_lookup(device_list, device_id);
if (device == NULL) {
crm_err("Requested device '%s' is not available", device_id);
return -ENODEV;
}
schedule_stonith_command(cmd, device);
} else {
const char *host = crm_element_value(dev, F_STONITH_TARGET);
if (cmd->options & st_opt_cs_nodeid) {
int nodeid = crm_atoi(host, NULL);
crm_node_t *node = crm_get_peer(nodeid, NULL);
if (node) {
host = node->uname;
}
}
/* If we get to here, then self-fencing is implicitly allowed */
get_capable_devices(host, cmd->action, cmd->default_timeout,
TRUE, cmd, stonith_fence_get_devices_cb);
}
return -EINPROGRESS;
}
xmlNode *
stonith_construct_reply(xmlNode * request, const char *output, xmlNode * data, int rc)
{
int lpc = 0;
xmlNode *reply = NULL;
const char *name = NULL;
const char *value = NULL;
const char *names[] = {
F_STONITH_OPERATION,
F_STONITH_CALLID,
F_STONITH_CLIENTID,
F_STONITH_CLIENTNAME,
F_STONITH_REMOTE_OP_ID,
F_STONITH_CALLOPTS
};
crm_trace("Creating a basic reply");
reply = create_xml_node(NULL, T_STONITH_REPLY);
crm_xml_add(reply, "st_origin", __FUNCTION__);
crm_xml_add(reply, F_TYPE, T_STONITH_NG);
crm_xml_add(reply, "st_output", output);
crm_xml_add_int(reply, F_STONITH_RC, rc);
CRM_CHECK(request != NULL, crm_warn("Can't create a sane reply"); return reply);
for (lpc = 0; lpc < DIMOF(names); lpc++) {
name = names[lpc];
value = crm_element_value(request, name);
crm_xml_add(reply, name, value);
}
if (data != NULL) {
crm_trace("Attaching reply output");
add_message_xml(reply, F_STONITH_CALLDATA, data);
}
return reply;
}
static xmlNode *
stonith_construct_async_reply(async_command_t * cmd, const char *output, xmlNode * data, int rc)
{
xmlNode *reply = NULL;
crm_trace("Creating a basic reply");
reply = create_xml_node(NULL, T_STONITH_REPLY);
crm_xml_add(reply, "st_origin", __FUNCTION__);
crm_xml_add(reply, F_TYPE, T_STONITH_NG);
crm_xml_add(reply, F_STONITH_OPERATION, cmd->op);
crm_xml_add(reply, F_STONITH_DEVICE, cmd->device);
crm_xml_add(reply, F_STONITH_REMOTE_OP_ID, cmd->remote_op_id);
crm_xml_add(reply, F_STONITH_CLIENTID, cmd->client);
crm_xml_add(reply, F_STONITH_CLIENTNAME, cmd->client_name);
crm_xml_add(reply, F_STONITH_TARGET, cmd->victim);
crm_xml_add(reply, F_STONITH_ACTION, cmd->op);
crm_xml_add(reply, F_STONITH_ORIGIN, cmd->origin);
crm_xml_add_int(reply, F_STONITH_CALLID, cmd->id);
crm_xml_add_int(reply, F_STONITH_CALLOPTS, cmd->options);
crm_xml_add_int(reply, F_STONITH_RC, rc);
crm_xml_add(reply, "st_output", output);
if (data != NULL) {
crm_info("Attaching reply output");
add_message_xml(reply, F_STONITH_CALLDATA, data);
}
return reply;
}
bool fencing_peer_active(crm_node_t *peer)
{
if (peer == NULL) {
return FALSE;
} else if (peer->uname == NULL) {
return FALSE;
} else if(peer->processes & (crm_proc_plugin | crm_proc_heartbeat | crm_proc_cpg)) {
return TRUE;
}
return FALSE;
}
/*!
* \internal
* \brief Determine if we need to use an alternate node to
* fence the target. If so return that node's uname
*
* \retval NULL, no alternate host
* \retval uname, uname of alternate host to use
*/
static const char *
check_alternate_host(const char *target)
{
const char *alternate_host = NULL;
if (g_hash_table_lookup(topology, target) && safe_str_eq(target, stonith_our_uname)) {
GHashTableIter gIter;
crm_node_t *entry = NULL;
g_hash_table_iter_init(&gIter, crm_peer_cache);
while (g_hash_table_iter_next(&gIter, NULL, (void **)&entry)) {
crm_trace("Checking for %s.%d != %s", entry->uname, entry->id, target);
if (fencing_peer_active(entry)
&& safe_str_neq(entry->uname, target)) {
alternate_host = entry->uname;
break;
}
}
if (alternate_host == NULL) {
crm_err("No alternate host available to handle complex self fencing request");
g_hash_table_iter_init(&gIter, crm_peer_cache);
while (g_hash_table_iter_next(&gIter, NULL, (void **)&entry)) {
crm_notice("Peer[%d] %s", entry->id, entry->uname);
}
}
}
return alternate_host;
}
static void
stonith_send_reply(xmlNode * reply, int call_options, const char *remote_peer,
const char *client_id)
{
if (remote_peer) {
send_cluster_message(crm_get_peer(0, remote_peer), crm_msg_stonith_ng, reply, FALSE);
} else {
do_local_reply(reply, client_id, is_set(call_options, st_opt_sync_call), remote_peer != NULL);
}
}
static int
handle_request(crm_client_t * client, uint32_t id, uint32_t flags, xmlNode * request,
const char *remote_peer)
{
int call_options = 0;
int rc = -EOPNOTSUPP;
xmlNode *data = NULL;
xmlNode *reply = NULL;
char *output = NULL;
const char *op = crm_element_value(request, F_STONITH_OPERATION);
const char *client_id = crm_element_value(request, F_STONITH_CLIENTID);
crm_element_value_int(request, F_STONITH_CALLOPTS, &call_options);
if (is_set(call_options, st_opt_sync_call)) {
CRM_ASSERT(client == NULL || client->request_id == id);
}
if (crm_str_eq(op, CRM_OP_REGISTER, TRUE)) {
xmlNode *reply = create_xml_node(NULL, "reply");
CRM_ASSERT(client);
crm_xml_add(reply, F_STONITH_OPERATION, CRM_OP_REGISTER);
crm_xml_add(reply, F_STONITH_CLIENTID, client->id);
crm_ipcs_send(client, id, reply, flags);
client->request_id = 0;
free_xml(reply);
return 0;
} else if (crm_str_eq(op, STONITH_OP_EXEC, TRUE)) {
rc = stonith_device_action(request, &output);
} else if (crm_str_eq(op, STONITH_OP_TIMEOUT_UPDATE, TRUE)) {
const char *call_id = crm_element_value(request, F_STONITH_CALLID);
const char *client_id = crm_element_value(request, F_STONITH_CLIENTID);
int op_timeout = 0;
crm_element_value_int(request, F_STONITH_TIMEOUT, &op_timeout);
do_stonith_async_timeout_update(client_id, call_id, op_timeout);
return 0;
} else if (crm_str_eq(op, STONITH_OP_QUERY, TRUE)) {
if (remote_peer) {
create_remote_stonith_op(client_id, request, TRUE); /* Record it for the future notification */
}
stonith_query(request, remote_peer, client_id, call_options);
return 0;
} else if (crm_str_eq(op, T_STONITH_NOTIFY, TRUE)) {
const char *flag_name = NULL;
CRM_ASSERT(client);
flag_name = crm_element_value(request, F_STONITH_NOTIFY_ACTIVATE);
if (flag_name) {
crm_debug("Setting %s callbacks for %s (%s): ON", flag_name, client->name, client->id);
client->options |= get_stonith_flag(flag_name);
}
flag_name = crm_element_value(request, F_STONITH_NOTIFY_DEACTIVATE);
if (flag_name) {
crm_debug("Setting %s callbacks for %s (%s): off", flag_name, client->name, client->id);
client->options |= get_stonith_flag(flag_name);
}
if (flags & crm_ipc_client_response) {
crm_ipcs_send_ack(client, id, flags, "ack", __FUNCTION__, __LINE__);
}
return 0;
} else if (crm_str_eq(op, STONITH_OP_RELAY, TRUE)) {
xmlNode *dev = get_xpath_object("//@" F_STONITH_TARGET, request, LOG_TRACE);
crm_notice("Peer %s has received a forwarded fencing request from %s to fence (%s) peer %s",
stonith_our_uname,
client ? client->name : remote_peer,
crm_element_value(dev, F_STONITH_ACTION),
crm_element_value(dev, F_STONITH_TARGET));
if (initiate_remote_stonith_op(NULL, request, FALSE) != NULL) {
rc = -EINPROGRESS;
}
} else if (crm_str_eq(op, STONITH_OP_FENCE, TRUE)) {
if (remote_peer || stand_alone) {
rc = stonith_fence(request);
} else if (call_options & st_opt_manual_ack) {
remote_fencing_op_t *rop = NULL;
xmlNode *dev = get_xpath_object("//@" F_STONITH_TARGET, request, LOG_TRACE);
const char *target = crm_element_value(dev, F_STONITH_TARGET);
crm_notice("Received manual confirmation that %s is fenced", target);
rop = initiate_remote_stonith_op(client, request, TRUE);
rc = stonith_manual_ack(request, rop);
} else {
const char *alternate_host = NULL;
xmlNode *dev = get_xpath_object("//@" F_STONITH_TARGET, request, LOG_TRACE);
const char *target = crm_element_value(dev, F_STONITH_TARGET);
const char *action = crm_element_value(dev, F_STONITH_ACTION);
const char *device = crm_element_value(dev, F_STONITH_DEVICE);
if (client) {
int tolerance = 0;
crm_notice("Client %s.%.8s wants to fence (%s) '%s' with device '%s'",
client->name, client->id, action, target, device ? device : "(any)");
crm_element_value_int(dev, F_STONITH_TOLERANCE, &tolerance);
if (stonith_check_fence_tolerance(tolerance, target, action)) {
rc = 0;
goto done;
}
} else {
crm_notice("Peer %s wants to fence (%s) '%s' with device '%s'",
remote_peer, action, target, device ? device : "(any)");
}
alternate_host = check_alternate_host(target);
if (alternate_host && client) {
const char *client_id = NULL;
crm_notice("Forwarding complex self fencing request to peer %s", alternate_host);
if (client) {
client_id = client->id;
} else {
client_id = crm_element_value(request, F_STONITH_CLIENTID);
}
/* Create a record of it, otherwise call_id will be 0 if we need to notify of failures */
create_remote_stonith_op(client_id, request, FALSE);
crm_xml_add(request, F_STONITH_OPERATION, STONITH_OP_RELAY);
crm_xml_add(request, F_STONITH_CLIENTID, client->id);
send_cluster_message(crm_get_peer(0, alternate_host), crm_msg_stonith_ng, request,
FALSE);
rc = -EINPROGRESS;
} else if (initiate_remote_stonith_op(client, request, FALSE) != NULL) {
rc = -EINPROGRESS;
}
}
} else if (crm_str_eq(op, STONITH_OP_FENCE_HISTORY, TRUE)) {
rc = stonith_fence_history(request, &data);
} else if (crm_str_eq(op, STONITH_OP_DEVICE_ADD, TRUE)) {
const char *id = NULL;
xmlNode *notify_data = create_xml_node(NULL, op);
rc = stonith_device_register(request, &id, FALSE);
crm_xml_add(notify_data, F_STONITH_DEVICE, id);
crm_xml_add_int(notify_data, F_STONITH_ACTIVE, g_hash_table_size(device_list));
do_stonith_notify(call_options, op, rc, notify_data);
free_xml(notify_data);
} else if (crm_str_eq(op, STONITH_OP_DEVICE_DEL, TRUE)) {
xmlNode *dev = get_xpath_object("//" F_STONITH_DEVICE, request, LOG_ERR);
const char *id = crm_element_value(dev, XML_ATTR_ID);
xmlNode *notify_data = create_xml_node(NULL, op);
rc = stonith_device_remove(id, FALSE);
crm_xml_add(notify_data, F_STONITH_DEVICE, id);
crm_xml_add_int(notify_data, F_STONITH_ACTIVE, g_hash_table_size(device_list));
do_stonith_notify(call_options, op, rc, notify_data);
free_xml(notify_data);
} else if (crm_str_eq(op, STONITH_OP_LEVEL_ADD, TRUE)) {
char *id = NULL;
xmlNode *notify_data = create_xml_node(NULL, op);
rc = stonith_level_register(request, &id);
crm_xml_add(notify_data, F_STONITH_DEVICE, id);
crm_xml_add_int(notify_data, F_STONITH_ACTIVE, g_hash_table_size(topology));
do_stonith_notify(call_options, op, rc, notify_data);
free_xml(notify_data);
free(id);
} else if (crm_str_eq(op, STONITH_OP_LEVEL_DEL, TRUE)) {
char *id = NULL;
xmlNode *notify_data = create_xml_node(NULL, op);
rc = stonith_level_remove(request, &id);
crm_xml_add(notify_data, F_STONITH_DEVICE, id);
crm_xml_add_int(notify_data, F_STONITH_ACTIVE, g_hash_table_size(topology));
do_stonith_notify(call_options, op, rc, notify_data);
free_xml(notify_data);
} else if (crm_str_eq(op, STONITH_OP_CONFIRM, TRUE)) {
async_command_t *cmd = create_async_command(request);
xmlNode *reply = stonith_construct_async_reply(cmd, NULL, NULL, 0);
crm_xml_add(reply, F_STONITH_OPERATION, T_STONITH_NOTIFY);
crm_notice("Broadcasting manual fencing confirmation for node %s", cmd->victim);
send_cluster_message(NULL, crm_msg_stonith_ng, reply, FALSE);
free_async_command(cmd);
free_xml(reply);
} else if(safe_str_eq(op, CRM_OP_RM_NODE_CACHE)) {
int id = 0;
const char *name = NULL;
crm_element_value_int(request, XML_ATTR_ID, &id);
name = crm_element_value(request, XML_ATTR_UNAME);
reap_crm_member(id, name);
return pcmk_ok;
} else {
crm_err("Unknown %s from %s", op, client ? client->name : remote_peer);
crm_log_xml_warn(request, "UnknownOp");
}
done:
/* Always reply unles the request is in process still.
* If in progress, a reply will happen async after the request
* processing is finished */
if (rc != -EINPROGRESS) {
crm_trace("Reply handling: %p %u %u %d %d %s", client, client?client->request_id:0,
id, is_set(call_options, st_opt_sync_call), call_options,
crm_element_value(request, F_STONITH_CALLOPTS));
if (is_set(call_options, st_opt_sync_call)) {
CRM_ASSERT(client == NULL || client->request_id == id);
}
reply = stonith_construct_reply(request, output, data, rc);
stonith_send_reply(reply, call_options, remote_peer, client_id);
}
free(output);
free_xml(data);
free_xml(reply);
return rc;
}
static void
handle_reply(crm_client_t * client, xmlNode * request, const char *remote_peer)
{
const char *op = crm_element_value(request, F_STONITH_OPERATION);
if (crm_str_eq(op, STONITH_OP_QUERY, TRUE)) {
process_remote_stonith_query(request);
} else if (crm_str_eq(op, T_STONITH_NOTIFY, TRUE)) {
process_remote_stonith_exec(request);
} else if (crm_str_eq(op, STONITH_OP_FENCE, TRUE)) {
/* Reply to a complex fencing op */
process_remote_stonith_exec(request);
} else {
crm_err("Unknown %s reply from %s", op, client ? client->name : remote_peer);
crm_log_xml_warn(request, "UnknownOp");
}
}
void
stonith_command(crm_client_t * client, uint32_t id, uint32_t flags, xmlNode * request,
const char *remote_peer)
{
int call_options = 0;
int rc = 0;
gboolean is_reply = FALSE;
char *op = crm_element_value_copy(request, F_STONITH_OPERATION);
/* F_STONITH_OPERATION can be overwritten in remote_op_done() with crm_xml_add()
*
* by 0x4C2E934: crm_xml_add (xml.c:377)
* by 0x40C5E9: remote_op_done (remote.c:178)
* by 0x40F1D3: process_remote_stonith_exec (remote.c:1084)
* by 0x40AD4F: stonith_command (commands.c:1891)
*
*/
if (get_xpath_object("//" T_STONITH_REPLY, request, LOG_DEBUG_3)) {
is_reply = TRUE;
}
crm_element_value_int(request, F_STONITH_CALLOPTS, &call_options);
crm_debug("Processing %s%s %u from %s (%16x)", op, is_reply ? " reply" : "",
id, client ? client->name : remote_peer, call_options);
if (is_set(call_options, st_opt_sync_call)) {
CRM_ASSERT(client == NULL || client->request_id == id);
}
if (is_reply) {
handle_reply(client, request, remote_peer);
} else {
rc = handle_request(client, id, flags, request, remote_peer);
}
crm_debug("Processed %s%s from %s: %s (%d)", op,
is_reply ? " reply" : "", client ? client->name : remote_peer,
rc > 0 ? "" : pcmk_strerror(rc), rc);
free(op);
}
diff --git a/tools/crm_mon.c b/tools/crm_mon.c
index 52343a2801..0b71275cf6 100644
--- a/tools/crm_mon.c
+++ b/tools/crm_mon.c
@@ -1,2947 +1,4080 @@
/*
- * Copyright (C) 2004 Andrew Beekhof
+ * Copyright (C) 2004-2015 Andrew Beekhof
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
+#include
#include <../lib/pengine/unpack.h>
#include <../pengine/pengine.h>
#include
-/* GMainLoop *mainloop = NULL; */
-
-void wait_for_refresh(int offset, const char *prefix, int msec);
void clean_up(int rc);
void crm_diff_update(const char *event, xmlNode * msg);
gboolean mon_refresh_display(gpointer user_data);
int cib_connect(gboolean full);
void mon_st_callback(stonith_t * st, stonith_event_t * e);
+static char *get_node_display_name(node_t *node);
+
+/*
+ * Definitions indicating which items to print
+ */
+
+#define mon_show_times (0x0001U)
+#define mon_show_stack (0x0002U)
+#define mon_show_dc (0x0004U)
+#define mon_show_count (0x0008U)
+#define mon_show_nodes (0x0010U)
+#define mon_show_resources (0x0020U)
+#define mon_show_attributes (0x0040U)
+#define mon_show_failcounts (0x0080U)
+#define mon_show_operations (0x0100U)
+#define mon_show_tickets (0x0200U)
+#define mon_show_bans (0x0400U)
+
+#define mon_show_headers (mon_show_times | mon_show_stack | mon_show_dc | mon_show_count)
+#define mon_show_default (mon_show_headers | mon_show_nodes | mon_show_resources)
+#define mon_show_all (mon_show_default | mon_show_attributes | mon_show_failcounts \
+ | mon_show_operations | mon_show_tickets | mon_show_bans)
+
+unsigned int show = mon_show_default;
+
+/*
+ * Definitions indicating how to output
+ */
+
+enum mon_output_format_e {
+ mon_output_none,
+ mon_output_monitor,
+ mon_output_plain,
+ mon_output_console,
+ mon_output_xml,
+ mon_output_html,
+ mon_output_cgi
+} output_format = mon_output_console;
+char *output_filename = NULL; /* if sending output to a file, its name */
+
+/* other globals */
char *xml_file = NULL;
-char *as_html_file = NULL;
-int as_xml = 0;
char *pid_file = NULL;
char *snmp_target = NULL;
char *snmp_community = NULL;
-gboolean as_console = TRUE;;
-gboolean simple_status = FALSE;
gboolean group_by_node = FALSE;
gboolean inactive_resources = FALSE;
-gboolean web_cgi = FALSE;
int reconnect_msec = 5000;
gboolean daemonize = FALSE;
GMainLoop *mainloop = NULL;
guint timer_id = 0;
GList *attr_list = NULL;
const char *crm_mail_host = NULL;
const char *crm_mail_prefix = NULL;
const char *crm_mail_from = NULL;
const char *crm_mail_to = NULL;
const char *external_agent = NULL;
const char *external_recipient = NULL;
cib_t *cib = NULL;
stonith_t *st = NULL;
xmlNode *current_cib = NULL;
gboolean one_shot = FALSE;
gboolean has_warnings = FALSE;
-gboolean print_failcount = FALSE;
-gboolean print_operations = FALSE;
gboolean print_timing = FALSE;
-gboolean print_nodes_attr = FALSE;
-gboolean print_last_updated = TRUE;
-gboolean print_last_change = TRUE;
-gboolean print_tickets = FALSE;
gboolean watch_fencing = FALSE;
-gboolean hide_headers = FALSE;
gboolean print_brief = FALSE;
gboolean print_pending = FALSE;
gboolean print_clone_detail = FALSE;
/* FIXME allow, detect, and correctly interpret glob pattern or regex? */
-const char *print_neg_location_prefix;
-const char *print_neg_location_prefix_toggle;
-
-#define FILTER_STR {"shutdown", "terminate", "standby", "fail-count", \
- "last-failure", "probe_complete", "#id", "#uname", \
- "#is_dc", "#kind", NULL}
+const char *print_neg_location_prefix = "";
-gboolean log_diffs = FALSE;
-gboolean log_updates = FALSE;
+/* Never display node attributes whose name starts with one of these prefixes */
+#define FILTER_STR { "shutdown", "terminate", "standby", "fail-count", \
+ "last-failure", "probe_complete", "#", NULL }
long last_refresh = 0;
crm_trigger_t *refresh_trigger = NULL;
/*
* 1.3.6.1.4.1.32723 has been assigned to the project by IANA
* http://www.iana.org/assignments/enterprise-numbers
*/
#define PACEMAKER_PREFIX "1.3.6.1.4.1.32723"
#define PACEMAKER_TRAP_PREFIX PACEMAKER_PREFIX ".1"
#define snmp_crm_trap_oid PACEMAKER_TRAP_PREFIX
#define snmp_crm_oid_node PACEMAKER_TRAP_PREFIX ".1"
#define snmp_crm_oid_rsc PACEMAKER_TRAP_PREFIX ".2"
#define snmp_crm_oid_task PACEMAKER_TRAP_PREFIX ".3"
#define snmp_crm_oid_desc PACEMAKER_TRAP_PREFIX ".4"
#define snmp_crm_oid_status PACEMAKER_TRAP_PREFIX ".5"
#define snmp_crm_oid_rc PACEMAKER_TRAP_PREFIX ".6"
#define snmp_crm_oid_trc PACEMAKER_TRAP_PREFIX ".7"
+/* Define exit codes for monitoring-compatible output */
+#define MON_STATUS_OK (0)
+#define MON_STATUS_WARN (1)
+
+/* Convenience macro for prettifying output (e.g. "node" vs "nodes") */
+#define s_if_plural(i) (((i) == 1)? "" : "s")
+
#if CURSES_ENABLED
-# define print_dot() if(as_console) { \
+# define print_dot() if (output_format == mon_output_console) { \
printw("."); \
clrtoeol(); \
refresh(); \
} else { \
fprintf(stdout, "."); \
}
#else
# define print_dot() fprintf(stdout, ".");
#endif
#if CURSES_ENABLED
-# define print_as(fmt, args...) if(as_console) { \
+# define print_as(fmt, args...) if (output_format == mon_output_console) { \
printw(fmt, ##args); \
clrtoeol(); \
refresh(); \
} else { \
fprintf(stdout, fmt, ##args); \
}
#else
# define print_as(fmt, args...) fprintf(stdout, fmt, ##args);
#endif
static void
blank_screen(void)
{
#if CURSES_ENABLED
int lpc = 0;
for (lpc = 0; lpc < LINES; lpc++) {
move(lpc, 0);
clrtoeol();
}
move(0, 0);
refresh();
#endif
}
static gboolean
mon_timer_popped(gpointer data)
{
int rc = pcmk_ok;
#if CURSES_ENABLED
- if(as_console) {
+ if (output_format == mon_output_console) {
clear();
refresh();
}
#endif
if (timer_id > 0) {
g_source_remove(timer_id);
}
print_as("Reconnecting...\n");
rc = cib_connect(TRUE);
if (rc != pcmk_ok) {
timer_id = g_timeout_add(reconnect_msec, mon_timer_popped, NULL);
}
return FALSE;
}
static void
mon_cib_connection_destroy(gpointer user_data)
{
print_as("Connection to the CIB terminated\n");
if (cib) {
cib->cmds->signoff(cib);
timer_id = g_timeout_add(reconnect_msec, mon_timer_popped, NULL);
}
return;
}
/*
* Mainloop signal handler.
*/
static void
mon_shutdown(int nsig)
{
clean_up(EX_OK);
}
#if ON_DARWIN
# define sighandler_t sig_t
#endif
#if CURSES_ENABLED
# ifndef HAVE_SIGHANDLER_T
typedef void (*sighandler_t) (int);
# endif
static sighandler_t ncurses_winch_handler;
static void
mon_winresize(int nsig)
{
static int not_done;
int lines = 0, cols = 0;
if (!not_done++) {
if (ncurses_winch_handler)
/* the original ncurses WINCH signal handler does the
* magic of retrieving the new window size;
* otherwise, we'd have to use ioctl or tgetent */
(*ncurses_winch_handler) (SIGWINCH);
getmaxyx(stdscr, lines, cols);
resizeterm(lines, cols);
mainloop_set_trigger(refresh_trigger);
}
not_done--;
}
#endif
int
cib_connect(gboolean full)
{
int rc = pcmk_ok;
static gboolean need_pass = TRUE;
CRM_CHECK(cib != NULL, return -EINVAL);
if (getenv("CIB_passwd") != NULL) {
need_pass = FALSE;
}
if (watch_fencing && st == NULL) {
st = stonith_api_new();
}
if (watch_fencing && st->state == stonith_disconnected) {
crm_trace("Connecting to stonith");
rc = st->cmds->connect(st, crm_system_name, NULL);
if (rc == pcmk_ok) {
crm_trace("Setting up stonith callbacks");
st->cmds->register_notification(st, T_STONITH_NOTIFY_FENCE, mon_st_callback);
}
}
if (cib->state != cib_connected_query && cib->state != cib_connected_command) {
crm_trace("Connecting to the CIB");
- if (as_console && need_pass && cib->variant == cib_remote) {
+ if ((output_format == mon_output_console) && need_pass && (cib->variant == cib_remote)) {
need_pass = FALSE;
print_as("Password:");
}
rc = cib->cmds->signon(cib, crm_system_name, cib_query);
if (rc != pcmk_ok) {
return rc;
}
rc = cib->cmds->query(cib, NULL, ¤t_cib, cib_scope_local | cib_sync_call);
if (rc == pcmk_ok) {
mon_refresh_display(NULL);
}
if (rc == pcmk_ok && full) {
if (rc == pcmk_ok) {
rc = cib->cmds->set_connection_dnotify(cib, mon_cib_connection_destroy);
if (rc == -EPROTONOSUPPORT) {
print_as
("Notification setup not supported, won't be able to reconnect after failure");
- if (as_console) {
+ if (output_format == mon_output_console) {
sleep(2);
}
rc = pcmk_ok;
}
}
if (rc == pcmk_ok) {
cib->cmds->del_notify_callback(cib, T_CIB_DIFF_NOTIFY, crm_diff_update);
rc = cib->cmds->add_notify_callback(cib, T_CIB_DIFF_NOTIFY, crm_diff_update);
}
if (rc != pcmk_ok) {
print_as("Notification setup failed, could not monitor CIB actions");
- if (as_console) {
+ if (output_format == mon_output_console) {
sleep(2);
}
clean_up(-rc);
}
}
}
return rc;
}
/* *INDENT-OFF* */
static struct crm_option long_options[] = {
/* Top-level Options */
{"help", 0, 0, '?', "\tThis text"},
{"version", 0, 0, '$', "\tVersion information" },
{"verbose", 0, 0, 'V', "\tIncrease debug output"},
{"quiet", 0, 0, 'Q', "\tDisplay only essential output" },
{"-spacer-", 1, 0, '-', "\nModes:"},
{"as-html", 1, 0, 'h', "\tWrite cluster status to the named html file"},
{"as-xml", 0, 0, 'X', "\t\tWrite cluster status as xml to stdout. This will enable one-shot mode."},
{"web-cgi", 0, 0, 'w', "\t\tWeb mode with output suitable for cgi"},
{"simple-status", 0, 0, 's', "\tDisplay the cluster status once as a simple one line output (suitable for nagios)"},
{"snmp-traps", 1, 0, 'S', "\tSend SNMP traps to this station", !ENABLE_SNMP},
{"snmp-community", 1, 0, 'C', "Specify community for SNMP traps(default is NULL)", !ENABLE_SNMP},
{"mail-to", 1, 0, 'T', "\tSend Mail alerts to this user. See also --mail-from, --mail-host, --mail-prefix", !ENABLE_ESMTP},
{"-spacer-", 1, 0, '-', "\nDisplay Options:"},
{"group-by-node", 0, 0, 'n', "\tGroup resources by node" },
{"inactive", 0, 0, 'r', "\t\tDisplay inactive resources" },
{"failcounts", 0, 0, 'f', "\tDisplay resource fail counts"},
{"operations", 0, 0, 'o', "\tDisplay resource operation history" },
{"timing-details", 0, 0, 't', "\tDisplay resource operation history with timing details" },
{"tickets", 0, 0, 'c', "\t\tDisplay cluster tickets"},
{"watch-fencing", 0, 0, 'W', "\tListen for fencing events. For use with --external-agent, --mail-to and/or --snmp-traps where supported"},
{"neg-locations", 2, 0, 'L', "Display negative location constraints [optionally filtered by id prefix]"},
{"show-node-attributes", 0, 0, 'A', "Display node attributes" },
{"hide-headers", 0, 0, 'D', "\tHide all headers" },
{"show-detail", 0, 0, 'R', "\tShow more details (node IDs, individual clone instances)" },
{"brief", 0, 0, 'b', "\t\tBrief output" },
{"pending", 0, 0, 'j', "\t\tDisplay pending state if 'record-pending' is enabled" },
{"-spacer-", 1, 0, '-', "\nAdditional Options:"},
{"interval", 1, 0, 'i', "\tUpdate frequency in seconds" },
{"one-shot", 0, 0, '1', "\t\tDisplay the cluster status once on the console and exit"},
{"disable-ncurses",0, 0, 'N', "\tDisable the use of ncurses", !CURSES_ENABLED},
{"daemonize", 0, 0, 'd', "\tRun in the background as a daemon"},
{"pid-file", 1, 0, 'p', "\t(Advanced) Daemon pid file location"},
{"mail-from", 1, 0, 'F', "\tMail alerts should come from the named user", !ENABLE_ESMTP},
{"mail-host", 1, 0, 'H', "\tMail alerts should be sent via the named host", !ENABLE_ESMTP},
{"mail-prefix", 1, 0, 'P', "Subjects for mail alerts should start with this string", !ENABLE_ESMTP},
{"external-agent", 1, 0, 'E', "A program to run when resource operations take place."},
{"external-recipient",1, 0, 'e', "A recipient for your program (assuming you want the program to send something to someone)."},
- {"xml-file", 1, 0, 'x', NULL, 1},
+ {"xml-file", 1, 0, 'x', NULL, pcmk_option_hidden},
{"-spacer-", 1, 0, '-', "\nExamples:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', "Display the cluster status on the console with updates as they occur:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_mon", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Display the cluster status on the console just once then exit:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_mon -1", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Display your cluster status, group resources by node, and include inactive resources in the list:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_mon --group-by-node --inactive", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Start crm_mon as a background daemon and have it write the cluster status to an HTML file:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_mon --daemonize --as-html /path/to/docroot/filename.html", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Start crm_mon and export the current cluster status as xml to stdout, then exit.:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_mon --as-xml", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Start crm_mon as a background daemon and have it send email alerts:", pcmk_option_paragraph|!ENABLE_ESMTP},
{"-spacer-", 1, 0, '-', " crm_mon --daemonize --mail-to user@example.com --mail-host mail.example.com", pcmk_option_example|!ENABLE_ESMTP},
{"-spacer-", 1, 0, '-', "Start crm_mon as a background daemon and have it send SNMP alerts:", pcmk_option_paragraph|!ENABLE_SNMP},
{"-spacer-", 1, 0, '-', " crm_mon --daemonize --snmp-traps snmptrapd.example.com", pcmk_option_example|!ENABLE_SNMP},
{NULL, 0, 0, 0}
};
/* *INDENT-ON* */
#if CURSES_ENABLED
static const char *
get_option_desc(char c)
{
int lpc;
for (lpc = 0; long_options[lpc].name != NULL; lpc++) {
if (long_options[lpc].name[0] == '-')
continue;
if (long_options[lpc].val == c) {
const char * tab = NULL;
tab = strrchr(long_options[lpc].desc, '\t');
return tab ? ++tab : long_options[lpc].desc;
}
}
return NULL;
}
+#define print_option_help(option, condition) \
+ print_as("%c %c: \t%s\n", ((condition)? '*': ' '), option, get_option_desc(option));
+
static gboolean
detect_user_input(GIOChannel *channel, GIOCondition condition, gpointer unused)
{
int c;
gboolean config_mode = FALSE;
while (1) {
/* Get user input */
c = getchar();
switch (c) {
case 'c':
- print_tickets = ! print_tickets;
+ show ^= mon_show_tickets;
break;
case 'f':
- print_failcount = ! print_failcount;
+ show ^= mon_show_failcounts;
break;
case 'n':
group_by_node = ! group_by_node;
break;
case 'o':
- print_operations = ! print_operations;
+ show ^= mon_show_operations;
+ if ((show & mon_show_operations) == 0) {
+ print_timing = 0;
+ }
break;
case 'r':
inactive_resources = ! inactive_resources;
break;
case 'R':
print_clone_detail = ! print_clone_detail;
break;
case 't':
print_timing = ! print_timing;
- if (print_timing)
- print_operations = TRUE;
+ if (print_timing) {
+ show |= mon_show_operations;
+ }
break;
case 'A':
- print_nodes_attr = ! print_nodes_attr;
+ show ^= mon_show_attributes;
break;
case 'L':
- if (print_neg_location_prefix) {
- /* toggle off */
- print_neg_location_prefix_toggle = print_neg_location_prefix;
- print_neg_location_prefix = NULL;
- } else if (print_neg_location_prefix_toggle) {
- /* toggle on */
- print_neg_location_prefix = print_neg_location_prefix_toggle;
- print_neg_location_prefix_toggle = NULL;
- } else {
- /* toggled on for the first time at runtime */
- print_neg_location_prefix = "";
- }
+ show ^= mon_show_bans;
break;
case 'D':
- hide_headers = ! hide_headers;
+ /* If any header is shown, clear them all, otherwise set them all */
+ if (show & mon_show_headers) {
+ show &= ~mon_show_headers;
+ } else {
+ show |= mon_show_headers;
+ }
break;
case 'b':
print_brief = ! print_brief;
break;
case 'j':
print_pending = ! print_pending;
break;
case '?':
config_mode = TRUE;
break;
default:
goto refresh;
}
if (!config_mode)
goto refresh;
blank_screen();
print_as("Display option change mode\n");
print_as("\n");
- print_as("%c c: \t%s\n", print_tickets ? '*': ' ', get_option_desc('c'));
- print_as("%c f: \t%s\n", print_failcount ? '*': ' ', get_option_desc('f'));
- print_as("%c n: \t%s\n", group_by_node ? '*': ' ', get_option_desc('n'));
- print_as("%c o: \t%s\n", print_operations ? '*': ' ', get_option_desc('o'));
- print_as("%c r: \t%s\n", inactive_resources ? '*': ' ', get_option_desc('r'));
- print_as("%c t: \t%s\n", print_timing ? '*': ' ', get_option_desc('t'));
- print_as("%c A: \t%s\n", print_nodes_attr ? '*': ' ', get_option_desc('A'));
- print_as("%c L: \t%s\n", print_neg_location_prefix ? '*': ' ', get_option_desc('L'));
- print_as("%c D: \t%s\n", hide_headers ? '*': ' ', get_option_desc('D'));
- print_as("%c R: \t%s\n", print_clone_detail ? '*': ' ', get_option_desc('R'));
- print_as("%c b: \t%s\n", print_brief ? '*': ' ', get_option_desc('b'));
- print_as("%c j: \t%s\n", print_pending ? '*': ' ', get_option_desc('j'));
+ print_option_help('c', show & mon_show_tickets);
+ print_option_help('f', show & mon_show_failcounts);
+ print_option_help('n', group_by_node);
+ print_option_help('o', show & mon_show_operations);
+ print_option_help('r', inactive_resources);
+ print_option_help('t', print_timing);
+ print_option_help('A', show & mon_show_attributes);
+ print_option_help('L', show & mon_show_bans);
+ print_option_help('D', (show & mon_show_headers) == 0);
+ print_option_help('R', print_clone_detail);
+ print_option_help('b', print_brief);
+ print_option_help('j', print_pending);
print_as("\n");
print_as("Toggle fields via field letter, type any other key to return");
}
refresh:
mon_refresh_display(NULL);
return TRUE;
}
#endif
int
main(int argc, char **argv)
{
int flag;
int argerr = 0;
int exit_code = 0;
int option_index = 0;
pid_file = strdup("/tmp/ClusterMon.pid");
crm_log_cli_init("crm_mon");
crm_set_options(NULL, "mode [options]", long_options,
"Provides a summary of cluster's current state."
"\n\nOutputs varying levels of detail in a number of different formats.\n");
#if !defined (ON_DARWIN) && !defined (ON_BSD)
/* prevent zombies */
signal(SIGCLD, SIG_IGN);
#endif
if (strcmp(crm_system_name, "crm_mon.cgi") == 0) {
- web_cgi = TRUE;
+ output_format = mon_output_cgi;
one_shot = TRUE;
}
while (1) {
flag = crm_get_option(argc, argv, &option_index);
if (flag == -1)
break;
switch (flag) {
case 'V':
crm_bump_log_level(argc, argv);
break;
case 'Q':
- print_last_updated = FALSE;
- print_last_change = FALSE;
+ show &= ~mon_show_times;
break;
case 'i':
reconnect_msec = crm_get_msec(optarg);
break;
case 'n':
group_by_node = TRUE;
break;
case 'r':
inactive_resources = TRUE;
break;
case 'W':
watch_fencing = TRUE;
break;
case 'd':
daemonize = TRUE;
break;
case 't':
print_timing = TRUE;
- print_operations = TRUE;
+ show |= mon_show_operations;
break;
case 'o':
- print_operations = TRUE;
+ show |= mon_show_operations;
break;
case 'f':
- print_failcount = TRUE;
+ show |= mon_show_failcounts;
break;
case 'A':
- print_nodes_attr = TRUE;
+ show |= mon_show_attributes;
break;
case 'L':
- print_neg_location_prefix = optarg ?: "";
+ show |= mon_show_bans;
+ print_neg_location_prefix = optarg? optarg : "";
break;
case 'D':
- hide_headers = TRUE;
+ show &= ~mon_show_headers;
break;
case 'b':
print_brief = TRUE;
break;
case 'j':
print_pending = TRUE;
break;
case 'R':
print_clone_detail = TRUE;
break;
case 'c':
- print_tickets = TRUE;
+ show |= mon_show_tickets;
break;
case 'p':
free(pid_file);
if(optarg == NULL) {
return crm_help(flag, EX_USAGE);
}
pid_file = strdup(optarg);
break;
case 'x':
if(optarg == NULL) {
return crm_help(flag, EX_USAGE);
}
xml_file = strdup(optarg);
one_shot = TRUE;
break;
case 'h':
if(optarg == NULL) {
return crm_help(flag, EX_USAGE);
}
- as_html_file = strdup(optarg);
+ output_format = mon_output_html;
+ output_filename = strdup(optarg);
umask(S_IWGRP | S_IWOTH);
break;
case 'X':
- as_xml = TRUE;
+ output_format = mon_output_xml;
one_shot = TRUE;
break;
case 'w':
- web_cgi = TRUE;
+ output_format = mon_output_cgi;
one_shot = TRUE;
break;
case 's':
- simple_status = TRUE;
+ output_format = mon_output_monitor;
one_shot = TRUE;
break;
case 'S':
snmp_target = optarg;
break;
case 'T':
crm_mail_to = optarg;
break;
case 'F':
crm_mail_from = optarg;
break;
case 'H':
crm_mail_host = optarg;
break;
case 'P':
crm_mail_prefix = optarg;
break;
case 'E':
external_agent = optarg;
break;
case 'e':
external_recipient = optarg;
break;
case '1':
one_shot = TRUE;
break;
case 'N':
- as_console = FALSE;
+ if (output_format == mon_output_console) {
+ output_format = mon_output_plain;
+ }
break;
case 'C':
snmp_community = optarg;
break;
case '$':
case '?':
return crm_help(flag, EX_OK);
break;
default:
printf("Argument code 0%o (%c) is not (?yet?) supported\n", flag, flag);
++argerr;
break;
}
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
if (argerr) {
return crm_help('?', EX_USAGE);
}
+ /* XML output always prints everything */
+ if (output_format == mon_output_xml) {
+ show = mon_show_all;
+ print_timing = TRUE;
+ }
+
if (one_shot) {
- as_console = FALSE;
+ if (output_format == mon_output_console) {
+ output_format = mon_output_plain;
+ }
} else if (daemonize) {
- as_console = FALSE;
+ if ((output_format == mon_output_console) || (output_format == mon_output_plain)) {
+ output_format = mon_output_none;
+ }
crm_enable_stderr(FALSE);
- if (!as_html_file && !snmp_target && !crm_mail_to && !external_agent && !as_xml) {
+ if ((output_format != mon_output_html) && (output_format != mon_output_xml)
+ && !snmp_target && !crm_mail_to && !external_agent) {
printf
("Looks like you forgot to specify one or more of: --as-html, --as-xml, --mail-to, --snmp-target, --external-agent\n");
return crm_help('?', EX_USAGE);
}
crm_make_daemon(crm_system_name, TRUE, pid_file);
- } else if (as_console) {
+ } else if (output_format == mon_output_console) {
#if CURSES_ENABLED
initscr();
cbreak();
noecho();
crm_enable_stderr(FALSE);
#else
one_shot = TRUE;
- as_console = FALSE;
+ output_format = mon_output_plain;
printf("Defaulting to one-shot mode\n");
printf("You need to have curses available at compile time to enable console mode\n");
#endif
}
crm_info("Starting %s", crm_system_name);
if (xml_file != NULL) {
current_cib = filename2xml(xml_file);
mon_refresh_display(NULL);
return exit_code;
}
if (current_cib == NULL) {
cib = cib_new();
do {
if (!one_shot) {
print_as("Attempting connection to the cluster...\n");
}
exit_code = cib_connect(!one_shot);
if (one_shot) {
break;
} else if (exit_code != pcmk_ok) {
sleep(reconnect_msec / 1000);
#if CURSES_ENABLED
- if(as_console) {
+ if (output_format == mon_output_console) {
clear();
refresh();
}
#endif
}
} while (exit_code == -ENOTCONN);
if (exit_code != pcmk_ok) {
- print_as("\nConnection to cluster failed: %s\n", pcmk_strerror(exit_code));
- if (as_console) {
+ if (output_format == mon_output_monitor) {
+ printf("CLUSTER WARN: Connection to cluster failed: %s\n", pcmk_strerror(exit_code));
+ clean_up(MON_STATUS_WARN);
+ } else {
+ print_as("\nConnection to cluster failed: %s\n", pcmk_strerror(exit_code));
+ }
+ if (output_format == mon_output_console) {
sleep(2);
}
clean_up(-exit_code);
}
}
if (one_shot) {
return exit_code;
}
mainloop = g_main_new(FALSE);
mainloop_add_signal(SIGTERM, mon_shutdown);
mainloop_add_signal(SIGINT, mon_shutdown);
#if CURSES_ENABLED
- if (as_console) {
+ if (output_format == mon_output_console) {
ncurses_winch_handler = signal(SIGWINCH, mon_winresize);
if (ncurses_winch_handler == SIG_DFL ||
ncurses_winch_handler == SIG_IGN || ncurses_winch_handler == SIG_ERR)
ncurses_winch_handler = NULL;
g_io_add_watch(g_io_channel_unix_new(STDIN_FILENO), G_IO_IN, detect_user_input, NULL);
}
#endif
refresh_trigger = mainloop_add_trigger(G_PRIORITY_LOW, mon_refresh_display, NULL);
g_main_run(mainloop);
g_main_destroy(mainloop);
crm_info("Exiting %s", crm_system_name);
clean_up(0);
return 0; /* never reached */
}
#define mon_warn(fmt...) do { \
if (!has_warnings) { \
- print_as("Warning:"); \
+ print_as("CLUSTER WARN:"); \
} else { \
print_as(","); \
} \
print_as(fmt); \
has_warnings = TRUE; \
} while(0)
static int
count_resources(pe_working_set_t * data_set, resource_t * rsc)
{
int count = 0;
GListPtr gIter = NULL;
if (rsc == NULL) {
gIter = data_set->resources;
} else if (rsc->children) {
gIter = rsc->children;
} else {
return is_not_set(rsc->flags, pe_rsc_orphan);
}
for (; gIter != NULL; gIter = gIter->next) {
count += count_resources(data_set, gIter->data);
}
return count;
}
-static int
+/*!
+ * \internal
+ * \brief Print one-line status suitable for use with monitoring software
+ *
+ * \param[in] data_set Working set of CIB state
+ *
+ * \note This function's output (and the return code when the program exits)
+ * should conform to https://www.monitoring-plugins.org/doc/guidelines.html
+ */
+static void
print_simple_status(pe_working_set_t * data_set)
{
- node_t *dc = NULL;
GListPtr gIter = NULL;
int nodes_online = 0;
int nodes_standby = 0;
int nodes_maintenance = 0;
- dc = data_set->dc_node;
-
- if (dc == NULL) {
- mon_warn("No DC ");
+ if (data_set->dc_node == NULL) {
+ mon_warn(" No DC");
}
for (gIter = data_set->nodes; gIter != NULL; gIter = gIter->next) {
node_t *node = (node_t *) gIter->data;
if (node->details->standby && node->details->online) {
nodes_standby++;
} else if (node->details->maintenance && node->details->online) {
nodes_maintenance++;
} else if (node->details->online) {
nodes_online++;
} else {
- mon_warn("offline node: %s", node->details->uname);
+ mon_warn(" offline node: %s", node->details->uname);
}
}
if (!has_warnings) {
- print_as("Ok: %d nodes online", nodes_online);
+ int nresources = count_resources(data_set, NULL);
+
+ print_as("CLUSTER OK: %d node%s online", nodes_online, s_if_plural(nodes_online));
if (nodes_standby > 0) {
- print_as(", %d standby nodes", nodes_standby);
+ print_as(", %d standby node%s", nodes_standby, s_if_plural(nodes_standby));
}
if (nodes_maintenance > 0) {
- print_as(", %d maintenance nodes", nodes_maintenance);
+ print_as(", %d maintenance node%s", nodes_maintenance, s_if_plural(nodes_maintenance));
}
- print_as(", %d resources configured", count_resources(data_set, NULL));
+ print_as(", %d resource%s configured", nresources, s_if_plural(nresources));
}
print_as("\n");
- return 0;
}
+/*!
+ * \internal
+ * \brief Print a [name]=[value][units] pair, optionally using time string
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] name Name to display
+ * \param[in] value Value to display (or NULL to convert time instead)
+ * \param[in] units Units to display (or NULL for no units)
+ * \param[in] epoch_time Epoch time to convert if value is NULL
+ */
static void
-print_date(time_t time)
+print_nvpair(FILE *stream, const char *name, const char *value,
+ const char *units, time_t epoch_time)
{
- int lpc = 0;
- char date_str[26];
+ /* print name= */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as(" %s=", name);
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ case mon_output_xml:
+ fprintf(stream, " %s=", name);
+ break;
+
+ default:
+ break;
+ }
+
+ /* If we have a value (and optionally units), print it */
+ if (value) {
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("%s%s", value, (units? units : ""));
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "%s%s", value, (units? units : ""));
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, "\"%s%s\"", value, (units? units : ""));
+ break;
+
+ default:
+ break;
+ }
+
+ /* Otherwise print user-friendly time string */
+ } else {
+ char *date_str, *c;
+
+ date_str = asctime(localtime(&epoch_time));
+ for (c = date_str; c != '\0'; ++c) {
+ if (*c == '\n') {
+ *c = '\0';
+ break;
+ }
+ }
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("'%s'", date_str);
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ case mon_output_xml:
+ fprintf(stream, "\"%s\"", date_str);
+ break;
- asctime_r(localtime(&time), date_str);
- for (; lpc < 26; lpc++) {
- if (date_str[lpc] == '\n') {
- date_str[lpc] = 0;
+ default:
+ break;
}
}
- print_as("'%s'", date_str);
}
-#include
+/*!
+ * \internal
+ * \brief Print whatever is needed to start a node section
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] node Node to print
+ */
static void
-print_rsc_summary(pe_working_set_t * data_set, node_t * node, resource_t * rsc, gboolean all)
+print_node_start(FILE *stream, node_t *node)
{
- gboolean printed = FALSE;
-
- time_t last_failure = 0;
- int failcount = get_failcount_full(node, rsc, &last_failure, FALSE, NULL, data_set);
+ char *node_name;
- if (all || failcount || last_failure > 0) {
- printed = TRUE;
- print_as(" %s: migration-threshold=%d", rsc_printable_id(rsc), rsc->migration_threshold);
- }
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ node_name = get_node_display_name(node);
+ print_as("* Node %s:\n", node_name);
+ free(node_name);
+ break;
- if (failcount > 0) {
- printed = TRUE;
- print_as(" fail-count=%d", failcount);
- }
+ case mon_output_html:
+ case mon_output_cgi:
+ node_name = get_node_display_name(node);
+ fprintf(stream, " Node: %s
\n \n", node_name);
+ free(node_name);
+ break;
- if (last_failure > 0) {
- printed = TRUE;
- print_as(" last-failure=");
- print_date(last_failure);
- }
+ case mon_output_xml:
+ fprintf(stream, " \n", node->details->uname);
+ break;
- if (printed) {
- print_as("\n");
+ default:
+ break;
}
}
+/*!
+ * \internal
+ * \brief Print whatever is needed to end a node section
+ *
+ * \param[in] stream File stream to display output to
+ */
static void
-print_rsc_history(pe_working_set_t * data_set, node_t * node, xmlNode * rsc_entry)
+print_node_end(FILE *stream)
{
- GListPtr gIter = NULL;
- GListPtr op_list = NULL;
- gboolean print_name = TRUE;
- GListPtr sorted_op_list = NULL;
- const char *rsc_id = crm_element_value(rsc_entry, XML_ATTR_ID);
- resource_t *rsc = pe_find_resource(data_set->resources, rsc_id);
+ switch (output_format) {
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "
\n");
+ break;
- xmlNode *rsc_op = NULL;
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
- for (rsc_op = __xml_first_child(rsc_entry); rsc_op != NULL; rsc_op = __xml_next(rsc_op)) {
- if (crm_str_eq((const char *)rsc_op->name, XML_LRM_TAG_RSC_OP, TRUE)) {
- op_list = g_list_append(op_list, rsc_op);
- }
+ default:
+ break;
}
+}
- sorted_op_list = g_list_sort(op_list, sort_op_by_callid);
- for (gIter = sorted_op_list; gIter != NULL; gIter = gIter->next) {
- xmlNode *xml_op = (xmlNode *) gIter->data;
- const char *value = NULL;
- const char *call = crm_element_value(xml_op, XML_LRM_ATTR_CALLID);
- const char *task = crm_element_value(xml_op, XML_LRM_ATTR_TASK);
- const char *op_rc = crm_element_value(xml_op, XML_LRM_ATTR_RC);
- const char *interval = crm_element_value(xml_op, XML_LRM_ATTR_INTERVAL);
- int rc = crm_parse_int(op_rc, "0");
-
- if (safe_str_eq(task, CRMD_ACTION_STATUS)
- && safe_str_eq(interval, "0")) {
- task = "probe";
- }
+/*!
+ * \internal
+ * \brief Print heading for resource history
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Current state of CIB
+ * \param[in] node Node that ran this resource
+ * \param[in] rsc Resource to print
+ * \param[in] rsc_id ID of resource to print
+ * \param[in] all Whether to print every resource or just failed ones
+ */
+static void
+print_rsc_history_start(FILE *stream, pe_working_set_t *data_set, node_t *node,
+ resource_t *rsc, const char *rsc_id, gboolean all)
+{
+ time_t last_failure = 0;
+ int failcount = rsc? get_failcount_full(node, rsc, &last_failure, FALSE, NULL, data_set) : 0;
- if (rc == 7 && safe_str_eq(task, "probe")) {
- continue;
+ if (!all && !failcount && (last_failure <= 0)) {
+ return;
+ }
- } else if (safe_str_eq(task, CRMD_ACTION_NOTIFY)) {
- continue;
- }
+ /* Print resource ID */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as(" %s:", rsc_id);
+ break;
- if (print_name) {
- print_name = FALSE;
- if (rsc == NULL) {
- print_as("Orphan resource: %s", rsc_id);
- } else {
- print_rsc_summary(data_set, node, rsc, TRUE);
- }
- }
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " %s:", rsc_id);
+ break;
- print_as(" + (%s) %s:", call, task);
- if (safe_str_neq(interval, "0")) {
- print_as(" interval=%sms", interval);
- }
+ case mon_output_xml:
+ fprintf(stream, " 0) {
- print_as(" %s=", attr);
- print_date(int_value);
- }
- }
+ /* If resource is an orphan, that's all we can say about it */
+ if (rsc == NULL) {
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as(" orphan");
+ break;
- attr = XML_RSC_OP_LAST_RUN;
- value = crm_element_value(xml_op, attr);
- if (value) {
- int_value = crm_parse_int(value, NULL);
- if (int_value > 0) {
- print_as(" %s=", attr);
- print_date(int_value);
- }
- }
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " orphan");
+ break;
- attr = XML_RSC_OP_T_EXEC;
- value = crm_element_value(xml_op, attr);
- if (value) {
- int_value = crm_parse_int(value, NULL);
- print_as(" %s=%dms", attr, int_value);
- }
+ case mon_output_xml:
+ fprintf(stream, " orphan=\"true\"");
+ break;
- attr = XML_RSC_OP_T_QUEUE;
- value = crm_element_value(xml_op, attr);
- if (value) {
- int_value = crm_parse_int(value, NULL);
- print_as(" %s=%dms", attr, int_value);
- }
+ default:
+ break;
}
- print_as(" rc=%s (%s)\n", op_rc, services_ocf_exitcode_str(rc));
- }
+ /* If resource is not an orphan, print some details */
+ } else if (all || failcount || (last_failure > 0)) {
- /* no need to free the contents */
- g_list_free(sorted_op_list);
-}
+ /* Print migration threshold */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as(" migration-threshold=%d", rsc->migration_threshold);
+ break;
-static void
-print_attr_msg(node_t * node, GListPtr rsc_list, const char *attrname, const char *attrvalue)
-{
- GListPtr gIter = NULL;
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " migration-threshold=%d", rsc->migration_threshold);
+ break;
- for (gIter = rsc_list; gIter != NULL; gIter = gIter->next) {
- resource_t *rsc = (resource_t *) gIter->data;
- const char *type = g_hash_table_lookup(rsc->meta, "type");
+ case mon_output_xml:
+ fprintf(stream, " orphan=\"false\" migration-threshold=\"%d\"",
+ rsc->migration_threshold);
+ break;
- if (rsc->children != NULL) {
- print_attr_msg(node, rsc->children, attrname, attrvalue);
+ default:
+ break;
}
- if (safe_str_eq(type, "ping") || safe_str_eq(type, "pingd")) {
- const char *name = g_hash_table_lookup(rsc->parameters, "name");
-
- if (name == NULL) {
- name = "pingd";
- }
-
- /* To identify the resource with the attribute name. */
- if (safe_str_eq(name, attrname)) {
- int host_list_num = 0;
- int expected_score = 0;
- int value = crm_parse_int(attrvalue, "0");
- const char *hosts = g_hash_table_lookup(rsc->parameters, "host_list");
- const char *multiplier = g_hash_table_lookup(rsc->parameters, "multiplier");
+ /* Print fail count if any */
+ if (failcount > 0) {
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as(" fail-count=%d", failcount);
+ break;
- if(hosts) {
- char **host_list = g_strsplit(hosts, " ", 0);
- host_list_num = g_strv_length(host_list);
- g_strfreev(host_list);
- }
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " fail-count=%d", failcount);
+ break;
- /* pingd multiplier is the same as the default value. */
- expected_score = host_list_num * crm_parse_int(multiplier, "1");
+ case mon_output_xml:
+ fprintf(stream, " fail-count=\"%d\"", failcount);
+ break;
- /* pingd is abnormal score. */
- if (value <= 0) {
- print_as("\t: Connectivity is lost");
- } else if (value < expected_score) {
- print_as("\t: Connectivity is degraded (Expected=%d)", expected_score);
- }
+ default:
+ break;
}
}
+
+ /* Print last failure time if any */
+ if (last_failure > 0) {
+ print_nvpair(stream, "last-failure", NULL, NULL, last_failure);
+ }
}
-}
-static int
-compare_attribute(gconstpointer a, gconstpointer b)
-{
- int rc;
+ /* End the heading */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("\n");
+ break;
- rc = strcmp((const char *)a, (const char *)b);
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "\n \n");
+ break;
- return rc;
+ case mon_output_xml:
+ fprintf(stream, ">\n");
+ break;
+
+ default:
+ break;
+ }
}
+/*!
+ * \internal
+ * \brief Print closing for resource history
+ *
+ * \param[in] stream File stream to display output to
+ */
static void
-create_attr_list(gpointer name, gpointer value, gpointer data)
+print_rsc_history_end(FILE *stream)
{
- int i;
- const char *filt_str[] = FILTER_STR;
+ switch (output_format) {
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "
\n \n");
+ break;
- CRM_CHECK(name != NULL, return);
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
- /* filtering automatic attributes */
- for (i = 0; filt_str[i] != NULL; i++) {
- if (g_str_has_prefix(name, filt_str[i])) {
- return;
- }
+ default:
+ break;
}
-
- attr_list = g_list_insert_sorted(attr_list, name, compare_attribute);
}
+/*!
+ * \internal
+ * \brief Print operation history
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Current state of CIB
+ * \param[in] node Node this operation is for
+ * \param[in] xml_op Root of XML tree describing this operation
+ * \param[in] task Task parsed from this operation's XML
+ * \param[in] interval Interval parsed from this operation's XML
+ * \param[in] rc Return code parsed from this operation's XML
+ */
static void
-print_node_attribute(gpointer name, gpointer node_data)
+print_op_history(FILE *stream, pe_working_set_t *data_set, node_t *node,
+ xmlNode *xml_op, const char *task, const char *interval, int rc)
{
const char *value = NULL;
- node_t *node = (node_t *) node_data;
+ const char *call = crm_element_value(xml_op, XML_LRM_ATTR_CALLID);
- value = g_hash_table_lookup(node->details->attrs, name);
- print_as(" + %-32s\t: %-10s", (char *)name, value);
- print_attr_msg(node, node->details->running_rsc, name, value);
- print_as("\n");
-}
+ /* Begin the operation description */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as(" + (%s) %s:", call, task);
+ break;
-static void
-print_node_summary(pe_working_set_t * data_set, gboolean operations)
-{
- xmlNode *lrm_rsc = NULL;
- xmlNode *rsc_entry = NULL;
- xmlNode *node_state = NULL;
- xmlNode *cib_status = get_object_root(XML_CIB_TAG_STATUS, data_set->input);
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " (%s) %s:", call, task);
+ break;
- if (operations) {
- print_as("\nOperations:\n");
- } else {
- print_as("\nMigration summary:\n");
+ case mon_output_xml:
+ fprintf(stream, " name, XML_CIB_TAG_STATE, TRUE)) {
- node_t *node = pe_find_node_id(data_set->nodes, ID(node_state));
+ /* Add name=value pairs as appropriate */
+ if (safe_str_neq(interval, "0")) {
+ print_nvpair(stream, "interval", interval, "ms", 0);
+ }
+ if (print_timing) {
+ int int_value;
+ const char *attr;
- if (node == NULL || node->details->online == FALSE) {
- continue;
+ attr = XML_RSC_OP_LAST_CHANGE;
+ value = crm_element_value(xml_op, attr);
+ if (value) {
+ int_value = crm_parse_int(value, NULL);
+ if (int_value > 0) {
+ print_nvpair(stream, attr, NULL, NULL, int_value);
}
+ }
- print_as("* Node %s: ", crm_element_value(node_state, XML_ATTR_UNAME));
- print_as("\n");
-
- lrm_rsc = find_xml_node(node_state, XML_CIB_TAG_LRM, FALSE);
- lrm_rsc = find_xml_node(lrm_rsc, XML_LRM_TAG_RESOURCES, FALSE);
+ attr = XML_RSC_OP_LAST_RUN;
+ value = crm_element_value(xml_op, attr);
+ if (value) {
+ int_value = crm_parse_int(value, NULL);
+ if (int_value > 0) {
+ print_nvpair(stream, attr, NULL, NULL, int_value);
+ }
+ }
+
+ attr = XML_RSC_OP_T_EXEC;
+ value = crm_element_value(xml_op, attr);
+ if (value) {
+ print_nvpair(stream, attr, value, "ms", 0);
+ }
+
+ attr = XML_RSC_OP_T_QUEUE;
+ value = crm_element_value(xml_op, attr);
+ if (value) {
+ print_nvpair(stream, attr, value, "ms", 0);
+ }
+ }
+
+ /* End the operation description */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as(" rc=%d (%s)\n", rc, services_ocf_exitcode_str(rc));
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " rc=%d (%s)\n", rc, services_ocf_exitcode_str(rc));
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " rc=\"%d\" rc_text=\"%s\" />\n", rc, services_ocf_exitcode_str(rc));
+ break;
+
+ default:
+ break;
+ }
+}
+
+/*!
+ * \internal
+ * \brief Print resource operation/failure history
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Current state of CIB
+ * \param[in] node Node that ran this resource
+ * \param[in] rsc_entry Root of XML tree describing resource status
+ * \param[in] operations Whether to print operations or just failcounts
+ */
+static void
+print_rsc_history(FILE *stream, pe_working_set_t *data_set, node_t *node,
+ xmlNode *rsc_entry, gboolean operations)
+{
+ GListPtr gIter = NULL;
+ GListPtr op_list = NULL;
+ gboolean printed = FALSE;
+ const char *rsc_id = crm_element_value(rsc_entry, XML_ATTR_ID);
+ resource_t *rsc = pe_find_resource(data_set->resources, rsc_id);
+ xmlNode *rsc_op = NULL;
+
+ /* If we're not showing operations, just print the resource failure summary */
+ if (operations == FALSE) {
+ print_rsc_history_start(stream, data_set, node, rsc, rsc_id, FALSE);
+ print_rsc_history_end(stream);
+ return;
+ }
+
+ /* Create a list of this resource's operations */
+ for (rsc_op = __xml_first_child(rsc_entry); rsc_op != NULL; rsc_op = __xml_next(rsc_op)) {
+ if (crm_str_eq((const char *)rsc_op->name, XML_LRM_TAG_RSC_OP, TRUE)) {
+ op_list = g_list_append(op_list, rsc_op);
+ }
+ }
+ op_list = g_list_sort(op_list, sort_op_by_callid);
+
+ /* Print each operation */
+ for (gIter = op_list; gIter != NULL; gIter = gIter->next) {
+ xmlNode *xml_op = (xmlNode *) gIter->data;
+ const char *task = crm_element_value(xml_op, XML_LRM_ATTR_TASK);
+ const char *interval = crm_element_value(xml_op, XML_LRM_ATTR_INTERVAL);
+ const char *op_rc = crm_element_value(xml_op, XML_LRM_ATTR_RC);
+ int rc = crm_parse_int(op_rc, "0");
+
+ /* Display 0-interval monitors as "probe" */
+ if (safe_str_eq(task, CRMD_ACTION_STATUS) && safe_str_eq(interval, "0")) {
+ task = "probe";
+ }
+
+ /* Ignore notifies and some probes */
+ if (safe_str_eq(task, CRMD_ACTION_NOTIFY) || (safe_str_eq(task, "probe") && (rc == 7))) {
+ continue;
+ }
+
+ /* If this is the first printed operation, print heading for resource */
+ if (printed == FALSE) {
+ printed = TRUE;
+ print_rsc_history_start(stream, data_set, node, rsc, rsc_id, TRUE);
+ }
+
+ /* Print the operation */
+ print_op_history(stream, data_set, node, xml_op, task, interval, rc);
+ }
+
+ /* Free the list we created (no need to free the individual items) */
+ g_list_free(op_list);
+
+ /* If we printed anything, close the resource */
+ if (printed) {
+ print_rsc_history_end(stream);
+ }
+}
+
+/*!
+ * \internal
+ * \brief Print node operation/failure history
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Current state of CIB
+ * \param[in] node_state Root of XML tree describing node status
+ * \param[in] operations Whether to print operations or just failcounts
+ */
+static void
+print_node_history(FILE *stream, pe_working_set_t *data_set,
+ xmlNode *node_state, gboolean operations)
+{
+ node_t *node = pe_find_node_id(data_set->nodes, ID(node_state));
+ xmlNode *lrm_rsc = NULL;
+ xmlNode *rsc_entry = NULL;
+
+ if (node && node->details && node->details->online) {
+ print_node_start(stream, node);
+
+ lrm_rsc = find_xml_node(node_state, XML_CIB_TAG_LRM, FALSE);
+ lrm_rsc = find_xml_node(lrm_rsc, XML_LRM_TAG_RESOURCES, FALSE);
+
+ /* Print history of each of the node's resources */
+ for (rsc_entry = __xml_first_child(lrm_rsc); rsc_entry != NULL;
+ rsc_entry = __xml_next(rsc_entry)) {
+
+ if (crm_str_eq((const char *)rsc_entry->name, XML_LRM_TAG_RESOURCE, TRUE)) {
+ print_rsc_history(stream, data_set, node, rsc_entry, operations);
+ }
+ }
+
+ print_node_end(stream);
+ }
+}
+
+/*!
+ * \internal
+ * \brief Print extended information about an attribute if appropriate
+ *
+ * \param[in] data_set Working set of CIB state
+ *
+ * \return TRUE if extended information was printed, FALSE otherwise
+ * \note Currently, extended information is only supported for ping/pingd
+ * resources, for which a message will be printed if connectivity is lost
+ * or degraded.
+ */
+static gboolean
+print_attr_msg(FILE *stream, node_t * node, GListPtr rsc_list, const char *attrname, const char *attrvalue)
+{
+ GListPtr gIter = NULL;
+
+ for (gIter = rsc_list; gIter != NULL; gIter = gIter->next) {
+ resource_t *rsc = (resource_t *) gIter->data;
+ const char *type = g_hash_table_lookup(rsc->meta, "type");
+
+ if (rsc->children != NULL) {
+ if (print_attr_msg(stream, node, rsc->children, attrname, attrvalue)) {
+ return TRUE;
+ }
+ }
- for (rsc_entry = __xml_first_child(lrm_rsc); rsc_entry != NULL;
- rsc_entry = __xml_next(rsc_entry)) {
- if (crm_str_eq((const char *)rsc_entry->name, XML_LRM_TAG_RESOURCE, TRUE)) {
- if (operations) {
- print_rsc_history(data_set, node, rsc_entry);
+ if (safe_str_eq(type, "ping") || safe_str_eq(type, "pingd")) {
+ const char *name = g_hash_table_lookup(rsc->parameters, "name");
+
+ if (name == NULL) {
+ name = "pingd";
+ }
+
+ /* To identify the resource with the attribute name. */
+ if (safe_str_eq(name, attrname)) {
+ int host_list_num = 0;
+ int expected_score = 0;
+ int value = crm_parse_int(attrvalue, "0");
+ const char *hosts = g_hash_table_lookup(rsc->parameters, "host_list");
+ const char *multiplier = g_hash_table_lookup(rsc->parameters, "multiplier");
+
+ if(hosts) {
+ char **host_list = g_strsplit(hosts, " ", 0);
+ host_list_num = g_strv_length(host_list);
+ g_strfreev(host_list);
+ }
- } else {
- const char *rsc_id = crm_element_value(rsc_entry, XML_ATTR_ID);
- resource_t *rsc = pe_find_resource(data_set->resources, rsc_id);
+ /* pingd multiplier is the same as the default value. */
+ expected_score = host_list_num * crm_parse_int(multiplier, "1");
- if (rsc) {
- print_rsc_summary(data_set, node, rsc, FALSE);
- } else {
- print_as(" %s: orphan\n", rsc_id);
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ if (value <= 0) {
+ print_as("\t: Connectivity is lost");
+ } else if (value < expected_score) {
+ print_as("\t: Connectivity is degraded (Expected=%d)", expected_score);
}
- }
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ if (value <= 0) {
+ fprintf(stream, " (connectivity is lost)");
+ } else if (value < expected_score) {
+ fprintf(stream, " (connectivity is degraded -- expected %d)",
+ expected_score);
+ }
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " expected=\"%d\"", expected_score);
+ break;
+
+ default:
+ break;
}
+ return TRUE;
+ }
+ }
+ }
+ return FALSE;
+}
+
+static int
+compare_attribute(gconstpointer a, gconstpointer b)
+{
+ int rc;
+
+ rc = strcmp((const char *)a, (const char *)b);
+
+ return rc;
+}
+
+static void
+create_attr_list(gpointer name, gpointer value, gpointer data)
+{
+ int i;
+ const char *filt_str[] = FILTER_STR;
+
+ CRM_CHECK(name != NULL, return);
+
+ /* filtering automatic attributes */
+ for (i = 0; filt_str[i] != NULL; i++) {
+ if (g_str_has_prefix(name, filt_str[i])) {
+ return;
+ }
+ }
+
+ attr_list = g_list_insert_sorted(attr_list, name, compare_attribute);
+}
+
+/* structure for passing multiple user data to g_list_foreach() */
+struct mon_attr_data {
+ FILE *stream;
+ node_t *node;
+};
+
+static void
+print_node_attribute(gpointer name, gpointer user_data)
+{
+ const char *value = NULL;
+ struct mon_attr_data *data = (struct mon_attr_data *) user_data;
+
+ value = g_hash_table_lookup(data->node->details->attrs, name);
+
+ /* Print attribute name and value */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as(" + %-32s\t: %-10s", (char *)name, value);
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(data->stream, " %s: %s",
+ (char *)name, value);
+ break;
+
+ case mon_output_xml:
+ fprintf(data->stream,
+ " stream, data->node, data->node->details->running_rsc,
+ name, value);
+
+ /* Close out the attribute */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("\n");
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(data->stream, "\n");
+ break;
+
+ case mon_output_xml:
+ fprintf(data->stream, " />\n");
+ break;
+
+ default:
+ break;
+ }
+}
+
+static void
+print_node_summary(FILE *stream, pe_working_set_t * data_set, gboolean operations)
+{
+ xmlNode *node_state = NULL;
+ xmlNode *cib_status = get_object_root(XML_CIB_TAG_STATUS, data_set->input);
+
+ /* Print heading */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ if (operations) {
+ print_as("\nOperations:\n");
+ } else {
+ print_as("\nMigration Summary:\n");
+ }
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ if (operations) {
+ fprintf(stream, "
\n Operations
\n");
+ } else {
+ fprintf(stream, "
\n Migration Summary
\n");
}
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
+ }
+
+ /* Print each node in the CIB status */
+ for (node_state = __xml_first_child(cib_status); node_state != NULL;
+ node_state = __xml_next(node_state)) {
+ if (crm_str_eq((const char *)node_state->name, XML_CIB_TAG_STATE, TRUE)) {
+ print_node_history(stream, data_set, node_state, operations);
}
}
+
+ /* Close section */
+ switch (output_format) {
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
+ }
}
static void
print_ticket(gpointer name, gpointer value, gpointer data)
{
ticket_t *ticket = (ticket_t *) value;
+ FILE *stream = (FILE *) data;
+
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("* %s:\t%s%s", ticket->id,
+ (ticket->granted? "granted" : "revoked"),
+ (ticket->standby? " [standby]" : ""));
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " %s: %s%s", ticket->id,
+ (ticket->granted? "granted" : "revoked"),
+ (ticket->standby? " [standby]" : ""));
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " id, (ticket->granted? "granted" : "revoked"),
+ (ticket->standby? "true" : "false"));
+ break;
- print_as(" %s\t%s%10s", ticket->id,
- ticket->granted ? "granted" : "revoked", ticket->standby ? " [standby]" : "");
+ default:
+ break;
+ }
if (ticket->last_granted > -1) {
- print_as(" last-granted=");
- print_date(ticket->last_granted);
+ print_nvpair(stdout, "last-granted", NULL, NULL, ticket->last_granted);
}
- print_as("\n");
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("\n");
+ break;
- return;
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "\n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " />\n");
+ break;
+
+ default:
+ break;
+ }
}
static void
-print_cluster_tickets(pe_working_set_t * data_set)
+print_cluster_tickets(FILE *stream, pe_working_set_t * data_set)
{
+ /* Print section heading */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("\nTickets:\n");
+ break;
- print_as("\nTickets:\n");
- g_hash_table_foreach(data_set->tickets, print_ticket, NULL);
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "
\n Tickets
\n \n");
+ break;
- return;
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
+ }
+
+ /* Print each ticket */
+ g_hash_table_foreach(data_set->tickets, print_ticket, stream);
+
+ /* Close section */
+ switch (output_format) {
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "
\n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
+ }
}
/*!
* \internal
* \brief Return human-friendly string representing node name
*
* The returned string will be in the format
- * uname[:containerID] [(nodeID)]
- * ":containerID" will be printed if the node is a remote container node.
+ * uname[@hostUname] [(nodeID)]
+ * "@hostUname" will be printed if the node is a guest node.
* "(nodeID)" will be printed if the node ID is different from the node uname,
* and detailed output has been requested.
*
* \param[in] node Node to represent
* \return Newly allocated string with representation of node name
* \note It is the caller's responsibility to free the result with free().
*/
static char *
get_node_display_name(node_t *node)
{
char *node_name;
- const char *node_container_id = NULL;
+ const char *node_host = NULL;
const char *node_id = NULL;
int name_len;
CRM_ASSERT((node != NULL) && (node->details != NULL) && (node->details->uname != NULL));
- /* Container ID is displayed only if this is a remote container node */
+ /* Host is displayed only if this is a guest node */
if (is_container_remote_node(node)) {
- node_container_id = node->details->remote_rsc->container->id;
+ if (node->details->remote_rsc->running_on) {
+ /* running_on is a list, but guest nodes will have exactly one entry
+ * unless they are in the process of migrating, in which case they
+ * will have two; either way, we can use the first item in the list
+ */
+ node_t *host_node = (node_t *) node->details->remote_rsc->running_on->data;
+
+ if (host_node && host_node->details) {
+ node_host = host_node->details->uname;
+ }
+ }
+ if (node_host == NULL) {
+ node_host = ""; /* so we at least get "uname@" to indicate guest */
+ }
}
/* Node ID is displayed if different from uname and detail is requested */
if (print_clone_detail && safe_str_neq(node->details->uname, node->details->id)) {
node_id = node->details->id;
}
/* Determine name length */
name_len = strlen(node->details->uname) + 1;
- if (node_container_id) {
- name_len += strlen(node_container_id) + 1; /* ":node_container_id" */
+ if (node_host) {
+ name_len += strlen(node_host) + 1; /* "@node_host" */
}
if (node_id) {
name_len += strlen(node_id) + 3; /* + " (node_id)" */
}
/* Allocate and populate display name */
node_name = malloc(name_len);
CRM_ASSERT(node_name != NULL);
strcpy(node_name, node->details->uname);
- if (node_container_id) {
- strcat(node_name, ":");
- strcat(node_name, node_container_id);
+ if (node_host) {
+ strcat(node_name, "@");
+ strcat(node_name, node_host);
}
if (node_id) {
strcat(node_name, " (");
strcat(node_name, node_id);
strcat(node_name, ")");
}
return node_name;
}
-static void print_neg_locations(pe_working_set_t *data_set)
-{
- GListPtr gIter, gIter2;
-
- print_as("\nNegative location constraints:\n");
+/*!
+ * \internal
+ * \brief Print a negative location constraint
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] node Node affected by constraint
+ * \param[in] location Constraint to print
+ */
+static void print_ban(FILE *stream, node_t *node, rsc_to_node_t *location)
+{
+ char *node_name = NULL;
+
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ node_name = get_node_display_name(node);
+ print_as(" %s\tprevents %s from running %son %s\n",
+ location->id, location->rsc_lh->id,
+ ((location->role_filter == RSC_ROLE_MASTER)? "as Master " : ""),
+ node_name);
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ node_name = get_node_display_name(node);
+ fprintf(stream, " %s prevents %s from running %son %s\n",
+ location->id, location->rsc_lh->id,
+ ((location->role_filter == RSC_ROLE_MASTER)? "as Master " : ""),
+ node_name);
+ break;
+
+ case mon_output_xml:
+ fprintf(stream,
+ " \n",
+ location->id, location->rsc_lh->id, node->details->uname, node->weight,
+ ((location->role_filter == RSC_ROLE_MASTER)? "true" : "false"));
+ break;
+
+ default:
+ break;
+ }
+ free(node_name);
+}
+
+/*!
+ * \internal
+ * \brief Print section for negative location constraints
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Working set corresponding to CIB status to display
+ */
+static void print_neg_locations(FILE *stream, pe_working_set_t *data_set)
+{
+ GListPtr gIter, gIter2;
+
+ /* Print section heading */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("\nNegative Location Constraints:\n");
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "
\n Negative Location Constraints
\n \n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
+ }
+
+ /* Print each ban */
for (gIter = data_set->placement_constraints; gIter != NULL; gIter = gIter->next) {
rsc_to_node_t *location = (rsc_to_node_t *) gIter->data;
if (!g_str_has_prefix(location->id, print_neg_location_prefix))
continue;
for (gIter2 = location->node_list_rh; gIter2 != NULL; gIter2 = gIter2->next) {
node_t *node = (node_t *) gIter2->data;
if (node->weight < 0) {
- char *node_name = get_node_display_name(node);
+ print_ban(stream, node, location);
+ }
+ }
+ }
+
+ /* Close section */
+ switch (output_format) {
+ case mon_output_cgi:
+ case mon_output_html:
+ fprintf(stream, "
\n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
+ }
+}
+
+static void
+crm_mon_get_parameters(resource_t *rsc, pe_working_set_t * data_set)
+{
+ get_rsc_attributes(rsc->parameters, rsc, NULL, data_set);
+ crm_trace("Beekhof: unpacked params for %s (%d)", rsc->id, g_hash_table_size(rsc->parameters));
+ if(rsc->children) {
+ GListPtr gIter = NULL;
+
+ for (gIter = rsc->children; gIter != NULL; gIter = gIter->next) {
+ crm_mon_get_parameters(gIter->data, data_set);
+ }
+ }
+}
+
+/*!
+ * \internal
+ * \brief Print node attributes section
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Working set of CIB state
+ */
+static void
+print_node_attributes(FILE *stream, pe_working_set_t *data_set)
+{
+ GListPtr gIter = NULL;
+
+ /* Print section heading */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("\nNode Attributes:\n");
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "
\n Node Attributes
\n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
+ }
+
+ /* Unpack all resource parameters (it would be more efficient to do this
+ * only when needed for the first time in print_attr_msg())
+ */
+ for (gIter = data_set->resources; gIter != NULL; gIter = gIter->next) {
+ crm_mon_get_parameters(gIter->data, data_set);
+ }
+
+ /* Display each node's attributes */
+ for (gIter = data_set->nodes; gIter != NULL; gIter = gIter->next) {
+ struct mon_attr_data data;
+
+ data.stream = stream;
+ data.node = (node_t *) gIter->data;
+
+ if (data.node && data.node->details && data.node->details->online) {
+ print_node_start(stream, data.node);
+ g_hash_table_foreach(data.node->details->attrs, create_attr_list, NULL);
+ g_list_foreach(attr_list, print_node_attribute, &data);
+ g_list_free(attr_list);
+ attr_list = NULL;
+ print_node_end(stream);
+ }
+ }
+
+ /* Print section footer */
+ switch (output_format) {
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
+ }
+}
+
+/*!
+ * \internal
+ * \brief Return resource display options corresponding to command-line choices
+ *
+ * \return Bitmask of pe_print_options suitable for resource print functions
+ */
+static int
+get_resource_display_options(void)
+{
+ int print_opts;
+
+ /* Determine basic output format */
+ switch (output_format) {
+ case mon_output_console:
+ print_opts = pe_print_ncurses;
+ break;
+ case mon_output_html:
+ case mon_output_cgi:
+ print_opts = pe_print_html;
+ break;
+ case mon_output_xml:
+ print_opts = pe_print_xml;
+ break;
+ default:
+ print_opts = pe_print_printf;
+ break;
+ }
+
+ /* Add optional display elements */
+ if (print_pending) {
+ print_opts |= pe_print_pending;
+ }
+ if (print_clone_detail) {
+ print_opts |= pe_print_clone_details;
+ }
+ if (!inactive_resources) {
+ print_opts |= pe_print_clone_active;
+ }
+ if (print_brief) {
+ print_opts |= pe_print_brief;
+ }
+ return print_opts;
+}
+
+/*!
+ * \internal
+ * \brief Return human-friendly string representing current time
+ *
+ * \return Current time as string (as by ctime() but without newline) on success
+ * or "Could not determine current time" on error
+ * \note The return value points to a statically allocated string which might be
+ * overwritten by subsequent calls to any of the C library date and time functions.
+ */
+static const char *
+crm_now_string(void)
+{
+ time_t a_time = time(NULL);
+ char *since_epoch = ctime(&a_time);
+
+ if ((a_time == (time_t) -1) || (since_epoch == NULL)) {
+ return "Could not determine current time";
+ }
+ since_epoch[strlen(since_epoch) - 1] = EOS; /* trim newline */
+ return (since_epoch);
+}
+
+/*!
+ * \internal
+ * \brief Print header for cluster summary if needed
+ *
+ * \param[in] stream File stream to display output to
+ */
+static void
+print_cluster_summary_header(FILE *stream)
+{
+ switch (output_format) {
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " Cluster Summary
\n \n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
+ }
+}
+
+/*!
+ * \internal
+ * \brief Print footer for cluster summary if needed
+ *
+ * \param[in] stream File stream to display output to
+ */
+static void
+print_cluster_summary_footer(FILE *stream)
+{
+ switch (output_format) {
+ case mon_output_cgi:
+ case mon_output_html:
+ fprintf(stream, "
\n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
+ }
+}
+
+/*!
+ * \internal
+ * \brief Print times the display was last updated and CIB last changed
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Working set of CIB state
+ */
+static void
+print_cluster_times(FILE *stream, pe_working_set_t *data_set)
+{
+ const char *last_written = crm_element_value(data_set->input, XML_CIB_ATTR_WRITTEN);
+ const char *user = crm_element_value(data_set->input, XML_ATTR_UPDATE_USER);
+ const char *client = crm_element_value(data_set->input, XML_ATTR_UPDATE_CLIENT);
+ const char *origin = crm_element_value(data_set->input, XML_ATTR_UPDATE_ORIG);
+
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("Last updated: %s", crm_now_string());
+ print_as("\t\tLast change: %s", last_written ? last_written : "");
+ if (user) {
+ print_as(" by %s", user);
+ }
+ if (client) {
+ print_as(" via %s", client);
+ }
+ if (origin) {
+ print_as(" on %s", origin);
+ }
+ print_as("\n");
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " Last updated: %s
\n", crm_now_string());
+ fprintf(stream, " Last change: %s", last_written ? last_written : "");
+ if (user) {
+ fprintf(stream, " by %s", user);
+ }
+ if (client) {
+ fprintf(stream, " via %s", client);
+ }
+ if (origin) {
+ fprintf(stream, " on %s", origin);
+ }
+ fprintf(stream, "
\n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " \n", crm_now_string());
+ fprintf(stream, " \n",
+ last_written ? last_written : "", user ? user : "",
+ client ? client : "", origin ? origin : "");
+ break;
+
+ default:
+ break;
+ }
+}
+
+/*!
+ * \internal
+ * \brief Print cluster stack
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] stack_s Stack name
+ */
+static void
+print_cluster_stack(FILE *stream, const char *stack_s)
+{
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("Stack: %s\n", stack_s);
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " Stack: %s
\n", stack_s);
+ break;
- print_as(" %s\tprevents %s from running %son %s\n",
- location->id, location->rsc_lh->id,
- location->role_filter == RSC_ROLE_MASTER ? "as Master " : "",
- node_name);
- free(node_name);
+ case mon_output_xml:
+ fprintf(stream, " \n", stack_s);
+ break;
+
+ default:
+ break;
+ }
+}
+
+/*!
+ * \internal
+ * \brief Print current DC and its version
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Working set of CIB state
+ */
+static void
+print_cluster_dc(FILE *stream, pe_working_set_t *data_set)
+{
+ node_t *dc = data_set->dc_node;
+ xmlNode *dc_version = get_xpath_object("//nvpair[@name='dc-version']",
+ data_set->input, LOG_DEBUG);
+ const char *dc_version_s = dc_version?
+ crm_element_value(dc_version, XML_NVPAIR_ATTR_VALUE)
+ : NULL;
+ const char *quorum = crm_element_value(data_set->input, XML_ATTR_HAVE_QUORUM);
+ char *dc_name = dc? get_node_display_name(dc) : NULL;
+
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("Current DC: ");
+ if (dc) {
+ print_as("%s (version %s) - partition %s quorum\n",
+ dc_name, (dc_version_s? dc_version_s : "unknown"),
+ (crm_is_true(quorum) ? "with" : "WITHOUT"));
+ } else {
+ print_as("NONE\n");
+ }
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " Current DC: ");
+ if (dc) {
+ fprintf(stream, "%s (version %s) - partition %s quorum",
+ dc_name, (dc_version_s? dc_version_s : "unknown"),
+ (crm_is_true(quorum)? "with" : "WITHOUT"));
+ } else {
+ fprintf(stream, "NONE");
+ }
+ fprintf(stream, "
\n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " details->uname, dc->details->id,
+ (crm_is_true(quorum) ? "true" : "false"));
+ } else {
+ fprintf(stream, "present=\"false\"");
}
+ fprintf(stream, " />\n");
+ break;
+
+ default:
+ break;
+ }
+ free(dc_name);
+}
+
+/*!
+ * \internal
+ * \brief Print counts of configured nodes and resources
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Working set of CIB state
+ * \param[in] stack_s Stack name
+ */
+static void
+print_cluster_counts(FILE *stream, pe_working_set_t *data_set, const char *stack_s)
+{
+ int nnodes = g_list_length(data_set->nodes);
+ int nresources = count_resources(data_set, NULL);
+ xmlNode *quorum_node = get_xpath_object("//nvpair[@name='" XML_ATTR_EXPECTED_VOTES "']",
+ data_set->input, LOG_DEBUG);
+ const char *quorum_votes = quorum_node?
+ crm_element_value(quorum_node, XML_NVPAIR_ATTR_VALUE)
+ : "unknown";
+
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("%d node%s and %d resource%s configured",
+ nnodes, s_if_plural(nnodes),
+ nresources, s_if_plural(nresources));
+ if (stack_s && strstr(stack_s, "classic openais") != NULL) {
+ print_as(", %s expected votes", quorum_votes);
+ }
+ print_as("\n\n");
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " %d node%s configured", nnodes, s_if_plural(nnodes));
+ if (stack_s && strstr(stack_s, "classic openais") != NULL) {
+ fprintf(stream, " (%s expected votes)", quorum_votes);
+ }
+ fprintf(stream, "
\n");
+ fprintf(stream, " %d resource%s configured
\n",
+ nresources, s_if_plural(nresources));
+ break;
+
+ case mon_output_xml:
+ fprintf(stream,
+ " \n",
+ g_list_length(data_set->nodes), quorum_votes);
+ fprintf(stream,
+ " \n",
+ count_resources(data_set, NULL));
+ break;
+
+ default:
+ break;
+ }
+}
+
+/*!
+ * \internal
+ * \brief Print cluster-wide options
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Working set of CIB state
+ *
+ * \note Currently this is only implemented for HTML and XML output, and
+ * prints only a few options. If there is demand, more could be added.
+ */
+static void
+print_cluster_options(FILE *stream, pe_working_set_t *data_set)
+{
+ switch (output_format) {
+ case mon_output_html:
+ fprintf(stream, "
\n Config Options
\n");
+ fprintf(stream, " \n");
+ fprintf(stream, " STONITH of failed nodes | %s |
\n",
+ is_set(data_set->flags, pe_flag_stonith_enabled)? "enabled" : "disabled");
+
+ fprintf(stream, " Cluster is | %ssymmetric |
\n",
+ is_set(data_set->flags, pe_flag_symmetric_cluster)? "" : "a");
+
+ fprintf(stream, " No Quorum Policy | ");
+ switch (data_set->no_quorum_policy) {
+ case no_quorum_freeze:
+ fprintf(stream, "Freeze resources");
+ break;
+ case no_quorum_stop:
+ fprintf(stream, "Stop ALL resources");
+ break;
+ case no_quorum_ignore:
+ fprintf(stream, "Ignore");
+ break;
+ case no_quorum_suicide:
+ fprintf(stream, "Suicide");
+ break;
+ }
+ fprintf(stream, " |
\n
\n \n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " flags, pe_flag_stonith_enabled)?
+ "true" : "false");
+ fprintf(stream, " symmetric-cluster=\"%s\"",
+ is_set(data_set->flags, pe_flag_symmetric_cluster)?
+ "true" : "false");
+ fprintf(stream, " no-quorum-policy=\"");
+ switch (data_set->no_quorum_policy) {
+ case no_quorum_freeze:
+ fprintf(stream, "freeze");
+ break;
+ case no_quorum_stop:
+ fprintf(stream, "stop");
+ break;
+ case no_quorum_ignore:
+ fprintf(stream, "ignore");
+ break;
+ case no_quorum_suicide:
+ fprintf(stream, "suicide");
+ break;
+ }
+ fprintf(stream, "\" />\n");
+ break;
+
+ default:
+ break;
+ }
+}
+
+/*!
+ * \internal
+ * \brief Get the name of the stack in use (or "unknown" if not available)
+ *
+ * \param[in] data_set Working set of CIB state
+ *
+ * \return String representing stack name
+ */
+static const char *
+get_cluster_stack(pe_working_set_t *data_set)
+{
+ xmlNode *stack = get_xpath_object("//nvpair[@name='cluster-infrastructure']",
+ data_set->input, LOG_DEBUG);
+ return stack? crm_element_value(stack, XML_NVPAIR_ATTR_VALUE) : "unknown";
+}
+
+/*!
+ * \internal
+ * \brief Print a summary of cluster-wide information
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Working set of CIB state
+ */
+static void
+print_cluster_summary(FILE *stream, pe_working_set_t *data_set)
+{
+ const char *stack_s = get_cluster_stack(data_set);
+ gboolean header_printed = FALSE;
+
+ if (show & mon_show_times) {
+ if (header_printed == FALSE) {
+ print_cluster_summary_header(stream);
+ header_printed = TRUE;
+ }
+ print_cluster_times(stream, data_set);
+ }
+
+ if (show & mon_show_stack) {
+ if (header_printed == FALSE) {
+ print_cluster_summary_header(stream);
+ header_printed = TRUE;
+ }
+ print_cluster_stack(stream, stack_s);
+ }
+
+ /* Always print DC if none, even if not requested */
+ if ((data_set->dc_node == NULL) || (show & mon_show_dc)) {
+ if (header_printed == FALSE) {
+ print_cluster_summary_header(stream);
+ header_printed = TRUE;
+ }
+ print_cluster_dc(stream, data_set);
+ }
+
+ if (show & mon_show_count) {
+ if (header_printed == FALSE) {
+ print_cluster_summary_header(stream);
+ header_printed = TRUE;
+ }
+ print_cluster_counts(stream, data_set, stack_s);
+ }
+
+ /* There is not a separate option for showing cluster options, so show with
+ * stack for now; a separate option could be added if there is demand
+ */
+ if (show & mon_show_stack) {
+ print_cluster_options(stream, data_set);
+ }
+
+ if (header_printed) {
+ print_cluster_summary_footer(stream);
+ }
+}
+
+/*!
+ * \internal
+ * \brief Print a failed action
+ *
+ * \param[in] stream File stream to display output to
+ * \param[in] xml_op Root of XML tree describing failed action
+ */
+static void
+print_failed_action(FILE *stream, xmlNode *xml_op)
+{
+ const char *op_key = crm_element_value(xml_op, XML_LRM_ATTR_TASK_KEY);
+ const char *op_key_attr = "op_key";
+ const char *last = crm_element_value(xml_op, XML_RSC_OP_LAST_CHANGE);
+ const char *node = crm_element_value(xml_op, XML_ATTR_UNAME);
+ const char *call = crm_element_value(xml_op, XML_LRM_ATTR_CALLID);
+ const char *exit_reason = crm_element_value(xml_op, XML_LRM_ATTR_EXIT_REASON);
+ int rc = crm_parse_int(crm_element_value(xml_op, XML_LRM_ATTR_RC), "0");
+ int status = crm_parse_int(crm_element_value(xml_op, XML_LRM_ATTR_OPSTATUS), "0");
+ char *exit_reason_cleaned;
+
+ /* If no op_key was given, use id instead */
+ if (op_key == NULL) {
+ op_key = ID(xml_op);
+ op_key_attr = "id";
+ }
+
+ /* If no exit reason was given, use "none" */
+ if (exit_reason == NULL) {
+ exit_reason = "none";
+ }
+
+ /* Print common action information */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("* %s on %s '%s' (%d): call=%s, status=%s, exitreason='%s'",
+ op_key, node, services_ocf_exitcode_str(rc), rc,
+ call, services_lrm_status_str(status), exit_reason);
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, " %s on %s '%s' (%d): call=%s, status=%s, exitreason='%s'",
+ op_key, node, services_ocf_exitcode_str(rc), rc,
+ call, services_lrm_status_str(status), exit_reason);
+ break;
+
+ case mon_output_xml:
+ exit_reason_cleaned = crm_xml_escape(exit_reason);
+ fprintf(stream, " parameters, rsc, NULL, data_set);
- crm_trace("Beekhof: unpacked params for %s (%d)", rsc->id, g_hash_table_size(rsc->parameters));
- if(rsc->children) {
- GListPtr gIter = NULL;
+ /* End the action listing */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("\n");
+ break;
- for (gIter = rsc->children; gIter != NULL; gIter = gIter->next) {
- crm_mon_get_parameters(gIter->data, data_set);
- }
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "\n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " />\n");
+ break;
+
+ default:
+ break;
}
}
/*!
* \internal
- * \brief Return resource display options corresponding to command-line choices
+ * \brief Print a section for failed actions
*
- * \return Bitmask of pe_print_options suitable for resource print functions
+ * \param[in] stream File stream to display output to
+ * \param[in] data_set Working set of CIB state
*/
-static int
-get_resource_display_options(void)
+static void
+print_failed_actions(FILE *stream, pe_working_set_t *data_set)
{
- int print_opts = as_console? pe_print_ncurses : pe_print_printf;
+ xmlNode *xml_op = NULL;
- /* Determine basic output format */
- if (as_xml) {
- print_opts = pe_print_xml;
- } else if (as_html_file || web_cgi) {
- print_opts = pe_print_html;
- } else if (as_console) {
- print_opts = pe_print_ncurses;
- } else {
- print_opts = pe_print_printf;
- }
+ /* Print section heading */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("\nFailed Actions:\n");
+ break;
- /* Add optional display elements */
- if (print_pending) {
- print_opts |= pe_print_pending;
- }
- if (print_clone_detail) {
- print_opts |= pe_print_clone_details;
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "
\n Failed Actions
\n \n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
}
- if (!inactive_resources) {
- print_opts |= pe_print_clone_active;
+
+ /* Print each failed action */
+ for (xml_op = __xml_first_child(data_set->failed); xml_op != NULL;
+ xml_op = __xml_next(xml_op)) {
+ print_failed_action(stream, xml_op);
}
- if (print_brief) {
- print_opts |= pe_print_brief;
+
+ /* End section */
+ switch (output_format) {
+ case mon_output_plain:
+ case mon_output_console:
+ print_as("\n");
+ break;
+
+ case mon_output_html:
+ case mon_output_cgi:
+ fprintf(stream, "
\n");
+ break;
+
+ case mon_output_xml:
+ fprintf(stream, " \n");
+ break;
+
+ default:
+ break;
}
- return print_opts;
}
/*!
* \internal
- * \brief Return human-friendly string representing current time
+ * \brief Print cluster status to screen
*
- * \return Current time as string (as by ctime() but without newline) on success
- * or "Could not determine current time" on error
- * \note The return value points to a statically allocated string which might be
- * overwritten by subsequent calls to any of the C library date and time functions.
+ * This uses the global display preferences set by command-line options
+ * to display cluster status in a human-friendly way.
+ *
+ * \param[in] data_set Working set of CIB state
*/
-static const char *
-crm_now_string(void)
-{
- time_t a_time = time(NULL);
- char *since_epoch = ctime(&a_time);
-
- if ((a_time == (time_t) -1) || (since_epoch == NULL)) {
- return "Could not determine current time";
- }
- since_epoch[strlen(since_epoch) - 1] = EOS; /* trim newline */
- return (since_epoch);
-}
-
-static int
+static void
print_status(pe_working_set_t * data_set)
{
- static int updates = 0;
-
GListPtr gIter = NULL;
- node_t *dc = NULL;
+ int print_opts = get_resource_display_options();
+
+ /* space-separated lists of node names */
char *online_nodes = NULL;
char *online_remote_nodes = NULL;
- char *online_remote_containers = NULL;
+ char *online_guest_nodes = NULL;
char *offline_nodes = NULL;
char *offline_remote_nodes = NULL;
- const char *stack_s = NULL;
- xmlNode *dc_version = NULL;
- xmlNode *quorum_node = NULL;
- xmlNode *stack = NULL;
-
- int print_opts = get_resource_display_options();
- const char *quorum_votes = "unknown";
- if (as_console) {
+ if (output_format == mon_output_console) {
blank_screen();
}
+ print_cluster_summary(stdout, data_set);
-
- updates++;
- dc = data_set->dc_node;
-
-
- if (print_last_updated && !hide_headers) {
- print_as("Last updated: %s\n", crm_now_string());
- }
-
- if (print_last_change && !hide_headers) {
- const char *last_written = crm_element_value(data_set->input, XML_CIB_ATTR_WRITTEN);
- const char *user = crm_element_value(data_set->input, XML_ATTR_UPDATE_USER);
- const char *client = crm_element_value(data_set->input, XML_ATTR_UPDATE_CLIENT);
- const char *origin = crm_element_value(data_set->input, XML_ATTR_UPDATE_ORIG);
-
- print_as("Last change: %s", last_written ? last_written : "");
- if (user) {
- print_as(" by %s", user);
- }
- if (client) {
- print_as(" via %s", client);
- }
- if (origin) {
- print_as(" on %s", origin);
- }
- print_as("\n");
- }
-
- stack =
- get_xpath_object("//nvpair[@name='cluster-infrastructure']", data_set->input, LOG_DEBUG);
- if (stack) {
- stack_s = crm_element_value(stack, XML_NVPAIR_ATTR_VALUE);
- if (!hide_headers) {
- print_as("Stack: %s\n", stack_s);
- }
- }
-
- dc_version = get_xpath_object("//nvpair[@name='dc-version']", data_set->input, LOG_DEBUG);
- if (dc == NULL) {
- print_as("Current DC: NONE\n");
- } else if (!hide_headers) {
- const char *quorum = crm_element_value(data_set->input, XML_ATTR_HAVE_QUORUM);
- char *dc_name = get_node_display_name(dc);
-
- print_as("Current DC: %s", dc_name);
- print_as(" - partition %s quorum\n", crm_is_true(quorum) ? "with" : "WITHOUT");
- if (dc_version) {
- print_as("Version: %s\n", crm_element_value(dc_version, XML_NVPAIR_ATTR_VALUE));
- }
- free(dc_name);
- }
-
- quorum_node =
- get_xpath_object("//nvpair[@name='" XML_ATTR_EXPECTED_VOTES "']", data_set->input,
- LOG_DEBUG);
- if (quorum_node) {
- quorum_votes = crm_element_value(quorum_node, XML_NVPAIR_ATTR_VALUE);
- }
-
- if(!hide_headers) {
- if(stack_s && strstr(stack_s, "classic openais") != NULL) {
- print_as("%d Nodes configured, %s expected votes\n", g_list_length(data_set->nodes),
- quorum_votes);
- } else {
- print_as("%d Nodes configured\n", g_list_length(data_set->nodes));
- }
- print_as("%d Resources configured\n", count_resources(data_set, NULL));
- print_as("\n\n");
- }
-
+ /* Gather node information (and print if in bad state or grouping by node) */
for (gIter = data_set->nodes; gIter != NULL; gIter = gIter->next) {
node_t *node = (node_t *) gIter->data;
const char *node_mode = NULL;
char *node_name = get_node_display_name(node);
+ /* Get node mode */
if (node->details->unclean) {
- if (node->details->online && node->details->unclean) {
+ if (node->details->online) {
node_mode = "UNCLEAN (online)";
} else if (node->details->pending) {
node_mode = "UNCLEAN (pending)";
} else {
node_mode = "UNCLEAN (offline)";
}
} else if (node->details->pending) {
node_mode = "pending";
} else if (node->details->standby_onfail && node->details->online) {
node_mode = "standby (on-fail)";
} else if (node->details->standby) {
if (node->details->online) {
node_mode = "standby";
} else {
node_mode = "OFFLINE (standby)";
}
} else if (node->details->maintenance) {
if (node->details->online) {
node_mode = "maintenance";
} else {
node_mode = "OFFLINE (maintenance)";
}
} else if (node->details->online) {
node_mode = "online";
if (group_by_node == FALSE) {
if (is_container_remote_node(node)) {
- online_remote_containers = add_list_element(online_remote_containers, node_name);
+ online_guest_nodes = add_list_element(online_guest_nodes, node_name);
} else if (is_baremetal_remote_node(node)) {
online_remote_nodes = add_list_element(online_remote_nodes, node_name);
} else {
online_nodes = add_list_element(online_nodes, node_name);
}
continue;
}
} else {
node_mode = "OFFLINE";
if (group_by_node == FALSE) {
if (is_baremetal_remote_node(node)) {
offline_remote_nodes = add_list_element(offline_remote_nodes, node_name);
} else if (is_container_remote_node(node)) {
- /* ignore offline container nodes */
+ /* ignore offline guest nodes */
} else {
offline_nodes = add_list_element(offline_nodes, node_name);
}
continue;
}
}
+ /* If we get here, node is in bad state, or we're grouping by node */
+
+ /* Print the node name and status */
if (is_container_remote_node(node)) {
- print_as("Container");
+ print_as("Guest");
} else if (is_baremetal_remote_node(node)) {
print_as("Remote");
}
print_as("Node %s: %s\n", node_name, node_mode);
- if (print_brief && group_by_node) {
- print_rscs_brief(node->details->running_rsc, "\t", print_opts | pe_print_rsconly,
- stdout, FALSE);
-
- } else if (group_by_node) {
- GListPtr gIter2 = NULL;
+ /* If we're grouping by node, print its resources */
+ if (group_by_node) {
+ if (print_brief) {
+ print_rscs_brief(node->details->running_rsc, "\t", print_opts | pe_print_rsconly,
+ stdout, FALSE);
+ } else {
+ GListPtr gIter2 = NULL;
- for (gIter2 = node->details->running_rsc; gIter2 != NULL; gIter2 = gIter2->next) {
- resource_t *rsc = (resource_t *) gIter2->data;
+ for (gIter2 = node->details->running_rsc; gIter2 != NULL; gIter2 = gIter2->next) {
+ resource_t *rsc = (resource_t *) gIter2->data;
- rsc->fns->print(rsc, "\t", print_opts | pe_print_rsconly, stdout);
+ rsc->fns->print(rsc, "\t", print_opts | pe_print_rsconly, stdout);
+ }
}
}
free(node_name);
}
+ /* If we're not grouping by node, summarize nodes by status */
if (online_nodes) {
print_as("Online: [%s ]\n", online_nodes);
free(online_nodes);
}
if (offline_nodes) {
print_as("OFFLINE: [%s ]\n", offline_nodes);
free(offline_nodes);
}
if (online_remote_nodes) {
print_as("RemoteOnline: [%s ]\n", online_remote_nodes);
free(online_remote_nodes);
}
if (offline_remote_nodes) {
print_as("RemoteOFFLINE: [%s ]\n", offline_remote_nodes);
free(offline_remote_nodes);
}
- if (online_remote_containers) {
- print_as("Containers: [%s ]\n", online_remote_containers);
- free(online_remote_containers);
- }
-
- if (group_by_node == FALSE && inactive_resources) {
- print_as("\nFull list of resources:\n");
-
- } else if (inactive_resources) {
- print_as("\nInactive resources:\n");
+ if (online_guest_nodes) {
+ print_as("GuestOnline: [%s ]\n", online_guest_nodes);
+ free(online_guest_nodes);
}
+ /* If we haven't already displayed resources grouped by node,
+ * or we need to print inactive resources, print a resources section */
if (group_by_node == FALSE || inactive_resources) {
+
+ /* If we're printing inactive resources, display a heading */
+ if (inactive_resources) {
+ if (group_by_node == FALSE) {
+ print_as("\nFull list of resources:\n");
+ } else {
+ print_as("\nInactive resources:\n");
+ }
+ }
print_as("\n");
+ /* If we haven't already printed resources grouped by node,
+ * and brief output was requested, print resource summary */
if (print_brief && group_by_node == FALSE) {
print_rscs_brief(data_set->resources, NULL, print_opts, stdout,
inactive_resources);
}
+ /* For each resource, display it if appropriate */
for (gIter = data_set->resources; gIter != NULL; gIter = gIter->next) {
resource_t *rsc = (resource_t *) gIter->data;
+ /* Complex resources may have some sub-resources active and some inactive */
gboolean is_active = rsc->fns->active(rsc, TRUE);
gboolean partially_active = rsc->fns->active(rsc, FALSE);
- if (print_brief && group_by_node == FALSE
- && rsc->variant == pe_native) {
+ /* Always ignore inactive orphan resources (deleted but not yet gone from CIB) */
+ if (is_set(rsc->flags, pe_rsc_orphan) && (is_active == FALSE)) {
continue;
}
- if (is_set(rsc->flags, pe_rsc_orphan) && is_active == FALSE) {
- continue;
-
- } else if (group_by_node == FALSE) {
- if (partially_active || inactive_resources) {
+ /* If we already printed resources grouped by node,
+ * only print inactive resources, if that was requested */
+ if (group_by_node == TRUE) {
+ if ((is_active == FALSE) && inactive_resources) {
rsc->fns->print(rsc, NULL, print_opts, stdout);
}
+ continue;
+ }
- } else if (is_active == FALSE && inactive_resources) {
- rsc->fns->print(rsc, NULL, print_opts, stdout);
+ /* Otherwise, print resource if it's at least partially active
+ * or we're displaying inactive resources,
+ * except for primitive resources already counted in a brief summary */
+ if (!(print_brief && (rsc->variant == pe_native))
+ && (partially_active || inactive_resources)) {
+ rsc->fns->print(rsc, NULL, print_opts, stdout);
}
}
}
- if (print_nodes_attr) {
- print_as("\nNode Attributes:\n");
-
- for (gIter = data_set->resources; gIter != NULL; gIter = gIter->next) {
- crm_mon_get_parameters(gIter->data, data_set);
- }
-
- for (gIter = data_set->nodes; gIter != NULL; gIter = gIter->next) {
- node_t *node = (node_t *) gIter->data;
- char *node_name;
-
- if (node == NULL || node->details->online == FALSE) {
- continue;
- }
-
- node_name = get_node_display_name(node);
- print_as("* Node %s:\n", node_name);
- free(node_name);
-
- g_hash_table_foreach(node->details->attrs, create_attr_list, NULL);
- g_list_foreach(attr_list, print_node_attribute, node);
- g_list_free(attr_list);
- attr_list = NULL;
- }
+ /* print Node Attributes section if requested */
+ if (show & mon_show_attributes) {
+ print_node_attributes(stdout, data_set);
}
- if (print_operations || print_failcount) {
- print_node_summary(data_set, print_operations);
+ /* If requested, print resource operations (which includes failcounts)
+ * or just failcounts
+ */
+ if (show & (mon_show_operations | mon_show_failcounts)) {
+ print_node_summary(stdout, data_set,
+ ((show & mon_show_operations)? TRUE : FALSE));
}
+ /* If there were any failed actions, print them */
if (xml_has_children(data_set->failed)) {
- xmlNode *xml_op = NULL;
-
- print_as("\nFailed actions:\n");
- for (xml_op = __xml_first_child(data_set->failed); xml_op != NULL;
- xml_op = __xml_next(xml_op)) {
- int status = 0;
- int rc = 0;
- const char *id = ID(xml_op);
- const char *op_key = crm_element_value(xml_op, XML_LRM_ATTR_TASK_KEY);
- const char *last = crm_element_value(xml_op, XML_RSC_OP_LAST_CHANGE);
- const char *node = crm_element_value(xml_op, XML_ATTR_UNAME);
- const char *call = crm_element_value(xml_op, XML_LRM_ATTR_CALLID);
- const char *rc_s = crm_element_value(xml_op, XML_LRM_ATTR_RC);
- const char *exit_reason = crm_element_value(xml_op, XML_LRM_ATTR_EXIT_REASON);
- const char *status_s = crm_element_value(xml_op, XML_LRM_ATTR_OPSTATUS);
-
- rc = crm_parse_int(rc_s, "0");
- status = crm_parse_int(status_s, "0");
-
- if (last) {
- time_t run_at = crm_parse_int(last, "0");
- char *run_at_s = ctime(&run_at);
- if(run_at_s) {
- run_at_s[24] = 0; /* Overwrite the newline */
- }
-
- print_as(" %s on %s '%s' (%d): call=%s, status=%s, exit-reason='%s', last-rc-change='%s', queued=%sms, exec=%sms\n",
- op_key ? op_key : id,
- node,
- services_ocf_exitcode_str(rc),
- rc,
- call,
- services_lrm_status_str(status),
- exit_reason ? exit_reason : "none",
- run_at_s,
- crm_element_value(xml_op, XML_RSC_OP_T_QUEUE),
- crm_element_value(xml_op, XML_RSC_OP_T_EXEC));
- } else {
- print_as(" %s on %s '%s' (%d): call=%s, status=%s, exitreason='%s'\n",
- op_key ? op_key : id,
- node,
- services_ocf_exitcode_str(rc),
- rc,
- call,
- services_lrm_status_str(status),
- exit_reason ? exit_reason : "unknown");
- }
- }
- print_as("\n");
+ print_failed_actions(stdout, data_set);
}
- if (print_tickets || print_neg_location_prefix) {
- /* For recording the tickets that are referenced in rsc_ticket constraints
- * but have never been granted yet.
- * To be able to print negative location constraint summary,
- * we also need them to be unpacked. */
- xmlNode *cib_constraints = get_object_root(XML_CIB_TAG_CONSTRAINTS, data_set->input);
- unpack_constraints(cib_constraints, data_set);
- }
- if (print_tickets) {
- print_cluster_tickets(data_set);
+ /* Print tickets if requested */
+ if (show & mon_show_tickets) {
+ print_cluster_tickets(stdout, data_set);
}
- if (print_neg_location_prefix) {
- print_neg_locations(data_set);
+
+ /* Print negative location constraints if requested */
+ if (show & mon_show_bans) {
+ print_neg_locations(stdout, data_set);
}
+
#if CURSES_ENABLED
- if (as_console) {
+ if (output_format == mon_output_console) {
refresh();
}
#endif
- return 0;
}
-static int
+/*!
+ * \internal
+ * \brief Print cluster status in XML format
+ *
+ * \param[in] data_set Working set of CIB state
+ */
+static void
print_xml_status(pe_working_set_t * data_set)
{
FILE *stream = stdout;
GListPtr gIter = NULL;
- node_t *dc = NULL;
- xmlNode *stack = NULL;
- xmlNode *quorum_node = NULL;
- const char *quorum_votes = "unknown";
int print_opts = get_resource_display_options();
- dc = data_set->dc_node;
-
fprintf(stream, "\n");
fprintf(stream, "\n", VERSION);
- /*** SUMMARY ***/
- fprintf(stream, " \n");
-
- if (print_last_updated) {
- fprintf(stream, " \n", crm_now_string());
- }
-
- if (print_last_change) {
- const char *last_written = crm_element_value(data_set->input, XML_CIB_ATTR_WRITTEN);
- const char *user = crm_element_value(data_set->input, XML_ATTR_UPDATE_USER);
- const char *client = crm_element_value(data_set->input, XML_ATTR_UPDATE_CLIENT);
- const char *origin = crm_element_value(data_set->input, XML_ATTR_UPDATE_ORIG);
-
- fprintf(stream,
- " \n",
- last_written ? last_written : "", user ? user : "", client ? client : "",
- origin ? origin : "");
- }
-
- stack = get_xpath_object("//nvpair[@name='cluster-infrastructure']",
- data_set->input, LOG_DEBUG);
- if (stack) {
- fprintf(stream, " \n",
- crm_element_value(stack, XML_NVPAIR_ATTR_VALUE));
- }
-
- if (!dc) {
- fprintf(stream, " \n");
- } else {
- const char *quorum = crm_element_value(data_set->input, XML_ATTR_HAVE_QUORUM);
- const char *uname = dc->details->uname;
- const char *id = dc->details->id;
- xmlNode *dc_version = get_xpath_object("//nvpair[@name='dc-version']",
- data_set->input,
- LOG_DEBUG);
-
- fprintf(stream,
- " \n",
- dc_version ? crm_element_value(dc_version, XML_NVPAIR_ATTR_VALUE) : "", uname, id,
- quorum ? (crm_is_true(quorum) ? "true" : "false") : "false");
- }
-
- quorum_node = get_xpath_object("//nvpair[@name='" XML_ATTR_EXPECTED_VOTES "']",
- data_set->input, LOG_DEBUG);
- if (quorum_node) {
- quorum_votes = crm_element_value(quorum_node, XML_NVPAIR_ATTR_VALUE);
- }
- fprintf(stream, " \n",
- g_list_length(data_set->nodes), quorum_votes);
-
- fprintf(stream, " \n",
- count_resources(data_set, NULL));
-
- fprintf(stream, " \n");
+ print_cluster_summary(stream, data_set);
/*** NODES ***/
fprintf(stream, " \n");
for (gIter = data_set->nodes; gIter != NULL; gIter = gIter->next) {
node_t *node = (node_t *) gIter->data;
const char *node_type = "unknown";
switch (node->details->type) {
case node_member:
node_type = "member";
break;
case node_remote:
node_type = "remote";
break;
case node_ping:
node_type = "ping";
break;
}
fprintf(stream, " details->uname);
fprintf(stream, "id=\"%s\" ", node->details->id);
fprintf(stream, "online=\"%s\" ", node->details->online ? "true" : "false");
fprintf(stream, "standby=\"%s\" ", node->details->standby ? "true" : "false");
fprintf(stream, "standby_onfail=\"%s\" ", node->details->standby_onfail ? "true" : "false");
fprintf(stream, "maintenance=\"%s\" ", node->details->maintenance ? "true" : "false");
fprintf(stream, "pending=\"%s\" ", node->details->pending ? "true" : "false");
fprintf(stream, "unclean=\"%s\" ", node->details->unclean ? "true" : "false");
fprintf(stream, "shutdown=\"%s\" ", node->details->shutdown ? "true" : "false");
fprintf(stream, "expected_up=\"%s\" ", node->details->expected_up ? "true" : "false");
fprintf(stream, "is_dc=\"%s\" ", node->details->is_dc ? "true" : "false");
fprintf(stream, "resources_running=\"%d\" ", g_list_length(node->details->running_rsc));
fprintf(stream, "type=\"%s\" ", node_type);
if (is_container_remote_node(node)) {
- fprintf(stream, "container_id=\"%s\" ", node->details->remote_rsc->container->id);
+ fprintf(stream, "id_as_resource=\"%s\" ", node->details->remote_rsc->container->id);
}
if (group_by_node) {
GListPtr lpc2 = NULL;
fprintf(stream, ">\n");
for (lpc2 = node->details->running_rsc; lpc2 != NULL; lpc2 = lpc2->next) {
resource_t *rsc = (resource_t *) lpc2->data;
rsc->fns->print(rsc, " ", print_opts | pe_print_rsconly, stream);
}
fprintf(stream, " \n");
} else {
fprintf(stream, "/>\n");
}
}
fprintf(stream, " \n");
/*** RESOURCES ***/
if (group_by_node == FALSE || inactive_resources) {
fprintf(stream, " \n");
for (gIter = data_set->resources; gIter != NULL; gIter = gIter->next) {
resource_t *rsc = (resource_t *) gIter->data;
gboolean is_active = rsc->fns->active(rsc, TRUE);
gboolean partially_active = rsc->fns->active(rsc, FALSE);
if (is_set(rsc->flags, pe_rsc_orphan) && is_active == FALSE) {
continue;
} else if (group_by_node == FALSE) {
if (partially_active || inactive_resources) {
rsc->fns->print(rsc, " ", print_opts, stream);
}
} else if (is_active == FALSE && inactive_resources) {
rsc->fns->print(rsc, " ", print_opts, stream);
}
}
fprintf(stream, " \n");
}
- /*** FAILURES ***/
+ /* print Node Attributes section if requested */
+ if (show & mon_show_attributes) {
+ print_node_attributes(stream, data_set);
+ }
+
+ /* If requested, print resource operations (which includes failcounts)
+ * or just failcounts
+ */
+ if (show & (mon_show_operations | mon_show_failcounts)) {
+ print_node_summary(stream, data_set,
+ ((show & mon_show_operations)? TRUE : FALSE));
+ }
+
+ /* If there were any failed actions, print them */
if (xml_has_children(data_set->failed)) {
- xmlNode *xml_op = NULL;
-
- fprintf(stream, " \n");
- for (xml_op = __xml_first_child(data_set->failed); xml_op != NULL;
- xml_op = __xml_next(xml_op)) {
- int status = 0;
- int rc = 0;
- int interval = 0;
- const char *id = ID(xml_op);
- const char *op_key = crm_element_value(xml_op, XML_LRM_ATTR_TASK_KEY);
- const char *task = crm_element_value(xml_op, XML_LRM_ATTR_TASK); // needed?
- const char *last = crm_element_value(xml_op, XML_RSC_OP_LAST_CHANGE);
- const char *node = crm_element_value(xml_op, XML_ATTR_UNAME);
- const char *call = crm_element_value(xml_op, XML_LRM_ATTR_CALLID);
- const char *rc_s = crm_element_value(xml_op, XML_LRM_ATTR_RC);
- const char *exit_reason = crm_element_value(xml_op, XML_LRM_ATTR_EXIT_REASON);
- const char *interval_s = crm_element_value(xml_op, XML_LRM_ATTR_INTERVAL);
- char *exit_reason_cleaned = NULL;
-
- rc = crm_parse_int(rc_s, "0");
- interval = crm_parse_int(interval_s, "0");
-
- exit_reason_cleaned = exit_reason ? crm_xml_escape(exit_reason) : NULL;
-
- if (last) {
- time_t run_at = crm_parse_int(last, "0");
- char *run_at_s = ctime(&run_at);
- if(run_at_s) {
- run_at_s[24] = 0; /* Overwrite the newline */
- }
+ print_failed_actions(stream, data_set);
+ }
- fprintf(stream, " \n",
- op_key ? "op_key" : "id",
- op_key ? op_key : id,
- node,
- services_ocf_exitcode_str(rc),
- exit_reason_cleaned ? exit_reason_cleaned : "none",
- rc,
- call,
- services_lrm_status_str(status),
- run_at_s,
- crm_element_value(xml_op, XML_RSC_OP_T_QUEUE),
- crm_element_value(xml_op, XML_RSC_OP_T_EXEC),
- interval,
- task);
- } else {
- print_as(" \n",
- op_key ? "op_key" : "id",
- op_key ? op_key : id,
- node,
- services_ocf_exitcode_str(rc),
- exit_reason_cleaned ? exit_reason_cleaned : "none",
- rc,
- call,
- services_lrm_status_str(status));
- }
- free(exit_reason_cleaned);
- }
- fprintf(stream, " \n");
+ /* Print tickets if requested */
+ if (show & mon_show_tickets) {
+ print_cluster_tickets(stream, data_set);
+ }
+
+ /* Print negative location constraints if requested */
+ if (show & mon_show_bans) {
+ print_neg_locations(stream, data_set);
}
fprintf(stream, "\n");
fflush(stream);
fclose(stream);
-
- return 0;
}
+/*!
+ * \internal
+ * \brief Print cluster status in HTML format (with HTTP headers if CGI)
+ *
+ * \param[in] data_set Working set of CIB state
+ * \param[in] filename Name of file to write HTML to (ignored if CGI)
+ *
+ * \return 0 on success, -1 on error
+ */
static int
-print_html_status(pe_working_set_t * data_set, const char *filename, gboolean web_cgi)
+print_html_status(pe_working_set_t * data_set, const char *filename)
{
FILE *stream;
GListPtr gIter = NULL;
- node_t *dc = NULL;
- static int updates = 0;
char *filename_tmp = NULL;
int print_opts = get_resource_display_options();
- if (web_cgi) {
+ if (output_format == mon_output_cgi) {
stream = stdout;
fprintf(stream, "Content-type: text/html\n\n");
} else {
filename_tmp = crm_concat(filename, "tmp", '.');
stream = fopen(filename_tmp, "w");
if (stream == NULL) {
crm_perror(LOG_ERR, "Cannot open %s for writing", filename_tmp);
free(filename_tmp);
return -1;
}
}
- updates++;
- dc = data_set->dc_node;
-
- fprintf(stream, "");
- fprintf(stream, "");
- fprintf(stream, "Cluster status");
-/* content="%d;url=http://webdesign.about.com" */
- fprintf(stream, "", reconnect_msec / 1000);
- fprintf(stream, "");
-
- /*** SUMMARY ***/
-
- fprintf(stream, "Cluster summary
");
- fprintf(stream, "Last updated: %s
\n", crm_now_string());
-
- if (dc == NULL) {
- fprintf(stream, "Current DC: NONE
");
- } else {
- char *dc_name = get_node_display_name(dc);
-
- fprintf(stream, "Current DC: %s
\n", dc_name);
- free(dc_name);
- }
- fprintf(stream, "%d Nodes configured.
", g_list_length(data_set->nodes));
- fprintf(stream, "%d Resources configured.
", count_resources(data_set, NULL));
-
- /*** CONFIG ***/
+ fprintf(stream, "\n");
+ fprintf(stream, " \n");
+ fprintf(stream, " Cluster status\n");
+ fprintf(stream, " \n", reconnect_msec / 1000);
+ fprintf(stream, " \n");
+ fprintf(stream, "\n");
- fprintf(stream, "Config Options
\n");
-
- fprintf(stream, "\n");
- fprintf(stream, "STONITH of failed nodes | : | %s |
\n",
- is_set(data_set->flags, pe_flag_stonith_enabled) ? "enabled" : "disabled");
-
- fprintf(stream, "Cluster is | : | %ssymmetric |
\n",
- is_set(data_set->flags, pe_flag_symmetric_cluster) ? "" : "a-");
-
- fprintf(stream, "No Quorum Policy | : | ");
- switch (data_set->no_quorum_policy) {
- case no_quorum_freeze:
- fprintf(stream, "Freeze resources");
- break;
- case no_quorum_stop:
- fprintf(stream, "Stop ALL resources");
- break;
- case no_quorum_ignore:
- fprintf(stream, "Ignore");
- break;
- case no_quorum_suicide:
- fprintf(stream, "Suicide");
- break;
- }
- fprintf(stream, "\n |
\n
\n");
+ print_cluster_summary(stream, data_set);
/*** NODE LIST ***/
- fprintf(stream, "Node List
\n");
+ fprintf(stream, "
\n Node List
\n");
fprintf(stream, "\n");
for (gIter = data_set->nodes; gIter != NULL; gIter = gIter->next) {
node_t *node = (node_t *) gIter->data;
char *node_name = get_node_display_name(node);
fprintf(stream, "- Node: %s: ", node_name);
if (node->details->standby_onfail && node->details->online) {
fprintf(stream, "standby (on-fail)\n");
} else if (node->details->standby && node->details->online) {
fprintf(stream, "standby\n");
} else if (node->details->standby) {
fprintf(stream, "OFFLINE (standby)\n");
} else if (node->details->maintenance && node->details->online) {
fprintf(stream, "maintenance\n");
} else if (node->details->maintenance) {
fprintf(stream, "OFFLINE (maintenance)\n");
} else if (node->details->online) {
fprintf(stream, "online\n");
} else {
fprintf(stream, "OFFLINE\n");
}
if (print_brief && group_by_node) {
fprintf(stream, "
\n");
print_rscs_brief(node->details->running_rsc, NULL, print_opts | pe_print_rsconly,
stream, FALSE);
fprintf(stream, "
\n");
} else if (group_by_node) {
GListPtr lpc2 = NULL;
fprintf(stream, "\n");
for (lpc2 = node->details->running_rsc; lpc2 != NULL; lpc2 = lpc2->next) {
resource_t *rsc = (resource_t *) lpc2->data;
fprintf(stream, "- ");
rsc->fns->print(rsc, NULL, print_opts | pe_print_rsconly, stream);
fprintf(stream, "
\n");
}
fprintf(stream, "
\n");
}
fprintf(stream, " \n");
}
fprintf(stream, "
\n");
if (group_by_node && inactive_resources) {
fprintf(stream, "Inactive Resources
\n");
} else if (group_by_node == FALSE) {
- fprintf(stream, "Resource List
\n");
+ fprintf(stream, "
\n Resource List
\n");
}
if (group_by_node == FALSE || inactive_resources) {
if (print_brief && group_by_node == FALSE) {
print_rscs_brief(data_set->resources, NULL, print_opts, stream,
inactive_resources);
}
for (gIter = data_set->resources; gIter != NULL; gIter = gIter->next) {
resource_t *rsc = (resource_t *) gIter->data;
gboolean is_active = rsc->fns->active(rsc, TRUE);
gboolean partially_active = rsc->fns->active(rsc, FALSE);
if (print_brief && group_by_node == FALSE
&& rsc->variant == pe_native) {
continue;
}
if (is_set(rsc->flags, pe_rsc_orphan) && is_active == FALSE) {
continue;
} else if (group_by_node == FALSE) {
if (partially_active || inactive_resources) {
rsc->fns->print(rsc, NULL, print_opts, stream);
}
} else if (is_active == FALSE && inactive_resources) {
rsc->fns->print(rsc, NULL, print_opts, stream);
}
}
}
- fprintf(stream, "");
+ /* print Node Attributes section if requested */
+ if (show & mon_show_attributes) {
+ print_node_attributes(stream, data_set);
+ }
+
+ /* If requested, print resource operations (which includes failcounts)
+ * or just failcounts
+ */
+ if (show & (mon_show_operations | mon_show_failcounts)) {
+ print_node_summary(stream, data_set,
+ ((show & mon_show_operations)? TRUE : FALSE));
+ }
+
+ /* If there were any failed actions, print them */
+ if (xml_has_children(data_set->failed)) {
+ print_failed_actions(stream, data_set);
+ }
+
+ /* Print tickets if requested */
+ if (show & mon_show_tickets) {
+ print_cluster_tickets(stream, data_set);
+ }
+
+ /* Print negative location constraints if requested */
+ if (show & mon_show_bans) {
+ print_neg_locations(stream, data_set);
+ }
+
+ fprintf(stream, "\n");
+ fprintf(stream, "\n");
fflush(stream);
fclose(stream);
- if (!web_cgi) {
+ if (output_format != mon_output_cgi) {
if (rename(filename_tmp, filename) != 0) {
crm_perror(LOG_ERR, "Unable to rename %s->%s", filename_tmp, filename);
}
free(filename_tmp);
}
return 0;
}
#if ENABLE_SNMP
# include
# include
# include
# include
# include
# include
# define add_snmp_field(list, oid_string, value) do { \
oid name[MAX_OID_LEN]; \
size_t name_length = MAX_OID_LEN; \
if (snmp_parse_oid(oid_string, name, &name_length)) { \
int s_rc = snmp_add_var(list, name, name_length, 's', (value)); \
if(s_rc != 0) { \
crm_err("Could not add %s=%s rc=%d", oid_string, value, s_rc); \
} else { \
crm_trace("Added %s=%s", oid_string, value); \
} \
} else { \
crm_err("Could not parse OID: %s", oid_string); \
} \
} while(0) \
# define add_snmp_field_int(list, oid_string, value) do { \
oid name[MAX_OID_LEN]; \
size_t name_length = MAX_OID_LEN; \
if (snmp_parse_oid(oid_string, name, &name_length)) { \
if(NULL == snmp_pdu_add_variable( \
list, name, name_length, ASN_INTEGER, \
(u_char *) & value, sizeof(value))) { \
crm_err("Could not add %s=%d", oid_string, value); \
} else { \
crm_trace("Added %s=%d", oid_string, value); \
} \
} else { \
crm_err("Could not parse OID: %s", oid_string); \
} \
} while(0) \
static int
snmp_input(int operation, netsnmp_session * session, int reqid, netsnmp_pdu * pdu, void *magic)
{
return 1;
}
static netsnmp_session *
crm_snmp_init(const char *target, char *community)
{
static netsnmp_session *session = NULL;
# ifdef NETSNMPV53
char target53[128];
snprintf(target53, sizeof(target53), "%s:162", target);
# endif
if (session) {
return session;
}
if (target == NULL) {
return NULL;
}
if (get_crm_log_level() > LOG_INFO) {
char *debug_tokens = strdup("run:shell,snmptrap,tdomain");
debug_register_tokens(debug_tokens);
snmp_set_do_debugging(1);
}
session = calloc(1, sizeof(netsnmp_session));
snmp_sess_init(session);
session->version = SNMP_VERSION_2c;
session->callback = snmp_input;
session->callback_magic = NULL;
if (community) {
session->community_len = strlen(community);
session->community = (unsigned char *)community;
}
session = snmp_add(session,
# ifdef NETSNMPV53
netsnmp_tdomain_transport(target53, 0, "udp"),
# else
netsnmp_transport_open_client("snmptrap", target),
# endif
NULL, NULL);
if (session == NULL) {
snmp_sess_perror("Could not create snmp transport", session);
}
return session;
}
#endif
static int
send_snmp_trap(const char *node, const char *rsc, const char *task, int target_rc, int rc,
int status, const char *desc)
{
int ret = 1;
#if ENABLE_SNMP
static oid snmptrap_oid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
static oid sysuptime_oid[] = { 1, 3, 6, 1, 2, 1, 1, 3, 0 };
netsnmp_pdu *trap_pdu;
netsnmp_session *session = crm_snmp_init(snmp_target, snmp_community);
trap_pdu = snmp_pdu_create(SNMP_MSG_TRAP2);
if (!trap_pdu) {
crm_err("Failed to create SNMP notification");
return SNMPERR_GENERR;
}
if (1) {
/* send uptime */
char csysuptime[20];
time_t now = time(NULL);
sprintf(csysuptime, "%ld", now);
snmp_add_var(trap_pdu, sysuptime_oid, sizeof(sysuptime_oid) / sizeof(oid), 't', csysuptime);
}
/* Indicate what the trap is by setting snmpTrapOid.0 */
ret =
snmp_add_var(trap_pdu, snmptrap_oid, sizeof(snmptrap_oid) / sizeof(oid), 'o',
snmp_crm_trap_oid);
if (ret != 0) {
crm_err("Failed set snmpTrapOid.0=%s", snmp_crm_trap_oid);
return ret;
}
/* Add extries to the trap */
if (rsc) {
add_snmp_field(trap_pdu, snmp_crm_oid_rsc, rsc);
}
add_snmp_field(trap_pdu, snmp_crm_oid_node, node);
add_snmp_field(trap_pdu, snmp_crm_oid_task, task);
add_snmp_field(trap_pdu, snmp_crm_oid_desc, desc);
add_snmp_field_int(trap_pdu, snmp_crm_oid_rc, rc);
add_snmp_field_int(trap_pdu, snmp_crm_oid_trc, target_rc);
add_snmp_field_int(trap_pdu, snmp_crm_oid_status, status);
/* Send and cleanup */
ret = snmp_send(session, trap_pdu);
if (ret == 0) {
/* error */
snmp_sess_perror("Could not send SNMP trap", session);
snmp_free_pdu(trap_pdu);
ret = SNMPERR_GENERR;
} else {
ret = SNMPERR_SUCCESS;
}
#else
crm_err("Sending SNMP traps is not supported by this installation");
#endif
return ret;
}
#if ENABLE_ESMTP
# include
# include
static void
print_recipient_status(smtp_recipient_t recipient, const char *mailbox, void *arg)
{
const smtp_status_t *status;
status = smtp_recipient_status(recipient);
printf("%s: %d %s", mailbox, status->code, status->text);
}
static void
event_cb(smtp_session_t session, int event_no, void *arg, ...)
{
int *ok;
va_list alist;
va_start(alist, arg);
switch (event_no) {
case SMTP_EV_CONNECT:
case SMTP_EV_MAILSTATUS:
case SMTP_EV_RCPTSTATUS:
case SMTP_EV_MESSAGEDATA:
case SMTP_EV_MESSAGESENT:
case SMTP_EV_DISCONNECT:
break;
case SMTP_EV_WEAK_CIPHER:{
int bits = va_arg(alist, long);
ok = va_arg(alist, int *);
crm_debug("SMTP_EV_WEAK_CIPHER, bits=%d - accepted.", bits);
*ok = 1;
break;
}
case SMTP_EV_STARTTLS_OK:
crm_debug("SMTP_EV_STARTTLS_OK - TLS started here.");
break;
case SMTP_EV_INVALID_PEER_CERTIFICATE:{
long vfy_result = va_arg(alist, long);
ok = va_arg(alist, int *);
/* There is a table in handle_invalid_peer_certificate() of mail-file.c */
crm_err("SMTP_EV_INVALID_PEER_CERTIFICATE: %ld", vfy_result);
*ok = 1;
break;
}
case SMTP_EV_NO_PEER_CERTIFICATE:
ok = va_arg(alist, int *);
crm_debug("SMTP_EV_NO_PEER_CERTIFICATE - accepted.");
*ok = 1;
break;
case SMTP_EV_WRONG_PEER_CERTIFICATE:
ok = va_arg(alist, int *);
crm_debug("SMTP_EV_WRONG_PEER_CERTIFICATE - accepted.");
*ok = 1;
break;
case SMTP_EV_NO_CLIENT_CERTIFICATE:
ok = va_arg(alist, int *);
crm_debug("SMTP_EV_NO_CLIENT_CERTIFICATE - accepted.");
*ok = 1;
break;
default:
crm_debug("Got event: %d - ignored.\n", event_no);
}
va_end(alist);
}
#endif
#define BODY_MAX 2048
#if ENABLE_ESMTP
static void
crm_smtp_debug(const char *buf, int buflen, int writing, void *arg)
{
char type = 0;
int lpc = 0, last = 0, level = *(int *)arg;
if (writing == SMTP_CB_HEADERS) {
type = 'H';
} else if (writing) {
type = 'C';
} else {
type = 'S';
}
for (; lpc < buflen; lpc++) {
switch (buf[lpc]) {
case 0:
case '\n':
if (last > 0) {
do_crm_log(level, " %.*s", lpc - last, buf + last);
} else {
do_crm_log(level, "%c: %.*s", type, lpc - last, buf + last);
}
last = lpc + 1;
break;
}
}
}
#endif
static int
send_custom_trap(const char *node, const char *rsc, const char *task, int target_rc, int rc,
int status, const char *desc)
{
pid_t pid;
/*setenv needs chars, these are ints */
char *rc_s = crm_itoa(rc);
char *status_s = crm_itoa(status);
char *target_rc_s = crm_itoa(target_rc);
crm_debug("Sending external notification to '%s' via '%s'", external_recipient, external_agent);
if(rsc) {
setenv("CRM_notify_rsc", rsc, 1);
}
setenv("CRM_notify_recipient", external_recipient, 1);
setenv("CRM_notify_node", node, 1);
setenv("CRM_notify_task", task, 1);
setenv("CRM_notify_desc", desc, 1);
setenv("CRM_notify_rc", rc_s, 1);
setenv("CRM_notify_target_rc", target_rc_s, 1);
setenv("CRM_notify_status", status_s, 1);
pid = fork();
if (pid == -1) {
crm_perror(LOG_ERR, "notification fork() failed.");
}
if (pid == 0) {
/* crm_debug("notification: I am the child. Executing the nofitication program."); */
execl(external_agent, external_agent, NULL);
}
crm_trace("Finished running custom notification program '%s'.", external_agent);
free(target_rc_s);
free(status_s);
free(rc_s);
return 0;
}
static int
send_smtp_trap(const char *node, const char *rsc, const char *task, int target_rc, int rc,
int status, const char *desc)
{
#if ENABLE_ESMTP
smtp_session_t session;
smtp_message_t message;
auth_context_t authctx;
struct sigaction sa;
int len = 25; /* Note: Check extra padding on the Subject line below */
int noauth = 1;
int smtp_debug = LOG_DEBUG;
char crm_mail_body[BODY_MAX];
char *crm_mail_subject = NULL;
memset(&sa, 0, sizeof(struct sigaction));
if (node == NULL) {
node = "-";
}
if (rsc == NULL) {
rsc = "-";
}
if (desc == NULL) {
desc = "-";
}
if (crm_mail_to == NULL) {
return 1;
}
if (crm_mail_host == NULL) {
crm_mail_host = "localhost:25";
}
if (crm_mail_prefix == NULL) {
crm_mail_prefix = "Cluster notification";
}
crm_debug("Sending '%s' mail to %s via %s", crm_mail_prefix, crm_mail_to, crm_mail_host);
len += strlen(crm_mail_prefix);
len += strlen(task);
len += strlen(rsc);
len += strlen(node);
len += strlen(desc);
len++;
crm_mail_subject = calloc(1, len);
/* If you edit this line, ensure you allocate enough memory for it by altering 'len' above */
snprintf(crm_mail_subject, len, "%s - %s event for %s on %s: %s\r\n", crm_mail_prefix, task,
rsc, node, desc);
len = 0;
len += snprintf(crm_mail_body + len, BODY_MAX - len, "\r\n%s\r\n", crm_mail_prefix);
len += snprintf(crm_mail_body + len, BODY_MAX - len, "====\r\n\r\n");
if (rc == target_rc) {
len += snprintf(crm_mail_body + len, BODY_MAX - len,
"Completed operation %s for resource %s on %s\r\n", task, rsc, node);
} else {
len += snprintf(crm_mail_body + len, BODY_MAX - len,
"Operation %s for resource %s on %s failed: %s\r\n", task, rsc, node, desc);
}
len += snprintf(crm_mail_body + len, BODY_MAX - len, "\r\nDetails:\r\n");
len += snprintf(crm_mail_body + len, BODY_MAX - len,
"\toperation status: (%d) %s\r\n", status, services_lrm_status_str(status));
if (status == PCMK_LRM_OP_DONE) {
len += snprintf(crm_mail_body + len, BODY_MAX - len,
"\tscript returned: (%d) %s\r\n", rc, services_ocf_exitcode_str(rc));
len += snprintf(crm_mail_body + len, BODY_MAX - len,
"\texpected return value: (%d) %s\r\n", target_rc,
services_ocf_exitcode_str(target_rc));
}
auth_client_init();
session = smtp_create_session();
message = smtp_add_message(session);
smtp_starttls_enable(session, Starttls_ENABLED);
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGPIPE, &sa, NULL);
smtp_set_server(session, crm_mail_host);
authctx = auth_create_context();
auth_set_mechanism_flags(authctx, AUTH_PLUGIN_PLAIN, 0);
smtp_set_eventcb(session, event_cb, NULL);
/* Now tell libESMTP it can use the SMTP AUTH extension.
*/
if (!noauth) {
crm_debug("Adding authentication context");
smtp_auth_set_context(session, authctx);
}
if (crm_mail_from == NULL) {
struct utsname us;
char auto_from[BODY_MAX];
CRM_ASSERT(uname(&us) == 0);
snprintf(auto_from, BODY_MAX, "crm_mon@%s", us.nodename);
smtp_set_reverse_path(message, auto_from);
} else {
/* NULL is ok */
smtp_set_reverse_path(message, crm_mail_from);
}
smtp_set_header(message, "To", NULL /*phrase */ , NULL /*addr */ ); /* "Phrase" */
smtp_add_recipient(message, crm_mail_to);
/* Set the Subject: header and override any subject line in the message headers. */
smtp_set_header(message, "Subject", crm_mail_subject);
smtp_set_header_option(message, "Subject", Hdr_OVERRIDE, 1);
smtp_set_message_str(message, crm_mail_body);
smtp_set_monitorcb(session, crm_smtp_debug, &smtp_debug, 1);
if (smtp_start_session(session)) {
char buf[128];
int rc = smtp_errno();
crm_err("SMTP server problem: %s (%d)", smtp_strerror(rc, buf, sizeof buf), rc);
} else {
char buf[128];
int rc = smtp_errno();
const smtp_status_t *smtp_status = smtp_message_transfer_status(message);
if (rc != 0) {
crm_err("SMTP server problem: %s (%d)", smtp_strerror(rc, buf, sizeof buf), rc);
}
crm_info("Send status: %d %s", smtp_status->code, crm_str(smtp_status->text));
smtp_enumerate_recipients(message, print_recipient_status, NULL);
}
smtp_destroy_session(session);
auth_destroy_context(authctx);
auth_client_exit();
#endif
return 0;
}
static void
handle_rsc_op(xmlNode * xml, const char *node_id)
{
int rc = -1;
int status = -1;
int action = -1;
int interval = 0;
int target_rc = -1;
int transition_num = -1;
gboolean notify = TRUE;
char *rsc = NULL;
char *task = NULL;
const char *desc = NULL;
const char *magic = NULL;
const char *id = NULL;
char *update_te_uuid = NULL;
const char *node = NULL;
xmlNode *n = xml;
xmlNode * rsc_op = xml;
if(strcmp((const char*)xml->name, XML_LRM_TAG_RSC_OP) != 0) {
xmlNode *cIter;
for(cIter = xml->children; cIter; cIter = cIter->next) {
handle_rsc_op(cIter, node_id);
}
return;
}
id = crm_element_value(rsc_op, XML_LRM_ATTR_TASK_KEY);
if (id == NULL) {
/* Compatability with <= 1.1.5 */
id = ID(rsc_op);
}
magic = crm_element_value(rsc_op, XML_ATTR_TRANSITION_MAGIC);
if (magic == NULL) {
/* non-change */
return;
}
if (FALSE == decode_transition_magic(magic, &update_te_uuid, &transition_num, &action,
&status, &rc, &target_rc)) {
crm_err("Invalid event %s detected for %s", magic, id);
return;
}
if (parse_op_key(id, &rsc, &task, &interval) == FALSE) {
crm_err("Invalid event detected for %s", id);
goto bail;
}
node = crm_element_value(rsc_op, XML_LRM_ATTR_TARGET);
while (n != NULL && safe_str_neq(XML_CIB_TAG_STATE, TYPE(n))) {
n = n->parent;
}
if(node == NULL && n) {
node = crm_element_value(n, XML_ATTR_UNAME);
}
if (node == NULL && n) {
node = ID(n);
}
if (node == NULL) {
node = node_id;
}
if (node == NULL) {
crm_err("No node detected for event %s (%s)", magic, id);
goto bail;
}
/* look up where we expected it to be? */
desc = pcmk_strerror(pcmk_ok);
if (status == PCMK_LRM_OP_DONE && target_rc == rc) {
crm_notice("%s of %s on %s completed: %s", task, rsc, node, desc);
if (rc == PCMK_OCF_NOT_RUNNING) {
notify = FALSE;
}
} else if (status == PCMK_LRM_OP_DONE) {
desc = services_ocf_exitcode_str(rc);
crm_warn("%s of %s on %s failed: %s", task, rsc, node, desc);
} else {
desc = services_lrm_status_str(status);
crm_warn("%s of %s on %s failed: %s", task, rsc, node, desc);
}
if (notify && snmp_target) {
send_snmp_trap(node, rsc, task, target_rc, rc, status, desc);
}
if (notify && crm_mail_to) {
send_smtp_trap(node, rsc, task, target_rc, rc, status, desc);
}
if (notify && external_agent) {
send_custom_trap(node, rsc, task, target_rc, rc, status, desc);
}
bail:
free(update_te_uuid);
free(rsc);
free(task);
}
static gboolean
mon_trigger_refresh(gpointer user_data)
{
mainloop_set_trigger(refresh_trigger);
return FALSE;
}
#define NODE_PATT "/lrm[@id="
static char *get_node_from_xpath(const char *xpath)
{
char *nodeid = NULL;
char *tmp = strstr(xpath, NODE_PATT);
if(tmp) {
tmp += strlen(NODE_PATT);
tmp += 1;
nodeid = strdup(tmp);
tmp = strstr(nodeid, "\'");
CRM_ASSERT(tmp);
tmp[0] = 0;
}
return nodeid;
}
static void crm_diff_update_v2(const char *event, xmlNode * msg)
{
xmlNode *change = NULL;
xmlNode *diff = get_message_xml(msg, F_CIB_UPDATE_RESULT);
for (change = __xml_first_child(diff); change != NULL; change = __xml_next(change)) {
const char *name = NULL;
const char *op = crm_element_value(change, XML_DIFF_OP);
const char *xpath = crm_element_value(change, XML_DIFF_PATH);
xmlNode *match = NULL;
const char *node = NULL;
if(op == NULL) {
continue;
} else if(strcmp(op, "create") == 0) {
match = change->children;
} else if(strcmp(op, "move") == 0) {
continue;
} else if(strcmp(op, "delete") == 0) {
continue;
} else if(strcmp(op, "modify") == 0) {
match = first_named_child(change, XML_DIFF_RESULT);
if(match) {
match = match->children;
}
}
if(match) {
name = (const char *)match->name;
}
crm_trace("Handling %s operation for %s %p, %s", op, xpath, match, name);
if(xpath == NULL) {
/* Version field, ignore */
} else if(name == NULL) {
crm_debug("No result for %s operation to %s", op, xpath);
CRM_ASSERT(strcmp(op, "delete") == 0 || strcmp(op, "move") == 0);
} else if(strcmp(name, XML_TAG_CIB) == 0) {
xmlNode *state = NULL;
xmlNode *status = first_named_child(match, XML_CIB_TAG_STATUS);
for (state = __xml_first_child(status); state != NULL; state = __xml_next(state)) {
node = crm_element_value(state, XML_ATTR_UNAME);
if (node == NULL) {
node = ID(state);
}
handle_rsc_op(state, node);
}
} else if(strcmp(name, XML_CIB_TAG_STATUS) == 0) {
xmlNode *state = NULL;
for (state = __xml_first_child(match); state != NULL; state = __xml_next(state)) {
node = crm_element_value(state, XML_ATTR_UNAME);
if (node == NULL) {
node = ID(state);
}
handle_rsc_op(state, node);
}
} else if(strcmp(name, XML_CIB_TAG_STATE) == 0) {
node = crm_element_value(match, XML_ATTR_UNAME);
if (node == NULL) {
node = ID(match);
}
handle_rsc_op(match, node);
} else if(strcmp(name, XML_CIB_TAG_LRM) == 0) {
node = ID(match);
handle_rsc_op(match, node);
} else if(strcmp(name, XML_LRM_TAG_RESOURCES) == 0) {
char *local_node = get_node_from_xpath(xpath);
handle_rsc_op(match, local_node);
free(local_node);
} else if(strcmp(name, XML_LRM_TAG_RESOURCE) == 0) {
char *local_node = get_node_from_xpath(xpath);
handle_rsc_op(match, local_node);
free(local_node);
} else if(strcmp(name, XML_LRM_TAG_RSC_OP) == 0) {
char *local_node = get_node_from_xpath(xpath);
handle_rsc_op(match, local_node);
free(local_node);
} else {
crm_err("Ignoring %s operation for %s %p, %s", op, xpath, match, name);
}
}
}
static void crm_diff_update_v1(const char *event, xmlNode * msg)
{
/* Process operation updates */
xmlXPathObject *xpathObj = xpath_search(msg,
"//" F_CIB_UPDATE_RESULT "//" XML_TAG_DIFF_ADDED
"//" XML_LRM_TAG_RSC_OP);
int lpc = 0, max = numXpathResults(xpathObj);
for (lpc = 0; lpc < max; lpc++) {
xmlNode *rsc_op = getXpathResult(xpathObj, lpc);
handle_rsc_op(rsc_op, NULL);
}
freeXpathObject(xpathObj);
}
void
crm_diff_update(const char *event, xmlNode * msg)
{
int rc = -1;
long now = time(NULL);
static bool stale = FALSE;
static int updates = 0;
static mainloop_timer_t *refresh_timer = NULL;
xmlNode *diff = get_message_xml(msg, F_CIB_UPDATE_RESULT);
print_dot();
if(refresh_timer == NULL) {
refresh_timer = mainloop_timer_add("refresh", 2000, FALSE, mon_trigger_refresh, NULL);
}
if (current_cib != NULL) {
rc = xml_apply_patchset(current_cib, diff, TRUE);
switch (rc) {
case -pcmk_err_diff_resync:
case -pcmk_err_diff_failed:
crm_notice("[%s] Patch aborted: %s (%d)", event, pcmk_strerror(rc), rc);
free_xml(current_cib); current_cib = NULL;
break;
case pcmk_ok:
updates++;
break;
default:
crm_notice("[%s] ABORTED: %s (%d)", event, pcmk_strerror(rc), rc);
free_xml(current_cib); current_cib = NULL;
}
}
if (current_cib == NULL) {
crm_trace("Re-requesting the full cib");
cib->cmds->query(cib, NULL, ¤t_cib, cib_scope_local | cib_sync_call);
}
if (crm_mail_to || snmp_target || external_agent) {
int format = 0;
crm_element_value_int(diff, "format", &format);
switch(format) {
case 1:
crm_diff_update_v1(event, msg);
break;
case 2:
crm_diff_update_v2(event, msg);
break;
default:
crm_err("Unknown patch format: %d", format);
}
}
if (current_cib == NULL) {
if(!stale) {
print_as("--- Stale data ---");
}
stale = TRUE;
return;
}
stale = FALSE;
/* Refresh
* - immediately if the last update was more than 5s ago
* - every 10 updates
* - at most 2s after the last update
*/
if ((now - last_refresh) > (reconnect_msec / 1000)) {
mainloop_set_trigger(refresh_trigger);
mainloop_timer_stop(refresh_timer);
updates = 0;
} else if(updates > 10) {
mainloop_set_trigger(refresh_trigger);
mainloop_timer_stop(refresh_timer);
updates = 0;
} else {
mainloop_timer_start(refresh_timer);
}
}
gboolean
mon_refresh_display(gpointer user_data)
{
xmlNode *cib_copy = copy_xml(current_cib);
pe_working_set_t data_set;
last_refresh = time(NULL);
if (cli_config_update(&cib_copy, NULL, FALSE) == FALSE) {
if (cib) {
cib->cmds->signoff(cib);
}
print_as("Upgrade failed: %s", pcmk_strerror(-pcmk_err_schema_validation));
- if (as_console) {
+ if (output_format == mon_output_console) {
sleep(2);
}
clean_up(EX_USAGE);
return FALSE;
}
set_working_set_defaults(&data_set);
data_set.input = cib_copy;
cluster_status(&data_set);
- if (as_html_file || web_cgi) {
- if (print_html_status(&data_set, as_html_file, web_cgi) != 0) {
- fprintf(stderr, "Critical: Unable to output html file\n");
- clean_up(EX_USAGE);
- }
- } else if (as_xml) {
- if (print_xml_status(&data_set) != 0) {
- fprintf(stderr, "Critical: Unable to output xml file\n");
- clean_up(EX_USAGE);
- }
- } else if (daemonize) {
- /* do nothing */
+ /* Unpack constraints if any section will need them
+ * (tickets may be referenced in constraints but not granted yet,
+ * and bans need negative location constraints) */
+ if (show & (mon_show_bans | mon_show_tickets)) {
+ xmlNode *cib_constraints = get_object_root(XML_CIB_TAG_CONSTRAINTS, data_set.input);
+ unpack_constraints(cib_constraints, &data_set);
+ }
- } else if (simple_status) {
- print_simple_status(&data_set);
- if (has_warnings) {
- clean_up(EX_USAGE);
- }
+ switch (output_format) {
+ case mon_output_html:
+ case mon_output_cgi:
+ if (print_html_status(&data_set, output_filename) != 0) {
+ fprintf(stderr, "Critical: Unable to output html file\n");
+ clean_up(EX_USAGE);
+ }
+ break;
- } else {
- print_status(&data_set);
+ case mon_output_xml:
+ print_xml_status(&data_set);
+ break;
+
+ case mon_output_monitor:
+ print_simple_status(&data_set);
+ if (has_warnings) {
+ clean_up(MON_STATUS_WARN);
+ }
+ break;
+
+ case mon_output_plain:
+ case mon_output_console:
+ print_status(&data_set);
+ break;
+
+ case mon_output_none:
+ break;
}
cleanup_calculations(&data_set);
return TRUE;
}
void
mon_st_callback(stonith_t * st, stonith_event_t * e)
{
char *desc = crm_strdup_printf("Operation %s requested by %s for peer %s: %s (ref=%s)",
e->operation, e->origin, e->target, pcmk_strerror(e->result),
e->id);
if (snmp_target) {
send_snmp_trap(e->target, NULL, e->operation, pcmk_ok, e->result, 0, desc);
}
if (crm_mail_to) {
send_smtp_trap(e->target, NULL, e->operation, pcmk_ok, e->result, 0, desc);
}
if (external_agent) {
send_custom_trap(e->target, NULL, e->operation, pcmk_ok, e->result, 0, desc);
}
free(desc);
}
/*
* De-init ncurses, signoff from the CIB and deallocate memory.
*/
void
clean_up(int rc)
{
#if ENABLE_SNMP
netsnmp_session *session = crm_snmp_init(NULL, NULL);
if (session) {
snmp_close(session);
snmp_shutdown("snmpapp");
}
#endif
#if CURSES_ENABLED
- if (as_console) {
- as_console = FALSE;
+ if (output_format == mon_output_console) {
+ output_format = mon_output_plain;
echo();
nocbreak();
endwin();
}
#endif
if (cib != NULL) {
cib->cmds->signoff(cib);
cib_delete(cib);
cib = NULL;
}
- free(as_html_file);
+ free(output_filename);
free(xml_file);
free(pid_file);
if (rc >= 0) {
crm_exit(rc);
}
return;
}