ws_machineService.cpp 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "ws_machineService.hpp"
  14. #include "jarray.hpp"
  15. #include "dadfs.hpp"
  16. #include "exception_util.hpp"
  17. #include "workunit.hpp"
  18. #include "roxiecommlib.hpp"
  19. #include "componentstatus.hpp"
  20. #include "rmtssh.hpp"
  21. #include "platform.h"
  22. #include "TpWrapper.hpp"
  23. static const int THREAD_POOL_SIZE = 40;
  24. static const int THREAD_POOL_STACK_SIZE = 64000;
  25. static const char* FEATURE_URL = "MachineInfoAccess";
  26. const unsigned ROXIECONTROLSTATETIMEOUT = 5000; //5 second
  27. class CMachineInfoThreadParam : public CWsMachineThreadParam
  28. {
  29. public:
  30. IMPLEMENT_IINTERFACE;
  31. IEspContext& m_context;
  32. CGetMachineInfoUserOptions& m_options; //From request
  33. CMachineData& m_machineData; //From request
  34. IArrayOf<IEspMachineInfoEx>& m_machineInfoTable; //For response
  35. StringArray& m_machineInfoColumns; //For response
  36. CMachineInfoThreadParam(Cws_machineEx* pService, IEspContext& context, CGetMachineInfoUserOptions& options,
  37. CMachineData& machineData, IArrayOf<IEspMachineInfoEx>& machineInfoTable, StringArray& machineInfoColumns )
  38. : CWsMachineThreadParam(NULL, NULL, NULL, pService),
  39. m_context(context),
  40. m_options(options),
  41. m_machineData(machineData),
  42. m_machineInfoTable(machineInfoTable),
  43. m_machineInfoColumns(machineInfoColumns)
  44. {
  45. }
  46. virtual void doWork()
  47. {
  48. m_pService->doGetMachineInfo(m_context, this);
  49. }
  50. void addColumn(const char* columnName)
  51. {
  52. synchronized block(s_mutex);
  53. if (m_machineInfoColumns.find(columnName) == NotFound)
  54. m_machineInfoColumns.append(columnName);
  55. }
  56. private:
  57. static Mutex s_mutex;
  58. };
  59. Mutex CMachineInfoThreadParam::s_mutex;
  60. class CRoxieStateInfoThreadParam : public CWsMachineThreadParam
  61. {
  62. public:
  63. StringAttr clusterName;
  64. IArrayOf<IEspMachineInfoEx>& machineInfoTable; //For response
  65. CRoxieStateInfoThreadParam(Cws_machineEx* pService, const char* _clusterName, IArrayOf<IEspMachineInfoEx>& _machineInfoTable)
  66. : CWsMachineThreadParam(pService), clusterName(_clusterName), machineInfoTable(_machineInfoTable)
  67. {
  68. }
  69. virtual void doWork()
  70. {
  71. m_pService->getRoxieStateInfo(this);
  72. }
  73. };
  74. class CGetMachineUsageThreadParam : public CWsMachineThreadParam
  75. {
  76. public:
  77. IEspContext& espContext;
  78. IPropertyTree* request;
  79. CGetMachineUsageThreadParam(Cws_machineEx* pService, IEspContext& _espContext, IPropertyTree* _request)
  80. : CWsMachineThreadParam(pService), espContext(_espContext), request(_request) {}
  81. virtual void doWork()
  82. {
  83. m_pService->getMachineUsage(espContext, this);
  84. }
  85. };
  86. void Cws_machineEx::init(IPropertyTree *cfg, const char *process, const char *service)
  87. {
  88. //Read settings from esp.xml
  89. StringBuffer xpath;
  90. xpath.appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]", process, service);
  91. Owned<IPropertyTree> pServiceNode = cfg->getPropTree(xpath.str());
  92. m_bMonitorDaliFileServer = pServiceNode->getPropBool("@monitorDaliFileServer", false);
  93. m_processFilters.setown( pServiceNode->getPropTree("ProcessFilters") );
  94. const char* pchExcludePartitions = pServiceNode->queryProp("@excludePartitions");
  95. if (pchExcludePartitions && *pchExcludePartitions)
  96. {
  97. StringArray sPartitions;
  98. sPartitions.appendList(pchExcludePartitions, ", ;");
  99. unsigned int numOfPartitions = sPartitions.ordinality();
  100. for (unsigned int i=0; i<numOfPartitions; i++)
  101. {
  102. const char* partition = sPartitions.item(i);
  103. if (!partition || !*partition)
  104. continue;
  105. if (strchr(partition, '*'))
  106. m_excludePartitionPatterns.insert( partition );
  107. else
  108. m_excludePartitions.insert( partition );
  109. }
  110. }
  111. m_useDefaultHPCCInit = pServiceNode->getPropBool("UseDefaultHPCCInit", true);//Still used by Rexec for now
  112. m_SSHConnectTimeoutSeconds = pServiceNode->getPropInt("SSHConnectTimeoutSeconds", 5);
  113. const char* machineInfoScript = pServiceNode->queryProp("MachineInfoFile");
  114. if (machineInfoScript && *machineInfoScript)
  115. m_machineInfoFile.append(machineInfoScript);
  116. else
  117. m_machineInfoFile.append("preflight");
  118. //Read settings from environment.xml
  119. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  120. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  121. Owned<IPropertyTree> pEnvironmentRoot = &constEnv->getPTree();
  122. Owned<IPropertyTree> pEnvSettings = pEnvironmentRoot->getPropTree("EnvSettings");
  123. if (pEnvSettings)
  124. {
  125. pEnvSettings->getProp("configs", environmentConfData.m_configsPath.clear());
  126. pEnvSettings->getProp("path", environmentConfData.m_executionPath.clear());
  127. pEnvSettings->getProp("runtime", environmentConfData.m_runtimePath.clear());
  128. pEnvSettings->getProp("lock", environmentConfData.m_lockPath.clear());
  129. pEnvSettings->getProp("pid", environmentConfData.m_pidPath.clear());
  130. pEnvSettings->getProp("user", environmentConfData.m_user.clear());
  131. }
  132. m_threadPoolSize = pServiceNode->getPropInt("ThreadPoolSize", THREAD_POOL_SIZE);
  133. m_threadPoolStackSize = pServiceNode->getPropInt("ThreadPoolStackSize", THREAD_POOL_STACK_SIZE);
  134. //Start thread pool
  135. Owned<IThreadFactory> pThreadFactory = new CWsMachineThreadFactory();
  136. m_threadPool.setown(createThreadPool("WsMachine Thread Pool", pThreadFactory,
  137. NULL, m_threadPoolSize, 10000, m_threadPoolStackSize)); //10 sec timeout for available thread; use stack size of 2MB
  138. setupLegacyFilters();
  139. Owned<IComponentStatusFactory> factory = getComponentStatusFactory();
  140. factory->init(pServiceNode);
  141. }
  142. StringBuffer& Cws_machineEx::getAcceptLanguage(IEspContext& context, StringBuffer& acceptLanguage)
  143. {
  144. context.getAcceptLanguage(acceptLanguage);
  145. if (!acceptLanguage.length())
  146. {
  147. acceptLanguage.set("en");
  148. return acceptLanguage;
  149. }
  150. acceptLanguage.setLength(2);
  151. VStringBuffer languageFile("%ssmc_xslt/nls/%s/hpcc.xml", getCFD(), acceptLanguage.str());
  152. if (!checkFileExists(languageFile.str()))
  153. acceptLanguage.set("en");
  154. return acceptLanguage;
  155. }
  156. bool Cws_machineEx::onGetMachineInfo(IEspContext &context, IEspGetMachineInfoRequest & req,
  157. IEspGetMachineInfoResponse & resp)
  158. {
  159. try
  160. {
  161. context.ensureFeatureAccess(FEATURE_URL, SecAccess_Read, ECLWATCH_MACHINE_INFO_ACCESS_DENIED, "Failed to Get Machine Information. Permission denied.");
  162. StringArray& addresses = req.getAddresses();
  163. if (addresses.empty())
  164. throw MakeStringException(ECLWATCH_INVALID_IP_OR_COMPONENT, "No network address specified.");
  165. CGetMachineInfoData machineInfoData;
  166. readMachineInfoRequest(context, req.getGetProcessorInfo(), req.getGetStorageInfo(), req.getLocalFileSystemsOnly(), req.getGetSoftwareInfo(),
  167. req.getApplyProcessFilter(), addresses, req.getAddProcessesToFilter(), machineInfoData);
  168. getMachineInfo(context, machineInfoData);
  169. setMachineInfoResponse(context, req, machineInfoData, resp);
  170. }
  171. catch(IException* e)
  172. {
  173. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  174. }
  175. return true;
  176. }
  177. bool Cws_machineEx::onGetMachineInfoEx(IEspContext &context, IEspGetMachineInfoRequestEx & req, IEspGetMachineInfoResponseEx & resp)
  178. {
  179. try
  180. {
  181. context.ensureFeatureAccess(FEATURE_URL, SecAccess_Read, ECLWATCH_MACHINE_INFO_ACCESS_DENIED, "Failed to Get Machine Information. Permission denied.");
  182. StringArray& addresses = req.getAddresses();
  183. if (addresses.empty())
  184. throw MakeStringException(ECLWATCH_INVALID_IP_OR_COMPONENT, "No network address specified.");
  185. double version = context.getClientVersion();
  186. CGetMachineInfoData machineInfoData;
  187. readMachineInfoRequest(context, true, true, true, true, true, addresses, NULL, machineInfoData);
  188. getMachineInfo(context, machineInfoData);
  189. if (machineInfoData.getMachineInfoTable().ordinality())
  190. resp.setMachines(machineInfoData.getMachineInfoTable());
  191. if (version >= 1.12)
  192. {
  193. StringBuffer acceptLanguage;
  194. resp.setAcceptLanguage(getAcceptLanguage(context, acceptLanguage).str());
  195. }
  196. }
  197. catch(IException* e)
  198. {
  199. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  200. }
  201. return true;
  202. }
  203. bool Cws_machineEx::onGetTargetClusterInfo(IEspContext &context, IEspGetTargetClusterInfoRequest & req,
  204. IEspGetTargetClusterInfoResponse & resp)
  205. {
  206. try
  207. {
  208. context.ensureFeatureAccess(FEATURE_URL, SecAccess_Read, ECLWATCH_MACHINE_INFO_ACCESS_DENIED, "Failed to Get Target Cluster Information. Permission denied.");
  209. StringArray& targetClusters = req.getTargetClusters();
  210. if (targetClusters.empty())
  211. throw MakeStringException(ECLWATCH_INVALID_IP_OR_COMPONENT, "No target cluster specified.");
  212. CGetMachineInfoData machineInfoData;
  213. Owned<IPropertyTree> targetClustersOut = createPTreeFromXMLString("<Root/>");
  214. readMachineInfoRequest(context, req.getGetProcessorInfo(), req.getGetStorageInfo(), req.getLocalFileSystemsOnly(), req.getGetSoftwareInfo(),
  215. req.getApplyProcessFilter(), req.getAddProcessesToFilter(), targetClusters, machineInfoData, targetClustersOut);
  216. getMachineInfo(context, machineInfoData);
  217. setTargetClusterInfoResponse(context, req, machineInfoData, targetClustersOut, resp);
  218. }
  219. catch(IException* e)
  220. {
  221. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  222. }
  223. return true;
  224. }
  225. ////////////////////////////////////////////////////////////////////////////////////////
  226. // Read Machine Infomation request and collect related settings from environment.xml //
  227. ////////////////////////////////////////////////////////////////////////////////////////
  228. void Cws_machineEx::readMachineInfoRequest(IEspContext& context, bool getProcessorInfo, bool getStorageInfo, bool localFileSystemsOnly, bool getSoftwareInfo, bool applyProcessFilter,
  229. StringArray& processes, const char* addProcessesToFilters, CGetMachineInfoData& machineInfoData)
  230. {
  231. StringBuffer userID, password;
  232. context.getUserID(userID);
  233. context.getPassword(password);
  234. machineInfoData.getOptions().setUserName(userID.str());
  235. machineInfoData.getOptions().setPassword(password.str());
  236. machineInfoData.getOptions().setGetProcessorInfo(getProcessorInfo);
  237. machineInfoData.getOptions().setGetStorageInfo(getStorageInfo);
  238. machineInfoData.getOptions().setLocalFileSystemsOnly(localFileSystemsOnly);
  239. machineInfoData.getOptions().setGetSoftwareInfo(getSoftwareInfo);
  240. machineInfoData.getOptions().setApplyProcessFilter(applyProcessFilter);
  241. machineInfoData.getOptions().getAdditionalProcessFilters().appendList(addProcessesToFilters, " ,\t");
  242. BoolHash uniqueProcesses;
  243. for (unsigned i=0; i<processes.ordinality(); i++)
  244. {
  245. StringArray address;
  246. address.appendList(processes.item(i), ":");
  247. StringBuffer address1, address2, processType, compName, path;
  248. unsigned processNumber = 0;
  249. if (!machineInfoData.getOptions().getGetSoftwareInfo())
  250. {
  251. parseAddresses(address.item(0), address1, address2);
  252. }
  253. else
  254. {
  255. if (address.ordinality() < 5)
  256. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "Invalid address format in '%s'.", processes.item(i));
  257. parseProcessString(address, address1, address2, processType, compName, path, processNumber);
  258. }
  259. setProcessRequest(machineInfoData, uniqueProcesses, address1.str(), address2.str(), processType.str(), compName.str(), path.str(), processNumber);
  260. if (strieq(processType.str(), eqRoxieServerProcess))
  261. machineInfoData.appendRoxieClusters(compName.str());
  262. }
  263. }
  264. void Cws_machineEx::readMachineInfoRequest(IEspContext& context, bool getProcessorInfo, bool getStorageInfo, bool localFileSystemsOnly, bool getSoftwareInfo, bool applyProcessFilter,
  265. const char* addProcessesToFilters, StringArray& targetClustersIn, CGetMachineInfoData& machineInfoData, IPropertyTree* targetClusterTreeOut)
  266. {
  267. StringBuffer userID, password;
  268. context.getUserID(userID);
  269. context.getPassword(password);
  270. machineInfoData.getOptions().setUserName(userID.str());
  271. machineInfoData.getOptions().setPassword(password.str());
  272. machineInfoData.getOptions().setGetProcessorInfo(getProcessorInfo);
  273. machineInfoData.getOptions().setGetStorageInfo(getStorageInfo);
  274. machineInfoData.getOptions().setLocalFileSystemsOnly(localFileSystemsOnly);
  275. machineInfoData.getOptions().setGetSoftwareInfo(getSoftwareInfo);
  276. machineInfoData.getOptions().setApplyProcessFilter(applyProcessFilter);
  277. machineInfoData.getOptions().getAdditionalProcessFilters().appendList(addProcessesToFilters, " ,\t");
  278. readSettingsForTargetClusters(context, targetClustersIn, machineInfoData, targetClusterTreeOut);
  279. }
  280. //Parses address request from machine information request in the form "192.168.1.4-6|."
  281. //The second address is the address retrieved from environment setting (could be a '.').
  282. void Cws_machineEx::parseAddresses(const char *address, StringBuffer& address1, StringBuffer& address2)
  283. {
  284. address1 = address;
  285. address2.clear();
  286. const char* props1 = strchr(address, '|');
  287. if (props1)
  288. {
  289. address2 = props1+1;
  290. address1.setLength(props1 - address);
  291. }
  292. address1.trim();
  293. address2.trim();
  294. }
  295. //Parses machine information request for each process in the form "192.168.1.4-6|.:ThorSlaveProcess:thor1:2:/var/lib/..."
  296. void Cws_machineEx::parseProcessString(StringArray& process, StringBuffer& address1, StringBuffer& address2,
  297. StringBuffer& processType, StringBuffer& compName, StringBuffer& path, unsigned& processNumber)
  298. {
  299. parseAddresses(process.item(0), address1, address2);
  300. processType.clear().append( process.item(1) ).trim();
  301. compName.clear().append( process.item(2) ).trim();
  302. EnvMachineOS os = (EnvMachineOS) atoi( process.item(3) );
  303. path.clear().append( process.item(4) ).trim();
  304. if (path.length())
  305. {
  306. char pat1, pat2;
  307. char rep1, rep2;
  308. if (os == MachineOsLinux)
  309. {
  310. pat1 = ':'; rep1 = '$';
  311. pat2 = '\\';rep2 = '/';
  312. }
  313. else
  314. {
  315. pat1 = '$'; rep1 = ':';
  316. pat2 = '/';rep2 = '\\';
  317. }
  318. path.replace( pat1, rep1 );
  319. path.replace( pat2, rep2 );
  320. if ((os == MachineOsLinux) && (path.charAt(0) != '/'))
  321. path.insert(0, '/');
  322. }
  323. if (process.ordinality() < 6)
  324. return;
  325. processNumber = atoi( process.item(5) );
  326. }
  327. void Cws_machineEx::setProcessRequest(CGetMachineInfoData& machineInfoData, BoolHash& uniqueProcesses, const char* address1, const char* address2,
  328. const char* processType, const char* compName, const char* path, unsigned processNumber)
  329. {
  330. IpAddress ipAddr;
  331. unsigned numIps = ipAddr.ipsetrange(address1);
  332. //address is like 192.168.1.4-6
  333. //so process each address in the range
  334. if (!ipAddr.isIp4())
  335. IPV6_NOT_IMPLEMENTED();
  336. //Always use "EclAgentProcess" to retrieve machine info for "AgentExecProcess"
  337. StringBuffer processTypeStr;
  338. if (processType && *processType)
  339. {
  340. if (strieq(processType, eqAgentExec))
  341. processTypeStr.append(eqEclAgent);
  342. else
  343. processTypeStr = processType;
  344. }
  345. while (numIps--)
  346. {
  347. unsigned numAddr;
  348. if (ipAddr.getNetAddress(sizeof(numAddr),&numAddr)!=sizeof(numAddr))
  349. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid network address.");
  350. ipAddr.ipincrement(1);
  351. //Clean possible duplication
  352. StringBuffer valuesToBeChecked;
  353. valuesToBeChecked.append(numAddr);
  354. if (machineInfoData.getOptions().getGetSoftwareInfo())
  355. valuesToBeChecked.appendf(":%s:%s:%d", processTypeStr.str(), compName, processNumber);
  356. bool* found = uniqueProcesses.getValue(valuesToBeChecked.str());
  357. if (found && *found)
  358. continue;
  359. uniqueProcesses.setValue(valuesToBeChecked.str(), true);
  360. addProcessRequestToMachineInfoData(machineInfoData, address1, address2, processTypeStr.str(), compName, path, processNumber);
  361. }
  362. }
  363. void Cws_machineEx::addProcessRequestToMachineInfoData(CGetMachineInfoData& machineInfoData, const char* address1, const char* address2,
  364. const char* processType, const char* compName, const char* path, unsigned processNumber)
  365. {
  366. CIArrayOf<CMachineData>& machines = machineInfoData.getMachineData();
  367. ForEachItemIn(idx, machines)
  368. {
  369. CMachineData& machine = machines.item(idx);
  370. if (streq(address1, machine.getNetworkAddress()))
  371. {
  372. addProcessData(&machine, processType, compName, path, processNumber);
  373. return;
  374. }
  375. }
  376. char pathSep;
  377. EnvMachineOS os;
  378. Owned<IConstEnvironment> constEnv = getConstEnvironment();
  379. Owned<IConstMachineInfo> pMachineInfo = constEnv->getMachineByAddress(address1);
  380. if (pMachineInfo.get())
  381. os = pMachineInfo->getOS();
  382. else
  383. os = MachineOsUnknown;
  384. if (os == MachineOsW2K)
  385. pathSep = '\\';
  386. else
  387. pathSep = '/';
  388. Owned<CMachineData> machineNew = new CMachineData(address1, address2, os, pathSep);
  389. //Read possible dependencies for all processes
  390. set<string>& dependenciesForAllProcesses = machineNew->getDependencies();
  391. StringBuffer xPath;
  392. xPath.appendf("Platform[@name='%s']/ProcessFilter[@name='any']/Process", machineNew->getOS() == MachineOsW2K ? "Windows" : "Linux");
  393. Owned<IPropertyTreeIterator> processes = m_processFilters->getElements(xPath.str());
  394. ForEach (*processes)
  395. {
  396. StringBuffer processName;
  397. processes->query().getProp("@name", processName);
  398. processName.toLowerCase().replaceString(".exe", "");
  399. if ((processName.length() > 0) && (!streq(processName.str(), "hoagentd"))) //hoagentd is not needed anymore
  400. dependenciesForAllProcesses.insert(processName.str());
  401. }
  402. if (m_bMonitorDaliFileServer && (dependenciesForAllProcesses.find("dafilesrv") == dependenciesForAllProcesses.end()))
  403. dependenciesForAllProcesses.insert("dafilesrv");
  404. addProcessData(machineNew, processType, compName, path, processNumber);
  405. machines.append(*machineNew.getClear());
  406. }
  407. //Create a CProcessData object and add it to CMachineData
  408. void Cws_machineEx::addProcessData(CMachineData* machine, const char* processType, const char* compName,
  409. const char* path, unsigned processNumber)
  410. {
  411. if (!machine)
  412. return;
  413. StringBuffer pathStr = path;
  414. if (pathStr.length() > 0)
  415. {
  416. char pathSep = machine->getPathSep();
  417. if (pathStr.charAt(pathStr.length() - 1) != pathSep)
  418. pathStr.append(pathSep);
  419. }
  420. Owned<CProcessData> process = new CProcessData(compName, processType, pathStr.str(), processNumber);
  421. //Copy dependencies for all processes
  422. set<string>& dependenciesForThisProcess = process->getDependencies();
  423. set<string>& dependenciesForAllProcesses = machine->getDependencies();
  424. set<string>::const_iterator it = dependenciesForAllProcesses.begin();
  425. set<string>::const_iterator iEnd = dependenciesForAllProcesses.end();
  426. for (; it != iEnd; it++) //add in sorted order simply by traversing the map
  427. dependenciesForThisProcess.insert((*it).c_str());
  428. //now collect "process-specific" dependencies
  429. StringBuffer xPath;
  430. xPath.appendf("Platform[@name='%s']/ProcessFilter[@name='%s']", machine->getOS() == MachineOsW2K ? "Windows" : "Linux", processType);
  431. IPropertyTree* processFilterNode = m_processFilters->queryPropTree( xPath.str() );
  432. if (!processFilterNode)
  433. {
  434. machine->getProcesses().append(*process.getClear());
  435. return;
  436. }
  437. Owned<IPropertyTreeIterator> processes = processFilterNode->getElements("Process");
  438. ForEach (*processes)
  439. {
  440. IPropertyTree* pProcess = &processes->query();
  441. const char* name = pProcess->queryProp("@name");
  442. if (!name || streq(name, "."))
  443. continue;
  444. StringBuffer processName = name;
  445. processName.toLowerCase().replaceString(".exe", "");
  446. if (processName.length() < 1)
  447. continue;
  448. //Environment.xml may contain old filter settings.
  449. if (isLegacyFilter(processType, processName.str()))
  450. continue;
  451. if (pProcess->getPropBool("@remove", false))
  452. dependenciesForThisProcess.erase(processName.str());
  453. else
  454. dependenciesForThisProcess.insert(processName.str());
  455. }
  456. process->setMultipleInstances(machine->getOS() == MachineOsLinux && processFilterNode->getPropBool("@multipleInstances", false));
  457. machine->getProcesses().append(*process.getClear());
  458. }
  459. //Collect process settings for the requested target clusters
  460. void Cws_machineEx::readSettingsForTargetClusters(IEspContext& context, StringArray& targetClusters, CGetMachineInfoData& machineInfoData, IPropertyTree* targetClustersOut)
  461. {
  462. unsigned ordinality= targetClusters.ordinality();
  463. if (ordinality < 1)
  464. return;
  465. Owned<IConstEnvironment> constEnv = getConstEnvironment();
  466. Owned<IPropertyTree> pEnvironmentRoot = &constEnv->getPTree();
  467. if (!pEnvironmentRoot)
  468. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get environment information.");
  469. BoolHash uniqueProcesses;
  470. for (unsigned index=0; index<ordinality; index++)
  471. {
  472. StringBuffer clusterType;
  473. const char* clusterName = targetClusters.item(index);
  474. const char* pClusterName = strchr(clusterName, ':');
  475. if (pClusterName)
  476. {
  477. clusterType.append(clusterName, 0, pClusterName - clusterName);
  478. pClusterName++;
  479. }
  480. if (!pClusterName || !*pClusterName)
  481. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Cluster name not specified.");
  482. if (clusterType.length() < 1)
  483. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Cluster type not specified.");
  484. StringBuffer path;
  485. path.appendf("Software/Topology/Cluster[@name='%s']", pClusterName);
  486. IPropertyTree* pCluster = pEnvironmentRoot->queryPropTree(path.str());
  487. if (!pCluster)
  488. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Cluster %s not found in environment setting.", pClusterName);
  489. Owned<IPropertyTreeIterator> clusterProcesses;
  490. if (strieq(clusterType.str(), eqThorCluster) || strieq(clusterType.str(), eqRoxieCluster))
  491. {
  492. clusterProcesses.setown(pCluster->getElements(clusterType.str()));
  493. if (!clusterProcesses->first())
  494. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Cluster %s not found in environment setting.", clusterType.str());
  495. }
  496. Owned<IPropertyTreeIterator> eclCCServerProcesses= pCluster->getElements(eqEclCCServer);
  497. Owned<IPropertyTreeIterator> eclServerProcesses= pCluster->getElements(eqEclServer);
  498. Owned<IPropertyTreeIterator> eclAgentProcesses= pCluster->getElements(eqEclAgent);
  499. Owned<IPropertyTreeIterator> eclSchedulerProcesses= pCluster->getElements(eqEclScheduler);
  500. IPropertyTree *targetClusterOut = targetClustersOut->addPropTree("TargetCluster", createPTree("TargetCluster"));
  501. targetClusterOut->setProp("@Name", pClusterName);
  502. targetClusterOut->setProp("@Type", clusterType.str());
  503. //Read Cluster processes
  504. if (clusterProcesses && clusterProcesses->first())
  505. ForEach(*clusterProcesses)
  506. readTargetClusterProcesses(clusterProcesses->query(), clusterType.str(), uniqueProcesses, machineInfoData, targetClusterOut);
  507. //Read eclCCServer process
  508. if (eclCCServerProcesses->first())
  509. readTargetClusterProcesses(eclCCServerProcesses->query(), eqEclCCServer, uniqueProcesses, machineInfoData, targetClusterOut);
  510. //Read eclServer process
  511. if (eclServerProcesses->first())
  512. readTargetClusterProcesses(eclServerProcesses->query(), eqEclServer, uniqueProcesses, machineInfoData, targetClusterOut);
  513. //Read eclAgent process
  514. if (eclAgentProcesses->first())
  515. readTargetClusterProcesses(eclAgentProcesses->query(), eqEclAgent, uniqueProcesses, machineInfoData, targetClusterOut);
  516. //Read eclScheduler process
  517. if (eclSchedulerProcesses->first())
  518. readTargetClusterProcesses(eclSchedulerProcesses->query(), eqEclScheduler, uniqueProcesses, machineInfoData, targetClusterOut);
  519. }
  520. }
  521. //Collect settings for one group of target cluster processes
  522. void Cws_machineEx::readTargetClusterProcesses(IPropertyTree &processNode, const char* nodeType, BoolHash& uniqueProcesses, CGetMachineInfoData& machineInfoData,
  523. IPropertyTree* targetClustersOut)
  524. {
  525. const char* process = processNode.queryProp("@process");
  526. if (!process || !*process)
  527. throw MakeStringException(ECLWATCH_INTERNAL_ERROR, "Process attribute not set for ECLCCServer in environment setting.");
  528. Owned<IConstEnvironment> constEnv = getConstEnvironment();
  529. Owned<IPropertyTree> pEnvironmentRoot = &constEnv->getPTree();
  530. if (!pEnvironmentRoot)
  531. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get environment information.");
  532. IPropertyTree* pEnvironmentSoftware = pEnvironmentRoot->queryPropTree("Software");
  533. if (!pEnvironmentSoftware)
  534. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get environment information.");
  535. IPropertyTree* pClusterProcess = NULL;
  536. if (strieq(nodeType, eqThorCluster) || strieq(nodeType, eqRoxieCluster))
  537. {
  538. StringBuffer path;
  539. path.appendf("Software/%s[@name='%s']", nodeType, process);
  540. pClusterProcess = pEnvironmentRoot->queryPropTree(path.str());
  541. if (!pClusterProcess)
  542. throw MakeStringException(ECLWATCH_INTERNAL_ERROR, "Process not set for %s in environment setting.", path.str());
  543. if (strieq(nodeType, eqRoxieCluster))
  544. machineInfoData.appendRoxieClusters(process);
  545. }
  546. IPropertyTree *pInfo = targetClustersOut->addPropTree("Process", createPTree("Process"));
  547. pInfo->setProp("@Name", process);
  548. pInfo->setProp("@Type", nodeType);
  549. StringBuffer dirStr;
  550. IPropertyTree* pEnvironmentDirectories = pEnvironmentSoftware->queryPropTree("Directories");
  551. if (!pClusterProcess)
  552. {
  553. if (!pEnvironmentDirectories || !getConfigurationDirectory(pEnvironmentDirectories, "run", nodeType, process, dirStr))
  554. dirStr.clear().append(processNode.queryProp("@directory"));
  555. getProcesses(constEnv, pEnvironmentSoftware, process, nodeType, dirStr.str(), machineInfoData, false, uniqueProcesses);
  556. return;
  557. }
  558. if (!pEnvironmentDirectories || !getConfigurationDirectory(pEnvironmentDirectories, "run", nodeType, process, dirStr))
  559. dirStr.clear().append(pClusterProcess->queryProp("@directory"));
  560. if (strieq(nodeType, eqThorCluster))
  561. {
  562. getProcesses(constEnv, pClusterProcess, process, eqThorMasterProcess, dirStr.str(), machineInfoData, true, uniqueProcesses);
  563. getThorProcesses(constEnv, pClusterProcess, process, eqThorSlaveProcess, dirStr.str(), machineInfoData, uniqueProcesses);
  564. getThorProcesses(constEnv, pClusterProcess, process, eqThorSpareProcess, dirStr.str(), machineInfoData, uniqueProcesses);
  565. }
  566. else if (strieq(nodeType, eqRoxieCluster))
  567. {
  568. BoolHash uniqueRoxieProcesses;
  569. getProcesses(constEnv, pClusterProcess, process, eqRoxieServerProcess, dirStr.str(), machineInfoData, true, uniqueProcesses, &uniqueRoxieProcesses);
  570. }
  571. }
  572. void Cws_machineEx::getThorProcesses(IConstEnvironment* constEnv, IPropertyTree* cluster, const char* processName,
  573. const char* processType, const char* directory, CGetMachineInfoData& machineInfoData, BoolHash& uniqueProcesses)
  574. {
  575. if (!constEnv || !cluster)
  576. return;
  577. StringBuffer groupName;
  578. if (strieq(processType, eqThorSlaveProcess))
  579. getClusterGroupName(*cluster, groupName);
  580. else if (strieq(processType, eqThorSpareProcess))
  581. getClusterSpareGroupName(*cluster, groupName);
  582. if (groupName.length() < 1)
  583. return;
  584. Owned<IGroup> nodeGroup = queryNamedGroupStore().lookup(groupName.str());
  585. if (!nodeGroup || (nodeGroup->ordinality() == 0))
  586. return;
  587. unsigned processNumber = 0;
  588. Owned<INodeIterator> gi = nodeGroup->getIterator();
  589. ForEach(*gi)
  590. {
  591. StringBuffer addressRead;
  592. gi->query().endpoint().getIpText(addressRead);
  593. if (addressRead.length() == 0)
  594. {
  595. WARNLOG("Network address not found for a node in node group %s", groupName.str());
  596. continue;
  597. }
  598. processNumber++;
  599. StringBuffer netAddress;
  600. const char* ip = addressRead.str();
  601. if (!streq(ip, "."))
  602. {
  603. netAddress.append(ip);
  604. }
  605. else
  606. {
  607. IpAddress ipaddr = queryHostIP();
  608. ipaddr.getIpText(netAddress);
  609. }
  610. if (netAddress.length() == 0)
  611. {
  612. WARNLOG("Network address not found for a node in node group %s", groupName.str());
  613. continue;
  614. }
  615. Owned<IConstMachineInfo> pMachineInfo = constEnv->getMachineByAddress(addressRead.str());
  616. if (!pMachineInfo.get())
  617. {
  618. WARNLOG("Machine not found at network address %s", addressRead.str());
  619. continue;
  620. }
  621. setProcessRequest(machineInfoData, uniqueProcesses, netAddress.str(), addressRead.str(), processType, processName, directory, processNumber);
  622. }
  623. return;
  624. }
  625. void Cws_machineEx::getProcesses(IConstEnvironment* constEnv, IPropertyTree* environment, const char* processName,
  626. const char* processType, const char* directory, CGetMachineInfoData& machineInfoData,
  627. bool isThorOrRoxieProcess, BoolHash& uniqueProcesses, BoolHash* uniqueRoxieProcesses)
  628. {
  629. Owned<IPropertyTreeIterator> processes= environment->getElements(processType);
  630. ForEach(*processes)
  631. {
  632. StringArray processInstances, directories;
  633. IPropertyTree &process = processes->query();
  634. //Thor master and roxie server has been checked before this call.
  635. if (!isThorOrRoxieProcess)
  636. {
  637. const char* name = process.queryProp("@name");
  638. if (!name || !*name || !streq(name, processName))
  639. continue;
  640. }
  641. const char* computerName = process.queryProp("@computer");
  642. if (computerName && *computerName)
  643. appendProcessInstance(computerName, directory, NULL, processInstances, directories);
  644. else
  645. {
  646. Owned<IPropertyTreeIterator> instances= process.getElements("Instance");
  647. ForEach(*instances)
  648. {
  649. IPropertyTree &instance = instances->query();
  650. appendProcessInstance(instance.queryProp("@computer"), directory, instance.queryProp("@directory"), processInstances, directories);
  651. }
  652. }
  653. if (processInstances.length() < 1)
  654. continue;
  655. for (unsigned i = 0; i < processInstances.length(); i++)
  656. {
  657. const char* name0 = processInstances.item(i);
  658. const char* directory0 = directories.item(i);
  659. if (uniqueRoxieProcesses)//to avoid duplicate entries for roxie (one machine has only one roxie process).
  660. {
  661. bool* found = uniqueRoxieProcesses->getValue(name0);
  662. if (found && *found)
  663. continue;
  664. uniqueRoxieProcesses->setValue(name0, true);
  665. }
  666. Owned<IConstMachineInfo> pMachineInfo = constEnv->getMachine(name0);
  667. if (!pMachineInfo.get())
  668. {
  669. WARNLOG("Machine %s not found in environment setting", name0);
  670. continue;
  671. }
  672. SCMStringBuffer ep;
  673. pMachineInfo->getNetAddress(ep);
  674. const char* ip = ep.str();
  675. if (!ip)
  676. {
  677. WARNLOG("Network address not found for machine %s", name0);
  678. continue;
  679. }
  680. StringBuffer netAddress, configNetAddress = ip;
  681. if (!streq(ip, "."))
  682. {
  683. netAddress.append(ip);
  684. }
  685. else
  686. {
  687. IpAddress ipaddr = queryHostIP();
  688. ipaddr.getIpText(netAddress);
  689. }
  690. setProcessRequest(machineInfoData, uniqueProcesses, netAddress.str(), configNetAddress.str(), processType, processName, directory0);
  691. }
  692. }
  693. return;
  694. }
  695. void Cws_machineEx::setupLegacyFilters()
  696. {
  697. unsigned idx = 0;
  698. while (legacyFilterStrings[idx])
  699. {
  700. m_legacyFilters.setValue(legacyFilterStrings[idx], true);
  701. idx++;
  702. }
  703. return;
  704. }
  705. bool Cws_machineEx::isLegacyFilter(const char* processType, const char* dependency)
  706. {
  707. if (!processType || !*processType || !dependency || !*dependency)
  708. return false;
  709. StringBuffer filterString;
  710. filterString.appendf("%s:%s", processType, dependency);
  711. bool* found = m_legacyFilters.getValue(filterString.str());
  712. if (found && *found)
  713. return true;
  714. return false;
  715. }
  716. //The stateHashes stores different state hashes in one roxie cluster.
  717. //It also stores how many roxie nodes have the same state hashes.
  718. unsigned Cws_machineEx::addRoxieStateHash(const char* hash, StateHashes& stateHashes, unsigned& totalUniqueHashes)
  719. {
  720. if (!hash || !*hash)
  721. return -1;
  722. unsigned hashID = 0;
  723. IStateHash* stateHash = stateHashes.getValue(hash);
  724. if (stateHash)
  725. {
  726. //if the stateHashes already has the same 'hash', increases the count for the 'stateHash'.
  727. //The 'StateHash' with the highest count will be the 'Major StateHash'.
  728. //If a roxie node does not contain the 'Major StateHash', it has a 'mismatch' state hash.
  729. hashID = stateHash->queryID();
  730. stateHash->incrementCount();
  731. }
  732. else
  733. {
  734. //Add a new 'StateHash'. Set its hashID to totalUniqueHashes and set its count to 1.
  735. hashID = totalUniqueHashes;
  736. Owned<IStateHash> newStateHash = new CStateHash(hashID, 1);
  737. stateHashes.setValue(hash, newStateHash);
  738. totalUniqueHashes++;
  739. }
  740. return hashID;
  741. }
  742. void Cws_machineEx::updateMajorRoxieStateHash(StateHashes& stateHashes, CIArrayOf<CRoxieStateData>& roxieStates)
  743. {
  744. //Find out which state hash is for the most of the roxie nodes inside this roxie cluster.
  745. unsigned majorHashID = 0;
  746. unsigned majorHashCount = 0;
  747. HashIterator hashes(stateHashes);
  748. ForEach(hashes)
  749. {
  750. IStateHash *hash = stateHashes.mapToValue(&hashes.query());
  751. unsigned hashCount = hash->queryCount();
  752. if (majorHashCount >= hashCount)
  753. continue;
  754. majorHashCount = hashCount;
  755. majorHashID = hash->queryID();
  756. }
  757. //Set the MajorHash to false if the roxie node's HashID() != majorHashID.
  758. ForEachItemIn(ii, roxieStates)
  759. {
  760. CRoxieStateData& roxieState = roxieStates.item(ii);
  761. if (roxieState.getHashID() != majorHashID)
  762. roxieState.setMajorHash(false);
  763. }
  764. }
  765. void Cws_machineEx::readRoxieStatus(const Owned<IPropertyTree> controlResp, CIArrayOf<CRoxieStateData>& roxieStates)
  766. {
  767. StateHashes stateHashes;
  768. unsigned totalUniqueHashes = 0;
  769. Owned<IPropertyTreeIterator> roxieEndpoints = controlResp->getElements("Endpoint");
  770. ForEach(*roxieEndpoints)
  771. {
  772. IPropertyTree& roxieEndpoint = roxieEndpoints->query();
  773. const char *ep = roxieEndpoint.queryProp("@ep");
  774. if (!ep || !*ep)
  775. continue;
  776. bool ok = false, attached = false, detached = false;
  777. const char *status = roxieEndpoint.queryProp("Status");
  778. if (status && strieq(status, "ok"))
  779. ok = true;
  780. const char *stateHash = roxieEndpoint.queryProp("State/@hash");
  781. if (roxieEndpoint.hasProp("Dali/@connected"))
  782. {
  783. if (roxieEndpoint.getPropBool("Dali/@connected"))
  784. attached = true;
  785. else
  786. detached = true;
  787. }
  788. StringArray locations;
  789. locations.appendListUniq(ep, ":");
  790. Owned<CRoxieStateData> roxieState = new CRoxieStateData(locations.item(0), addRoxieStateHash(stateHash, stateHashes, totalUniqueHashes));
  791. roxieState->setState(ok, attached, detached, stateHash);
  792. roxieStates.append(*roxieState.getClear());
  793. }
  794. if (totalUniqueHashes > 1)
  795. updateMajorRoxieStateHash(stateHashes, roxieStates);
  796. }
  797. void Cws_machineEx::getRoxieStateInfo(CRoxieStateInfoThreadParam* param)
  798. {
  799. const char* clusterName = param->clusterName.get();
  800. if (!clusterName || !*clusterName)
  801. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "Roxie cluster not specified.");
  802. SocketEndpointArray servers;
  803. getRoxieProcessServers(clusterName, servers);
  804. if (!servers.length())
  805. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Roxie Process server not found.");
  806. Owned<IRoxieCommunicationClient> roxieClient = createRoxieCommunicationClient(servers.item(0), ROXIECONTROLSTATETIMEOUT);
  807. Owned<IPropertyTree> controlResp = roxieClient->sendRoxieControlAllNodes("<control:state/>", true);
  808. if (!controlResp)
  809. throw MakeStringException(ECLWATCH_INTERNAL_ERROR, "Failed to get control response from roxie %s.", clusterName);
  810. CIArrayOf<CRoxieStateData> roxieStates;
  811. readRoxieStatus(controlResp, roxieStates);
  812. ForEachItemIn(i, param->machineInfoTable)
  813. {
  814. IEspMachineInfoEx& machineInfo = param->machineInfoTable.item(i);
  815. if (!streq(machineInfo.getProcessType(), eqRoxieServerProcess) || !streq(machineInfo.getComponentName(), clusterName))
  816. continue;
  817. //This method is thread safe because each machineInfo (for one roxie node) belongs to only one Roxie cluster.
  818. //It is impossible for different threads to update the same machineInfo.
  819. bool foundRoxieState = false;
  820. ForEachItemIn(ii, roxieStates)
  821. {
  822. CRoxieStateData& roxieState = roxieStates.item(ii);
  823. if (!roxieState.matchIPAddress(machineInfo.getAddress()))
  824. continue;
  825. StringBuffer state, stateDetails;
  826. roxieState.reportState(state, stateDetails);
  827. machineInfo.setRoxieState(state.str());
  828. machineInfo.setRoxieStateDetails(stateDetails.str());
  829. foundRoxieState = true;
  830. }
  831. if (!foundRoxieState)
  832. {
  833. machineInfo.setRoxieState("??");
  834. machineInfo.setRoxieStateDetails("Roxie state not found");
  835. }
  836. }
  837. }
  838. void Cws_machineEx::getMachineInfo(IEspContext& context, bool getRoxieState, CGetMachineInfoData& machineInfoData)
  839. {
  840. UnsignedArray threadHandles;
  841. if (!getRoxieState)
  842. {
  843. CIArrayOf<CMachineData>& machines = machineInfoData.getMachineData();
  844. ForEachItemIn(idx, machines)
  845. {
  846. Owned<CMachineInfoThreadParam> pThreadReq = new CMachineInfoThreadParam(this, context, machineInfoData.getOptions(),
  847. machines.item(idx), machineInfoData.getMachineInfoTable(), machineInfoData.getMachineInfoColumns());
  848. PooledThreadHandle handle = m_threadPool->start( pThreadReq.getClear());
  849. threadHandles.append(handle);
  850. }
  851. }
  852. else
  853. {
  854. StringArray& roxieClusters = machineInfoData.getRoxieClusters();
  855. ForEachItemIn(i, roxieClusters)
  856. {
  857. Owned<CRoxieStateInfoThreadParam> pThreadReq = new CRoxieStateInfoThreadParam(this, roxieClusters.item(i),
  858. machineInfoData.getMachineInfoTable());
  859. PooledThreadHandle handle = m_threadPool->start( pThreadReq.getClear());
  860. threadHandles.append(handle);
  861. }
  862. machineInfoData.getMachineInfoColumns().append("Roxie State");
  863. }
  864. //Block for worker threads to finish, if necessary and then collect results
  865. //Not use joinAll() because multiple threads may call this method. Each call uses the pool to create
  866. //its own threads of checking query state. Each call should only join the ones created by that call.
  867. ForEachItemIn(i, threadHandles)
  868. m_threadPool->join(threadHandles.item(i));
  869. }
  870. ////////////////////////////////////////////////////////////////////
  871. // Get Machine Information based on Machine Information request //
  872. ////////////////////////////////////////////////////////////////////
  873. void Cws_machineEx::getMachineInfo(IEspContext& context, CGetMachineInfoData& machineInfoData)
  874. {
  875. double version = context.getClientVersion();
  876. getMachineInfo(context, false, machineInfoData);
  877. if ((version >= 1.13) && !machineInfoData.getRoxieClusters().empty())
  878. getMachineInfo(context, true, machineInfoData);
  879. }
  880. // the following method is invoked on worker threads of CMachineInfoThreadParam
  881. void Cws_machineEx::doGetMachineInfo(IEspContext& context, CMachineInfoThreadParam* pParam)
  882. {
  883. #ifdef DETECT_WS_MC_MEM_LEAKS
  884. static bool firstTime = true;
  885. if (firstTime)
  886. {
  887. firstTime = false;
  888. unsigned t = setAllocHook(true);
  889. }
  890. #endif //DETECT_WS_MC_MEM_LEAKS
  891. int error = 0;
  892. StringBuffer preflightCommand, response;
  893. buildPreflightCommand(context, pParam, preflightCommand);
  894. if (preflightCommand.length() < 1)
  895. {
  896. response.append("Failed in creating Machine Information command.\n");
  897. error = -1;
  898. }
  899. else
  900. {
  901. error = runCommand(context, pParam->m_machineData.getNetworkAddress(), pParam->m_machineData.getNetworkAddressInEnvSetting(), pParam->m_machineData.getOS(), preflightCommand.str(), pParam->m_options.getUserName(), pParam->m_options.getPassword(), response);
  902. if ((error == 0) && (response.length() > 0))
  903. readPreflightResponse(context, pParam, response.str(), error);
  904. }
  905. //Set IArrayOf<IEspMachineInfoEx> based on Preflight Response
  906. setMachineInfo(context, pParam, response.str(), error);
  907. #ifdef DETECT_WS_MC_MEM_LEAKS
  908. DBGLOG("Allocated=%d", setAllocHook(false));
  909. #endif //DETECT_WS_MC_MEM_LEAKS
  910. }
  911. void Cws_machineEx::buildPreflightCommand(IEspContext& context, CMachineInfoThreadParam* pParam, StringBuffer& preflightCommand)
  912. {
  913. preflightCommand.clear().appendf("/%s/sbin/%s -p=%s", environmentConfData.m_executionPath.str(),
  914. m_machineInfoFile.str(), environmentConfData.m_pidPath.str());
  915. if (preflightCommand.charAt(preflightCommand.length() - 1) == pParam->m_machineData.getPathSep())
  916. preflightCommand.remove(preflightCommand.length()-1, 1);
  917. bool checkDependency = false;
  918. CIArrayOf<CProcessData>& processes = pParam->m_machineData.getProcesses();
  919. ForEachItemIn(idx, processes)
  920. {
  921. CProcessData& process = processes.item(idx);
  922. if (!process.getName() || !*process.getName())
  923. continue;
  924. StringBuffer procName;
  925. if (streq(process.getType(), eqThorSlaveProcess))
  926. procName.appendf("thorslave_%s_%d,%s_slave_%d", process.getName(), process.getProcessNumber(), process.getName(), process.getProcessNumber());
  927. else if (streq(process.getType(), eqThorMasterProcess))
  928. procName.appendf("%s,%s_master", process.getName(), process.getName());
  929. else
  930. procName.append(process.getName());
  931. if (idx < 1)
  932. preflightCommand.appendf(" -n=%s", procName.str());
  933. else
  934. preflightCommand.appendf(",%s", procName.str());
  935. if (!process.getDependencies().empty())
  936. checkDependency = true;
  937. }
  938. if (checkDependency || !pParam->m_options.getApplyProcessFilter())
  939. preflightCommand.append(" -d=ALL");
  940. if (pParam->m_options.getGetStorageInfo() && !pParam->m_options.getLocalFileSystemsOnly())
  941. preflightCommand.append(" -m=YES");
  942. }
  943. int Cws_machineEx::runCommand(IEspContext& context, const char* sAddress, const char* sConfigAddress, EnvMachineOS os,
  944. const char* sCommand, const char* sUserId, const char* sPassword, StringBuffer& response)
  945. {
  946. int exitCode = -1;
  947. try
  948. {
  949. StringBuffer command(sCommand);
  950. StringBuffer userId;
  951. StringBuffer password;
  952. bool bLinux;
  953. if (sConfigAddress && *sConfigAddress)
  954. getAccountAndPlatformInfo(sConfigAddress, userId, password, bLinux);
  955. else
  956. getAccountAndPlatformInfo(sAddress, userId, password, bLinux);
  957. if (!sUserId || !*sUserId || !sPassword ||!*sPassword)
  958. {
  959. //BUG: 9825 - remote execution on linux needs to use individual accounts
  960. //use userid/password in ESP context for remote execution...
  961. if (bLinux)
  962. {
  963. userId.clear();
  964. password.clear();
  965. context.getUserID(userId);
  966. context.getPassword(password);
  967. }
  968. }
  969. else
  970. {
  971. userId.clear().append(sUserId);
  972. password.clear().append(sPassword);
  973. }
  974. // make sure there actually is something in command before we run a remote connection
  975. if (command.length() < 1)
  976. return exitCode;
  977. Owned<IFRunSSH> connection = createFRunSSH();
  978. connection->init(command.str(),NULL,NULL,NULL,m_SSHConnectTimeoutSeconds,0);
  979. // executed as single connection
  980. connection->exec(IpAddress(sAddress),NULL,false);
  981. response.append(connection->getReplyText()[0]);
  982. exitCode = connection->getReply()[0];
  983. int len = response.length();
  984. if (len > 0 && response.charAt(--len) == '\n') // strip newline
  985. response.setLength(len);
  986. if (response.length() && !exitCode)
  987. response.insert(0, "Response: ");
  988. else if (!exitCode)
  989. response.insert(0, "No response recieved.\n");
  990. }
  991. // CFRunSSH uses a MakeStringExceptionDirect throw to pass code and result string
  992. catch(IException* e)
  993. {
  994. exitCode = e->errorCode();
  995. // errorCode == -1 on successful CFRunSSH execution
  996. if(exitCode == -1)
  997. exitCode = 0;
  998. StringBuffer buf;
  999. e->errorMessage(buf);
  1000. response.append(buf.str());
  1001. int len = response.length();
  1002. if (len > 0 && response.charAt(--len) == '\n') // strip newline
  1003. response.setLength(len);
  1004. // on successful connection
  1005. if (response.length() && !exitCode)
  1006. response.insert(0,"Response: ");
  1007. else if (!exitCode)
  1008. response.insert(0, "No response recieved.\n");
  1009. e->Release();
  1010. }
  1011. #ifndef NO_CATCHALL
  1012. catch(...)
  1013. {
  1014. response.append("An unknown exception occurred!");
  1015. exitCode = -1;
  1016. }
  1017. #endif
  1018. return exitCode;
  1019. }
  1020. int Cws_machineEx::invokeProgram(const char *command_line, StringBuffer& response)
  1021. {
  1022. char buffer[128];
  1023. FILE *fp;
  1024. // Run the command so that it writes its output to a pipe. Open this
  1025. // pipe with read text attribute so that we can read it
  1026. // like a text file.
  1027. if (getEspLogLevel()>LogNormal)
  1028. {
  1029. DBGLOG("command_line=<%s>", command_line);
  1030. }
  1031. #ifndef NO_CONNECTION_DEBUG
  1032. if( (fp = popen( command_line, "r" )) == NULL )
  1033. return -1;
  1034. #else
  1035. if( (fp = fopen( "c:\\temp\\preflight_result.txt", "r" )) == NULL )
  1036. return -1;
  1037. #endif
  1038. // Read pipe until end of file. End of file indicates that
  1039. //the stream closed its standard out (probably meaning it
  1040. //terminated).
  1041. while ( !feof(fp) )
  1042. if ( fgets( buffer, 128, fp) )
  1043. response.append( buffer );
  1044. if (getEspLogLevel()>LogNormal)
  1045. {
  1046. DBGLOG("response=<%s>", response.str());
  1047. }
  1048. // Close pipe and print return value of CHKDSK.
  1049. #ifndef NO_CONNECTION_DEBUG
  1050. return pclose( fp );
  1051. #else
  1052. return fclose( fp );
  1053. #endif
  1054. }
  1055. void Cws_machineEx::readPreflightResponse(IEspContext& context, CMachineInfoThreadParam* pParam, const char* response, int error)
  1056. {
  1057. if (!response || !*response)
  1058. return;
  1059. StringBuffer computerUpTime;
  1060. readALineFromResult(response, "ComputerUpTime:", computerUpTime, true);
  1061. if (computerUpTime.length() < 1)
  1062. computerUpTime.append("-");
  1063. else
  1064. {
  1065. const char* pStr = strchr(computerUpTime.str(), ' ');
  1066. if (pStr)
  1067. {
  1068. pStr++;
  1069. pStr = strchr(pStr, ' ');
  1070. if (pStr)
  1071. {
  1072. pStr++;
  1073. if (pStr)
  1074. pParam->m_machineData.setComputerUpTime(pStr);
  1075. }
  1076. }
  1077. if (!pStr)
  1078. pParam->m_machineData.setComputerUpTime(computerUpTime);
  1079. }
  1080. if (pParam->m_options.getGetProcessorInfo())
  1081. {
  1082. StringBuffer CPUIdle;
  1083. readALineFromResult(response, "CPU-Idle:", CPUIdle, true);
  1084. if (CPUIdle.length() < 1)
  1085. pParam->m_machineData.setCPULoad(0);
  1086. else
  1087. {
  1088. if (CPUIdle.charAt(CPUIdle.length() - 1) == '%')
  1089. CPUIdle.setLength(CPUIdle.length() - 1);
  1090. pParam->m_machineData.setCPULoad(100-atoi(CPUIdle.str()));
  1091. }
  1092. }
  1093. if (pParam->m_options.getGetStorageInfo())
  1094. readStorageData(response, pParam);
  1095. if (pParam->m_options.getGetSoftwareInfo())
  1096. readProcessData(response, pParam);
  1097. }
  1098. void Cws_machineEx::readALineFromResult(const char *result, const char *start, StringBuffer& value, bool trim)
  1099. {
  1100. if (!result || !*result)
  1101. return;
  1102. const char* pStr = strstr(result, start);
  1103. if (!pStr)
  1104. return;
  1105. pStr += strlen(start);
  1106. if (!pStr)
  1107. return;
  1108. const char* pStr1 = strchr(pStr, 0x0a);
  1109. if (pStr1)
  1110. value.append(pStr, 0, pStr1 - pStr);
  1111. else
  1112. value.append(pStr);
  1113. if (trim)
  1114. value.trim();
  1115. }
  1116. void Cws_machineEx::readStorageData(const char* response, CMachineInfoThreadParam* pParam)
  1117. {
  1118. if (!response || !*response)
  1119. return;
  1120. const char* pStr = strstr(response, "---SpaceUsedAndFree---");
  1121. if (!pStr)
  1122. DBGLOG("Storage information not found on %s", pParam->m_machineData.getNetworkAddress());
  1123. bool isTitleLine = true;
  1124. CIArrayOf<CStorageData>& storage = pParam->m_machineData.getStorage();
  1125. while (pStr)
  1126. {
  1127. StringBuffer buf;
  1128. const char* pStr1 = strchr(pStr, 0x0a);
  1129. if (pStr1)
  1130. {
  1131. buf.append(pStr, 0, pStr1 - pStr);
  1132. pStr = pStr1+1;
  1133. }
  1134. else
  1135. {
  1136. buf.append(pStr);
  1137. pStr = NULL;
  1138. }
  1139. if (isTitleLine)
  1140. {
  1141. isTitleLine = false;
  1142. continue;
  1143. }
  1144. if (buf.length() > 0)
  1145. {
  1146. StringBuffer diskSpaceTitle;
  1147. int diskSpacePercentAvail = 0;
  1148. __int64 diskSpaceAvailable = 0, diskSpaceTotal = 0;
  1149. if (!readStorageSpace(buf.str(), diskSpaceTitle, diskSpaceAvailable, diskSpaceTotal, diskSpacePercentAvail))
  1150. DBGLOG("Invalid storage information on %s: %s", pParam->m_machineData.getNetworkAddress(), buf.str());
  1151. else if ((diskSpaceTitle.length() > 0) && !excludePartition(diskSpaceTitle.str()))
  1152. {
  1153. Owned<CStorageData> diskData = new CStorageData(diskSpaceTitle, diskSpaceAvailable, diskSpaceTotal, diskSpacePercentAvail);
  1154. storage.append(*diskData.getClear());
  1155. }
  1156. }
  1157. if (!pStr || (strnicmp(pStr, "---ProcessList1---", 18)==0))
  1158. break;
  1159. }
  1160. }
  1161. bool Cws_machineEx::readStorageSpace(const char *line, StringBuffer& title, __int64& free, __int64& total, int& percentAvail)
  1162. {
  1163. if (!line || !*line)
  1164. return false;
  1165. StringBuffer freeStr, usedStr;
  1166. const char* pStr = line;
  1167. const char* pStr1 = strchr(pStr, ':');
  1168. if (!pStr1)
  1169. return false;
  1170. title.clear().append(pStr, 0, pStr1 - pStr);
  1171. pStr = pStr1 + 2;
  1172. pStr1 = (char*) strchr(pStr, ' ');
  1173. if (!pStr1)
  1174. return false;
  1175. usedStr.append(pStr, 0, pStr1 - pStr);
  1176. pStr = pStr1 + 1;
  1177. if (!pStr)
  1178. return false;
  1179. freeStr.append(pStr);
  1180. __int64 factor1 = 1;
  1181. if (freeStr.length() > 9)
  1182. {
  1183. freeStr.setLength(freeStr.length()-6);
  1184. factor1 = 1000000;
  1185. }
  1186. free = atol(freeStr.str())*factor1;
  1187. __int64 factor2 = 1;
  1188. if (usedStr.length() > 9)
  1189. {
  1190. usedStr.setLength(usedStr.length()-6);
  1191. factor2 = 1000000;
  1192. }
  1193. __int64 used = atol(usedStr.str())*factor2;
  1194. total = free + used;
  1195. if (total > 0)
  1196. percentAvail = (int) ((free*100)/total);
  1197. free = (__int64) free /1000; //MByte
  1198. total = (__int64) total /1000; //MByte
  1199. return true;
  1200. }
  1201. void Cws_machineEx::buildProcessPath(StringBuffer &processPath, const char * processName, CMachineInfoThreadParam * pParam)
  1202. {
  1203. if (environmentConfData.m_pidPath.charAt(environmentConfData.m_pidPath.length() - 1) != pParam->m_machineData.getPathSep())
  1204. processPath.setf("%s%c%s:", environmentConfData.m_pidPath.str(), pParam->m_machineData.getPathSep(), processName);
  1205. else
  1206. processPath.setf("%s%s:", environmentConfData.m_pidPath.str(), processName);
  1207. }
  1208. void Cws_machineEx::readProcessData(const char* response, CMachineInfoThreadParam* pParam)
  1209. {
  1210. if (!response || !*response)
  1211. return;
  1212. CIArrayOf<CProcessData>& processes = pParam->m_machineData.getProcesses();
  1213. ForEachItemIn(idx, processes)
  1214. {
  1215. CProcessData& process = processes.item(idx);
  1216. if (!process.getName() || !*process.getName())
  1217. continue;
  1218. StringBuffer procName, catError, processPath, processData;
  1219. if (streq(process.getType(), eqThorSlaveProcess))
  1220. {
  1221. procName.appendf("thorslave_%s_%d", process.getName(), process.getProcessNumber());
  1222. buildProcessPath(processPath,procName.str(),pParam);
  1223. catError.setf("cat: %s",processPath.str());
  1224. catError.insert(catError.length()-1,".pid");
  1225. if (!strstr(response,catError.str()))
  1226. readALineFromResult(response, processPath.str(), processData, true);
  1227. else
  1228. {
  1229. procName.setf("%s_slave_%d", process.getName(), process.getProcessNumber());
  1230. buildProcessPath(processPath,procName.str(),pParam);
  1231. readALineFromResult(response, processPath.str(), processData, true);
  1232. }
  1233. }
  1234. else if (streq(process.getType(), eqThorMasterProcess))
  1235. {
  1236. procName.appendf("%s", process.getName());
  1237. buildProcessPath(processPath,procName.str(),pParam);
  1238. catError.setf("cat: %s",processPath.str());
  1239. catError.insert(catError.length()-1,".pid");
  1240. if (!strstr(response,catError.str()))
  1241. readALineFromResult(response, processPath.str(), processData, true);
  1242. else
  1243. {
  1244. procName.setf("%s_master", process.getName());
  1245. buildProcessPath(processPath,procName.str(),pParam);
  1246. readALineFromResult(response, processPath.str(), processData, true);
  1247. }
  1248. }
  1249. else
  1250. {
  1251. procName.append(process.getName());
  1252. buildProcessPath(processPath,procName.str(),pParam);
  1253. readALineFromResult(response, processPath.str(), processData, true);
  1254. }
  1255. if (processData.length() < 1)
  1256. {
  1257. DBGLOG("Information for process %s not found", processPath.str());
  1258. continue;
  1259. }
  1260. const char* pStr = strchr(processData.str(), ' ');
  1261. if (!pStr)
  1262. {
  1263. DBGLOG("incorrect data for process %s: %s", processPath.str(), processData.str());
  1264. continue;
  1265. }
  1266. unsigned len = pStr - processData.str();
  1267. StringBuffer pid, upTime;
  1268. pid.append(processData.str(), 0, len);
  1269. len++;
  1270. upTime.append(processData.str(), len, processData.length() - len);
  1271. upTime.replaceString("-", " day(s) ");
  1272. process.setPID(pid.str());
  1273. process.setUpTime(upTime.str());
  1274. }
  1275. readRunningProcesses(response, pParam);
  1276. }
  1277. void Cws_machineEx::readRunningProcesses(const char* response, CMachineInfoThreadParam* pParam)
  1278. {
  1279. if (!response || !*response)
  1280. return;
  1281. const char* pStr = strstr(response, "---ProcessList2---");
  1282. if (!pStr)
  1283. DBGLOG("Running process not found on %s", pParam->m_machineData.getNetworkAddress());
  1284. IArrayOf<IEspProcessInfo>& runningProcesses = pParam->m_machineData.getRunningProcesses();
  1285. while (pStr)
  1286. {
  1287. //read a line
  1288. StringBuffer lineStr;
  1289. const char* pStr1 = strchr(pStr, 0x0a);
  1290. if (!pStr1)
  1291. {
  1292. lineStr.append(pStr);
  1293. pStr = NULL;
  1294. }
  1295. else
  1296. {
  1297. lineStr.append(pStr, 0, pStr1 - pStr);
  1298. pStr = pStr1+1;
  1299. }
  1300. if (lineStr.length() < 1)
  1301. continue;
  1302. StringBuffer pidStr, desc, param;
  1303. pStr1 = lineStr.str();
  1304. const char* pStr2 = strchr(pStr1, ' ');
  1305. if (!pStr2)
  1306. continue;
  1307. pidStr.append(pStr1, 0, pStr2 - pStr1);
  1308. param.append(pStr2+1);
  1309. if (param.length() < 1)
  1310. continue;
  1311. if (streq(param.str(), "ps"))
  1312. continue;
  1313. bool isNumber = true;
  1314. for (unsigned i = 0; i < pidStr.length(); i++)
  1315. {
  1316. if (!isdigit(pidStr.charAt(i)))
  1317. {
  1318. isNumber = false;
  1319. break;
  1320. }
  1321. }
  1322. if (!isNumber)
  1323. continue;
  1324. int pid = atoi(pidStr.str());
  1325. desc = param;
  1326. if ((desc.charAt(0) == '.') && (param.charAt(1) == '/'))
  1327. desc.remove(0, 2);
  1328. if (desc.charAt(desc.length() - 1) == '/')
  1329. desc.remove(desc.length() - 1, 1);
  1330. if (desc.charAt(0) == '[')
  1331. {
  1332. desc.remove(0, 1);
  1333. if (desc.charAt(desc.length() - 1) == ']')
  1334. desc.remove(desc.length() - 1, 1);
  1335. }
  1336. Owned<IEspProcessInfo> info = createProcessInfo("","");
  1337. info->setPID(pid);
  1338. info->setParameter(param.str());
  1339. info->setDescription(desc.str());
  1340. runningProcesses.append(*info.getClear());
  1341. }
  1342. }
  1343. void Cws_machineEx::setMachineInfo(IEspContext& context, CMachineInfoThreadParam* pParam, const char* response, int error)
  1344. {
  1345. //Read additionalProcessFilters which will be used in setProcessInfo()/setProcessComponent()
  1346. set<string>& additionalProcesses = pParam->m_machineData.getAdditinalProcessFilters();
  1347. StringArray& additionalProcessFilters = pParam->m_options.getAdditionalProcessFilters();
  1348. if (pParam->m_options.getApplyProcessFilter() && !additionalProcessFilters.empty())
  1349. {
  1350. int len = additionalProcessFilters.length();
  1351. for (int i=0; i<len; i++)
  1352. {
  1353. StringBuffer processName = additionalProcessFilters.item(i);
  1354. processName.toLowerCase().replaceString(".exe", "");
  1355. if (processName.length() > 0)
  1356. additionalProcesses.insert(processName.str());
  1357. }
  1358. }
  1359. CIArrayOf<CProcessData>& processes = pParam->m_machineData.getProcesses();
  1360. ForEachItemIn(idx, processes)
  1361. {
  1362. CProcessData& process = processes.item(idx);
  1363. Owned<IEspMachineInfoEx> pMachineInfo = static_cast<IEspMachineInfoEx*>(new CMachineInfoEx(""));
  1364. setProcessInfo(context, pParam, response, error, process, idx<1, pMachineInfo);
  1365. synchronized block(mutex_machine_info_table);
  1366. pParam->m_machineInfoTable.append(*pMachineInfo.getLink());
  1367. }
  1368. }
  1369. void Cws_machineEx::setProcessInfo(IEspContext& context, CMachineInfoThreadParam* pParam, const char* response,
  1370. int error, CProcessData& process, bool firstProcess, IEspMachineInfoEx* pMachineInfo)
  1371. {
  1372. double version = context.getClientVersion();
  1373. bool isEclAgentProcess = process.getType() && strieq(process.getType(), eqEclAgent);
  1374. pMachineInfo->setAddress(pParam->m_machineData.getNetworkAddress());
  1375. pMachineInfo->setConfigAddress(pParam->m_machineData.getNetworkAddressInEnvSetting());
  1376. pMachineInfo->setOS(pParam->m_machineData.getOS());
  1377. if (process.getName() && *process.getName())
  1378. pMachineInfo->setComponentName(process.getName());
  1379. if (process.getPath() && *process.getPath())
  1380. pMachineInfo->setComponentPath(process.getPath());
  1381. //set DisplayType
  1382. if (process.getType() && *process.getType())
  1383. {
  1384. if (isEclAgentProcess)
  1385. {
  1386. pMachineInfo->setProcessType(eqAgentExec);
  1387. pMachineInfo->setDisplayType("Agent Exec");
  1388. }
  1389. else
  1390. {
  1391. pMachineInfo->setProcessType(process.getType());
  1392. StringBuffer displayName;
  1393. getProcessDisplayName(process.getType(), displayName);
  1394. pMachineInfo->setDisplayType(displayName.str());
  1395. }
  1396. }
  1397. else if (process.getName() && *process.getName())
  1398. {
  1399. pMachineInfo->setDisplayType(process.getName());
  1400. }
  1401. if ((version > 1.09) && process.getType() && strieq(process.getType(), eqThorSlaveProcess))
  1402. {
  1403. pMachineInfo->setProcessNumber(process.getProcessNumber());
  1404. }
  1405. if (error != 0 || !response || !*response)
  1406. {
  1407. StringBuffer description;
  1408. if (!response || !*response)
  1409. description.append("Failed in getting Machine Information");
  1410. else
  1411. description = response;
  1412. pMachineInfo->setDescription(description.str());
  1413. }
  1414. else
  1415. {
  1416. //Now, add more columns based on 'response'
  1417. pMachineInfo->setUpTime(pParam->m_machineData.getComputerUpTime());
  1418. pParam->addColumn("Up Time");
  1419. if (pParam->m_options.getGetStorageInfo())
  1420. {
  1421. IArrayOf<IEspStorageInfo> storageArray;
  1422. CIArrayOf<CStorageData>& storage = pParam->m_machineData.getStorage();
  1423. ForEachItemIn(idx, storage)
  1424. {
  1425. CStorageData& diskData = storage.item(idx);
  1426. Owned<IEspStorageInfo> info = static_cast<IEspStorageInfo*>(new CStorageInfo(""));
  1427. info->setDescription(diskData.getDiskSpaceTitle());
  1428. info->setTotal(diskData.getDiskSpaceTotal());
  1429. info->setAvailable(diskData.getDiskSpaceAvailable());
  1430. info->setPercentAvail(diskData.getDiskSpacePercentAvail());
  1431. storageArray.append(*info.getLink());
  1432. pParam->addColumn(diskData.getDiskSpaceTitle());
  1433. }
  1434. pMachineInfo->setStorage(storageArray);
  1435. storageArray.kill();
  1436. }
  1437. if (pParam->m_options.getGetProcessorInfo())
  1438. {
  1439. IArrayOf<IEspProcessorInfo> processorArray;
  1440. Owned<IEspProcessorInfo> info = static_cast<IEspProcessorInfo*>(new CProcessorInfo(""));
  1441. info->setLoad(pParam->m_machineData.getCPULoad());
  1442. processorArray.append(*info.getLink());
  1443. pMachineInfo->setProcessors(processorArray);
  1444. processorArray.kill();
  1445. pParam->addColumn("CPU Load");
  1446. }
  1447. if (pParam->m_options.getGetSoftwareInfo())
  1448. {
  1449. IArrayOf<IEspSWRunInfo> processArray;
  1450. IEspComponentInfo* pComponentInfo = &pMachineInfo->updateComponentInfo();
  1451. setProcessComponent(context, pParam, process, firstProcess, processArray, pComponentInfo);
  1452. if (processArray.ordinality())
  1453. {
  1454. //Set running processes if ApplyProcessFilter is set to false
  1455. //Set processes not running if ApplyProcessFilter is set to true
  1456. pMachineInfo->setRunning(processArray);
  1457. }
  1458. pParam->addColumn("Processes");
  1459. pParam->addColumn("Condition");
  1460. pParam->addColumn("State");
  1461. pParam->addColumn("UpTime");
  1462. }
  1463. }
  1464. }
  1465. void Cws_machineEx::setProcessComponent(IEspContext& context, CMachineInfoThreadParam* pParam, CProcessData& process,
  1466. bool firstProcess, IArrayOf<IEspSWRunInfo>& processArray, IEspComponentInfo* pComponentInfo)
  1467. {
  1468. const char* procType = process.getType();
  1469. const char* procPID = process.getPID();
  1470. //If a component (ex. dropzone) has no process type, it is not a process and does not have a PID.
  1471. //FTSlaveProcess may not have a PID since it is launched dynamically during a spray.
  1472. if (pParam->m_options.getApplyProcessFilter() && (isEmptyString(procPID) &&
  1473. !isEmptyString(procType) && !strieq(procType, "FTSlaveProcess")))
  1474. {
  1475. Owned<IEspSWRunInfo> info = static_cast<IEspSWRunInfo*>(new CSWRunInfo(""));
  1476. info->setName(process.getName());
  1477. info->setInstances(0);
  1478. processArray.append( *info.getLink() );
  1479. }
  1480. set<string>& additionalProcesses = pParam->m_machineData.getAdditinalProcessFilters();
  1481. map<string, Linked<IEspSWRunInfo> > runningProcessMap; //save only one description of each process
  1482. set<string>& dependencies = process.getDependencies();
  1483. IArrayOf<IEspProcessInfo>& runningProcesses = pParam->m_machineData.getRunningProcesses();
  1484. if (runningProcesses.length() > 0)
  1485. {
  1486. if (!pParam->m_options.getApplyProcessFilter()) //need to display all of the running processes
  1487. enumerateRunningProcesses( pParam, process, &runningProcessMap, firstProcess);
  1488. else if (!dependencies.empty() || !additionalProcesses.empty())
  1489. enumerateRunningProcesses(pParam, process, NULL, firstProcess);
  1490. }
  1491. map<string, Linked<IEspSWRunInfo> >::const_iterator it = runningProcessMap.begin();
  1492. map<string, Linked<IEspSWRunInfo> >::const_iterator iEnd = runningProcessMap.end();
  1493. for (; it != iEnd; it++) //add in sorted order simply by traversing the map
  1494. {
  1495. Linked<IEspSWRunInfo> info( (*it).second );
  1496. processArray.append( *info.getLink() );
  1497. }
  1498. bool dependencyDown = false;
  1499. if (!dependencies.empty())
  1500. {
  1501. dependencyDown = true;
  1502. if (pParam->m_options.getApplyProcessFilter())
  1503. {
  1504. set<string>::const_iterator it = dependencies.begin();
  1505. set<string>::const_iterator iEnd = dependencies.end();
  1506. for (; it != iEnd; it++)
  1507. {
  1508. Owned<IEspSWRunInfo> info = static_cast<IEspSWRunInfo*>(new CSWRunInfo(""));
  1509. info->setName(it->c_str());
  1510. info->setInstances(0);
  1511. processArray.append( *info.getLink() );
  1512. }
  1513. }
  1514. }
  1515. if (pParam->m_options.getApplyProcessFilter() && !additionalProcesses.empty())
  1516. {
  1517. set<string>::const_iterator it = additionalProcesses.begin();
  1518. set<string>::const_iterator iEnd = additionalProcesses.end();
  1519. for (; it != iEnd; it++)
  1520. {
  1521. Owned<IEspSWRunInfo> info = static_cast<IEspSWRunInfo*>(new CSWRunInfo(""));
  1522. info->setName(it->c_str());
  1523. info->setInstances(0);
  1524. processArray.append( *info.getLink() );
  1525. }
  1526. }
  1527. if (!dependencyDown && (!isEmptyString(procPID) || isEmptyString(procType) || strieq(procType, "FTSlaveProcess")))
  1528. {
  1529. //conditions: unknown, normal, warning, minor, major, critical, fatal
  1530. pComponentInfo->setCondition( 1 );
  1531. pComponentInfo->setState(5);
  1532. if (process.getUpTime() && *process.getUpTime())
  1533. pComponentInfo->setUpTime( process.getUpTime() );
  1534. }
  1535. else
  1536. {
  1537. pComponentInfo->setCondition(2); //Warnning
  1538. pComponentInfo->setState(0);
  1539. }
  1540. }
  1541. //Erase this process from dependencies and, if firstProcess, additionalProcesses;
  1542. //If processMap is not NULL, add this process to processMap
  1543. void Cws_machineEx::enumerateRunningProcesses(CMachineInfoThreadParam* pParam, CProcessData& process, map<string, Linked<IEspSWRunInfo> >* runningProcessMap, bool firstProcess)
  1544. {
  1545. set<string>& dependencies = process.getDependencies();
  1546. set<string>& additionalProcesses = pParam->m_machineData.getAdditinalProcessFilters();
  1547. IArrayOf<IEspProcessInfo>& runningProcesses = pParam->m_machineData.getRunningProcesses();
  1548. ForEachItemIn(k, runningProcesses)
  1549. {
  1550. IEspProcessInfo& processInfo = runningProcesses.item(k);
  1551. //Erase this process from dependencies and, if firstProcess, additionalProcesses
  1552. const char* pName = processInfo.getDescription();
  1553. if (pParam->m_machineData.getOS() == MachineOsW2K)
  1554. {
  1555. StringBuffer sName = pName;
  1556. pName = sName.toLowerCase().replaceString(".exe", "").str();
  1557. if (!dependencies.empty())
  1558. dependencies.erase(pName);
  1559. if (pParam->m_options.getApplyProcessFilter() && firstProcess && !additionalProcesses.empty())
  1560. additionalProcesses.erase(pName);
  1561. }
  1562. else
  1563. {
  1564. //dafilesrv would probably be running from a global directory
  1565. //and not component's installation directory so ignore their paths
  1566. const char* pPath = pName;
  1567. if ( !strieq(pName, "dafilesrv"))
  1568. {
  1569. const char* param = processInfo.getParameter();
  1570. if (param && *param)
  1571. {
  1572. if (strncmp(param, "bash ", 5))
  1573. pPath = param;
  1574. else
  1575. pPath = param + 5;
  1576. if (!pPath || !*pPath)
  1577. continue;
  1578. //params typically is like "/c$/esp_dir/esp [parameters...]"
  1579. //so just pick the full path
  1580. const char* pch = strchr(pPath, ' ');
  1581. if (pch)
  1582. {
  1583. StringBuffer sPath = pPath;
  1584. sPath.setLength( pch - pPath );
  1585. pPath = sPath.str();
  1586. }
  1587. }
  1588. }
  1589. if (!dependencies.empty())
  1590. {
  1591. const char* pProcessName;
  1592. if (process.getType() && !strncmp(process.getType(), "Thor", 4) && !strnicmp(pName, "thor", 4))
  1593. {
  1594. const char* pch = strrchr(pPath, pParam->m_machineData.getPathSep());
  1595. pProcessName = pch ? pch+1 : pName;
  1596. }
  1597. else
  1598. {
  1599. const char* pName0 = process.getMultipleInstances() ? pPath : pName;
  1600. const char* pch = strrchr(pName0, pParam->m_machineData.getPathSep());
  1601. pProcessName = pch ? pch+1 : pName0;
  1602. }
  1603. dependencies.erase(pProcessName);
  1604. if (pParam->m_options.getApplyProcessFilter() && firstProcess && !additionalProcesses.empty())
  1605. additionalProcesses.erase(pProcessName);
  1606. }
  1607. pName = pPath;
  1608. }
  1609. if (!runningProcessMap)
  1610. continue;
  1611. //Add this process to runningProcessMap
  1612. map<string, Linked<IEspSWRunInfo> >::iterator it = runningProcessMap->find(pName);
  1613. if ( it != runningProcessMap->end()) //not in the set
  1614. {
  1615. Linked<IEspSWRunInfo>& linkedPtr = (*it).second;
  1616. linkedPtr->setInstances( linkedPtr->getInstances() + 1);
  1617. }
  1618. else
  1619. {
  1620. Owned<IEspSWRunInfo> info = static_cast<IEspSWRunInfo*>(new CSWRunInfo(""));
  1621. info->setName(pName);
  1622. info->setInstances(1);
  1623. runningProcessMap->insert(pair<string, Linked<IEspSWRunInfo> >(pName, info));
  1624. }
  1625. }
  1626. }
  1627. void Cws_machineEx::getProcessDisplayName(const char* processName, StringBuffer& displayName)
  1628. {
  1629. //produces "LDAPServerProcess" as "LDAP Server" and "EspService" as "Esp Service", etc.
  1630. const char* end = strstr(processName, "Process");
  1631. if (!end)
  1632. end = processName + strlen(processName);
  1633. displayName.append(*processName);
  1634. processName++;
  1635. bool bLower = false;
  1636. while (processName < end)
  1637. {
  1638. char ch = *processName;
  1639. if (!isupper(ch))
  1640. bLower = true;
  1641. else
  1642. {
  1643. if (bLower || //last char was uppercase or the following character is lowercase?
  1644. ((processName+1 < end) && islower(*(processName+1))))
  1645. {
  1646. displayName.append(' ');
  1647. }
  1648. bLower = false;
  1649. }
  1650. displayName.append(*processName);
  1651. processName++;
  1652. }
  1653. displayName.append('\0');
  1654. return;
  1655. }
  1656. bool Cws_machineEx::excludePartition(const char* partition) const
  1657. {
  1658. //first see if this partition is meant to be excluded as is - for instance
  1659. //if partition is /dev and /dev is one of the predefined partitions to be excluded
  1660. set<string>::const_iterator it = m_excludePartitions.find( partition );
  1661. set<string>::const_iterator itEnd = m_excludePartitions.end();
  1662. bool found = false;
  1663. if (it != itEnd)
  1664. found = true;
  1665. else
  1666. {
  1667. //now check if /dev* is one of the partitions to be excluded
  1668. set<string>::const_iterator itBegin = m_excludePartitionPatterns.begin();
  1669. itEnd = m_excludePartitionPatterns.end();
  1670. unsigned int partitionLen = strlen(partition);
  1671. for (it=itBegin; it != itEnd; it++)
  1672. {
  1673. const string& pattern = *it;
  1674. if ((found = ::WildMatch(partition, partitionLen, pattern.c_str(), pattern.length(), false)))
  1675. break;
  1676. }
  1677. }
  1678. return found;
  1679. }
  1680. void Cws_machineEx::appendProcessInstance(const char* name, const char* directory1, const char* directory2, StringArray& processInstances, StringArray& directories)
  1681. {
  1682. if (!name || !*name)
  1683. return;
  1684. processInstances.append(name);
  1685. if (directory1 && *directory1)
  1686. directories.append(directory1);
  1687. else if (directory2 && *directory2)
  1688. directories.append(directory2);
  1689. else
  1690. directories.append("Setting not found");
  1691. }
  1692. //////////////////////////////////////////////////////////////////
  1693. // Set Machine Infomation for response //
  1694. //////////////////////////////////////////////////////////////////
  1695. void Cws_machineEx::setMachineInfoResponse(IEspContext& context, IEspGetMachineInfoRequest& req,
  1696. CGetMachineInfoData& machineInfoData, IEspGetMachineInfoResponse& resp)
  1697. {
  1698. IEspRequestInfoStruct& reqInfo = resp.updateRequestInfo();
  1699. #if 0
  1700. StringBuffer user;
  1701. StringBuffer pw;
  1702. context.getUserID(user);
  1703. context.getPassword(pw);
  1704. reqInfo.setUserName(user.str());
  1705. reqInfo.setPassword(pw.str());
  1706. #endif
  1707. reqInfo.setSecurityString(req.getSecurityString());
  1708. reqInfo.setGetProcessorInfo(req.getGetProcessorInfo());
  1709. reqInfo.setGetStorageInfo(req.getGetStorageInfo());
  1710. double version = context.getClientVersion();
  1711. if (version > 1.10)
  1712. reqInfo.setLocalFileSystemsOnly(req.getLocalFileSystemsOnly());
  1713. reqInfo.setGetSoftwareInfo(req.getGetSoftwareInfo());
  1714. reqInfo.setAutoRefresh( req.getAutoRefresh() );
  1715. reqInfo.setMemThreshold(req.getMemThreshold());
  1716. reqInfo.setDiskThreshold(req.getDiskThreshold());
  1717. reqInfo.setCpuThreshold(req.getCpuThreshold());
  1718. reqInfo.setMemThresholdType(req.getMemThresholdType());
  1719. reqInfo.setDiskThresholdType(req.getDiskThresholdType());
  1720. reqInfo.setApplyProcessFilter( req.getApplyProcessFilter() );
  1721. reqInfo.setClusterType( req.getClusterType() );
  1722. reqInfo.setCluster( req.getCluster() );
  1723. reqInfo.setAddProcessesToFilter( req.getAddProcessesToFilter() );
  1724. reqInfo.setOldIP( req.getOldIP() );
  1725. reqInfo.setPath( req.getPath() );
  1726. reqInfo.setSortBy("Address");
  1727. if (machineInfoData.getMachineInfoColumns().ordinality())
  1728. resp.setColumns(machineInfoData.getMachineInfoColumns());
  1729. if (machineInfoData.getMachineInfoTable().ordinality())
  1730. resp.setMachines(machineInfoData.getMachineInfoTable());
  1731. char timeStamp[32];
  1732. getTimeStamp(timeStamp);
  1733. resp.setTimeStamp( timeStamp );
  1734. if (version >= 1.12)
  1735. {
  1736. StringBuffer acceptLanguage;
  1737. resp.setAcceptLanguage(getAcceptLanguage(context, acceptLanguage).str());
  1738. }
  1739. }
  1740. void Cws_machineEx::getTimeStamp(char* timeStamp)
  1741. {
  1742. //set time stamp in the result for this machine
  1743. time_t tNow;
  1744. time(&tNow);
  1745. #ifdef _WIN32
  1746. struct tm *ltNow;
  1747. ltNow = localtime(&tNow);
  1748. strftime(timeStamp, 32, "%m/%d/%y %H:%M:%S", ltNow);
  1749. #else
  1750. struct tm ltNow;
  1751. localtime_r(&tNow, &ltNow);
  1752. strftime(timeStamp, 32, "%m/%d/%y %H:%M:%S", &ltNow);
  1753. #endif
  1754. }
  1755. void Cws_machineEx::setTargetClusterInfoResponse(IEspContext& context, IEspGetTargetClusterInfoRequest& req,
  1756. CGetMachineInfoData& machineInfoData, IPropertyTree* targetClusterTree, IEspGetTargetClusterInfoResponse& resp)
  1757. {
  1758. IEspRequestInfoStruct& reqInfo = resp.updateRequestInfo();
  1759. #if 0
  1760. StringBuffer user;
  1761. StringBuffer pw;
  1762. context.getUserID(user);
  1763. context.getPassword(pw);
  1764. reqInfo.setUserName(user.str());
  1765. reqInfo.setPassword(pw.str());
  1766. #endif
  1767. reqInfo.setGetProcessorInfo(req.getGetProcessorInfo());
  1768. reqInfo.setGetStorageInfo(req.getGetStorageInfo());
  1769. double version = context.getClientVersion();
  1770. if (version > 1.10)
  1771. reqInfo.setLocalFileSystemsOnly(req.getLocalFileSystemsOnly());
  1772. reqInfo.setGetSoftwareInfo(req.getGetSoftwareInfo());
  1773. reqInfo.setAutoRefresh( req.getAutoRefresh() );
  1774. reqInfo.setMemThreshold(req.getMemThreshold());
  1775. reqInfo.setDiskThreshold(req.getDiskThreshold());
  1776. reqInfo.setCpuThreshold(req.getCpuThreshold());
  1777. reqInfo.setMemThresholdType(req.getMemThresholdType());
  1778. reqInfo.setDiskThresholdType(req.getDiskThresholdType());
  1779. reqInfo.setApplyProcessFilter( req.getApplyProcessFilter() );
  1780. reqInfo.setAddProcessesToFilter( req.getAddProcessesToFilter() );
  1781. reqInfo.setSortBy("Address");
  1782. if (machineInfoData.getMachineInfoColumns().ordinality())
  1783. resp.setColumns(machineInfoData.getMachineInfoColumns());
  1784. if (machineInfoData.getMachineInfoTable().ordinality())
  1785. {
  1786. IArrayOf<IEspTargetClusterInfo> targetClusterInfoList;
  1787. setTargetClusterInfo(targetClusterTree, machineInfoData.getMachineInfoTable(), targetClusterInfoList);
  1788. if (targetClusterInfoList.ordinality())
  1789. resp.setTargetClusterInfoList(targetClusterInfoList);
  1790. }
  1791. char timeStamp[32];
  1792. getTimeStamp(timeStamp);
  1793. resp.setTimeStamp( timeStamp );
  1794. if (version >= 1.12)
  1795. {
  1796. StringBuffer acceptLanguage;
  1797. resp.setAcceptLanguage(getAcceptLanguage(context, acceptLanguage).str());
  1798. }
  1799. }
  1800. void Cws_machineEx::setTargetClusterInfo(IPropertyTree* pTargetClusterTree, IArrayOf<IEspMachineInfoEx>& machineArray, IArrayOf<IEspTargetClusterInfo>& targetClusterInfoList)
  1801. {
  1802. if (!pTargetClusterTree)
  1803. return;
  1804. unsigned machineCount = machineArray.ordinality();
  1805. if (machineCount < 1)
  1806. return;
  1807. Owned<IPropertyTreeIterator> targetClusters = pTargetClusterTree->getElements("TargetCluster");
  1808. ForEach(*targetClusters)
  1809. {
  1810. IPropertyTree& targetCluster = targetClusters->query();
  1811. StringBuffer targetName, targetType;
  1812. targetCluster.getProp("@Name", targetName);
  1813. targetCluster.getProp("@Type", targetType);
  1814. Owned<IEspTargetClusterInfo> targetClusterInfo = static_cast<IEspTargetClusterInfo*>(new CTargetClusterInfo(""));
  1815. targetClusterInfo->setName( targetName.str() );
  1816. targetClusterInfo->setType( targetType.str() );
  1817. IArrayOf<IEspMachineInfoEx> machineArrayNew;
  1818. Owned<IPropertyTreeIterator> processes = targetCluster.getElements("Process");
  1819. ForEach(*processes)
  1820. {
  1821. IPropertyTree& process = processes->query();
  1822. StringBuffer processName, processType;
  1823. process.getProp("@Name", processName);
  1824. process.getProp("@Type", processType);
  1825. for (unsigned i = 0; i < machineCount; i++)
  1826. {
  1827. IEspMachineInfoEx& machineInfoEx = machineArray.item(i);
  1828. const char* name = machineInfoEx.getComponentName();
  1829. const char* type = machineInfoEx.getProcessType();
  1830. if (!name || !type || !strieq(name, processName.str()) || !strieq(getProcessTypeFromMachineType(type), processType.str()))
  1831. continue;
  1832. Owned<IEspMachineInfoEx> pMachineInfo = static_cast<IEspMachineInfoEx*>(new CMachineInfoEx(""));
  1833. pMachineInfo->copy(machineInfoEx);
  1834. machineArrayNew.append(*pMachineInfo.getLink());
  1835. //Cannot break here because more than one processes match (ex. EclAgent/AgentExec)
  1836. }
  1837. }
  1838. if (machineArrayNew.ordinality())
  1839. targetClusterInfo->setProcesses(machineArrayNew);
  1840. targetClusterInfoList.append(*targetClusterInfo.getLink());
  1841. }
  1842. }
  1843. const char* Cws_machineEx::getProcessTypeFromMachineType(const char* machineType)
  1844. {
  1845. const char* processType ="Unknown";
  1846. if (!machineType || !*machineType)
  1847. return processType;
  1848. if (strieq(machineType, eqThorMasterProcess) || strieq(machineType, eqThorSlaveProcess) || strieq(machineType, eqThorSpareProcess))
  1849. {
  1850. processType = eqThorCluster;
  1851. }
  1852. else if (strieq(machineType, eqRoxieServerProcess))
  1853. {
  1854. processType = eqRoxieCluster;
  1855. }
  1856. else if (strieq(machineType, eqAgentExec))
  1857. {
  1858. processType = eqEclAgent;
  1859. }
  1860. else
  1861. {
  1862. processType = machineType;
  1863. }
  1864. return processType;
  1865. }
  1866. IConstEnvironment* Cws_machineEx::getConstEnvironment()
  1867. {
  1868. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  1869. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  1870. if (!constEnv)
  1871. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get environment information.");
  1872. return constEnv.getLink();
  1873. }
  1874. //Used in Rexec
  1875. IPropertyTree* Cws_machineEx::getComponent(const char* compType, const char* compName)
  1876. {
  1877. StringBuffer xpath;
  1878. xpath.append("Software/").append(compType).append("[@name='").append(compName).append("']");
  1879. Owned<IConstEnvironment> constEnv = getConstEnvironment();
  1880. Owned<IPropertyTree> pEnvRoot = &constEnv->getPTree();
  1881. return pEnvRoot->getPropTree( xpath.str() );
  1882. }
  1883. void Cws_machineEx::getAccountAndPlatformInfo(const char* address, StringBuffer& userId, StringBuffer& password, bool& bLinux)
  1884. {
  1885. Owned<IConstEnvironment> constEnv = getConstEnvironment();
  1886. Owned<IConstMachineInfo> machine = constEnv->getMachineByAddress(address);
  1887. if (!machine && strieq(address, "."))
  1888. {
  1889. machine.setown(constEnv->getMachineByAddress("127.0.0.1"));
  1890. }
  1891. if (!machine)
  1892. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Machine %s is not defined in environment!", address);
  1893. Owned<IConstDomainInfo> domain = machine->getDomain();
  1894. if (!domain)
  1895. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Machine %s does not have any domain information!", address);
  1896. userId.clear();
  1897. password.clear();
  1898. StringBufferAdaptor strval1(userId);
  1899. StringBufferAdaptor strval2(password);
  1900. domain->getAccountInfo(strval1, strval2);
  1901. StringBuffer domainName;
  1902. StringBufferAdaptor strval3(domainName);
  1903. domain->getName(strval3);
  1904. if ((machine->getOS() == MachineOsW2K) && domainName.length())
  1905. {
  1906. domainName.append('\\');
  1907. userId.insert(0, domainName);
  1908. }
  1909. bLinux = machine->getOS() == MachineOsLinux;
  1910. }
  1911. IPropertyTree* Cws_machineEx::createDiskUsageReq(IPropertyTree* envDirectories, const char* pathName,
  1912. const char* componentType, const char* componentName)
  1913. {
  1914. StringBuffer path;
  1915. if (!getConfigurationDirectory(envDirectories, pathName, componentType, componentName, path))
  1916. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get %s Disk Path for component %s", pathName, componentName);
  1917. Owned<IPropertyTree> diskReq = createPTree("Folder");
  1918. diskReq->addProp("@name", pathName);
  1919. diskReq->addProp("@path", path);
  1920. return diskReq.getClear();
  1921. }
  1922. IPropertyTree* Cws_machineEx::createMachineUsageReq(IConstEnvironment* constEnv, const char* computer)
  1923. {
  1924. Owned<IConstMachineInfo> machine = constEnv->getMachine(computer);
  1925. if (!machine)
  1926. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get machine %s", computer);
  1927. Owned<IPropertyTree> machineReq = createPTree("Machine");
  1928. machineReq->addProp("@name", computer);
  1929. SCMStringBuffer netAddress;
  1930. machine->getNetAddress(netAddress);
  1931. machineReq->addProp("@netAddress", netAddress.str());
  1932. machineReq->addPropInt("@OS", machine->getOS());
  1933. return machineReq.getClear();
  1934. }
  1935. void Cws_machineEx::readThorUsageReq(const char* name, IConstEnvironment* constEnv, IPropertyTree* usageReq)
  1936. {
  1937. if (isEmptyString(name))
  1938. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Empty Thor name");
  1939. Owned<IPropertyTree> componentReq = createPTree("Component");
  1940. componentReq->addProp("@name", name);
  1941. componentReq->addProp("@type", eqThorCluster);
  1942. Owned<IPropertyTree> envRoot = &constEnv->getPTree();
  1943. IPropertyTree* envDirectories = envRoot->queryPropTree("Software/Directories");
  1944. Owned<IPropertyTree> logFolder = createDiskUsageReq(envDirectories, "log", "thor", name);
  1945. Owned<IPropertyTree> dataFolder = createDiskUsageReq(envDirectories, "data", "thor", name);
  1946. Owned<IPropertyTree> repFolder = createDiskUsageReq(envDirectories, "mirror", "thor", name);
  1947. VStringBuffer xpath("Software/ThorCluster[@name='%s']/ThorSlaveProcess", name);
  1948. Owned<IPropertyTreeIterator> slaveProcesses= envRoot->getElements(xpath);
  1949. ForEach(*slaveProcesses)
  1950. {
  1951. IPropertyTree& slaveProcess = slaveProcesses->query();
  1952. const char* computer = slaveProcess.queryProp("@computer");
  1953. if (isEmptyString(computer))
  1954. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get @computer for %s", xpath.str());
  1955. Owned<IPropertyTree> newMachineReq = createMachineUsageReq(constEnv, computer);
  1956. //Not sure we need those folders here. Add them just in case.
  1957. if (logFolder)
  1958. newMachineReq->addPropTree(logFolder->queryName(), LINK(logFolder));
  1959. if (dataFolder)
  1960. newMachineReq->addPropTree(dataFolder->queryName(), LINK(dataFolder));
  1961. if (repFolder)
  1962. newMachineReq->addPropTree(repFolder->queryName(), LINK(repFolder));
  1963. componentReq->addPropTree(newMachineReq->queryName(), LINK(newMachineReq));
  1964. }
  1965. //Read ThorMasterProcess in case it is on a different machine
  1966. xpath.setf("Software/ThorCluster[@name='%s']/ThorMasterProcess/@computer", name);
  1967. const char* computer = envRoot->queryProp(xpath);
  1968. if (isEmptyString(computer))
  1969. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get %s", xpath.str());
  1970. Owned<IPropertyTree> machineReq = createMachineUsageReq(constEnv, computer);
  1971. xpath.setf("Machine[@netAddress='%s']", machineReq->queryProp("@netAddress"));
  1972. if (componentReq->queryPropTree(xpath))
  1973. { //ThorMasterProcess is running on one of the ThorSlaveProcess machines.
  1974. //So, we do not add this machine again.
  1975. usageReq->addPropTree(componentReq->queryName(), LINK(componentReq));
  1976. return;
  1977. }
  1978. //Not sure we need those folders here. Add them just in case.
  1979. if (logFolder)
  1980. machineReq->addPropTree(logFolder->queryName(), LINK(logFolder));
  1981. if (dataFolder)
  1982. machineReq->addPropTree(dataFolder->queryName(), LINK(dataFolder));
  1983. if (repFolder)
  1984. machineReq->addPropTree(repFolder->queryName(), LINK(repFolder));
  1985. componentReq->addPropTree(machineReq->queryName(), LINK(machineReq));
  1986. usageReq->addPropTree(componentReq->queryName(), LINK(componentReq));
  1987. }
  1988. void Cws_machineEx::readRoxieUsageReq(const char* name, IConstEnvironment* constEnv, IPropertyTree* usageReq)
  1989. {
  1990. if (isEmptyString(name))
  1991. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Empty Roxie name");
  1992. Owned<IPropertyTree> componentReq = createPTree("Component");
  1993. componentReq->addProp("@name", name);
  1994. componentReq->addProp("@type", eqRoxieCluster);
  1995. Owned<IPropertyTree> envRoot = &constEnv->getPTree();
  1996. IPropertyTree* envDirectories = envRoot->queryPropTree("Software/Directories");
  1997. Owned<IPropertyTree> logFolder = createDiskUsageReq(envDirectories, "log", "roxie", name);
  1998. Owned<IPropertyTree> dataFolder = createDiskUsageReq(envDirectories, "data", "roxie", name);
  1999. VStringBuffer xpath("Software/RoxieCluster[@name='%s']/RoxieServerProcess", name);
  2000. Owned<IPropertyTreeIterator> slaveProcesses= envRoot->getElements(xpath);
  2001. ForEach(*slaveProcesses)
  2002. {
  2003. IPropertyTree& slaveProcess = slaveProcesses->query();
  2004. const char* computer = slaveProcess.queryProp("@computer");
  2005. if (isEmptyString(computer))
  2006. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get @computer for %s", xpath.str());
  2007. Owned<IPropertyTree> newMachineReq = createMachineUsageReq(constEnv, computer);
  2008. //Not sure we need those folders here. Add them just in case.
  2009. if (logFolder)
  2010. newMachineReq->addPropTree(logFolder->queryName(), LINK(logFolder));
  2011. if (dataFolder)
  2012. newMachineReq->addPropTree(dataFolder->queryName(), LINK(dataFolder));
  2013. componentReq->addPropTree(newMachineReq->queryName(), LINK(newMachineReq));
  2014. }
  2015. usageReq->addPropTree(componentReq->queryName(), LINK(componentReq));
  2016. }
  2017. void Cws_machineEx::readDropZoneUsageReq(const char* name, IConstEnvironment* constEnv, IPropertyTree* usageReq)
  2018. {
  2019. if (isEmptyString(name))
  2020. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Empty DropZone name");
  2021. Owned<IConstDropZoneInfo> envDropZone = constEnv->getDropZone(name);
  2022. if (!envDropZone || !envDropZone->isECLWatchVisible())
  2023. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Dropzone %s not found", name);
  2024. SCMStringBuffer directory;
  2025. envDropZone->getDirectory(directory);
  2026. if (directory.length() == 0)
  2027. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get directory for DropZone %s", name);
  2028. Owned<IPropertyTree> dataFolder = createPTree("Folder");
  2029. dataFolder->addProp("@name", "data");
  2030. dataFolder->addProp("@path", directory.str());
  2031. Owned<IPropertyTree> componentReq = createPTree("Component");
  2032. componentReq->addProp("@name", name);
  2033. componentReq->addProp("@type", eqDropZone);
  2034. SCMStringBuffer computerName;
  2035. envDropZone->getComputerName(computerName);
  2036. if (computerName.length() == 0)
  2037. {
  2038. OS_TYPE os = (getPathSepChar(directory.str()) == '/') ? OS_LINUX : OS_WINDOWS;
  2039. Owned<IConstDropZoneServerInfoIterator> servers = envDropZone->getServers();
  2040. ForEach(*servers)
  2041. {
  2042. IConstDropZoneServerInfo &server = servers->query();
  2043. StringBuffer serverNetAddress;
  2044. server.getServer(serverNetAddress.clear());
  2045. Owned<IPropertyTree> machineReq = createPTree("Machine");
  2046. machineReq->addProp("@name", serverNetAddress.str());
  2047. machineReq->addProp("@netAddress", serverNetAddress.str());
  2048. machineReq->addPropInt("@OS", os);
  2049. machineReq->addPropTree(dataFolder->queryName(), LINK(dataFolder));
  2050. componentReq->addPropTree(machineReq->queryName(), LINK(machineReq));
  2051. }
  2052. }
  2053. else
  2054. { //legacy dropzone settings
  2055. Owned<IPropertyTree> machineReq = createMachineUsageReq(constEnv, computerName.str());
  2056. machineReq->addPropTree(dataFolder->queryName(), LINK(dataFolder));
  2057. componentReq->addPropTree(machineReq->queryName(), LINK(machineReq));
  2058. }
  2059. usageReq->addPropTree(componentReq->queryName(), LINK(componentReq));
  2060. }
  2061. void Cws_machineEx::readOtherComponentUsageReq(const char* name, const char* type, IConstEnvironment* constEnv, IPropertyTree* usageReq)
  2062. {
  2063. if (isEmptyString(name))
  2064. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Empty Component name");
  2065. if (!strieq(type, eqDali) && !strieq(type, eqEclAgent) && !strieq(type, eqSashaServer))
  2066. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Component usage function is not supported for %s", type);
  2067. Owned<IPropertyTree> envRoot = &constEnv->getPTree();
  2068. VStringBuffer xpath("Software/%s[@name='%s']/Instance/@computer", type, name);
  2069. const char* computer = envRoot->queryProp(xpath);
  2070. if (isEmptyString(computer))
  2071. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get %s", xpath.str());
  2072. Owned<IPropertyTree> machineReq = createMachineUsageReq(constEnv, computer);
  2073. IPropertyTree* envDirectories = envRoot->queryPropTree("Software/Directories");
  2074. Owned<IPropertyTree> logFolder = createDiskUsageReq(envDirectories, "log", type, name);
  2075. if (logFolder)
  2076. machineReq->addPropTree(logFolder->queryName(), LINK(logFolder));
  2077. StringAttr componentType;
  2078. if (strieq(type, eqDali))
  2079. componentType.set("dali");
  2080. else if (strieq(type, eqEclAgent))
  2081. componentType.set("eclAgent");
  2082. else
  2083. componentType.set("sasha");
  2084. Owned<IPropertyTree> dataFolder = createDiskUsageReq(envDirectories, "data", componentType.get(), name);
  2085. if (dataFolder)
  2086. machineReq->addPropTree(dataFolder->queryName(), LINK(dataFolder));
  2087. if (strieq(type, eqDali))
  2088. {
  2089. Owned<IPropertyTree> repFolder = createDiskUsageReq(envDirectories, "mirror", "dali", name);
  2090. if (repFolder)
  2091. machineReq->addPropTree(repFolder->queryName(), LINK(repFolder));
  2092. }
  2093. Owned<IPropertyTree> componentReq = createPTree("Component");
  2094. componentReq->addProp("@name", name);
  2095. componentReq->addProp("@type", type);
  2096. componentReq->addPropTree(machineReq->queryName(), LINK(machineReq));
  2097. usageReq->addPropTree(componentReq->queryName(), LINK(componentReq));
  2098. }
  2099. void Cws_machineEx::setUniqueMachineUsageReq(IPropertyTree* usageReq, IPropertyTree* uniqueUsages)
  2100. {
  2101. Owned<IPropertyTreeIterator> components= usageReq->getElements("Component");
  2102. ForEach(*components)
  2103. {
  2104. IPropertyTree& component = components->query();
  2105. Owned<IPropertyTreeIterator> machines= component.getElements("Machine");
  2106. ForEach(*machines)
  2107. {
  2108. IPropertyTree& machine = machines->query();
  2109. const char* netAddress = machine.queryProp("@netAddress");
  2110. VStringBuffer xpath("Machine[@netAddress='%s']", netAddress);
  2111. IPropertyTree* uniqueMachineReqTree = uniqueUsages->queryPropTree(xpath);
  2112. if (!uniqueMachineReqTree)
  2113. {
  2114. uniqueUsages->addPropTree(machine.queryName(), LINK(&machine));
  2115. continue;
  2116. }
  2117. //Add unique disk folders from the usageReq.
  2118. Owned<IPropertyTreeIterator> folders = machine.getElements("Folder");
  2119. ForEach(*folders)
  2120. {
  2121. IPropertyTree& folder = folders->query();
  2122. const char* aDiskPath = folder.queryProp("@path");
  2123. xpath.setf("Folder[@path='%s']", aDiskPath);
  2124. IPropertyTree* uniqueFolderReqTree = uniqueMachineReqTree->queryPropTree(xpath);
  2125. if (!uniqueFolderReqTree)
  2126. {
  2127. Owned<IPropertyTree> folderReq = createPTree("Folder");
  2128. folderReq->addProp("@name", folder.queryProp("@name"));
  2129. folderReq->addProp("@path", aDiskPath);
  2130. uniqueMachineReqTree->addPropTree(folderReq->queryName(), LINK(folderReq));
  2131. }
  2132. }
  2133. }
  2134. }
  2135. }
  2136. IArrayOf<IConstComponent>& Cws_machineEx::listComponentsByType(IPropertyTree* envRoot,
  2137. const char* componentType, IArrayOf<IConstComponent>& componentList)
  2138. {
  2139. VStringBuffer xpath("Software/%s", componentType);
  2140. Owned<IPropertyTreeIterator> components= envRoot->getElements(xpath);
  2141. ForEach(*components)
  2142. {
  2143. Owned<IEspComponent> component = createComponent();
  2144. component->setName(components->query().queryProp("@name"));
  2145. component->setType(componentType);
  2146. componentList.append(*component.getClear());
  2147. }
  2148. return componentList;
  2149. }
  2150. IArrayOf<IConstComponent>& Cws_machineEx::listComponentsForCheckingUsage(IConstEnvironment* constEnv,
  2151. IArrayOf<IConstComponent>& componentList)
  2152. {
  2153. Owned<IPropertyTree> envRoot = &constEnv->getPTree();
  2154. listComponentsByType(envRoot, eqThorCluster, componentList);
  2155. listComponentsByType(envRoot, eqRoxieCluster, componentList);
  2156. listComponentsByType(envRoot, eqDali, componentList);
  2157. listComponentsByType(envRoot, eqEclAgent, componentList);
  2158. listComponentsByType(envRoot, eqSashaServer, componentList);
  2159. listComponentsByType(envRoot, eqDropZone, componentList);
  2160. return componentList;
  2161. }
  2162. void Cws_machineEx::readComponentUsageReq(IEspGetComponentUsageRequest& req, IConstEnvironment* constEnv, IPropertyTree* usageReq, IPropertyTree* uniqueUsages)
  2163. {
  2164. IArrayOf<IConstComponent>& componentList = req.getComponents();
  2165. if (!componentList.length())
  2166. listComponentsForCheckingUsage(constEnv, componentList);
  2167. ForEachItemIn(i, componentList)
  2168. {
  2169. IConstComponent& component = componentList.item(i);
  2170. const char* type = component.getType();
  2171. if (isEmptyString(type))
  2172. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Empty Component Type");
  2173. if (strieq(type, eqThorCluster))
  2174. readThorUsageReq(component.getName(), constEnv, usageReq);
  2175. else if (strieq(type, eqRoxieCluster))
  2176. readRoxieUsageReq(component.getName(), constEnv, usageReq);
  2177. else if (strieq(type, eqDropZone))
  2178. readDropZoneUsageReq(component.getName(), constEnv, usageReq);
  2179. else
  2180. readOtherComponentUsageReq(component.getName(), type, constEnv, usageReq);
  2181. }
  2182. //Add unique machines from the usageReq to uniqueUsages.
  2183. setUniqueMachineUsageReq(usageReq, uniqueUsages);
  2184. }
  2185. void Cws_machineEx::getMachineUsages(IEspContext& context, IPropertyTree* uniqueUsages)
  2186. {
  2187. UnsignedArray threadHandles;
  2188. Owned<IPropertyTreeIterator> requests= uniqueUsages->getElements("Machine");
  2189. ForEach(*requests)
  2190. {
  2191. Owned<CGetMachineUsageThreadParam> threadParam = new CGetMachineUsageThreadParam(this, context, &requests->query());
  2192. PooledThreadHandle handle = m_threadPool->start(threadParam.getClear());
  2193. threadHandles.append(handle);
  2194. }
  2195. ForEachItemIn(i, threadHandles)
  2196. m_threadPool->join(threadHandles.item(i));
  2197. }
  2198. bool Cws_machineEx::readDiskSpaceResponse(const char* response, __int64& free, __int64& used, int& percentAvail, StringBuffer& pathUsed)
  2199. {
  2200. if (isEmptyString(response))
  2201. return false;
  2202. StringArray data;
  2203. data.appendList(response, " ");
  2204. if (data.length() < 2)
  2205. return false;
  2206. used = atol(data.item(0));
  2207. free = atol(data.item(1));
  2208. __int64 total = free + used;
  2209. if (total > 0)
  2210. percentAvail = (int) ((free*100)/total);
  2211. //The given path (ex. /var/lib/HPCCSystems/hpcc-mirror/thor) in the usage request does not exist.
  2212. //The data.item(2) is the path (ex. /var/lib/HPCCSystems/hpcc-mirror/) the usage script is used
  2213. //to read the DiskSpace.
  2214. if (data.length() > 2)
  2215. pathUsed.set(data.item(2));
  2216. return true;
  2217. }
  2218. void Cws_machineEx::getMachineUsage(IEspContext& context, CGetMachineUsageThreadParam* param)
  2219. {
  2220. VStringBuffer command("/%s/sbin/usage -d=", environmentConfData.m_executionPath.str());
  2221. unsigned pathCount = 0;
  2222. Owned<IPropertyTreeIterator> diskPathList = param->request->getElements("Folder");
  2223. ForEach(*diskPathList)
  2224. {
  2225. IPropertyTree& t = diskPathList->query();
  2226. if (pathCount > 0)
  2227. command.append(",");
  2228. command.appendf("%s", t.queryProp("@path"));
  2229. pathCount++;
  2230. }
  2231. ESPLOG(LogMax, "command(%s)", command.str());
  2232. StringBuffer response;
  2233. int error = runCommand(context, param->request->queryProp("@netAddress"), nullptr,
  2234. (EnvMachineOS) param->request->getPropInt("@OS", MachineOsLinux), command.str(), nullptr, nullptr, response);
  2235. if (error != 0 || isEmptyString(response))
  2236. {
  2237. if (isEmptyString(response))
  2238. param->request->addProp("@error", "Failed in getting component usage.");
  2239. else
  2240. param->request->addProp("@error", response);
  2241. return;
  2242. }
  2243. ESPLOG(LogMax, "response(%s)", response.str());
  2244. ForEach(*diskPathList)
  2245. {
  2246. IPropertyTree& diskPathTree = diskPathList->query();
  2247. StringBuffer aDiskPathResp, pathUsed;
  2248. VStringBuffer diskPath("%s:", diskPathTree.queryProp("@path"));
  2249. readALineFromResult(response, diskPath, aDiskPathResp, true);
  2250. int percentAvail = 0;
  2251. __int64 diskSpaceAvailable = 0, diskSpaceUsed = 0;
  2252. if (!readDiskSpaceResponse(aDiskPathResp.str(), diskSpaceAvailable, diskSpaceUsed, percentAvail, pathUsed))
  2253. {
  2254. DBGLOG("Failed to read disc space on %s: %s", param->request->queryProp("@netAddress"), aDiskPathResp.str());
  2255. diskPathTree.addProp("@error", "Failed to read disc space.");
  2256. continue;
  2257. }
  2258. diskPathTree.addPropInt64("@used", diskSpaceUsed);
  2259. diskPathTree.addPropInt64("@available", diskSpaceAvailable);
  2260. diskPathTree.addPropInt("@percentAvail", percentAvail);
  2261. if (!pathUsed.isEmpty())
  2262. diskPathTree.addProp("@pathUsed", pathUsed);
  2263. }
  2264. }
  2265. void Cws_machineEx::readComponentUsageResult(IEspContext& context, IPropertyTree* usageReq,
  2266. IPropertyTree* uniqueUsages, IArrayOf<IEspComponentUsage>& componentUsages)
  2267. {
  2268. Owned<IPropertyTreeIterator> components= usageReq->getElements("Component");
  2269. ForEach(*components)
  2270. {
  2271. IPropertyTree& component = components->query();
  2272. Owned<IEspComponentUsage> componentUsage = createComponentUsage();
  2273. componentUsage->setName(component.queryProp("@name"));
  2274. componentUsage->setType(component.queryProp("@type"));
  2275. IArrayOf<IEspMachineUsage> machineUsages;
  2276. Owned<IPropertyTreeIterator> machines= component.getElements("Machine");
  2277. ForEach(*machines)
  2278. {
  2279. IPropertyTree& machine = machines->query();
  2280. const char* netAddress = machine.queryProp("@netAddress");
  2281. Owned<IEspMachineUsage> machineUsage = createMachineUsage();
  2282. machineUsage->setName(machine.queryProp("@name"));
  2283. machineUsage->setNetAddress(netAddress);
  2284. VStringBuffer xpath("Machine[@netAddress='%s']", netAddress);
  2285. IPropertyTree* uniqueMachineReqTree = uniqueUsages->queryPropTree(xpath);
  2286. if (!uniqueMachineReqTree)
  2287. {
  2288. machineUsage->setDescription("No data returns.");
  2289. machineUsages.append(*machineUsage.getClear());
  2290. continue;
  2291. }
  2292. const char* error = uniqueMachineReqTree->queryProp("@error");
  2293. if (!isEmptyString(error))
  2294. {
  2295. machineUsage->setDescription(error);
  2296. machineUsages.append(*machineUsage.getClear());
  2297. continue;
  2298. }
  2299. IArrayOf<IEspDiskUsage> diskUsages;
  2300. Owned<IPropertyTreeIterator> folders = machine.getElements("Folder");
  2301. ForEach(*folders)
  2302. {
  2303. IPropertyTree& folder = folders->query();
  2304. const char* aDiskPath = folder.queryProp("@path");
  2305. Owned<IEspDiskUsage> diskUsage = createDiskUsage();
  2306. diskUsage->setName(folder.queryProp("@name"));
  2307. diskUsage->setPath(aDiskPath);
  2308. xpath.setf("Folder[@path='%s']", aDiskPath);
  2309. IPropertyTree* folderTree = uniqueMachineReqTree->queryPropTree(xpath);
  2310. if (!folderTree)
  2311. diskUsage->setDescription("No data returns.");
  2312. else
  2313. {
  2314. const char* error = folderTree->queryProp("@error");
  2315. if (!isEmptyString(error))
  2316. diskUsage->setDescription(error);
  2317. else
  2318. {
  2319. diskUsage->setAvailable(folderTree->getPropInt64("@available"));
  2320. diskUsage->setInUse(folderTree->getPropInt64("@used"));
  2321. diskUsage->setPercentAvailable(folderTree->getPropInt("@percentAvail"));
  2322. const char* pathUsed = folderTree->queryProp("@pathUsed");
  2323. if (!isEmptyString(pathUsed))
  2324. {
  2325. VStringBuffer desc("%s not found. Read disk usgae from %s", aDiskPath, pathUsed);
  2326. diskUsage->setDescription(desc);
  2327. }
  2328. }
  2329. }
  2330. diskUsages.append(*diskUsage.getClear());
  2331. }
  2332. machineUsage->setDiskUsages(diskUsages);
  2333. machineUsages.append(*machineUsage.getClear());
  2334. }
  2335. componentUsage->setMachineUsages(machineUsages);
  2336. componentUsages.append(*componentUsage.getClear());
  2337. }
  2338. }
  2339. bool Cws_machineEx::onGetComponentUsage(IEspContext& context, IEspGetComponentUsageRequest& req,
  2340. IEspGetComponentUsageResponse& resp)
  2341. {
  2342. try
  2343. {
  2344. context.ensureFeatureAccess(FEATURE_URL, SecAccess_Read, ECLWATCH_MACHINE_INFO_ACCESS_DENIED, "Failed to Get Machine Information. Permission denied.");
  2345. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  2346. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  2347. Owned<IPropertyTree> usageReq = createPTree("Req");
  2348. Owned<IPropertyTree> uniqueUsages = createPTree("Usage");
  2349. readComponentUsageReq(req, constEnv, usageReq, uniqueUsages);
  2350. getMachineUsages(context, uniqueUsages);
  2351. IArrayOf<IEspComponentUsage> componentUsages;
  2352. readComponentUsageResult(context, usageReq, uniqueUsages, componentUsages);
  2353. resp.setComponentUsages(componentUsages);
  2354. }
  2355. catch(IException* e)
  2356. {
  2357. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2358. }
  2359. return true;
  2360. }
  2361. StringArray& Cws_machineEx::listTargetClusterNames(IConstEnvironment* constEnv, StringArray& targetClusters)
  2362. {
  2363. Owned<IStringIterator> targets = getTargetClusters(nullptr, nullptr);
  2364. ForEach(*targets)
  2365. {
  2366. SCMStringBuffer target;
  2367. targetClusters.append(targets->str(target).str());
  2368. }
  2369. return targetClusters;
  2370. }
  2371. void Cws_machineEx::readTargetClusterUsageReq(IEspGetTargetClusterUsageRequest& req, IConstEnvironment* constEnv,
  2372. IPropertyTree* usageReq, IPropertyTree* uniqueUsages)
  2373. {
  2374. StringArray& targetClusters = req.getTargetClusters();
  2375. if (targetClusters.empty())
  2376. listTargetClusterNames(constEnv, targetClusters);
  2377. Owned<IPropertyTree> envRoot = &constEnv->getPTree();
  2378. ForEachItemIn(i, targetClusters)
  2379. {
  2380. const char* targetClusterName = targetClusters.item(i);
  2381. if (isEmptyString(targetClusterName))
  2382. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Empty Target Cluster specified.");
  2383. Owned<IConstWUClusterInfo> targetClusterInfo = getTargetClusterInfo(targetClusterName);
  2384. if (!targetClusterInfo)
  2385. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Could not find information about target cluster %s ", targetClusterName);
  2386. Owned<IPropertyTree> targetClusterTree = createPTree("TargetCluster");
  2387. targetClusterTree->addProp("@name", targetClusterName);
  2388. const StringArray& thors = targetClusterInfo->getThorProcesses();
  2389. ForEachItemIn(i, thors)
  2390. {
  2391. const char* thor = thors.item(i);
  2392. if (!isEmptyString(thor))
  2393. readThorUsageReq(thor, constEnv, targetClusterTree);
  2394. }
  2395. SCMStringBuffer roxie;
  2396. targetClusterInfo->getRoxieProcess(roxie);
  2397. if (roxie.length())
  2398. readRoxieUsageReq(roxie.str(), constEnv, targetClusterTree);
  2399. SCMStringBuffer eclAgent;
  2400. targetClusterInfo->getAgentName(eclAgent);
  2401. if (eclAgent.length())
  2402. readOtherComponentUsageReq(eclAgent.str(), eqEclAgent, constEnv, targetClusterTree);
  2403. usageReq->addPropTree(targetClusterTree->queryName(), LINK(targetClusterTree));
  2404. }
  2405. Owned<IPropertyTreeIterator> targetClusterItr= usageReq->getElements("TargetCluster");
  2406. ForEach(*targetClusterItr)
  2407. {
  2408. IPropertyTree& targetCluster = targetClusterItr->query();
  2409. setUniqueMachineUsageReq(&targetCluster, uniqueUsages);
  2410. }
  2411. }
  2412. void Cws_machineEx::readTargetClusterUsageResult(IEspContext& context, IPropertyTree* usageReq,
  2413. IPropertyTree* uniqueUsages, IArrayOf<IEspTargetClusterUsage>& targetClusterUsages)
  2414. {
  2415. Owned<IPropertyTreeIterator> targetClusters= usageReq->getElements("TargetCluster");
  2416. ForEach(*targetClusters)
  2417. {
  2418. IPropertyTree& targetCluster = targetClusters->query();
  2419. Owned<IEspTargetClusterUsage> targetClusterUsage = createTargetClusterUsage();
  2420. targetClusterUsage->setName(targetCluster.queryProp("@name"));
  2421. IArrayOf<IEspComponentUsage> componentUsages;
  2422. readComponentUsageResult(context, &targetCluster, uniqueUsages, componentUsages);
  2423. targetClusterUsage->setComponentUsages(componentUsages);
  2424. targetClusterUsages.append(*targetClusterUsage.getClear());
  2425. }
  2426. }
  2427. bool Cws_machineEx::onGetTargetClusterUsage(IEspContext& context, IEspGetTargetClusterUsageRequest& req,
  2428. IEspGetTargetClusterUsageResponse& resp)
  2429. {
  2430. try
  2431. {
  2432. context.ensureFeatureAccess(FEATURE_URL, SecAccess_Read, ECLWATCH_MACHINE_INFO_ACCESS_DENIED, "Failed to Get Machine Information. Permission denied.");
  2433. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  2434. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  2435. Owned<IPropertyTree> usageReq = createPTree("Req");
  2436. Owned<IPropertyTree> uniqueUsages = createPTree("Usage");
  2437. readTargetClusterUsageReq(req, constEnv, usageReq, uniqueUsages);
  2438. getMachineUsages(context, uniqueUsages);
  2439. IArrayOf<IEspTargetClusterUsage> targetClusterUsages;
  2440. readTargetClusterUsageResult(context, usageReq, uniqueUsages, targetClusterUsages);
  2441. resp.setTargetClusterUsages(targetClusterUsages);
  2442. }
  2443. catch(IException* e)
  2444. {
  2445. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2446. }
  2447. return true;
  2448. }
  2449. bool Cws_machineEx::getEclAgentNameFromNodeGroupName(const char* nodeGroupName, StringBuffer& agentName)
  2450. {
  2451. //Node group name for an eclagent should be: 'hthor__' + ECLAgentName[ + '_' + a number]
  2452. if ((strlen(nodeGroupName) <= 7) || strnicmp(nodeGroupName, "hthor__", 7))
  2453. return false;
  2454. agentName.set(nodeGroupName + 7);
  2455. const char* ptr = strrchr(agentName.str(), '_');
  2456. if (isEmptyString(ptr) || isEmptyString(++ptr))
  2457. return true;
  2458. const char* ptrSuffix = ptr - 1;
  2459. do
  2460. {
  2461. if (!isdigit(ptr[0]))
  2462. return true;
  2463. ptr++;
  2464. } while(!isEmptyString(ptr));
  2465. agentName.setLength(agentName.length() - (ptr - ptrSuffix));
  2466. return true;
  2467. }
  2468. void Cws_machineEx::getThorClusterNamesByGroupName(IPropertyTree* envRoot, const char* group, StringArray& thorClusterNames)
  2469. {
  2470. Owned<IPropertyTreeIterator> thorClusters= envRoot->getElements("Software/ThorCluster");
  2471. ForEach(*thorClusters)
  2472. {
  2473. IPropertyTree& thorCluster = thorClusters->query();
  2474. const char *nodeGroupName = thorCluster.queryProp("@nodeGroup");
  2475. if (!isEmptyString(nodeGroupName) && !strieq(nodeGroupName, group))
  2476. continue;
  2477. if (!isEmptyString(nodeGroupName))
  2478. thorClusterNames.append(nodeGroupName);
  2479. else
  2480. {
  2481. const char *name = thorCluster.queryProp("@name");
  2482. if (strieq(name, group))
  2483. thorClusterNames.append(name);
  2484. }
  2485. }
  2486. }
  2487. StringArray& Cws_machineEx::listThorHThorNodeGroups(IConstEnvironment* constEnv, StringArray& nodeGroups)
  2488. {
  2489. BoolHash uniqueThorClusterGroupNames;
  2490. Owned<IPropertyTree> envRoot = &constEnv->getPTree();
  2491. Owned<IPropertyTreeIterator> it =envRoot->getElements("Software/ThorCluster");
  2492. ForEach(*it)
  2493. {
  2494. IPropertyTree& cluster = it->query();
  2495. StringBuffer thorClusterGroupName;
  2496. getClusterGroupName(cluster, thorClusterGroupName);
  2497. if (!thorClusterGroupName.length())
  2498. continue;
  2499. bool* found = uniqueThorClusterGroupNames.getValue(thorClusterGroupName);
  2500. if (found && *found)
  2501. continue;
  2502. nodeGroups.append(thorClusterGroupName);
  2503. uniqueThorClusterGroupNames.setValue(thorClusterGroupName, true);
  2504. }
  2505. it.setown(envRoot->getElements("Software/EclAgentProcess"));
  2506. ForEach(*it)
  2507. {
  2508. IPropertyTree &cluster = it->query();
  2509. const char* name = cluster.queryProp("@name");
  2510. if (!name||!*name)
  2511. continue;
  2512. unsigned ins = 0;
  2513. Owned<IPropertyTreeIterator> insts = cluster.getElements("Instance");
  2514. ForEach(*insts)
  2515. {
  2516. ins++;
  2517. StringBuffer gname("hthor__");
  2518. gname.append(name);
  2519. if (ins>1)
  2520. gname.append('_').append(ins);
  2521. nodeGroups.append(gname);
  2522. }
  2523. }
  2524. return nodeGroups;
  2525. }
  2526. void Cws_machineEx::readNodeGroupUsageReq(IEspGetNodeGroupUsageRequest& req, IConstEnvironment* constEnv,
  2527. IPropertyTree* usageReq, IPropertyTree* uniqueUsages)
  2528. {
  2529. StringArray& nodeGroups = req.getNodeGroups();
  2530. if (nodeGroups.empty())
  2531. listThorHThorNodeGroups(constEnv, nodeGroups);
  2532. if (nodeGroups.empty())
  2533. throw MakeStringException(ECLWATCH_INVALID_INPUT, "No node group found.");
  2534. Owned<IPropertyTree> envRoot = &constEnv->getPTree();
  2535. ForEachItemIn(i, nodeGroups)
  2536. {
  2537. const char* nodeGroupName = nodeGroups.item(i);
  2538. if (isEmptyString(nodeGroupName))
  2539. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Empty node group specified.");
  2540. Owned<IPropertyTree> nodeGroupTree = createPTree("NodeGroup");
  2541. nodeGroupTree->addProp("@name", nodeGroupName);
  2542. StringBuffer agentName;
  2543. if (getEclAgentNameFromNodeGroupName(nodeGroupName, agentName))
  2544. readOtherComponentUsageReq(agentName.str(), eqEclAgent, constEnv, nodeGroupTree);
  2545. else
  2546. {
  2547. StringArray thorNames;
  2548. getThorClusterNamesByGroupName(envRoot, nodeGroupName, thorNames);
  2549. if (thorNames.length() == 0)
  2550. throw MakeStringException(ECLWATCH_INVALID_INPUT, "No thor/hthor can be found for node group name %s.", nodeGroupName);
  2551. ForEachItemIn(ii, thorNames)
  2552. readThorUsageReq(thorNames.item(ii), constEnv, nodeGroupTree);
  2553. }
  2554. usageReq->addPropTree(nodeGroupTree->queryName(), LINK(nodeGroupTree));
  2555. }
  2556. Owned<IPropertyTreeIterator> nodeGroupItr= usageReq->getElements("NodeGroup");
  2557. ForEach(*nodeGroupItr)
  2558. {
  2559. IPropertyTree& nodeGroup = nodeGroupItr->query();
  2560. setUniqueMachineUsageReq(&nodeGroup, uniqueUsages);
  2561. }
  2562. }
  2563. void Cws_machineEx::readNodeGroupUsageResult(IEspContext& context, IPropertyTree* usageReq,
  2564. IPropertyTree* uniqueUsages, IArrayOf<IEspNodeGroupUsage>& nodeGroupUsages)
  2565. {
  2566. Owned<IPropertyTreeIterator> nodeGroups= usageReq->getElements("NodeGroup");
  2567. ForEach(*nodeGroups)
  2568. {
  2569. IPropertyTree& nodeGroup = nodeGroups->query();
  2570. Owned<IEspNodeGroupUsage> nodeGroupUsage = createNodeGroupUsage();
  2571. nodeGroupUsage->setName(nodeGroup.queryProp("@name"));
  2572. IArrayOf<IEspComponentUsage> componentUsages;
  2573. readComponentUsageResult(context, &nodeGroup, uniqueUsages, componentUsages);
  2574. nodeGroupUsage->setComponentUsages(componentUsages);
  2575. nodeGroupUsages.append(*nodeGroupUsage.getClear());
  2576. }
  2577. }
  2578. bool Cws_machineEx::onGetNodeGroupUsage(IEspContext& context, IEspGetNodeGroupUsageRequest& req,
  2579. IEspGetNodeGroupUsageResponse& resp)
  2580. {
  2581. try
  2582. {
  2583. context.ensureFeatureAccess(FEATURE_URL, SecAccess_Read, ECLWATCH_MACHINE_INFO_ACCESS_DENIED, "Failed to Get Machine Information. Permission denied.");
  2584. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  2585. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  2586. Owned<IPropertyTree> usageReq = createPTree("Req");
  2587. Owned<IPropertyTree> uniqueUsages = createPTree("Usage");
  2588. readNodeGroupUsageReq(req, constEnv, usageReq, uniqueUsages);
  2589. getMachineUsages(context, uniqueUsages);
  2590. IArrayOf<IEspNodeGroupUsage> nodeGroupUsages;
  2591. readNodeGroupUsageResult(context, usageReq, uniqueUsages, nodeGroupUsages);
  2592. resp.setNodeGroupUsages(nodeGroupUsages);
  2593. }
  2594. catch(IException* e)
  2595. {
  2596. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2597. }
  2598. return true;
  2599. }
  2600. bool Cws_machineEx::onGetComponentStatus(IEspContext &context, IEspGetComponentStatusRequest &req, IEspGetComponentStatusResponse &resp)
  2601. {
  2602. try
  2603. {
  2604. context.ensureFeatureAccess(FEATURE_URL, SecAccess_Read, ECLWATCH_MACHINE_INFO_ACCESS_DENIED, "Failed to Get Component Status. Permission denied.");
  2605. Owned<IComponentStatusFactory> factory = getComponentStatusFactory();
  2606. Owned<IESPComponentStatusInfo> status = factory->getComponentStatus();
  2607. if (!status) //Should never happen
  2608. return false;
  2609. int statusID = status->getComponentStatusID();
  2610. if (statusID < 0)
  2611. {
  2612. resp.setStatus("Not reported");
  2613. }
  2614. else
  2615. {
  2616. resp.setComponentType(status->getComponentType());
  2617. resp.setEndPoint(status->getEndPoint());
  2618. resp.setReporter(status->getReporter());
  2619. resp.setComponentStatus(status->getComponentStatus());
  2620. resp.setTimeReportedStr(status->getTimeReportedStr());
  2621. IConstStatusReport* componentStatus = status->getStatusReport();
  2622. if (componentStatus)
  2623. resp.setStatusReport(*componentStatus);
  2624. resp.setComponentStatusList(status->getComponentStatusList());
  2625. }
  2626. resp.setComponentStatusID(statusID);
  2627. resp.setStatusCode(0);
  2628. }
  2629. catch(IException* e)
  2630. {
  2631. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2632. }
  2633. return true;
  2634. }
  2635. bool Cws_machineEx::onUpdateComponentStatus(IEspContext &context, IEspUpdateComponentStatusRequest &req, IEspUpdateComponentStatusResponse &resp)
  2636. {
  2637. try
  2638. {
  2639. context.ensureFeatureAccess(FEATURE_URL, SecAccess_Write, ECLWATCH_MACHINE_INFO_ACCESS_DENIED, "Failed to Update Component Status. Permission denied.");
  2640. const char* reporter = req.getReporter();
  2641. if (!reporter || !*reporter)
  2642. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Report not specified.");
  2643. Owned<IComponentStatusFactory> factory = getComponentStatusFactory();
  2644. factory->updateComponentStatus(reporter, req.getComponentStatusList());
  2645. resp.setStatusCode(0);
  2646. }
  2647. catch(IException* e)
  2648. {
  2649. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2650. }
  2651. return true;
  2652. }