1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*****************************************************************************\
* $Id:$
*****************************************************************************
* Copyright (C) 2004-2008 The Regents of the University of California.
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
* Written by Jim Garlick <garlick@llnl.gov>
* UCRL-CODE-2002-008.
*
* This file is part of PowerMan, a remote power management program.
* For details, see <http://www.llnl.gov/linux/powerman/>.
*
* PowerMan 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.
*
* PowerMan 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 PowerMan; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\*****************************************************************************/
/* ipmipower.c - simulate FreeIPMI ipmipower */
/* achu: I'm not supporting a full-fledged ipmipower, there are over
* 20 different configurations you can set (via cmdline, prompt, and
* config file) and 7 power control commands (on, off, stat, cycle,
* reset, pulse, soft). Many of the commands accepts all, hostrange,
* or single host.
*
* Assumptions:
*
* Users configure ipmipower authentication via FreeIPMI's
* configuration file. So there is no ipmipower authentication
* configuration via powerman. Thus, I assume there is no need for
* those configuration capabilities in this simulator (cmdline, config
* file, or prompt).
*
* Users configure hostnames via cmdline in the powerman.conf. So we
* support the -h/--hostname option on the command line, but not
* anywhere else.
*
* powerman's ipmipower.dev only utilizes on, off, and stat in
* ipmipower. So we only support those power control commands in the
* command line prompt of this simulator (not on the cmdline).
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#if HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <stdarg.h>
#include <libgen.h>
#include <assert.h>
#include "hash.h"
#include "hostlist.h"
#include "xread.h"
static void usage(void);
static void _noop_handler(int signum);
static void _prompt_loop(void);
static char *prog;
static char *hostname = NULL;
#define OPTIONS "p:h:"
#if HAVE_GETOPT_LONG
#define GETOPT(ac,av,opt,lopt) getopt_long(ac,av,opt,lopt,NULL)
static const struct option longopts[] = {
{ "hostname", required_argument, 0, 'h' },
{0, 0, 0, 0},
};
#else
#define GETOPT(ac,av,opt,lopt) getopt(ac,av,opt)
#endif
int
main(int argc, char *argv[])
{
int i, c;
prog = basename(argv[0]);
while ((c = GETOPT(argc, argv, OPTIONS, longopts)) != -1) {
switch (c) {
case 'h':
hostname = optarg;
break;
default:
usage();
}
}
if (optind < argc)
usage();
if (hostname == NULL)
usage();
if (signal(SIGPIPE, _noop_handler) == SIG_ERR) {
perror("signal");
exit(1);
}
_prompt_loop();
exit(0);
}
static void
usage(void)
{
fprintf(stderr, "Usage: %s -h hostlist\n", prog);
exit(1);
}
static void
_noop_handler(int signum)
{
fprintf(stderr, "%s: received signal %d\n", prog, signum);
}
#define CMD_PROMPT "ipmipower> "
#define OFF_STATUS "off"
#define ON_STATUS "on"
#define OK_STATUS "ok"
#define HASH_SIZE 1024
static void
_stat(hash_t hstatus, const char *nodes)
{
hostlist_iterator_t hlitr;
hostlist_t hlnodes;
char *node;
char *str;
assert(hstatus);
if (!(hlnodes = hostlist_create(nodes))) {
perror("hostlist_create");
exit(1);
}
if (!(hlitr = hostlist_iterator_create(hlnodes))) {
perror("hostlist_iterator_create");
exit(1);
}
while ((node = hostlist_next(hlitr))) {
if ((str = hash_find(hstatus, node)))
printf("%s: %s\n", node, str);
else
printf("%s: %s\n", node, "invalid hostname");
free(node);
}
hostlist_iterator_destroy(hlitr);
hostlist_destroy(hlnodes);
}
static void
_onoff(hash_t hstatus, const char *nodes, const char *state)
{
hostlist_iterator_t hlitr;
hostlist_t hlnodes;
char *node;
char *str;
assert(hstatus);
if (!(hlnodes = hostlist_create(nodes))) {
perror("hostlist_create");
exit(1);
}
if (!(hlitr = hostlist_iterator_create(hlnodes))) {
perror("hostlist_iterator_create");
exit(1);
}
while ((node = hostlist_next(hlitr))) {
if ((str = hash_find(hstatus, node))) {
printf("%s: %s\n", node, OK_STATUS);
hash_remove(hstatus, node);
if (!hash_insert(hstatus, (void *)node, (void *)state)) {
perror("hash_insert");
exit(1);
}
/* XXX: Don't free 'node' here, it needs to be alloc'd for
* the hash key. It's a mem-leak. Fix later.
*/
} else {
printf("%s: %s\n", node, "invalid hostname");
free(node);
}
}
hostlist_iterator_destroy(hlitr);
hostlist_destroy(hlnodes);
}
static void
_prompt_loop(void)
{
int i;
char buf[128];
char bufnode[128];
hash_t hstatus = NULL;
hostlist_t hl = NULL;
hostlist_iterator_t hlitr = NULL;
char *node;
assert(hostname);
if (!(hstatus = hash_create(HASH_SIZE,
(hash_key_f)hash_key_string,
(hash_cmp_f)strcmp,
(hash_del_f)NULL))) {
perror("hash_create");
exit(1);
}
if (!(hl = hostlist_create(hostname))) {
perror("hostlist_create");
exit(1);
}
if (!(hlitr = hostlist_iterator_create(hl))) {
perror("hostlist_iterator");
exit(1);
}
/* all nodes begin as off */
while ((node = hostlist_next(hlitr))) {
if (!hash_insert(hstatus, (void *)node, OFF_STATUS)) {
perror("hash_insert");
exit(1);
}
/* XXX: Don't free 'node' here, it needs to be alloc'd for
* the hash key. It's a mem-leak. Fix later.
*/
}
hostlist_iterator_destroy(hlitr);
hostlist_destroy(hl);
while (1) {
if (xreadline(CMD_PROMPT, buf, sizeof(buf)) == NULL) {
break;
} else if (strlen(buf) == 0) {
continue;
} else if (!strcmp(buf, "quit")) {
break;
} else if (!strcmp(buf, "stat")) {
_stat(hstatus, hostname);
} else if (sscanf(buf, "stat %s", bufnode) == 1) {
_stat(hstatus, bufnode);
} else if (!strcmp(buf, "on")) {
_onoff(hstatus, hostname, ON_STATUS);
} else if (sscanf(buf, "on %s", bufnode) == 1) {
_onoff(hstatus, bufnode, ON_STATUS);
} else if (!strcmp(buf, "off")) {
_onoff(hstatus, hostname, OFF_STATUS);
} else if (sscanf(buf, "off %s", bufnode) == 1) {
_onoff(hstatus, bufnode, OFF_STATUS);
} else
printf("unknown command - type \"help\"\n");
}
hash_destroy(hstatus);
}
/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/
|