root/tools/stonith_admin.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. add_env_params
  2. add_stonith_device
  3. add_tolerance
  4. add_stonith_params
  5. set_tag
  6. build_arg_context
  7. main

   1 /*
   2  * Copyright 2009-2020 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <sys/param.h>
  13 #include <stdio.h>
  14 #include <sys/types.h>
  15 #include <sys/stat.h>
  16 #include <unistd.h>
  17 #include <sys/utsname.h>
  18 
  19 #include <errno.h>
  20 #include <fcntl.h>
  21 #include <stdbool.h>
  22 #include <stdlib.h>
  23 #include <string.h>
  24 
  25 #include <crm/crm.h>
  26 #include <crm/msg_xml.h>
  27 #include <crm/common/ipc.h>
  28 #include <crm/cluster/internal.h>
  29 #include <crm/common/cmdline_internal.h>
  30 #include <crm/common/output_internal.h>
  31 
  32 #include <crm/stonith-ng.h>
  33 #include <crm/fencing/internal.h>
  34 #include <crm/cib.h>
  35 #include <crm/pengine/status.h>
  36 
  37 #include <crm/common/xml.h>
  38 #include <pacemaker-internal.h>
  39 
  40 #define SUMMARY "stonith_admin - Access the Pacemaker fencing API"
  41 
  42 char action = 0;
  43 
  44 struct {
  45     gboolean as_nodeid;
  46     gboolean broadcast;
  47     gboolean cleanup;
  48     gboolean installed;
  49     gboolean metadata;
  50     gboolean registered;
  51     gboolean validate_cfg;
  52     stonith_key_value_t *devices;
  53     stonith_key_value_t *params;
  54     int fence_level;
  55     int timeout ;
  56     int tolerance;
  57     int delay;
  58     char *agent;
  59     char *confirm_host;
  60     char *fence_host;
  61     char *history;
  62     char *last_fenced;
  63     char *query;
  64     char *reboot_host;
  65     char *register_dev;
  66     char *register_level;
  67     char *targets;
  68     char *terminate;
  69     char *unfence_host;
  70     char *unregister_dev;
  71     char *unregister_level;
  72 } options = {
  73     .timeout = 120,
  74     .delay = 0
  75 };
  76 
  77 gboolean add_env_params(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);
  78 gboolean add_stonith_device(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);
  79 gboolean add_stonith_params(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);
  80 gboolean add_tolerance(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);
  81 gboolean set_tag(const gchar *option_name, const gchar *optarg, gpointer data, GError **error);
  82 
  83 #define INDENT "                                    "
  84 
  85 /* *INDENT-OFF* */
  86 static GOptionEntry defn_entries[] = {
  87     { "register", 'R', 0, G_OPTION_ARG_STRING, &options.register_dev,
  88       "Register the named stonith device. Requires: --agent.\n"
  89       INDENT "Optional: --option, --env-option.",
  90       "DEVICE" },
  91     { "deregister", 'D', 0, G_OPTION_ARG_STRING, &options.unregister_dev,
  92       "De-register the named stonith device.",
  93       "DEVICE" },
  94     { "register-level", 'r', 0, G_OPTION_ARG_STRING, &options.register_level,
  95       "Register a stonith level for the named target,\n"
  96       INDENT "specified as one of NAME, @PATTERN, or ATTR=VALUE.\n"
  97       INDENT "Requires: --index and one or more --device entries.",
  98       "TARGET" },
  99     { "deregister-level", 'd', 0, G_OPTION_ARG_STRING, &options.unregister_level,
 100       "Unregister a stonith level for the named target,\n"
 101       INDENT "specified as for --register-level. Requires: --index",
 102       "TARGET" },
 103 
 104     { NULL }
 105 };
 106 
 107 static GOptionEntry query_entries[] = {
 108     { "list", 'l', 0, G_OPTION_ARG_STRING, &options.terminate,
 109       "List devices that can terminate the specified host.\n"
 110       INDENT "Optional: --timeout",
 111       "HOST" },
 112     { "list-registered", 'L', 0, G_OPTION_ARG_NONE, &options.registered,
 113       "List all registered devices. Optional: --timeout.",
 114       NULL },
 115     { "list-installed", 'I', 0, G_OPTION_ARG_NONE, &options.installed,
 116       "List all installed devices. Optional: --timeout.",
 117       NULL },
 118     { "list-targets", 's', 0, G_OPTION_ARG_STRING, &options.targets,
 119       "List the targets that can be fenced by the\n"
 120       INDENT "named device. Optional: --timeout.",
 121       "DEVICE" },
 122     { "metadata", 'M', 0, G_OPTION_ARG_NONE, &options.metadata,
 123       "Show agent metadata. Requires: --agent.\n"
 124       INDENT "Optional: --timeout.",
 125       NULL },
 126     { "query", 'Q', 0, G_OPTION_ARG_STRING, &options.query,
 127       "Check the named device's status. Optional: --timeout.",
 128       "DEVICE" },
 129     { "history", 'H', 0, G_OPTION_ARG_STRING, &options.history,
 130       "Show last successful fencing operation for named node\n"
 131       INDENT "(or '*' for all nodes). Optional: --timeout, --cleanup,\n"
 132       INDENT "--quiet (show only the operation's epoch timestamp),\n"
 133       INDENT "--verbose (show all recorded and pending operations),\n"
 134       INDENT "--broadcast (update history from all nodes available).",
 135       "NODE" },
 136     { "last", 'h', 0, G_OPTION_ARG_STRING, &options.last_fenced,
 137       "Indicate when the named node was last fenced.\n"
 138       INDENT "Optional: --as-node-id.",
 139       "NODE" },
 140     { "validate", 'K', 0, G_OPTION_ARG_NONE, &options.validate_cfg,
 141       "Validate a fence device configuration.\n"
 142       INDENT "Requires: --agent. Optional: --option, --env-option,\n"
 143       INDENT "--quiet (print no output, only return status).",
 144       NULL },
 145 
 146     { NULL }
 147 };
 148 
 149 static GOptionEntry fence_entries[] = {
 150     { "fence", 'F', 0, G_OPTION_ARG_STRING, &options.fence_host,
 151       "Fence named host. Optional: --timeout, --tolerance, --delay.",
 152       "HOST" },
 153     { "unfence", 'U', 0, G_OPTION_ARG_STRING, &options.unfence_host,
 154       "Unfence named host. Optional: --timeout, --tolerance, --delay.",
 155       "HOST" },
 156     { "reboot", 'B', 0, G_OPTION_ARG_STRING, &options.reboot_host,
 157       "Reboot named host. Optional: --timeout, --tolerance, --delay.",
 158       "HOST" },
 159     { "confirm", 'C', 0, G_OPTION_ARG_STRING, &options.confirm_host,
 160       "Tell cluster that named host is now safely down.",
 161       "HOST", },
 162 
 163     { NULL }
 164 };
 165 
 166 static GOptionEntry addl_entries[] = {
 167     { "cleanup", 'c', 0, G_OPTION_ARG_NONE, &options.cleanup,
 168       "Cleanup wherever appropriate. Requires --history.",
 169       NULL },
 170     { "broadcast", 'b', 0, G_OPTION_ARG_NONE, &options.broadcast,
 171       "Broadcast wherever appropriate.",
 172       NULL },
 173     { "agent", 'a', 0, G_OPTION_ARG_STRING, &options.agent,
 174       "The agent to use (for example, fence_xvm;\n"
 175       INDENT "with --register, --metadata, --validate).",
 176       "AGENT" },
 177     { "option", 'o', 0, G_OPTION_ARG_CALLBACK, add_stonith_params,
 178       "Specify a device configuration parameter as NAME=VALUE\n"
 179       INDENT "(may be specified multiple times; with --register,\n"
 180       INDENT "--validate).",
 181       "PARAM" },
 182     { "env-option", 'e', 0, G_OPTION_ARG_CALLBACK, add_env_params,
 183       "Specify a device configuration parameter with the\n"
 184       INDENT "specified name, using the value of the\n"
 185       INDENT "environment variable of the same name prefixed with\n"
 186       INDENT "OCF_RESKEY_ (may be specified multiple times;\n"
 187       INDENT "with --register, --validate).",
 188       "PARAM" },
 189     { "tag", 'T', 0, G_OPTION_ARG_CALLBACK, set_tag,
 190       "Identify fencing operations in logs with the specified\n"
 191       INDENT "tag; useful when multiple entities might invoke\n"
 192       INDENT "stonith_admin (used with most commands).",
 193       "TAG" },
 194     { "device", 'v', 0, G_OPTION_ARG_CALLBACK, add_stonith_device,
 195       "Device ID (with --register-level, device to associate with\n"
 196       INDENT "a given host and level; may be specified multiple times)"
 197 #if SUPPORT_CIBSECRETS
 198       "\n" INDENT "(with --validate, name to use to load CIB secrets)"
 199 #endif
 200       ".",
 201       "DEVICE" },
 202     { "index", 'i', 0, G_OPTION_ARG_INT, &options.fence_level,
 203       "The stonith level (1-9) (with --register-level,\n"
 204       INDENT "--deregister-level).",
 205       "LEVEL" },
 206     { "timeout", 't', 0, G_OPTION_ARG_INT, &options.timeout,
 207       "Operation timeout in seconds (default 120;\n"
 208       INDENT "used with most commands).",
 209       "SECONDS" },
 210     { "delay", 'y', 0, G_OPTION_ARG_INT, &options.delay,
 211       "Apply a fencing delay in seconds. Any static/random delays from\n"
 212       INDENT "pcmk_delay_base/max will be added, otherwise all\n"
 213       INDENT "disabled with the value -1\n"
 214       INDENT "(default 0; with --fence, --reboot, --unfence).",
 215       "SECONDS" },
 216     { "as-node-id", 'n', 0, G_OPTION_ARG_NONE, &options.as_nodeid,
 217       "(Advanced) The supplied node is the corosync node ID\n"
 218       INDENT "(with --last).",
 219       NULL },
 220     { "tolerance", 0, 0, G_OPTION_ARG_CALLBACK, add_tolerance,
 221       "(Advanced) Do nothing if an equivalent --fence request\n"
 222       INDENT "succeeded less than this many seconds earlier\n"
 223       INDENT "(with --fence, --unfence, --reboot).",
 224       "SECONDS" },
 225 
 226     { NULL }
 227 };
 228 /* *INDENT-ON* */
 229 
 230 static pcmk__supported_format_t formats[] = {
 231     PCMK__SUPPORTED_FORMAT_HTML,
 232     PCMK__SUPPORTED_FORMAT_TEXT,
 233     PCMK__SUPPORTED_FORMAT_XML,
 234     { NULL, NULL, NULL }
 235 };
 236 
 237 static const int st_opts = st_opt_sync_call | st_opt_allow_suicide;
 238 
 239 static char *name = NULL;
 240 
 241 gboolean
 242 add_env_params(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
     /* [previous][next][first][last][top][bottom][index][help] */
 243     char *key = crm_strdup_printf("OCF_RESKEY_%s", optarg);
 244     const char *env = getenv(key);
 245     gboolean retval = TRUE;
 246 
 247     if (env == NULL) {
 248         g_set_error(error, PCMK__EXITC_ERROR, CRM_EX_INVALID_PARAM, "Invalid option: -e %s", optarg);
 249         retval = FALSE;
 250     } else {
 251         crm_info("Got: '%s'='%s'", optarg, env);
 252         options.params = stonith_key_value_add(options.params, optarg, env);
 253     }
 254 
 255     free(key);
 256     return retval;
 257 }
 258 
 259 gboolean
 260 add_stonith_device(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
     /* [previous][next][first][last][top][bottom][index][help] */
 261     options.devices = stonith_key_value_add(options.devices, NULL, optarg);
 262     return TRUE;
 263 }
 264 
 265 gboolean
 266 add_tolerance(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
     /* [previous][next][first][last][top][bottom][index][help] */
 267     options.tolerance = crm_get_msec(optarg) / 1000;
 268     return TRUE;
 269 }
 270 
 271 gboolean
 272 add_stonith_params(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
     /* [previous][next][first][last][top][bottom][index][help] */
 273     char *name = NULL;
 274     char *value = NULL;
 275     int rc = 0;
 276     gboolean retval = TRUE;
 277 
 278     crm_info("Scanning: -o %s", optarg);
 279 
 280     rc = pcmk_scan_nvpair(optarg, &name, &value);
 281 
 282     if (rc != 2) {
 283         rc = pcmk_legacy2rc(rc);
 284         g_set_error(error, PCMK__RC_ERROR, rc, "Invalid option: -o %s: %s", optarg, pcmk_rc_str(rc));
 285         retval = FALSE;
 286     } else {
 287         crm_info("Got: '%s'='%s'", name, value);
 288         options.params = stonith_key_value_add(options.params, name, value);
 289     }
 290 
 291     free(name);
 292     free(value);
 293     return retval;
 294 }
 295 
 296 gboolean
 297 set_tag(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) {
     /* [previous][next][first][last][top][bottom][index][help] */
 298     free(name);
 299     name = crm_strdup_printf("%s.%s", crm_system_name, optarg);
 300     return TRUE;
 301 }
 302 
 303 static GOptionContext *
 304 build_arg_context(pcmk__common_args_t *args, GOptionGroup **group) {
     /* [previous][next][first][last][top][bottom][index][help] */
 305     GOptionContext *context = NULL;
 306 
 307     GOptionEntry extra_prog_entries[] = {
 308         { "quiet", 'q', 0, G_OPTION_ARG_NONE, &(args->quiet),
 309           "Be less descriptive in output.",
 310           NULL },
 311 
 312         { NULL }
 313     };
 314 
 315     context = pcmk__build_arg_context(args, "text (default), html, xml", group, NULL);
 316 
 317     /* Add the -q option, which cannot be part of the globally supported options
 318      * because some tools use that flag for something else.
 319      */
 320     pcmk__add_main_args(context, extra_prog_entries);
 321 
 322     pcmk__add_arg_group(context, "definition", "Device Definition Commands:",
 323                         "Show device definition help", defn_entries);
 324     pcmk__add_arg_group(context, "queries", "Queries:",
 325                         "Show query help", query_entries);
 326     pcmk__add_arg_group(context, "fence", "Fencing Commands:",
 327                         "Show fence help", fence_entries);
 328     pcmk__add_arg_group(context, "additional", "Additional Options:",
 329                         "Show additional options", addl_entries);
 330     return context;
 331 }
 332 
 333 int
 334 main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 335 {
 336     int rc = 0;
 337     bool no_connect = false;
 338     bool required_agent = false;
 339 
 340     char *target = NULL;
 341     const char *device = NULL;
 342 
 343     crm_exit_t exit_code = CRM_EX_OK;
 344     stonith_t *st = NULL;
 345 
 346     pcmk__output_t *out = NULL;
 347     pcmk__common_args_t *args = pcmk__new_common_args(SUMMARY);
 348 
 349     GError *error = NULL;
 350     GOptionContext *context = NULL;
 351     GOptionGroup *output_group = NULL;
 352     gchar **processed_args = NULL;
 353 
 354     context = build_arg_context(args, &output_group);
 355     pcmk__register_formats(output_group, formats);
 356 
 357     crm_log_cli_init("stonith_admin");
 358 
 359     name = strdup(crm_system_name);
 360 
 361     processed_args = pcmk__cmdline_preproc(argv, "adehilorstvBCDFHQRTU");
 362 
 363     if (!g_option_context_parse_strv(context, &processed_args, &error)) {
 364         fprintf(stderr, "%s: %s\n", g_get_prgname(), error->message);
 365         exit_code = CRM_EX_USAGE;
 366         goto done;
 367     }
 368 
 369     for (int i = 0; i < args->verbosity; i++) {
 370         crm_bump_log_level(argc, argv);
 371     }
 372 
 373     rc = pcmk__output_new(&out, args->output_ty, args->output_dest, argv);
 374     if (rc != pcmk_rc_ok) {
 375         fprintf(stderr, "Error creating output format %s: %s\n",
 376                 args->output_ty, pcmk_rc_str(rc));
 377         exit_code = CRM_EX_ERROR;
 378         goto done;
 379     }
 380 
 381     stonith__register_messages(out);
 382 
 383     if (args->version) {
 384         out->version(out, false);
 385         goto done;
 386     }
 387 
 388     if (options.validate_cfg) {
 389         required_agent = true;
 390         no_connect = true;
 391         action = 'K';
 392     }
 393 
 394     if (options.installed) {
 395         no_connect = true;
 396         action = 'I';
 397     }
 398 
 399     if (options.registered) {
 400         action = 'L';
 401     }
 402 
 403     if (options.register_dev != NULL) {
 404         required_agent = true;
 405         action = 'R';
 406         device = options.register_dev;
 407     }
 408 
 409     if (options.query != NULL) {
 410         action = 'Q';
 411         device = options.query;
 412     }
 413 
 414     if (options.unregister_dev != NULL) {
 415         action = 'D';
 416         device = options.unregister_dev;
 417     }
 418 
 419     if (options.targets != NULL) {
 420         action = 's';
 421         device = options.targets;
 422     }
 423 
 424     if (options.terminate != NULL) {
 425         action = 'L';
 426         target = options.terminate;
 427     }
 428 
 429     if (options.metadata) {
 430         no_connect = true;
 431         required_agent = true;
 432         action = 'M';
 433     }
 434 
 435     if (options.reboot_host != NULL) {
 436         no_connect = true;
 437         action = 'B';
 438         target = options.reboot_host;
 439         crm_log_args(argc, argv);
 440     }
 441 
 442     if (options.fence_host != NULL) {
 443         no_connect = true;
 444         action = 'F';
 445         target = options.fence_host;
 446         crm_log_args(argc, argv);
 447     }
 448 
 449     if (options.unfence_host != NULL) {
 450         no_connect = true;
 451         action = 'U';
 452         target = options.unfence_host;
 453         crm_log_args(argc, argv);
 454     }
 455 
 456     if (options.confirm_host != NULL) {
 457         action = 'C';
 458         target = options.confirm_host;
 459         crm_log_args(argc, argv);
 460     }
 461 
 462     if (options.last_fenced != NULL) {
 463         action = 'h';
 464         target = options.last_fenced;
 465     }
 466 
 467     if (options.history != NULL) {
 468         action = 'H';
 469         target = options.history;
 470     }
 471 
 472     if (options.register_level != NULL) {
 473         action = 'r';
 474         target = options.register_level;
 475     }
 476 
 477     if (options.unregister_level != NULL) {
 478         action = 'd';
 479         target = options.unregister_level;
 480     }
 481 
 482     if (optind > argc || action == 0) {
 483         char *help = g_option_context_get_help(context, TRUE, NULL);
 484 
 485         out->err(out, "%s", help);
 486         g_free(help);
 487         exit_code = CRM_EX_USAGE;
 488         goto done;
 489     }
 490 
 491     if (required_agent && options.agent == NULL) {
 492         char *help = g_option_context_get_help(context, TRUE, NULL);
 493 
 494         out->err(out, "Please specify an agent to query using -a,--agent [value]");
 495         out->err(out, "%s", help);
 496         g_free(help);
 497         exit_code = CRM_EX_USAGE;
 498         goto done;
 499     }
 500 
 501     out->quiet = args->quiet;
 502 
 503     st = stonith_api_new();
 504     if (st == NULL) {
 505         rc = -ENOMEM;
 506     } else if (!no_connect) {
 507         rc = st->cmds->connect(st, name, NULL);
 508     }
 509     if (rc < 0) {
 510         out->err(out, "Could not connect to fencer: %s", pcmk_strerror(rc));
 511         exit_code = CRM_EX_DISCONNECT;
 512         goto done;
 513     }
 514 
 515     switch (action) {
 516         case 'I':
 517             rc = pcmk__fence_installed(out, st, options.timeout*1000);
 518             if (rc != pcmk_rc_ok) {
 519                 out->err(out, "Failed to list installed devices: %s", pcmk_strerror(rc));
 520             }
 521 
 522             break;
 523 
 524         case 'L':
 525             rc = pcmk__fence_registered(out, st, target, options.timeout*1000);
 526             if (rc != pcmk_rc_ok) {
 527                 out->err(out, "Failed to list registered devices: %s", pcmk_strerror(rc));
 528             }
 529 
 530             break;
 531 
 532         case 'Q':
 533             rc = st->cmds->monitor(st, st_opts, device, options.timeout);
 534             if (rc != pcmk_rc_ok) {
 535                 rc = st->cmds->list(st, st_opts, device, NULL, options.timeout);
 536             }
 537             rc = pcmk_legacy2rc(rc);
 538             break;
 539 
 540         case 's':
 541             rc = pcmk__fence_list_targets(out, st, device, options.timeout*1000);
 542             if (rc != pcmk_rc_ok) {
 543                 out->err(out, "Couldn't list targets: %s", pcmk_strerror(rc));
 544             }
 545 
 546             break;
 547 
 548         case 'R':
 549             rc = st->cmds->register_device(st, st_opts, device, NULL, options.agent,
 550                                            options.params);
 551             rc = pcmk_legacy2rc(rc);
 552             break;
 553 
 554         case 'D':
 555             rc = st->cmds->remove_device(st, st_opts, device);
 556             rc = pcmk_legacy2rc(rc);
 557             break;
 558 
 559         case 'd':
 560             rc = pcmk__fence_unregister_level(st, target, options.fence_level);
 561             break;
 562 
 563         case 'r':
 564             rc = pcmk__fence_register_level(st, target, options.fence_level, options.devices);
 565             break;
 566 
 567         case 'M':
 568             rc = pcmk__fence_metadata(out, st, options.agent, options.timeout*1000);
 569             if (rc != pcmk_rc_ok) {
 570                 out->err(out, "Can't get fence agent meta-data: %s", pcmk_strerror(rc));
 571             }
 572 
 573             break;
 574 
 575         case 'C':
 576             rc = st->cmds->confirm(st, st_opts, target);
 577             rc = pcmk_legacy2rc(rc);
 578             break;
 579 
 580         case 'B':
 581             rc = pcmk__fence_action(st, target, "reboot", name, options.timeout*1000,
 582                                     options.tolerance*1000, options.delay);
 583             break;
 584 
 585         case 'F':
 586             rc = pcmk__fence_action(st, target, "off", name, options.timeout*1000,
 587                                     options.tolerance*1000, options.delay);
 588             break;
 589 
 590         case 'U':
 591             rc = pcmk__fence_action(st, target, "on", name, options.timeout*1000,
 592                                     options.tolerance*1000, options.delay);
 593             break;
 594 
 595         case 'h':
 596             rc = pcmk__fence_last(out, target, options.as_nodeid);
 597             break;
 598 
 599         case 'H':
 600             rc = pcmk__fence_history(out, st, target, options.timeout*1000, args->verbosity,
 601                                      options.broadcast, options.cleanup);
 602             break;
 603 
 604         case 'K':
 605             device = options.devices ? options.devices->key : NULL;
 606             rc = pcmk__fence_validate(out, st, options.agent, device, options.params,
 607                                         options.timeout*1000);
 608             break;
 609     }
 610 
 611     crm_info("Command returned: %s (%d)", pcmk_rc_str(rc), rc);
 612     exit_code = pcmk_rc2exitc(rc);
 613 
 614   done:
 615     g_strfreev(processed_args);
 616     g_clear_error(&error);
 617     pcmk__free_arg_context(context);
 618 
 619     if (out != NULL) {
 620         out->finish(out, exit_code, true, NULL);
 621         pcmk__output_free(out);
 622     }
 623     free(name);
 624     stonith_key_value_freeall(options.params, 1, 1);
 625 
 626     if (st != NULL) {
 627         st->cmds->disconnect(st);
 628         stonith_api_delete(st);
 629     }
 630 
 631     return exit_code;
 632 }

/* [previous][next][first][last][top][bottom][index][help] */