📄 baytech.c
字号:
/* $Id: baytech.c,v 1.23 2005/01/25 19:54:05 gshi Exp $ *//* * Stonith module for BayTech Remote Power Controllers (RPC-x devices) * * Copyright (c) 2000 Alan Robertson <alanr@unix.sh> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#define DEVICE "BayTech power switch"#define DOESNT_USE_STONITHKILLCOMM 1#include "stonith_plugin_common.h"#define PIL_PLUGIN baytech#define PIL_PLUGIN_S "baytech"#define PIL_PLUGINLICENSE LICENSE_LGPL#define PIL_PLUGINLICENSEURL URL_LGPL#include <pils/plugin.h>#include "stonith_signal.h"static StonithPlugin * baytech_new(void);static void baytech_destroy(StonithPlugin *);static int baytech_set_config(StonithPlugin *, StonithNVpair *);static const char ** baytech_get_confignames(StonithPlugin * s);static const char * baytech_get_info(StonithPlugin * s, int InfoType);static int baytech_status(StonithPlugin *);static int baytech_reset_req(StonithPlugin * s, int request, const char * host);static char ** baytech_hostlist(StonithPlugin *);static struct stonith_ops baytechOps ={ baytech_new, /* Create new STONITH object */ baytech_destroy, /* Destroy STONITH object */ baytech_get_info, /* Return STONITH info string */ baytech_get_confignames, /* Return STONITH config vars */ baytech_set_config, /* set configuration from vars */ baytech_status, /* Return STONITH device status */ baytech_reset_req, /* Request a reset */ baytech_hostlist, /* Return list of supported hosts */};PIL_PLUGIN_BOILERPLATE2("1.0", Debug)static const PILPluginImports* PluginImports;static PILPlugin* OurPlugin;static PILInterface* OurInterface;static StonithImports* OurImports;static void* interfprivate;#include "stonith_expect_helpers.h"#define MAXOUTLET 32PIL_rcPIL_PLUGIN_INIT(PILPlugin*us, const PILPluginImports* imports);PIL_rcPIL_PLUGIN_INIT(PILPlugin*us, const PILPluginImports* imports){ /* Force the compiler to do a little type checking */ (void)(PILPluginInitFun)PIL_PLUGIN_INIT; PluginImports = imports; OurPlugin = us; /* Register ourself as a plugin */ imports->register_plugin(us, &OurPIExports); /* Register our interface implementation */ return imports->register_interface(us, PIL_PLUGINTYPE_S , PIL_PLUGIN_S , &baytechOps , NULL /*close */ , &OurInterface , (void*)&OurImports , &interfprivate); }/* * I have an RPC-5. This code has been tested with this switch. * * The BayTech switches are quite nice, but the dialogues are a bit of a * pain for mechanical parsing. */struct pluginDevice { StonithPlugin sp; const char * pluginid; char * idinfo; char * unitid; const struct BayTechModelInfo* modelinfo; pid_t pid; int rdfd; int wrfd; char * device; char * user; char * passwd;};struct BayTechModelInfo { const char * type; /* Baytech model info */ size_t socklen; /* Length of socket name string */ struct Etoken * expect; /* Expect string before outlet list */};static int parse_socket_line(struct pluginDevice*,const char *, int *, char *);static const char * pluginid = "BayTech-Stonith";static const char * NOTpluginID = "Hey, dummy this has been destroyed (BayTech)";/* * Different expect strings that we get from the Baytech * Remote Power Controllers... */#define BAYTECHASSOC "Bay Technical Associates"static struct Etoken BayTechAssoc[] = { {BAYTECHASSOC, 0, 0}, {NULL,0,0}};static struct Etoken UnitId[] = { {"Unit ID: ", 0, 0}, {NULL,0,0}};static struct Etoken login[] = { {"username>", 0, 0} ,{NULL,0,0}};static struct Etoken password[] = { {"password>", 0, 0} , {"username>", 0, 0} ,{NULL,0,0}};static struct Etoken Selection[] = { {"election>", 0, 0} ,{NULL,0,0}};static struct Etoken RPC[] = { {"RPC", 0, 0} ,{NULL,0,0}};static struct Etoken LoginOK[] = { {"RPC", 0, 0}, {"Invalid password", 1, 0} , {NULL,0,0}};static struct Etoken GTSign[] = { {">", 0, 0} ,{NULL,0,0}};static struct Etoken Menu[] = { {"Menu:", 0, 0} ,{NULL,0,0}};static struct Etoken Temp[] = { {"emperature: ", 0, 0} , {NULL,0,0}};static struct Etoken Break[] = { {"Status", 0, 0} , {NULL,0,0}};static struct Etoken PowerApplied[] = { {"ower applied to outlet", 0, 0} , {NULL,0,0}};/* We may get a notice about rebooting, or a request for confirmation */static struct Etoken Rebooting[] = { {"ebooting selected outlet", 0, 0} , {"(Y/N)>", 1, 0} , {"already off.", 2, 0} , {NULL,0,0}};static struct Etoken TurningOnOff[] = { {"RPC", 0, 0} , {"(Y/N)>", 1, 0} , {"already ", 2, 0} , {NULL,0,0}};static struct BayTechModelInfo ModelInfo [] = { {"BayTech RPC-5", 18, Temp},/* This first model will be the default */ {"BayTech RPC-3", 10, Break}, {"BayTech RPC-3A", 10, Break}, {NULL, 0, NULL},};static int RPC_connect_device(struct pluginDevice * bt);static int RPCLogin(struct pluginDevice * bt);static int RPCRobustLogin(struct pluginDevice * bt);static int RPCNametoOutletList(struct pluginDevice*, const char * name, int outletlist[]);static int RPCReset(struct pluginDevice*, int unitnum, const char * rebootid);static int RPCLogout(struct pluginDevice * bt);static int RPC_onoff(struct pluginDevice*, int unitnum, const char * unitid, int request);/* Login to the Baytech Remote Power Controller (RPC) */static intRPCLogin(struct pluginDevice * bt){ char IDinfo[128]; static char IDbuf[128]; char * idptr = IDinfo; char * delim; int j; EXPECT(bt->rdfd, RPC, 10); /* Look for the unit type info */ if (EXPECT_TOK(bt->rdfd, BayTechAssoc, 2, IDinfo , sizeof(IDinfo), Debug) < 0) { LOG(PIL_CRIT, "%s", "No initial response from " DEVICE "."); return(errno == ETIMEDOUT ? S_TIMEOUT : S_OOPS); } idptr += strspn(idptr, WHITESPACE); /* * We should be looking at something like this: * RPC-5 Telnet Host * Revision F 4.22, (C) 1999 * Bay Technical Associates */ /* Truncate the result after the RPC-5 part */ if ((delim = strchr(idptr, ' ')) != NULL) { *delim = EOS; } snprintf(IDbuf, sizeof(IDbuf), "BayTech RPC%s", idptr); REPLSTR(bt->idinfo, IDbuf); bt->modelinfo = &ModelInfo[0]; for (j=0; ModelInfo[j].type != NULL; ++j) { /* * TIMXXX - * Look at device ID as this really describes the model. */ if (strcasecmp(ModelInfo[j].type, IDbuf) == 0) { bt->modelinfo = &ModelInfo[j]; break; } } /* Look for the unit id info */ EXPECT(bt->rdfd, UnitId, 10); SNARF(bt->rdfd, IDbuf, 2); delim = IDbuf + strcspn(IDbuf, WHITESPACE); *delim = EOS; REPLSTR(bt->unitid, IDbuf); /* Expect "username>" */ EXPECT(bt->rdfd, login, 2); SEND(bt->wrfd, bt->user); SEND(bt->wrfd, "\r"); /* Expect "password>" */ switch (StonithLookFor(bt->rdfd, password, 5)) { case 0: /* Good! */ break; case 1: /* OOPS! got another username prompt */ LOG(PIL_CRIT, "%s", "Invalid username for " DEVICE "."); return(S_ACCESS); default: return(errno == ETIMEDOUT ? S_TIMEOUT : S_OOPS); } SEND(bt->wrfd, bt->passwd); SEND(bt->wrfd, "\r"); /* Expect "RPC-x Menu" */ switch (StonithLookFor(bt->rdfd, LoginOK, 5)) { case 0: /* Good! */ break; case 1: /* Uh-oh - bad password */ LOG(PIL_CRIT, "%s", "Invalid password for " DEVICE "."); return(S_ACCESS); default: return(errno == ETIMEDOUT ? S_TIMEOUT : S_OOPS); } EXPECT(bt->rdfd, Menu, 2); return(S_OK);}static intRPCRobustLogin(struct pluginDevice * bt){ int rc=S_OOPS; int j; for (j=0; j < 20 && rc != S_OK; ++j) { if (RPC_connect_device(bt) != S_OK) { continue; } rc = RPCLogin(bt); } return rc;}/* Log out of the Baytech RPC */static intRPCLogout(struct pluginDevice* bt){ int rc; /* Make sure we're in the right menu... */ SEND(bt->wrfd, "\r"); /* Expect "Selection>" */ rc = StonithLookFor(bt->rdfd, Selection, 5); /* Option 6 is Logout */ SEND(bt->wrfd, "6\r"); close(bt->wrfd); close(bt->rdfd); bt->wrfd = bt->rdfd = -1; return(rc >= 0 ? S_OK : (errno == ETIMEDOUT ? S_TIMEOUT : S_OOPS));}/* Reset (power-cycle) the given outlet number */static intRPCReset(struct pluginDevice* bt, int unitnum, const char * rebootid){ char unum[32]; SEND(bt->wrfd, "\r"); /* Make sure we're in the top level menu */ /* Expect "RPC-x Menu" */ EXPECT(bt->rdfd, RPC, 5); EXPECT(bt->rdfd, Menu, 5); /* OK. Request sub-menu 1 (Outlet Control) */ SEND(bt->wrfd, "1\r"); /* Verify that we're in the sub-menu */ /* Expect: "RPC-x>" */ EXPECT(bt->rdfd, RPC, 5); EXPECT(bt->rdfd, GTSign, 5); /* Send REBOOT command for given outlet */ snprintf(unum, sizeof(unum), "REBOOT %d\r", unitnum); SEND(bt->wrfd, unum); /* Expect "ebooting "... or "(Y/N)" (if confirmation turned on) */ retry: switch (StonithLookFor(bt->rdfd, Rebooting, 5)) { case 0: /* Got "Rebooting" Do nothing */ break; case 1: /* Got that annoying command confirmation :-( */ SEND(bt->wrfd, "Y\r"); goto retry; case 2: /* Outlet is turned off */ LOG(PIL_CRIT, "%s: %s." , "Host is OFF", rebootid); return(S_ISOFF); default: return(errno == ETIMEDOUT ? S_RESETFAIL : S_OOPS); } LOG(PIL_INFO, "Host %s (outlet %d) being rebooted." , rebootid, unitnum); /* Expect "ower applied to outlet" */ if (StonithLookFor(bt->rdfd, PowerApplied, 30) < 0) { return(errno == ETIMEDOUT ? S_RESETFAIL : S_OOPS); } /* All Right! Power is back on. Life is Good! */ LOG(PIL_INFO, "%s %s (outlet %d)." , "Power restored to host", rebootid, unitnum); /* Expect: "RPC-x>" */ EXPECT(bt->rdfd, RPC,5); EXPECT(bt->rdfd, GTSign, 5); /* Pop back to main menu */ SEND(bt->wrfd, "MENU\r"); return(S_OK);}static intRPC_onoff(struct pluginDevice* bt, int unitnum, const char * unitid, int req){ char unum[32]; const char * onoff = (req == ST_POWERON ? "on" : "off"); int rc; if ((rc = RPCRobustLogin(bt) != S_OK)) { LOG(PIL_CRIT, "%s", "Cannot log into " DEVICE "."); return(rc); } SEND(bt->wrfd, "\r"); /* Make sure we're in the top level menu */ /* Expect "RPC-x Menu" */ EXPECT(bt->rdfd, RPC, 5); EXPECT(bt->rdfd, Menu, 5); /* OK. Request sub-menu 1 (Outlet Control) */ SEND(bt->wrfd, "1\r"); /* Verify that we're in the sub-menu */ /* Expect: "RPC-x>" */ EXPECT(bt->rdfd, RPC, 5); EXPECT(bt->rdfd, GTSign, 5); /* Send ON/OFF command for given outlet */ snprintf(unum, sizeof(unum), "%s %d\r" , onoff, unitnum); SEND(bt->wrfd, unum); /* Expect "RPC->x "... or "(Y/N)" (if confirmation turned on) */ if (StonithLookFor(bt->rdfd, TurningOnOff, 10) == 1) { /* They've turned on that annoying command confirmation :-( */ SEND(bt->wrfd, "Y\r"); EXPECT(bt->rdfd, TurningOnOff, 10); } EXPECT(bt->rdfd, GTSign, 10); /* All Right! Command done. Life is Good! */ LOG(PIL_INFO, "%s %s (outlet %d) %s %s." , "Power to host", unitid, unitnum, "turned", onoff); /* Pop back to main menu */ SEND(bt->wrfd, "MENU\r"); return(S_OK);}/* * Map the given host name into an (AC) Outlet number on the power strip */static int
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -