/src/modules/nodeupdown.c

https://code.google.com/ · C · 155 lines · 86 code · 24 blank · 45 comment · 9 complexity · 33a0311fc32fc9fd09e24fdd2fa44382 MD5 · raw file

  1. /*****************************************************************************\
  2. * $Id$
  3. *****************************************************************************
  4. * Copyright (C) 2001-2006 The Regents of the University of California.
  5. * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  6. * Written by Jim Garlick <garlick@llnl.gov>.
  7. * UCRL-CODE-2003-005.
  8. *
  9. * This file is part of Pdsh, a parallel remote shell program.
  10. * For details, see <http://www.llnl.gov/linux/pdsh/>.
  11. *
  12. * Pdsh is free software; you can redistribute it and/or modify it under
  13. * the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; either version 2 of the License, or (at your option)
  15. * any later version.
  16. *
  17. * Pdsh is distributed in the hope that it will be useful, but WITHOUT ANY
  18. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with Pdsh; if not, write to the Free Software Foundation, Inc.,
  24. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  25. \*****************************************************************************/
  26. #if HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <nodeupdown.h>
  32. #include "src/common/hostlist.h"
  33. #include "src/common/err.h"
  34. #include "src/common/xmalloc.h"
  35. #include "src/pdsh/mod.h"
  36. #ifndef GENDERS_ALTNAME_ATTRIBUTE
  37. # define GENDERS_ALTNAME_ATTRIBUTE "altname"
  38. #endif
  39. #if STATIC_MODULES
  40. # define pdsh_module_info nodeupdown_module_info
  41. # define pdsh_module_priority nodeupdown_module_priority
  42. #endif
  43. int pdsh_module_priority = 120;
  44. static int mod_nodeupdown_postop(opt_t *opt);
  45. static int nodeupdown_opt_v(opt_t *, int, char *);
  46. static void remove_all_down_nodes(hostlist_t);
  47. static bool verify = false;
  48. /*
  49. * Export pdsh module operations structure
  50. */
  51. struct pdsh_module_operations nodeupdown_module_ops = {
  52. (ModInitF) NULL,
  53. (ModExitF) NULL,
  54. (ModReadWcollF) NULL,
  55. (ModPostOpF) mod_nodeupdown_postop
  56. };
  57. /*
  58. * Export rcmd module operations
  59. */
  60. struct pdsh_rcmd_operations nodeupdown_rcmd_ops = {
  61. (RcmdInitF) NULL,
  62. (RcmdSigF) NULL,
  63. (RcmdF) NULL,
  64. };
  65. /*
  66. * Export module options
  67. */
  68. struct pdsh_module_option nodeupdown_module_options[] =
  69. { { 'v', NULL, "exclude targets if they are down",
  70. DSH | PCP, (optFunc) nodeupdown_opt_v
  71. },
  72. PDSH_OPT_TABLE_END
  73. };
  74. /*
  75. * Nodeupdown module info
  76. */
  77. struct pdsh_module pdsh_module_info = {
  78. "misc",
  79. "nodeupdown",
  80. "Al Chu <chu11@llnl.gov>",
  81. "remove targets if down according to libnodeupdown",
  82. DSH | PCP,
  83. &nodeupdown_module_ops,
  84. &nodeupdown_rcmd_ops,
  85. &nodeupdown_module_options[0],
  86. };
  87. static int nodeupdown_opt_v(opt_t *pdsh_opt, int opt, char *arg)
  88. {
  89. verify = true;
  90. return 0;
  91. }
  92. static int mod_nodeupdown_postop(opt_t *opt)
  93. {
  94. if (!verify || !opt->wcoll)
  95. return 0;
  96. remove_all_down_nodes(opt->wcoll);
  97. return 0;
  98. }
  99. /*
  100. * Remove down nodes from hostlist wcoll using "nodeupdown_is_down_node"
  101. * on each member of wcoll. Supposedly, it doesn't matter whether you
  102. * pass in the canonical or altname.
  103. */
  104. static void
  105. remove_all_down_nodes(hostlist_t wcoll)
  106. {
  107. nodeupdown_t nh = NULL;
  108. char * host = NULL;
  109. hostlist_iterator_t i = NULL;
  110. if ((nh = nodeupdown_handle_create()) == NULL)
  111. errx("%p: Unable to create nodeupdown handle.\n");
  112. #if HAVE_NODEUPDOWN_LOAD_DATA_6
  113. if (nodeupdown_load_data(nh, NULL, NULL, NULL, 0, 0) < 0)
  114. #else
  115. if (nodeupdown_load_data(nh, NULL, 0, 0, NULL) < 0)
  116. #endif
  117. errx("%p: nodeupdown: %s\n", nodeupdown_errormsg(nh));
  118. i = hostlist_iterator_create(wcoll);
  119. while ((host = hostlist_next(i))) {
  120. if (nodeupdown_is_node_down(nh, host) > 0)
  121. hostlist_remove(i);
  122. free(host);
  123. }
  124. hostlist_iterator_destroy(i);
  125. if (nodeupdown_handle_destroy(nh) < 0)
  126. err("%p: nodeupdown_handle_destroy: %s\n", nodeupdown_errormsg(nh));
  127. return;
  128. }
  129. /*
  130. * vi: tabstop=4 shiftwidth=4 expandtab
  131. */