00001
00002
00003
00004
00005
00006
00007
00008 #include "procStat.h"
00009 #include "objMetaOpr.h"
00010 #include "resource.h"
00011 #include "miscServerFunct.h"
00012 #include "rodsLog.h"
00013 #include "rsGlobalExtern.h"
00014 #include "rcGlobalExtern.h"
00015
00016
00017
00018 #include "eirods_resource_backport.h"
00019
00020
00021 int
00022 rsProcStat (rsComm_t *rsComm, procStatInp_t *procStatInp,
00023 genQueryOut_t **procStatOut)
00024 {
00025 int status;
00026 int remoteFlag;
00027 rodsServerHost_t *rodsServerHost;
00028
00029 if (*procStatInp->rodsZone != '\0') {
00030 remoteFlag = getRcatHost (MASTER_RCAT, procStatInp->rodsZone,
00031 &rodsServerHost);
00032 if (remoteFlag < 0) {
00033 rodsLog (LOG_ERROR,
00034 "rsProcStat: getRcatHost() failed. erro=%d", remoteFlag);
00035 return (remoteFlag);
00036 }
00037 if (rodsServerHost->localFlag == REMOTE_HOST) {
00038 status = remoteProcStat (rsComm, procStatInp, procStatOut,
00039 rodsServerHost);
00040 } else {
00041 status = _rsProcStat (rsComm, procStatInp, procStatOut);
00042 }
00043 } else {
00044 status = _rsProcStat (rsComm, procStatInp, procStatOut);
00045 }
00046 return status;
00047 }
00048
00049 int
00050 _rsProcStat (rsComm_t *rsComm, procStatInp_t *procStatInp,
00051 genQueryOut_t **procStatOut)
00052 {
00053 int status = -1;
00054 rodsServerHost_t *rodsServerHost = NULL;
00055 int remoteFlag = -1;
00056 rodsHostAddr_t addr;
00057 procStatInp_t myProcStatInp;
00058 char *tmpStr = NULL;
00059
00060 if (getValByKey (&procStatInp->condInput, ALL_KW) != NULL) {
00061 status = _rsProcStatAll (rsComm, procStatInp, procStatOut);
00062 return status;
00063 }
00064 if (getValByKey (&procStatInp->condInput, EXEC_LOCALLY_KW) != NULL) {
00065 status = localProcStat (rsComm, procStatInp, procStatOut);
00066 return status;
00067 }
00068
00069 bzero (&addr, sizeof (addr));
00070 bzero (&myProcStatInp, sizeof (myProcStatInp));
00071 if (*procStatInp->addr != '\0') {
00072 rstrcpy (addr.hostAddr, procStatInp->addr, LONG_NAME_LEN);
00073 remoteFlag = resolveHost (&addr, &rodsServerHost);
00074 } else if ((tmpStr = getValByKey (&procStatInp->condInput, RESC_NAME_KW)) != NULL) {
00075 rescGrpInfo_t *rescGrpInfo = new rescGrpInfo_t;
00076
00077
00078 eirods::error err = eirods::get_resc_grp_info( tmpStr, *rescGrpInfo );
00079 if( !err.ok() ) {
00080 rodsLog (LOG_ERROR,
00081 "_rsProcStat: _getRescInfo of %s error. stat = %d",
00082 tmpStr, status);
00083 delete rescGrpInfo;
00084 return status;
00085 }
00086 rstrcpy (procStatInp->addr, rescGrpInfo->rescInfo->rescLoc, NAME_LEN);
00087 rodsServerHost = (rodsServerHost_t*)rescGrpInfo->rescInfo->rodsServerHost;
00088 if (rodsServerHost == NULL) {
00089 remoteFlag = SYS_INVALID_SERVER_HOST;
00090 } else {
00091 remoteFlag = rodsServerHost->localFlag;
00092 }
00093
00094 delete rescGrpInfo;
00095 } else {
00096
00097 remoteFlag = getRcatHost (MASTER_RCAT, NULL, &rodsServerHost);
00098 }
00099 if (remoteFlag < 0) {
00100 rodsLog (LOG_ERROR,
00101 "_rsProcStat: getRcatHost() failed. erro=%d", remoteFlag);
00102 return (remoteFlag);
00103 } else if (remoteFlag == REMOTE_HOST) {
00104 addKeyVal (&myProcStatInp.condInput, EXEC_LOCALLY_KW, "");
00105 status = remoteProcStat (rsComm, &myProcStatInp, procStatOut,
00106 rodsServerHost);
00107 rmKeyVal (&myProcStatInp.condInput, EXEC_LOCALLY_KW);
00108 } else {
00109 status = localProcStat (rsComm, procStatInp, procStatOut);
00110 }
00111 return status;
00112 }
00113
00114 int
00115 _rsProcStatAll (rsComm_t *rsComm, procStatInp_t *procStatInp,
00116 genQueryOut_t **procStatOut)
00117 {
00118 rodsServerHost_t *tmpRodsServerHost;
00119 procStatInp_t myProcStatInp;
00120 int status;
00121 genQueryOut_t *singleProcStatOut = NULL;
00122 int savedStatus = 0;
00123
00124 bzero (&myProcStatInp, sizeof (myProcStatInp));
00125 tmpRodsServerHost = ServerHostHead;
00126 while (tmpRodsServerHost != NULL) {
00127
00128 eirods::error err = eirods::get_host_status_by_host_info( tmpRodsServerHost );
00129 if( err.ok() && err.code() == INT_RESC_STATUS_UP ) {
00130 if (tmpRodsServerHost->localFlag == LOCAL_HOST) {
00131 setLocalSrvAddr (myProcStatInp.addr);
00132 status = localProcStat (rsComm, &myProcStatInp, &singleProcStatOut);
00133 } else {
00134 rstrcpy (myProcStatInp.addr, tmpRodsServerHost->hostName->name, NAME_LEN);
00135 addKeyVal (&myProcStatInp.condInput, EXEC_LOCALLY_KW, "");
00136 status = remoteProcStat (rsComm, &myProcStatInp, &singleProcStatOut, tmpRodsServerHost);
00137 rmKeyVal (&myProcStatInp.condInput, EXEC_LOCALLY_KW);
00138 }
00139
00140 if (status < 0) {
00141 savedStatus = status;
00142 }
00143
00144 if (singleProcStatOut != NULL) {
00145 if (*procStatOut == NULL) {
00146 *procStatOut = singleProcStatOut;
00147 } else {
00148 catGenQueryOut (*procStatOut, singleProcStatOut, MAX_PROC_STAT_CNT);
00149 freeGenQueryOut (&singleProcStatOut);
00150 }
00151 singleProcStatOut = NULL;
00152 }
00153
00154 }
00155
00156 tmpRodsServerHost = tmpRodsServerHost->next;
00157 }
00158 return savedStatus;
00159 }
00160
00161 int
00162 localProcStat (rsComm_t *rsComm, procStatInp_t *procStatInp,
00163 genQueryOut_t **procStatOut)
00164 {
00165 int numProc, status;
00166 procLog_t procLog;
00167 char childPath[MAX_NAME_LEN];
00168 #ifndef USE_BOOST_FS
00169 DIR *dirPtr;
00170 struct dirent *myDirent;
00171 #ifndef windows_platform
00172 struct stat statbuf;
00173 #else
00174 struct irodsntstat statbuf;
00175 #endif
00176 #endif
00177 int count = 0;
00178
00179 numProc = getNumFilesInDir (ProcLogDir) + 2;
00180
00181 bzero (&procLog, sizeof (procLog));
00182
00183 if (*procStatInp->addr != '\0') {
00184 rstrcpy (procLog.serverAddr, procStatInp->addr, NAME_LEN);
00185 } else {
00186 setLocalSrvAddr (procLog.serverAddr);
00187 }
00188 if (numProc <= 0) {
00189
00190 initProcStatOut (procStatOut, 1);
00191 addProcToProcStatOut (&procLog, *procStatOut);
00192 return numProc;
00193 } else {
00194 initProcStatOut (procStatOut, numProc);
00195 }
00196
00197
00198 #ifdef USE_BOOST_FS
00199 path srcDirPath (ProcLogDir);
00200 if (!exists(srcDirPath) || !is_directory(srcDirPath)) {
00201 #else
00202 dirPtr = opendir (ProcLogDir);
00203 if (dirPtr == NULL) {
00204 #endif
00205 status = USER_INPUT_PATH_ERR - errno;
00206 rodsLogError (LOG_ERROR, status,
00207 "localProcStat: opendir local dir error for %s", ProcLogDir);
00208 return status;
00209 }
00210 #ifdef USE_BOOST_FS
00211 directory_iterator end_itr;
00212 for (directory_iterator itr(srcDirPath); itr != end_itr;++itr) {
00213 path p = itr->path();
00214 path cp = p.filename();
00215 if (!isdigit (*cp.c_str())) continue;
00216 snprintf (childPath, MAX_NAME_LEN, "%s",
00217 p.c_str ());
00218 #else
00219 while ((myDirent = readdir (dirPtr)) != NULL) {
00220 if (strcmp (myDirent->d_name, ".") == 0 ||
00221 strcmp (myDirent->d_name, "..") == 0) {
00222 continue;
00223 }
00224 if (!isdigit (*myDirent->d_name)) continue;
00225 snprintf (childPath, MAX_NAME_LEN, "%s/%s", ProcLogDir,
00226 myDirent->d_name);
00227 #endif
00228 #ifdef USE_BOOST_FS
00229 if (!exists (p)) {
00230 #else
00231 #ifndef windows_platform
00232 status = stat (childPath, &statbuf);
00233 #else
00234 status = iRODSNt_stat(childPath, &statbuf);
00235 #endif
00236 if (status != 0) {
00237 #endif
00238 rodsLogError (LOG_ERROR, status,
00239 "localProcStat: stat error for %s", childPath);
00240 continue;
00241 }
00242 #ifdef USE_BOOST_FS
00243 if (is_regular_file(p)) {
00244 #else
00245 if (statbuf.st_mode & S_IFREG) {
00246 #endif
00247 if (count >= numProc) {
00248 rodsLog (LOG_ERROR,
00249 "localProcStat: proc count %d exceeded", numProc);
00250 break;
00251 }
00252 #ifdef USE_BOOST_FS
00253 procLog.pid = atoi (cp.c_str());
00254 #else
00255 procLog.pid = atoi (myDirent->d_name);
00256 #endif
00257 if (readProcLog (procLog.pid, &procLog) < 0) continue;
00258 status = addProcToProcStatOut (&procLog, *procStatOut);
00259 if (status < 0) continue;
00260 count++;
00261 } else {
00262 continue;
00263 }
00264 }
00265 #ifndef USE_BOOST_FS
00266 closedir (dirPtr);
00267 #endif
00268 return 0;
00269 }
00270
00271 int
00272 remoteProcStat (rsComm_t *rsComm, procStatInp_t *procStatInp,
00273 genQueryOut_t **procStatOut, rodsServerHost_t *rodsServerHost)
00274 {
00275 int status;
00276 procLog_t procLog;
00277
00278 if (rodsServerHost == NULL) {
00279 rodsLog (LOG_ERROR,
00280 "remoteProcStat: Invalid rodsServerHost");
00281 return SYS_INVALID_SERVER_HOST;
00282 }
00283
00284 if (procStatInp == NULL || procStatOut == NULL) return USER__NULL_INPUT_ERR;
00285
00286 status = svrToSvrConnect (rsComm, rodsServerHost);
00287
00288 if (status >= 0) {
00289 status = rcProcStat (rodsServerHost->conn, procStatInp, procStatOut);
00290 }
00291 if (status < 0 && *procStatOut == NULL) {
00292
00293 initProcStatOut (procStatOut, 1);
00294 bzero (&procLog, sizeof (procLog));
00295 rstrcpy (procLog.serverAddr, rodsServerHost->hostName->name,
00296 NAME_LEN);
00297 addProcToProcStatOut (&procLog, *procStatOut);
00298 }
00299 return status;
00300 }
00301
00302 int
00303 initProcStatOut (genQueryOut_t **procStatOut, int numProc)
00304 {
00305 genQueryOut_t *myProcStatOut;
00306
00307 if (procStatOut == NULL || numProc <= 0) return USER__NULL_INPUT_ERR;
00308
00309 myProcStatOut = *procStatOut = (genQueryOut_t*)malloc (sizeof (genQueryOut_t));
00310 bzero (myProcStatOut, sizeof (genQueryOut_t));
00311
00312 myProcStatOut->continueInx = -1;
00313
00314 myProcStatOut->attriCnt = 9;
00315
00316 myProcStatOut->sqlResult[0].attriInx = PID_INX;
00317 myProcStatOut->sqlResult[0].len = NAME_LEN;
00318 myProcStatOut->sqlResult[0].value =
00319 (char*)malloc (NAME_LEN * numProc);
00320 bzero (myProcStatOut->sqlResult[0].value, NAME_LEN * numProc);
00321
00322 myProcStatOut->sqlResult[1].attriInx = STARTTIME_INX;
00323 myProcStatOut->sqlResult[1].len = NAME_LEN;
00324 myProcStatOut->sqlResult[1].value =
00325 (char*)malloc (NAME_LEN * numProc);
00326 bzero (myProcStatOut->sqlResult[1].value, NAME_LEN * numProc);
00327
00328 myProcStatOut->sqlResult[2].attriInx = CLIENT_NAME_INX;
00329 myProcStatOut->sqlResult[2].len = NAME_LEN;
00330 myProcStatOut->sqlResult[2].value =
00331 (char*)malloc (NAME_LEN * numProc);
00332 bzero (myProcStatOut->sqlResult[2].value, NAME_LEN * numProc);
00333
00334 myProcStatOut->sqlResult[3].attriInx = CLIENT_ZONE_INX;
00335 myProcStatOut->sqlResult[3].len = NAME_LEN;
00336 myProcStatOut->sqlResult[3].value =
00337 (char*)malloc (NAME_LEN * numProc);
00338 bzero (myProcStatOut->sqlResult[3].value, NAME_LEN * numProc);
00339
00340 myProcStatOut->sqlResult[4].attriInx = PROXY_NAME_INX;
00341 myProcStatOut->sqlResult[4].len = NAME_LEN;
00342 myProcStatOut->sqlResult[4].value =
00343 (char*)malloc (NAME_LEN * numProc);
00344 bzero (myProcStatOut->sqlResult[4].value, NAME_LEN * numProc);
00345
00346 myProcStatOut->sqlResult[5].attriInx = PROXY_ZONE_INX;
00347 myProcStatOut->sqlResult[5].len = NAME_LEN;
00348 myProcStatOut->sqlResult[5].value =
00349 (char*)malloc (NAME_LEN * numProc);
00350 bzero (myProcStatOut->sqlResult[5].value, NAME_LEN * numProc);
00351
00352 myProcStatOut->sqlResult[6].attriInx = REMOTE_ADDR_INX;
00353 myProcStatOut->sqlResult[6].len = NAME_LEN;
00354 myProcStatOut->sqlResult[6].value =
00355 (char*)malloc (NAME_LEN * numProc);
00356 bzero (myProcStatOut->sqlResult[6].value, NAME_LEN * numProc);
00357
00358 myProcStatOut->sqlResult[7].attriInx = SERVER_ADDR_INX;
00359 myProcStatOut->sqlResult[7].len = NAME_LEN;
00360 myProcStatOut->sqlResult[7].value =
00361 (char*)malloc (NAME_LEN * numProc);
00362 bzero (myProcStatOut->sqlResult[7].value, NAME_LEN * numProc);
00363
00364 myProcStatOut->sqlResult[8].attriInx = PROG_NAME_INX;
00365 myProcStatOut->sqlResult[8].len = NAME_LEN;
00366 myProcStatOut->sqlResult[8].value =
00367 (char*)malloc (NAME_LEN * numProc);
00368 bzero (myProcStatOut->sqlResult[8].value, NAME_LEN * numProc);
00369
00370
00371 return 0;
00372 }
00373
00374 int
00375 addProcToProcStatOut (procLog_t *procLog, genQueryOut_t *procStatOut)
00376 {
00377 int rowCnt;
00378
00379 if (procLog == NULL || procStatOut == NULL) return USER__NULL_INPUT_ERR;
00380 rowCnt = procStatOut->rowCnt;
00381
00382 snprintf (&procStatOut->sqlResult[0].value[NAME_LEN * rowCnt],
00383 NAME_LEN, "%d", procLog->pid);
00384 snprintf (&procStatOut->sqlResult[1].value[NAME_LEN * rowCnt],
00385 NAME_LEN, "%u", procLog->startTime);
00386 rstrcpy (&procStatOut->sqlResult[2].value[NAME_LEN * rowCnt],
00387 procLog->clientName, NAME_LEN);
00388 rstrcpy (&procStatOut->sqlResult[3].value[NAME_LEN * rowCnt],
00389 procLog->clientZone, NAME_LEN);
00390 rstrcpy (&procStatOut->sqlResult[4].value[NAME_LEN * rowCnt],
00391 procLog->proxyName, NAME_LEN);
00392 rstrcpy (&procStatOut->sqlResult[5].value[NAME_LEN * rowCnt],
00393 procLog->proxyZone, NAME_LEN);
00394 rstrcpy (&procStatOut->sqlResult[6].value[NAME_LEN * rowCnt],
00395 procLog->remoteAddr, NAME_LEN);
00396 rstrcpy (&procStatOut->sqlResult[7].value[NAME_LEN * rowCnt],
00397 procLog->serverAddr, NAME_LEN);
00398 rstrcpy (&procStatOut->sqlResult[8].value[NAME_LEN * rowCnt],
00399 procLog->progName, NAME_LEN);
00400
00401 procStatOut->rowCnt++;
00402
00403 return 0;
00404 }
00405