/src/modules/nodeupdown.c
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 30#include <stdio.h> 31#include <stdlib.h> 32#include <nodeupdown.h> 33 34#include "src/common/hostlist.h" 35#include "src/common/err.h" 36#include "src/common/xmalloc.h" 37#include "src/pdsh/mod.h" 38 39#ifndef GENDERS_ALTNAME_ATTRIBUTE 40# define GENDERS_ALTNAME_ATTRIBUTE "altname" 41#endif 42 43#if STATIC_MODULES 44# define pdsh_module_info nodeupdown_module_info 45# define pdsh_module_priority nodeupdown_module_priority 46#endif 47 48int pdsh_module_priority = 120; 49 50static int mod_nodeupdown_postop(opt_t *opt); 51static int nodeupdown_opt_v(opt_t *, int, char *); 52static void remove_all_down_nodes(hostlist_t); 53 54static bool verify = false; 55 56/* 57 * Export pdsh module operations structure 58 */ 59struct pdsh_module_operations nodeupdown_module_ops = { 60 (ModInitF) NULL, 61 (ModExitF) NULL, 62 (ModReadWcollF) NULL, 63 (ModPostOpF) mod_nodeupdown_postop 64}; 65 66/* 67 * Export rcmd module operations 68 */ 69struct pdsh_rcmd_operations nodeupdown_rcmd_ops = { 70 (RcmdInitF) NULL, 71 (RcmdSigF) NULL, 72 (RcmdF) NULL, 73}; 74 75/* 76 * Export module options 77 */ 78struct pdsh_module_option nodeupdown_module_options[] = 79 { { 'v', NULL, "exclude targets if they are down", 80 DSH | PCP, (optFunc) nodeupdown_opt_v 81 }, 82 PDSH_OPT_TABLE_END 83 }; 84 85/* 86 * Nodeupdown module info 87 */ 88struct pdsh_module pdsh_module_info = { 89 "misc", 90 "nodeupdown", 91 "Al Chu <chu11@llnl.gov>", 92 "remove targets if down according to libnodeupdown", 93 DSH | PCP, 94 95 &nodeupdown_module_ops, 96 &nodeupdown_rcmd_ops, 97 &nodeupdown_module_options[0], 98}; 99 100static int nodeupdown_opt_v(opt_t *pdsh_opt, int opt, char *arg) 101{ 102 verify = true; 103 return 0; 104} 105 106static int mod_nodeupdown_postop(opt_t *opt) 107{ 108 if (!verify || !opt->wcoll) 109 return 0; 110 111 remove_all_down_nodes(opt->wcoll); 112 113 return 0; 114} 115 116/* 117 * Remove down nodes from hostlist wcoll using "nodeupdown_is_down_node" 118 * on each member of wcoll. Supposedly, it doesn't matter whether you 119 * pass in the canonical or altname. 120 */ 121static void 122remove_all_down_nodes(hostlist_t wcoll) 123{ 124 nodeupdown_t nh = NULL; 125 char * host = NULL; 126 hostlist_iterator_t i = NULL; 127 128 if ((nh = nodeupdown_handle_create()) == NULL) 129 errx("%p: Unable to create nodeupdown handle.\n"); 130 131#if HAVE_NODEUPDOWN_LOAD_DATA_6 132 if (nodeupdown_load_data(nh, NULL, NULL, NULL, 0, 0) < 0) 133#else 134 if (nodeupdown_load_data(nh, NULL, 0, 0, NULL) < 0) 135#endif 136 errx("%p: nodeupdown: %s\n", nodeupdown_errormsg(nh)); 137 138 i = hostlist_iterator_create(wcoll); 139 while ((host = hostlist_next(i))) { 140 if (nodeupdown_is_node_down(nh, host) > 0) 141 hostlist_remove(i); 142 free(host); 143 } 144 hostlist_iterator_destroy(i); 145 146 if (nodeupdown_handle_destroy(nh) < 0) 147 err("%p: nodeupdown_handle_destroy: %s\n", nodeupdown_errormsg(nh)); 148 149 return; 150} 151 152 153/* 154 * vi: tabstop=4 shiftwidth=4 expandtab 155 */