Back to index

nordugrid-arc-nox  1.1.0~rc6
arccat.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <iostream>
00008 #include <list>
00009 #include <string>
00010 
00011 #include <arc/ArcLocation.h>
00012 #include <arc/IString.h>
00013 #include <arc/Logger.h>
00014 #include <arc/OptionParser.h>
00015 #include <arc/StringConv.h>
00016 #include <arc/client/JobController.h>
00017 #include <arc/client/JobSupervisor.h>
00018 #include <arc/UserConfig.h>
00019 
00020 int main(int argc, char **argv) {
00021 
00022   setlocale(LC_ALL, "");
00023 
00024   Arc::Logger logger(Arc::Logger::getRootLogger(), "arccat");
00025   Arc::LogStream logcerr(std::cerr);
00026   logcerr.setFormat(Arc::ShortFormat);
00027   Arc::Logger::getRootLogger().addDestination(logcerr);
00028   Arc::Logger::getRootLogger().setThreshold(Arc::WARNING);
00029 
00030   Arc::ArcLocation::Init(argv[0]);
00031 
00032   Arc::OptionParser options(istring("[job ...]"),
00033                             istring("The arccat command performs the cat "
00034                                     "command on the stdout, stderr or grid\n"
00035                                     "manager's error log of the job."),
00036                             istring("Argument to -c has the format "
00037                                     "Flavour:URL e.g.\n"
00038                                     "ARC0:ldap://grid.tsl.uu.se:2135/"
00039                                     "nordugrid-cluster-name=grid.tsl.uu.se,"
00040                                     "Mds-Vo-name=local,o=grid"));
00041 
00042   bool all = false;
00043   options.AddOption('a', "all",
00044                     istring("all jobs"),
00045                     all);
00046 
00047   std::string joblist;
00048   options.AddOption('j', "joblist",
00049                     istring("file containing a list of jobs"),
00050                     istring("filename"),
00051                     joblist);
00052 
00053   std::list<std::string> clusters;
00054   options.AddOption('c', "cluster",
00055                     istring("explicity select or reject a specific cluster"),
00056                     istring("[-]name"),
00057                     clusters);
00058 
00059   std::list<std::string> status;
00060   options.AddOption('s', "status",
00061                     istring("only select jobs whose status is statusstr"),
00062                     istring("statusstr"),
00063                     status);
00064 
00065   bool show_stdout = true;
00066   options.AddOption('o', "stdout",
00067                     istring("show the stdout of the job (default)"),
00068                     show_stdout);
00069 
00070   bool show_stderr = false;
00071   options.AddOption('e', "stderr",
00072                     istring("show the stderr of the job"),
00073                     show_stderr);
00074 
00075   bool show_gmlog = false;
00076   options.AddOption('l', "gmlog",
00077                     istring("show the grid manager's error log of the job"),
00078                     show_gmlog);
00079 
00080   int timeout = -1;
00081   options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
00082                     istring("seconds"), timeout);
00083 
00084   std::string conffile;
00085   options.AddOption('z', "conffile",
00086                     istring("configuration file (default ~/.arc/client.conf)"),
00087                     istring("filename"), conffile);
00088 
00089   std::string debug;
00090   options.AddOption('d', "debug",
00091                     istring("FATAL, ERROR, WARNING, INFO, VERBOSE or DEBUG"),
00092                     istring("debuglevel"), debug);
00093 
00094   bool version = false;
00095   options.AddOption('v', "version", istring("print version information"),
00096                     version);
00097 
00098   std::list<std::string> jobs = options.Parse(argc, argv);
00099 
00100   // If debug is specified as argument, it should be set before loading the configuration.
00101   if (!debug.empty())
00102     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(debug));
00103 
00104   Arc::UserConfig usercfg(conffile, joblist);
00105   if (!usercfg) {
00106     logger.msg(Arc::ERROR, "Failed configuration initialization");
00107     return 1;
00108   }
00109 
00110   if (debug.empty() && !usercfg.Verbosity().empty())
00111     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));
00112 
00113   if (timeout > 0)
00114     usercfg.Timeout(timeout);
00115 
00116   if (version) {
00117     std::cout << Arc::IString("%s version %s", "arccat", VERSION)
00118               << std::endl;
00119     return 0;
00120   }
00121 
00122   if ((!joblist.empty() || !status.empty()) && jobs.empty() && clusters.empty())
00123     all = true;
00124 
00125   if (jobs.empty() && clusters.empty() && !all) {
00126     logger.msg(Arc::ERROR, "No jobs given");
00127     return 1;
00128   }
00129 
00130   if (!jobs.empty() || all)
00131     usercfg.ClearSelectedServices();
00132 
00133   if (!clusters.empty()) {
00134     usercfg.ClearSelectedServices();
00135     usercfg.AddServices(clusters, Arc::COMPUTING);
00136   }
00137 
00138   Arc::JobSupervisor jobmaster(usercfg, jobs);
00139   if (!jobmaster.JobsFound()) {
00140     std::cout << "No jobs" << std::endl;
00141     return 0;
00142   }
00143   std::list<Arc::JobController*> jobcont = jobmaster.GetJobControllers();
00144 
00145   // If the user specified a joblist on the command line joblist equals
00146   // usercfg.JobListFile(). If not use the default, ie. usercfg.JobListFile().
00147   if (jobcont.empty()) {
00148     logger.msg(Arc::ERROR, "No job controller plugins loaded");
00149     return 1;
00150   }
00151 
00152   std::string whichfile;
00153   if (show_gmlog)
00154     whichfile = "gmlog";
00155   else if (show_stderr)
00156     whichfile = "stderr";
00157   else
00158     whichfile = "stdout";
00159 
00160   int retval = 0;
00161   for (std::list<Arc::JobController*>::iterator it = jobcont.begin();
00162        it != jobcont.end(); it++)
00163     if (!(*it)->Cat(status, whichfile))
00164       retval = 1;
00165 
00166   return retval;
00167 }