Exemple #1
0
static int
ibmhmc_set_config(StonithPlugin * s, StonithNVpair* list)
{
	struct pluginDevice* dev = NULL;
	StonithNamesToGet	namestocopy [] =
	{	{ST_IPADDR,	NULL}
	,	{NULL,		NULL}
	};
	int rc;
	char get_hmcver[MAX_CMD_LEN];
	char firstchar;
	int firstnum;
	char* output = NULL;
	int status;
	const char *mansyspats;
	int len;
	
	ERRIFWRONGDEV(s,S_OOPS);

	if(Debug){
		LOG(PIL_DEBUG, "%s: called\n", __FUNCTION__);
	}
	
	dev = (struct pluginDevice*) s;

	if ((rc = OurImports->CopyAllValues(namestocopy, list)) != S_OK) {
		return rc;
	}
	if(Debug){
		LOG(PIL_DEBUG, "%s: ipaddr=%s\n", __FUNCTION__
		,	namestocopy[0].s_value);	
	}

	if (get_num_tokens(namestocopy[0].s_value) == 1) {
		/* name=value pairs on command line, look for managedsyspat */
		mansyspats = OurImports->GetValue(list, ST_MANSYSPAT);
		if (mansyspats != NULL) {
			if (get_hmc_mansyspats(dev, mansyspats) != S_OK) {
				FREE(namestocopy[0].s_value);
				return S_OOPS;
			}
		}
		/* look for password */
		dev->password = STRDUP(OurImports->GetValue(list, ST_PASSWD));
		dev->hmc = namestocopy[0].s_value;
	}else{
		/* -p or -F option with args "ipaddr [managedsyspat]..." */
		char *pch = namestocopy[0].s_value;

		/* skip over ipaddr and null-terminate */
		pch += strcspn(pch, WHITESPACE);
		*pch = EOS;

		/* skip over white-space up to next token */
		pch++;
		pch += strspn(pch, WHITESPACE);
		if (get_hmc_mansyspats(dev, pch) != S_OK) {
			FREE(namestocopy[0].s_value);
			return S_OOPS;
		}

		dev->hmc = STRDUP(namestocopy[0].s_value);
		FREE(namestocopy[0].s_value);
	}
	
	/* check whether the HMC has ssh command enabled */
	if (check_hmc_status(dev) != S_OK) {
		LOG(PIL_CRIT, "HMC %s does not have remote "
		"command execution using the ssh facility enabled", dev->hmc);
		return S_BADCONFIG;
	}		

	/* get the HMC's version info */
	snprintf(get_hmcver, MAX_CMD_LEN
	,	SSH_CMD " -l " HMCROOT " %s lshmc -v | grep RM", dev->hmc);
	if (Debug) {
		LOG(PIL_DEBUG, "%s: get_hmcver=%s", __FUNCTION__, get_hmcver);
	}

	output = do_shell_cmd(get_hmcver, &status, dev->password);
	if (Debug) {
		LOG(PIL_DEBUG, "%s: output=%s\n", __FUNCTION__
		, output ? output : "(nil)");
	}
	if (output == NULL) {
		return S_BADCONFIG;
	}		

	/* parse the HMC's version info (i.e. "*RM V4R2.1" or "*RM R3V2.6") */
	if ((sscanf(output, "*RM %c%1d", &firstchar, &firstnum) == 2)
	&& ((firstchar == 'V') || (firstchar == 'R'))) {
		dev->hmcver = firstnum;
		if(Debug){
			LOG(PIL_DEBUG, "%s: HMC %s version is %d"
			,	__FUNCTION__, dev->hmc, dev->hmcver);
		}
	}else{
		LOG(PIL_CRIT, "%s: unable to determine HMC %s version"
		,	__FUNCTION__, dev->hmc);
		FREE(output);
		return S_BADCONFIG;
	}

	len = strlen(output+4) + sizeof(DEVICE) + 1;
	if (dev->idinfo != NULL) {
		FREE(dev->idinfo);
		dev->idinfo = NULL;
	}
	dev->idinfo = MALLOC(len * sizeof(char));
	if (dev->idinfo == NULL) {
		LOG(PIL_CRIT, "out of memory");
		FREE(output);
		return S_OOPS;
	}
	snprintf(dev->idinfo, len, "%s %s", DEVICE, output+4);
	FREE(output);

	if (S_OK != get_hmc_hostlist(dev)){
		LOG(PIL_CRIT, "unable to obtain list of managed systems in %s"
		,	__FUNCTION__);
		return S_BADCONFIG;
	}
	
	return S_OK;
}
Exemple #2
0
static int
get_hmc_mansyspats(struct pluginDevice * dev, const char *mansyspats)
{
	char *patscopy;
	int numpats;	
	int i;
	char *tmp;

	if(Debug){
		LOG(PIL_DEBUG, "%s: called, mansyspats=%s\n"
		,	__FUNCTION__, mansyspats);
	}
	
	patscopy = STRDUP(mansyspats);
	if (patscopy == NULL) {
		LOG(PIL_CRIT, "%s: out of memory", __FUNCTION__);
		return S_OOPS;
	}

	numpats = get_num_tokens(patscopy);	
	if (numpats > 0) {
		dev->mansyspats = MALLOC((numpats+1)*sizeof(char *));
		if (dev->mansyspats == NULL) {
			LOG(PIL_CRIT, "%s: out of memory"
			,	__FUNCTION__);
			FREE(patscopy);
			return S_OOPS;
		}

		memset(dev->mansyspats, 0, (numpats+1)*sizeof(char *));

		/* White-space split the output here */
		i = 0;
		tmp = strtok(patscopy, WHITESPACE);
		while (tmp != NULL) {
			dev->mansyspats[i] = STRDUP(tmp);
			if (dev->mansyspats[i] == NULL) {
				LOG(PIL_CRIT, "%s: out of memory"
				,	__FUNCTION__);
				free_hmc_mansyspats(dev);
				dev->mansyspats = NULL;
				FREE(patscopy);
				return S_OOPS;
			}
	
			if(Debug){
				LOG(PIL_DEBUG, "%s: adding pattern %s\n"
				,	__FUNCTION__, dev->mansyspats[i]);
			}

			/* no patterns necessary if all specified */
			if (strcmp(dev->mansyspats[i], "*") == 0) {
				stonith_free_hostlist(dev->mansyspats);
				dev->mansyspats = NULL;
				break;
			}

			i++;
			tmp = strtok(NULL, WHITESPACE);
		}
	}
	FREE(patscopy);
	return S_OK;
}
Exemple #3
0
static int
bladehpi_set_config(StonithPlugin *s, StonithNVpair *list)
{
	struct pluginDevice *	dev = NULL;
	StonithNamesToGet	namestocopy [] =
	{	{ST_ENTITYROOT,	NULL}
	,	{NULL,		NULL}
	};
	int			rc, i;
	SaErrorT		ohrc;
	
	if (Debug) {
		LOG(PIL_DEBUG, "%s: called", __FUNCTION__);
	}
	
	ERRIFWRONGDEV(s, S_OOPS);

	dev = (struct pluginDevice *)s;

	if (Debug) {
		LOG(PIL_DEBUG, "%s conditionally compiled with:"
#ifdef IBMBC_WAIT_FOR_OFF
		" IBMBC_WAIT_FOR_OFF"
#endif
		, dev->pluginid);
	}
	
	if ((rc = OurImports->CopyAllValues(namestocopy, list)) != S_OK) {
		return rc;
	}

	if (Debug) {
		LOG(PIL_DEBUG, "%s = %s", ST_ENTITYROOT
		,	namestocopy[0].s_value);	
	}

	if (get_num_tokens(namestocopy[0].s_value) == 1) {
		/* name=value pairs on command line, look for soft_reset */
		const char *softreset = 
			OurImports->GetValue(list, ST_SOFTRESET);
		if (softreset != NULL) {
			if (!strcasecmp(softreset, "true") ||
			    !strcmp(softreset, "1")) {
				dev->softreset = 1;
			} else if (!strcasecmp(softreset, "false") ||
				   !strcmp(softreset, "0")) {
				dev->softreset = 0;
			} else {
				LOG(PIL_CRIT, "Invalid %s %s, must be "
					"true, 1, false or 0"
				,	ST_SOFTRESET, softreset);
				FREE(namestocopy[0].s_value);
				return S_OOPS;
			}
		}
	} else {
		/* -p or -F option with args "entity_root [soft_reset]..." */
		char *pch = namestocopy[0].s_value;

		/* skip over entity_root and null-terminate */
		pch += strcspn(pch, WHITESPACE);
		*pch = EOS;

		/* skip over white-space up to next token */
		pch++;
		pch += strspn(pch, WHITESPACE);
		if (!strcasecmp(pch, "true") || !strcmp(pch, "1")) {
			dev->softreset = 1;
		} else if (!strcasecmp(pch, "false") || !strcmp(pch, "0")) {
			dev->softreset = 0;
		} else {
			LOG(PIL_CRIT, "Invalid %s %s, must be "
				"true, 1, false or 0"
			,	ST_SOFTRESET, pch);
			FREE(namestocopy[0].s_value);
			return S_OOPS;
		}
	}

	dev->device = STRDUP(namestocopy[0].s_value);
	FREE(namestocopy[0].s_value);
	if (dev->device == NULL) {
		LOG(PIL_CRIT, "Out of memory for strdup in %s", __FUNCTION__);
		return S_OOPS;
	}

	if (strcspn(dev->device, WHITESPACE) != strlen(dev->device) ||
	    sscanf(dev->device, SYSTEM_CHASSIS_FMT, &i) != 1 || i < 0) {
		LOG(PIL_CRIT, "Invalid %s %s, must be of format %s"
		,	ST_ENTITYROOT, dev->device, SYSTEM_CHASSIS_FMT);
		return S_BADCONFIG;
	}
	
	dev->ohver = saHpiVersionGet();
	if (dev->ohver > SAHPI_INTERFACE_VERSION) {
		LOG(PIL_CRIT, "Installed OpenHPI interface (%x) greater than "
			"one used by plugin (%x), incompatibilites may exist"
		,	dev->ohver, SAHPI_INTERFACE_VERSION);
		return S_BADCONFIG;
	}

	ohrc = saHpiSessionOpen(SAHPI_UNSPECIFIED_DOMAIN_ID
				    , &dev->ohsession, NULL);
	if (ohrc != SA_OK) {
		LOG(PIL_CRIT, "Unable to open HPI session (%d)", ohrc);
		return S_BADCONFIG;
	}

	ohrc = saHpiDiscover(dev->ohsession);
	if (ohrc != SA_OK) {
		LOG(PIL_CRIT, "Unable to discover resources (%d)", ohrc);
		return S_BADCONFIG;
	}

	if (get_bladehpi_hostlist(dev) != S_OK) {
		LOG(PIL_CRIT, "Unable to obtain list of hosts in %s"
		,	__FUNCTION__);
		return S_BADCONFIG;
	}

	return S_OK;
}
Exemple #4
0
int main(int argc, char **argv)
{
    signal(SIGINT, handle_signal);

    char line[MAXLINE]; /* The current input line */

    /* Points to the beginning of the list of current command tokens */
    cmd_tok *current_cmd = NULL;

    /* Points to a token within the command tokens list */
    cmd_tok *current_tok_ptr = NULL;

    print_prompt();
    int length = 0;
    while((length = getln(line, MAXLINE)) > 0)
    {
        if (length == 1 && line[0] == '\n')
        {
            /* They pushed enter without any text */
            print_prompt();
            continue;
        }
        char *token = strtok(line, " ");
        current_cmd = create_cmd_tok(token);
        current_tok_ptr = current_cmd;
        token = strtok(NULL, " ");

        /* Continue to tokenize input line */
        while (token != NULL)
        {
            current_tok_ptr->next_token = create_cmd_tok(token);
            current_tok_ptr = current_tok_ptr->next_token;
            token = strtok(NULL, " ");
        }

        /* Process the command */

        /* Check if it was one of the internal commands: exit, cd */
        if (strcmp(current_cmd->current_token, "exit") == 0)
        {
            destroy_cmd_toks(current_cmd);
            return 0;
        }
        else if (strcmp(current_cmd->current_token, "cd") == 0)
        {
            char *directory = NULL;
            int num_args = get_num_tokens(current_cmd);
            if (num_args == 1)
            {
                /* The user just typed "cd".  Go to home directory. */
                directory = getenv("HOME");
            }
            else
            {
                /* Get the token after "cd" */
                directory = current_cmd->next_token->current_token;
            }

            int cd_return_val = chdir(directory);

            /* chdir returns 0 if successful */
            if (cd_return_val != 0)
            {
                /* Print error messages */
                char *error_msg =
                    (char *)malloc(sizeof(argv[0]) +
                                   sizeof(directory) + 7*sizeof(char));
                strcpy(error_msg, argv[0]);
                strcat(error_msg, ": cd: ");
                strcat(error_msg, directory);
                perror(error_msg);
                free(error_msg);
            }
        }
        else
        {
            /* Assume input was an external executable command */
            char **args = create_args(current_cmd);

            /* In case executable is not on path, allocate memory for
               args[0] with ./ prepended */
            char *arg0 = NULL;
            int child_pid = fork();
            if (child_pid == 0)
            {
                execvp(args[0], args);

                /* execvp only returns if it fails */

                /* Didn't find command on path,
                try looking in current directory */
                arg0 = (char *)malloc(sizeof(args[0]) + 3*sizeof(char));
                strcpy(arg0, "./");
                strcat(arg0, args[0]);
                execvp(arg0, args);

                /* Couldn't find command */
                print_cmd_not_found(args[0]);
                return 0;
            }
            else
            {
                signal(SIGINT, child_terminate_handler);
                wait(NULL);
            }
            signal(SIGINT, handle_signal);
            free(args);
            free(arg0);
        }

        /* Free and reset command pointers */
        destroy_cmd_toks(current_cmd);
        current_cmd = NULL;
        current_tok_ptr = NULL;

        print_prompt();
    }

    /* In case they exit with control-D,
       want prompt to show up on new line */
    printf("\n");

    return 0;
}
int main(int n_args, char** args) {
  init_logging();
  isage::util::print_pstats();

  int num_topics;
  int num_epochs_;

  isage::wtm::LDAVStrategy strategy;
  
  std::string output_usage_name;
  std::string heldout_output_usage_name;

  po::variables_map vm;
  {
    po::options_description desc("Allowed options");
    desc.add_options()
      ("help", "produce help message")
      ("vocab-size", po::value< int >()->default_value(10),
       "number of vocab words (default: 10)")
      ("words-per-doc", po::value<int>()->default_value(10),
       "number of words per document (default: 10)")
      ("bias", po::value<double>()->default_value(.8),
       "Bernoulli parameter p for how to partition the vocab words. (default: 0.8)")
      ("num-docs", po::value<int>()->default_value(10),
       "number of documents to generate (default: 10)")
      //////////////////////////
      ("topics", po::value<int>(&num_topics)->default_value(10), 
       "number of topics to use")
      ("train-epochs", po::value<int>(&num_epochs_)->default_value(5),
       "Number of epochs to run")
      ("em-iterations", po::value<int>(&(strategy.num_learn_iters))->default_value(100), 
       "number of EM iterations to run")
      ("e-steps", po::value<int>(&(strategy.num_e_iters))->default_value(25), 
       "number of iterations to perform, per E-step")
      ("m-steps", po::value<int>(&(strategy.num_m_iters))->default_value(1), 
       "number of iterations to perform, per M-step")
      ("update-hypers", po::value<int>(&(strategy.hyper_update_iter))->default_value(-1),
       "how often to update the hyperparameters (default: -1 == never update)")
      ("update-model-interval", po::value<int>(&(strategy.update_model_every))->default_value(5), "update the model every [some] number of EM steps (default: 5)")
      ("print-topics-every", po::value<int>(&(strategy.print_topics_every))->default_value(5), "print topics every [some] number of EM steps (default: 5)")
      ("print-usage-every", po::value<int>(&(strategy.print_usage_every))->default_value(5), "print topic usage every [some] number of EM steps (default: 5)")
      ("top-k", po::value<int>(&(strategy.print_topics_k))->default_value(10), "number of words per topic to print (default: 10)")
      ("em-verbosity", po::value<int>(&(strategy.em_verbosity))->default_value(1),
       "how verbose should EM output be (default: 1; higher == more verbose)")
      ("eta-density-threshold", po::value<double>(&(strategy.eta_density_threshold))->default_value(1E-4),
       "the threshold t for counting the number of eta parameters are above t (default: 1E-4)")
      ////////////////////////////////
      ("topic-usage-file", po::value<std::string>(&output_usage_name)->default_value("-"), 
       "filename to write topic usage to (default: - (to console)")
      ("heldout-topic-usage-file", po::value<std::string>(&heldout_output_usage_name)->default_value("-"), 
       "filename to write heldout topic usage to (default: - (to console)")
      ("inferencer-serialization", po::value<std::string>(), "filename to serialize inference state to")
      ("serialized-inferencer", po::value<std::string>(), "filename to READ serialized inference state from")
      ////////////////////////////////
      ;

    po::store(po::parse_command_line(n_args, args, desc), vm);
    if (vm.count("help")) {
      ERROR << desc << "\n";
      return 1;
    }
    po::notify(vm);
  }

  typedef std::string string;
  typedef string VocabType;
  typedef isage::wtm::Vocabulary< VocabType > SVocab;
  typedef double CountType;
  typedef isage::wtm::Document< VocabType, CountType > Doc;
  typedef isage::wtm::Corpus< Doc > Corpus;
  typedef std::vector<double> TopicType;
  typedef isage::wtm::DiscreteLDA< VocabType, std::vector<double> > Model;
  typedef isage::wtm::DiscreteVariational< Doc, VocabType, TopicType > Variational;

  isage::util::SmartWriter usage_outer(output_usage_name);
  isage::util::SmartWriter assign_outer("assignments");
  
  Variational* var_inf = NULL;
  SVocab word_vocab("__OOV__");
  for(int wi = 1; wi <= vm["vocab-size"].as<int>(); ++wi) {
    word_vocab.make_word("word_" + std::to_string(wi));
  }

  Corpus corpus("train_corpus");
  corpus.generate(vm["num-docs"].as<int>(),
		  vm["words-per-doc"].as<int>(),
		  vm["bias"].as<double>(),
		  word_vocab
		  );
  int num_words_total = get_num_tokens(corpus);
  INFO << "Number of documents: " << corpus.num_docs();
  INFO << "Number of word tokens total: " << num_words_total;
  INFO << "Number of vocab types: " << word_vocab.num_words();

  isage::wtm::SymmetricHyperparams shp;
  shp.h_theta = 1.0/(double)num_topics;
  shp.h_word =  0.1; 
  INFO << "Creating model with " << num_topics << " topics";
  Model dm(num_topics, &shp, &word_vocab);
  INFO << "Done creating model.";
  var_inf = new Variational(&dm, &corpus, &word_vocab);
  isage::wtm::UniformHyperSeedWeightedInitializer initer(num_topics, corpus.num_docs(), (double)num_words_total/(double)corpus.num_docs());
  var_inf->init(initer);

  for(int epoch = 0; epoch < num_epochs_; ++epoch) {
    INFO << "Starting learning epoch " << epoch;
    var_inf->learn(strategy, epoch, usage_outer, assign_outer);
    INFO << "Done with inference in epoch " << epoch;
    // // create and open a character archive for output
    // if(vm.count("inferencer-serialization")) {
    //   std::string sfname = vm["inferencer-serialization"].as<std::string>() + 
    // 	".iteration" + std::to_string((1+epoch));	
    //   std::ofstream ofs(sfname, std::ios::out|std::ios::binary);
    //   boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
    //   out.push(boost::iostreams::gzip_compressor());
    //   out.push(ofs);
    //   boost::archive::binary_oarchive oa(out);
    //   oa << (*var_inf);
    //   INFO << "see " << sfname << " for serialized inferencer";
    // }
    dm.print_topics(strategy.print_topics_k, word_vocab);
  }

  if(var_inf != NULL) {
    delete var_inf;
  }
  return 0;
}