00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "reIn2p3SysRule.h"
00011 #include "genQuery.h"
00012 #ifndef windows_platform
00013 #include <sys/socket.h>
00014 #include <pthread.h>
00015 #include <netinet/in.h>
00016 #include <arpa/inet.h>
00017
00018 static pthread_mutex_t my_mutex;
00019 #endif
00020
00021 short threadIsAlive[MAX_NSERVERS];
00022
00023 int rodsMonPerfLog(char *serverName, char *resc, char *output, ruleExecInfo_t *rei) {
00024
00025 char condstr[MAX_NAME_LEN], fname[MAX_NAME_LEN], msg[MAX_MESSAGE_SIZE], splc[MAX_VALUE][MAX_NAME_LEN],
00026 splresc[MAX_VALUE][MAX_NAME_LEN], spldsk[MAX_VALUE][MAX_NAME_LEN], splmb[MAX_VALUE][MAX_NAME_LEN],
00027 monStatus[MAX_NAME_LEN], suffix[MAX_VALUE], *result;
00028 const char *delim1 = "#";
00029 const char *delim2 = ",";
00030 int indx, timestamp, day, rc1, rc2, rc3, rc4;
00031 FILE *foutput;
00032 time_t tps;
00033 generalRowInsertInp_t generalRowInsertInp;
00034 generalAdminInp_t generalAdminInp1, generalAdminInp2;
00035 genQueryInp_t genQueryInp;
00036 struct tm *now;
00037
00038 genQueryOut_t *genQueryOut = NULL;
00039 tps = time(NULL);
00040 now = localtime(&tps);
00041
00042
00043 if ( strcmp(output, MON_OUTPUT_NO_ANSWER) == 0 ) {
00044 strncpy(monStatus, RESC_AUTO_DOWN, MAX_NAME_LEN);
00045 }
00046 else {
00047 strncpy(monStatus, RESC_AUTO_UP, MAX_NAME_LEN);
00048 }
00049
00050 strSplit(output, delim1, splc);
00051 strSplit(resc, delim2, splresc);
00052 strSplit(splc[4], delim2, spldsk);
00053 strSplit(splc[7], delim2, splmb);
00054 indx = 0;
00055 while ( strcmp(splresc[indx],"") != 0 ) {
00056 if ( strcmp(monStatus, RESC_AUTO_DOWN) == 0 ) {
00057 rstrcpy(spldsk[indx], "-1", MAX_NAME_LEN);
00058 rstrcpy(splmb[indx], "-1", MAX_NAME_LEN);
00059 }
00060 sprintf(msg, "server=%s resource=%s cpu=%s, mem=%s, swp=%s, rql=%s, dsk=%s, nin=%s, nout=%s, dskAv(MB)=%s\n",
00061 serverName, splresc[indx], splc[0], splc[1], splc[2],
00062 splc[3], spldsk[indx], splc[5], splc[6], splmb[indx]);
00063 sprintf(suffix, "%d.%d.%d", now->tm_year+1900, now->tm_mon+1, now->tm_mday);
00064 sprintf(fname, "%s.%s", OUTPUT_MON_PERF, suffix);
00065 day = now->tm_mday;
00066
00067 timestamp = time(&tps);
00068
00069
00070 generalRowInsertInp.tableName = "serverload";
00071 generalRowInsertInp.arg1 = serverName;
00072 generalRowInsertInp.arg2 = splresc[indx];
00073 generalRowInsertInp.arg3 = splc[0];
00074 generalRowInsertInp.arg4 = splc[1];
00075 generalRowInsertInp.arg5 = splc[2];
00076 generalRowInsertInp.arg6 = splc[3];
00077 generalRowInsertInp.arg7 = spldsk[indx];
00078 generalRowInsertInp.arg8 = splc[5];
00079 generalRowInsertInp.arg9 = splc[6];
00080
00081 generalAdminInp1.arg0 = "modify";
00082 generalAdminInp1.arg1 = "resource";
00083 generalAdminInp1.arg2 = splresc[indx];
00084 generalAdminInp1.arg3 = "freespace";
00085 generalAdminInp1.arg4 = splmb[indx];
00086 generalAdminInp2.arg0 = "modify";
00087 generalAdminInp2.arg1 = "resource";
00088 generalAdminInp2.arg2 = splresc[indx];
00089 generalAdminInp2.arg3 = "status";
00090 generalAdminInp2.arg4 = monStatus;
00091 memset(&genQueryInp, 0, sizeof (genQueryInp));
00092 addInxIval(&genQueryInp.selectInp, COL_R_RESC_STATUS, 1);
00093 snprintf(condstr, MAX_NAME_LEN, "= '%s'", splresc[indx]);
00094 addInxVal(&genQueryInp.sqlCondInp, COL_R_RESC_NAME, condstr);
00095 genQueryInp.maxRows = MAX_SQL_ROWS;
00096 #ifndef windows_platform
00097 pthread_mutex_lock(&my_mutex);
00098 #endif
00099
00100 foutput = fopen(fname, "a");
00101 if (foutput != NULL) {
00102 fprintf(foutput, "time=%i %s", timestamp, msg);
00103 fclose(foutput);
00104 }
00105
00106 rc1 = rsGeneralRowInsert(rei->rsComm, &generalRowInsertInp);
00107 rc2 = rsGeneralAdmin(rei->rsComm, &generalAdminInp1);
00108 rc3 = rsGenQuery(rei->rsComm, &genQueryInp, &genQueryOut);
00109 if ( rc3 >= 0 ) {
00110 result = genQueryOut->sqlResult[0].value;
00111 if ( strcmp(result, "\0") == 0 || ( strncmp(result,"auto-",5) == 0 && strcmp(result, monStatus) != 0 ) ) {
00112 rc4 = rsGeneralAdmin(rei->rsComm, &generalAdminInp2);
00113 }
00114 } else {
00115 rodsLog(LOG_ERROR, "msiServerMonPerf: unable to retrieve the status metadata for the resource %s", splresc[indx]);
00116 }
00117 #ifndef windows_platform
00118 pthread_mutex_unlock(&my_mutex);
00119 #endif
00120 if ( rc1 != 0 ) {
00121 fprintf(foutput, "time=%i : unable to insert the entries for server %s into the iCAT\n",
00122 timestamp, serverName);
00123 }
00124 if ( rc2 != 0 ) {
00125 rodsLog(LOG_ERROR, "msiServerMonPerf: unable to register the free space metadata for the resource %s", splresc[indx]);
00126 }
00127 if ( rc4 != 0 ) {
00128 rodsLog(LOG_ERROR, "msiServerMonPerf: unable to register the status metadata for the resource %s", splresc[indx]);
00129 }
00130 indx += 1;
00131 }
00132
00133 clearGenQueryInp(&genQueryInp);
00134 freeGenQueryOut(&genQueryOut);
00135
00136 return (0);
00137 }
00138
00139 int strSplit(char *s, const char *ct, char splchain[MAX_VALUE][MAX_NAME_LEN]) {
00140
00141
00142
00143 int i;
00144 char cs[MAX_NAME_LEN];
00145
00146 if(s != NULL && ct != NULL) {
00147 for(i = 0; (rstrcpy(cs, strtok(s, ct), MAX_NAME_LEN)); i++) {
00148
00149 rstrcpy(splchain[i], cs, MAX_NAME_LEN);
00150 s = NULL;
00151 }
00152 }
00153 return 0;
00154 }
00155
00156 int getListOfResc(rsComm_t *rsComm, char serverList[MAX_VALUE][MAX_NAME_LEN], int nservers,
00157 monInfo_t monList[MAX_NSERVERS], int *nlist) {
00158
00159
00160
00161
00162
00163 int i, j, k, index[MAX_NSERVERS], l, status;
00164 genQueryInp_t genQueryInp;
00165 genQueryOut_t *genQueryOut;
00166
00167 memset(&genQueryInp, 0, sizeof(genQueryInp));
00168 memset(&index, -1, MAX_NSERVERS*sizeof(int));
00169 genQueryInp.maxRows = MAX_SQL_ROWS;
00170
00171 clearGenQueryInp(&genQueryInp);
00172 addInxIval(&genQueryInp.selectInp, COL_R_LOC, 1);
00173 addInxIval(&genQueryInp.selectInp, COL_R_RESC_NAME, 1);
00174 addInxIval(&genQueryInp.selectInp, COL_R_TYPE_NAME, 1);
00175 addInxIval(&genQueryInp.selectInp, COL_R_VAULT_PATH, 1);
00176
00177 status = rsGenQuery(rsComm, &genQueryInp, &genQueryOut);
00178 if ( genQueryOut->rowCnt > 0 ) {
00179 l = 0;
00180 for (i=0; i<genQueryOut->attriCnt; i++) {
00181 for (j=0; j<genQueryOut->rowCnt; j++) {
00182 char *tResult;
00183 tResult = genQueryOut->sqlResult[i].value;
00184 tResult += j*genQueryOut->sqlResult[i].len;
00185 switch (i) {
00186 case 0:
00187 if ( nservers >= 0 ) {
00188 for (k=0; k<nservers; k++) {
00189 if ( strcmp(serverList[k], tResult) == 0 ) {
00190 index[j] = l;
00191 l++;
00192 }
00193 }
00194 }
00195 else {
00196 index[j] = l;
00197 l++;
00198 }
00199 if ( index[j] != -1 ) {
00200 rstrcpy(monList[index[j]].serverName, tResult, LONG_NAME_LEN);
00201 }
00202 break;
00203 case 1:
00204 if ( index[j] != -1 ) {
00205 rstrcpy(monList[index[j]].rescName, tResult, MAX_NAME_LEN);
00206 }
00207 break;
00208 case 2:
00209 if ( index[j] != -1 ) {
00210 rstrcpy(monList[index[j]].rescType, tResult, LONG_NAME_LEN);
00211 }
00212 break;
00213 case 3:
00214 if ( index[j] != -1 ) {
00215 rstrcpy(monList[index[j]].vaultPath, tResult, LONG_NAME_LEN);
00216 }
00217 break;
00218 }
00219 }
00220 }
00221 (*nlist) = l;
00222 clearGenQueryInp(&genQueryInp);
00223 freeGenQueryOut(&genQueryOut);
00224 return (0);
00225 }
00226 return (-1);
00227 }
00228
00229 void *startMonScript(void *arg) {
00230
00231
00232
00233
00234
00235 char *output;
00236 msParam_t msp1, msp2, msp3, msp4, msp5, msout;
00237 int thrid, status;
00238 int retval;
00239
00240 thrInp_t *tinput = (thrInp_t*)arg;
00241 #ifndef windows_platform
00242 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
00243 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
00244 #endif
00245 fillStrInMsParam(&msp1, tinput->cmd);
00246 fillStrInMsParam(&msp2, tinput->cmdArgv);
00247 fillStrInMsParam(&msp3, tinput->execAddr);
00248 fillStrInMsParam(&msp4, tinput->hintPath);
00249 fillIntInMsParam(&msp5, tinput->addPathToArgv);
00250 thrid = tinput->threadId;
00251
00252 threadIsAlive[thrid] = 0;
00253 status = msiExecCmd(&msp1, &msp2, &msp3, &msp4, &msp5, &msout, &(tinput->rei));
00254
00255 if (status < 0) {
00256 char noanswer[MAXSTR] = MON_OUTPUT_NO_ANSWER;
00257 rodsLogError (LOG_ERROR, status, "Call to msiExecCmd failed in msiServerMonPerf. ");
00258 rodsMonPerfLog(tinput->execAddr, tinput->rescName, noanswer, &(tinput->rei));
00259 threadIsAlive[thrid] = 1;
00260 retval = -1;
00261 #ifndef windows_platform
00262 pthread_exit((void *)&retval);
00263 #endif
00264 }
00265
00266
00267
00268 if ( (char *) (*((execCmdOut_t *) msout.inOutStruct)).stdoutBuf.buf != NULL) {
00269 output = (char *) (*((execCmdOut_t *) msout.inOutStruct)).stdoutBuf.buf;
00270 rodsMonPerfLog(tinput->execAddr, tinput->rescName, output, &(tinput->rei));
00271 }
00272 else {
00273 char noanswer[MAXSTR] = MON_OUTPUT_NO_ANSWER;
00274 rodsLog(LOG_ERROR, "Server monitoring: no output for the server %s, status = %i \n", tinput->execAddr, status);
00275 rodsMonPerfLog(tinput->execAddr, tinput->rescName, noanswer, &(tinput->rei));
00276 threadIsAlive[thrid] = 1;
00277 retval = -1;
00278 #ifndef windows_platform
00279 pthread_exit((void *)&retval);
00280 #endif
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 threadIsAlive[thrid] = 1;
00296
00297 retval = 0;
00298 #ifndef windows_platform
00299 pthread_exit((void *)&retval);
00300 #endif
00301 }
00302
00303 int checkIPaddress(char *IP, unsigned char IPcomp[IPV4]) {
00304
00305 const char *delimIP = ".";
00306 char *eltstrIP, IPclone[MAX_NAME_LEN];
00307 int i, nelt = 0;
00308
00309 strcpy(IPclone, IP);
00310 i = atoi(strtok(IPclone, delimIP));
00311 if ( i < 0 || i > 255 ) return -1;
00312 IPcomp[0] = i;
00313 while ( (eltstrIP = strtok(NULL, delimIP)) ) {
00314 nelt++;
00315 i = atoi(eltstrIP);
00316 if ( i < 0 || i > 255 ) return -1;
00317 IPcomp[nelt] = i;
00318 }
00319 if ( (nelt + 1) != IPV4 ) return -1;
00320 return (0);
00321
00322 }
00323
00324 int checkHostAccessControl (char *username, char *hostclient, char *groupsname)
00325 {
00326
00327 char *configDir, hostControlAccessFile[LONG_NAME_LEN];
00328 char grouplist[MAX_SQL_ROWS][MAXSTR];
00329 const char *delim = " \t\n";
00330 int groupok, i, indxc, iok, nelt;
00331 char line[MAXLEN], *eltstr, tempArr[NFIELDS][MAXLEN];
00332 unsigned char result, IPEntry[IPV4], subnetEntry[IPV4], visitorIP[IPV4];
00333 FILE *fp;
00334
00335
00336 configDir = getConfigDir ();
00337 snprintf (hostControlAccessFile, LONG_NAME_LEN, "%s/%s", configDir,
00338 HOST_ACCESS_CONTROL_FILE);
00339 fp = fopen(hostControlAccessFile, "r");
00340 if (fp == NULL) {
00341 rodsLog (LOG_NOTICE,
00342 "hostAuthCheck: can't open HostControlAccess file %s", hostControlAccessFile);
00343 return (UNIX_FILE_OPEN_ERR - errno);
00344 }
00345
00346 nelt = 0;
00347 strncpy(grouplist[0], strtok(groupsname, delim), MAXSTR);
00348 while ( (eltstr = strtok(NULL, delim)) ) {
00349 nelt++;
00350 strncpy(grouplist[nelt], eltstr, MAXSTR);
00351 }
00352
00353 while ( !feof(fp) ) {
00354 indxc = 0;
00355 if( fgets(line, MAXLEN, fp) ) {
00356 if ( line[0] != '#' && line[0] != '\n' ) {
00357 eltstr = strtok(line, delim);
00358 strncpy(tempArr[indxc], eltstr, MAXSTR);
00359 while ( (eltstr = strtok(NULL, delim)) ) {
00360 indxc++;
00361 strncpy(tempArr[indxc], eltstr, MAXSTR);
00362 }
00363 if ( (indxc+1) == NFIELDS && checkIPaddress(tempArr[2], IPEntry) == 0 &&
00364 checkIPaddress(tempArr[3], subnetEntry) == 0 &&
00365 checkIPaddress(hostclient, visitorIP) == 0 ) {
00366
00367 groupok = 1;
00368 for ( i = 0; i <= nelt; i++ ) {
00369 if ( strcmp(tempArr[1], grouplist[i]) == 0 ) {
00370 groupok = 0;
00371 break;
00372 }
00373 }
00374 if ( strcmp(tempArr[1], "all") == 0 || groupok == 0 ) {
00375 if ( strcmp(tempArr[0], "all") == 0 || strcmp(tempArr[0], username) == 0 ) {
00376 iok = 1;
00377
00378
00379 for ( i = 0; i < IPV4; i++ ) {
00380 result = ~( visitorIP[i] ^ IPEntry[i] ) | subnetEntry[i];
00381 if ( result != 255 ) {
00382 iok = 0;
00383 }
00384 }
00385 if ( iok == 1 ) { fclose( fp ); return (0); }
00386 }
00387 }
00388 }
00389 }
00390 }
00391 }
00392 fclose( fp );
00393 return (-1);
00394 }
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 int msiCheckHostAccessControl (ruleExecInfo_t *rei) {
00433 char group[MAX_NAME_LEN], *hostclient, *result, *username;
00434 char condstr[MAX_NAME_LEN];
00435 int i, rc, status;
00436 genQueryInp_t genQueryInp;
00437 genQueryOut_t *genQueryOut = NULL;
00438 rsComm_t *rsComm;
00439
00440 RE_TEST_MACRO (" Calling msiCheckHostAccessControl")
00441
00442
00443 group[0] = '\0';
00444 rsComm = rei->rsComm;
00445
00446
00447 username = rsComm->clientUser.userName;
00448
00449 hostclient = inet_ntoa(rsComm->remoteAddr.sin_addr);
00450
00451 memset(&genQueryInp, 0, sizeof (genQueryInp));
00452 snprintf(condstr, MAX_NAME_LEN, "= '%s'", username);
00453 addInxVal(&genQueryInp.sqlCondInp, COL_USER_NAME, condstr);
00454 addInxIval(&genQueryInp.selectInp, COL_USER_GROUP_NAME, 1);
00455 genQueryInp.maxRows = MAX_SQL_ROWS;
00456 status = rsGenQuery(rsComm, &genQueryInp, &genQueryOut);
00457 if ( status >= 0 ) {
00458 for (i=0; i<genQueryOut->rowCnt; i++) {
00459 result = genQueryOut->sqlResult[0].value;
00460 result += i*genQueryOut->sqlResult[0].len;
00461 strcat(group, result);
00462 strcat(group, " ");
00463 }
00464 } else {
00465 rstrcpy(group, "all", MAX_NAME_LEN);
00466 }
00467 clearGenQueryInp(&genQueryInp);
00468 freeGenQueryOut(&genQueryOut);
00469
00470 rc = checkHostAccessControl(username, hostclient, group);
00471 if ( rc < 0 ) {
00472 rodsLog (LOG_NOTICE, "Access to user %s from host %s has been refused.\n", username, hostclient);
00473 }
00474
00475 return (rei->status);
00476
00477 }
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 int msiServerMonPerf (msParam_t *verb, msParam_t *ptime, ruleExecInfo_t *rei) {
00526 char buffer[MAX_NAME_LEN], line[MAX_VALUE], splchain[MAX_VALUE][MAX_NAME_LEN], *verbosity;
00527 char serverList[MAX_VALUE][MAX_NAME_LEN];
00528 char cmd[MAX_NAME_LEN];
00529 char probtime[LEN_SECONDS], measTime[LEN_SECONDS];
00530 FILE *filein;
00531 const char *delim = " \n";
00532 char valinit[MAX_NAME_LEN] = "";
00533 char val[MAX_NAME_LEN] = "";
00534 int check, i, indx, j, looptime, maxtime, nresc, nservers, rc, thrCount, threadsNotfinished;
00535 const char *probtimeDef = "10";
00536
00537 rsComm_t *rsComm;
00538 monInfo_t rescList[MAX_NSERVERS];
00539 thrInp_t *thrInput;
00540 int addPathToArgv = 0;
00541 char *hintPath = "";
00542
00543 RE_TEST_MACRO (" Calling msiServerMonPerf")
00544
00545
00546
00547 rsComm = rei->rsComm;
00548
00549 if (verb->inOutStruct != NULL) {
00550 verbosity = (char *) verb->inOutStruct;
00551 if (strcmp(verbosity, "verbose") == 0 ) {
00552 strcat(valinit, "-v ");
00553 }
00554 }
00555
00556 strcat(valinit, " -t ");
00557
00558 strncpy(probtime, (char *) ptime->inOutStruct, LEN_SECONDS);
00559 if( atoi(probtime) > 0) {
00560 strcat(valinit, probtime);
00561 strncpy(measTime, probtime, LEN_SECONDS);
00562 }
00563 else {
00564 strcat(valinit, probtimeDef);
00565 strncpy(measTime, probtimeDef, LEN_SECONDS);
00566 }
00567
00568 rstrcpy(val, "", MAX_NAME_LEN);
00569
00570
00571 nresc = 0;
00572
00573 nservers = -1;
00574
00575 if((filein = fopen(MON_CFG_FILE, "r")) != NULL) {
00576 i = 0;
00577 while(fgets(line, sizeof line, filein) != NULL) {
00578
00579 if(line[0] != '#') {
00580 rstrcpy(buffer, strdup(line), MAX_NAME_LEN);
00581 strSplit(buffer, delim, splchain);
00582 rstrcpy(serverList[i], splchain[0], MAX_NAME_LEN);
00583 i++;
00584 }
00585 }
00586
00587 nservers = i;
00588
00589 fclose(filein);
00590 }
00591 getListOfResc(rsComm, serverList, nservers, rescList, &nresc);
00592
00593 strcpy(cmd, MON_PERF_SCRIPT);
00594 #ifndef windows_platform
00595 pthread_t *threads = (pthread_t*)malloc(sizeof(pthread_t) * nresc);
00596 pthread_mutex_init(&my_mutex, NULL);
00597 #endif
00598 thrInput = (thrInp_t*)malloc(sizeof(thrInp_t) * nresc);
00599 thrCount = 0;
00600
00601 for (i = 0; i < nresc; i++) {
00602
00603
00604
00605 check = 0;
00606 for (j = 0; j < thrCount; j++) {
00607 if ( strcmp(thrInput[j].execAddr, rescList[i].serverName) == 0 ) {
00608 indx = j;
00609 check = 1;
00610 }
00611 }
00612 if ( check == 0 ) {
00613 strcpy(thrInput[thrCount].cmdArgv, valinit);
00614 strcat(thrInput[thrCount].cmdArgv, " -fs ");
00615 if ( strcmp(rescList[thrCount].rescType, "unix file system") == 0 ) {
00616 strcat(thrInput[thrCount].cmdArgv, rescList[i].vaultPath);
00617 } else {
00618 strcat(thrInput[thrCount].cmdArgv, "none");
00619 }
00620 rstrcpy(thrInput[thrCount].cmd, cmd, LONG_NAME_LEN);
00621 rstrcpy(thrInput[thrCount].execAddr, rescList[i].serverName, LONG_NAME_LEN);
00622 rstrcpy(thrInput[thrCount].hintPath, hintPath, MAX_NAME_LEN);
00623 thrInput[thrCount].addPathToArgv = addPathToArgv;
00624 thrInput[thrCount].threadId = thrCount;
00625 rstrcpy(thrInput[thrCount].rescName, rescList[i].rescName, MAX_NAME_LEN);
00626 memcpy(&(thrInput[thrCount].rei), rei, sizeof(ruleExecInfo_t));
00627 thrCount += 1;
00628 }
00629 else {
00630 rstrcat(thrInput[indx].rescName, ",", MAX_NAME_LEN);
00631 rstrcat(thrInput[indx].rescName, rescList[i].rescName, MAX_NAME_LEN);
00632 if ( strcmp(rescList[i].rescType, "unix file system") == 0 ) {
00633 strcat(thrInput[indx].cmdArgv, ",");
00634 strcat(thrInput[indx].cmdArgv, rescList[i].vaultPath);
00635 } else {
00636 strcat(thrInput[indx].cmdArgv, ",none");
00637 }
00638 }
00639 rstrcpy(val, "", MAX_NAME_LEN);
00640 }
00641
00642 for ( i = 0; i < thrCount; i++ ) {
00643 #ifndef windows_platform
00644 if ( pthread_create(&threads[i], NULL, *startMonScript, (void *) &thrInput[i]) < 0) {
00645 rodsLog(LOG_ERROR, "msiServerMonPerf: pthread_create error\n");
00646 exit(1);
00647 }
00648 #endif
00649 }
00650
00651 maxtime = atoi(measTime) + TIMEOUT;
00652 looptime = 0;
00653 while (1) {
00654 sleep(1);
00655 looptime += 1;
00656 if ( looptime >= maxtime ) {
00657 for (i = 0; i < thrCount; i++) {
00658 if ( !threadIsAlive[i] ) {
00659 #ifndef windows_platform
00660 rc = pthread_cancel(threads[i]);
00661 #endif
00662 if ( rc == 0 ) {
00663 char noanswer[MAXSTR] = MON_OUTPUT_NO_ANSWER;
00664 threadIsAlive[i] = 1;
00665 rodsMonPerfLog(thrInput[i].execAddr,
00666 thrInput[i].rescName,
00667 noanswer,
00668 &(thrInput[i].rei));
00669 }
00670 }
00671 }
00672 }
00673 threadsNotfinished = 1;
00674 for (i = 0; i < thrCount; i++) {
00675 if ( threadIsAlive[i] == 0 ) {
00676 threadsNotfinished = 0;
00677 }
00678 }
00679 if ( threadsNotfinished ) {
00680 break;
00681 }
00682 }
00683
00684 #ifndef windows_platform
00685 free(threads);
00686 #endif
00687 free(thrInput);
00688
00689 return (rei->status);
00690
00691 }
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731 int msiFlushMonStat (msParam_t *inpParam1, msParam_t *inpParam2, ruleExecInfo_t *rei) {
00732 int elapseTime, defaultTimespan, rc;
00733 char secAgo[MAXLEN], *tablename, *timespan;
00734 generalRowPurgeInp_t generalRowPurgeInp;
00735 rsComm_t *rsComm;
00736
00737 RE_TEST_MACRO (" Calling msiFlushMonStat");
00738
00739 defaultTimespan = 24;
00740
00741 if (rei == NULL || rei->rsComm == NULL) {
00742 rodsLog (LOG_ERROR, "msiFlushMonStat: input rei or rsComm is NULL");
00743 return (SYS_INTERNAL_NULL_INPUT_ERR);
00744 }
00745
00746 rsComm = rei->rsComm;
00747
00748 if ( inpParam1 == NULL ) {
00749 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00750 "msiFlushMonStat: input Param1 is NULL");
00751 return (rei->status);
00752 }
00753
00754 if ( strcmp (inpParam1->type, STR_MS_T) == 0 ) {
00755 timespan = (char *) inpParam1->inOutStruct;
00756 } else {
00757 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00758 "msiFlushMonStat: Unsupported input Param1 type %s",
00759 inpParam1->type);
00760 return (rei->status);
00761 }
00762
00763 if ( inpParam2 == NULL ) {
00764 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00765 "msiFlushMonStat: input Param2 is NULL");
00766 return (rei->status);
00767 }
00768
00769 if ( strcmp (inpParam2->type, STR_MS_T) == 0 ) {
00770 tablename = (char *) inpParam2->inOutStruct;
00771 } else {
00772 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00773 "msiFlushMonStat: Unsupported input Param2 type %s",
00774 inpParam2->type);
00775 return (rei->status);
00776 }
00777
00778 if ( atoi(timespan) > 0 ) {
00779 elapseTime = atoi(timespan) * 3600;
00780 }
00781 else {
00782 elapseTime = defaultTimespan * 3600;
00783 }
00784
00785 if ( strcmp(tablename, "serverload") != 0 &&
00786 strcmp(tablename, "serverloaddigest") != 0 ) {
00787 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00788 "msiFlushMonStat: table %s does not exist", tablename);
00789 return (rei->status);
00790 }
00791
00792 generalRowPurgeInp.tableName = tablename;
00793 snprintf (secAgo, MAXLEN, "%i", elapseTime);
00794 generalRowPurgeInp.secondsAgo = secAgo;
00795 rc = rsGeneralRowPurge(rsComm, &generalRowPurgeInp);
00796
00797 if ( rc != 0 && rc != CAT_SUCCESS_BUT_WITH_NO_INFO ) {
00798 rodsLog (LOG_ERROR, "msiFlushMonStat failed, error %i", rc);
00799 }
00800
00801 return (rei->status);
00802 }
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 int msiDigestMonStat(msParam_t *cpu_wght, msParam_t *mem_wght, msParam_t *swap_wght, msParam_t *runq_wght,
00860 msParam_t *disk_wght, msParam_t *netin_wght, msParam_t *netout_wght,
00861 ruleExecInfo_t *rei) {
00862 char rescList[MAX_NSERVERS][MAX_NAME_LEN], *tResult,
00863 timeList[MAX_NSERVERS][MAX_NAME_LEN];
00864 char condStr1[MAX_NAME_LEN], condStr2[MAX_NAME_LEN], loadStr[MAX_NAME_LEN];
00865 int i, j, loadFactor, nresc, rc, status, totalWeight, weight[NRESULT];
00866 rsComm_t *rsComm;
00867 generalRowInsertInp_t generalRowInsertInp;
00868 genQueryInp_t genQueryInp;
00869 genQueryOut_t *genQueryOut = NULL;
00870
00871 RE_TEST_MACRO (" Calling msiDigestMonStat");
00872
00873 if (rei == NULL || rei->rsComm == NULL) {
00874 rodsLog (LOG_ERROR,
00875 "msiDigestMonStat: input rei or rsComm is NULL");
00876 return (SYS_INTERNAL_NULL_INPUT_ERR);
00877 }
00878
00879 rsComm = rei->rsComm;
00880
00881 if ( cpu_wght == NULL || mem_wght == NULL || swap_wght == NULL || runq_wght == NULL
00882 || disk_wght == NULL || netin_wght == NULL || netout_wght == NULL ) {
00883 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00884 "msiDigestMonStat: at least one of the input param is NULL");
00885 return (rei->status);
00886 }
00887
00888 if ( strcmp (cpu_wght->type, STR_MS_T) == 0 ) {
00889 weight[0] = atoi((const char*)cpu_wght->inOutStruct);
00890 } else {
00891 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00892 "msiDigestMonStat: Unsupported input cpu_wght type %s",
00893 cpu_wght->type);
00894 return (rei->status);
00895 }
00896
00897 if ( strcmp (mem_wght->type, STR_MS_T) == 0 ) {
00898 weight[1] = atoi((const char*)mem_wght->inOutStruct);
00899 } else {
00900 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00901 "msiDigestMonStat: Unsupported input mem_wght type %s",
00902 mem_wght->type);
00903 return (rei->status);
00904 }
00905
00906 if ( strcmp (swap_wght->type, STR_MS_T) == 0 ) {
00907 weight[2] = atoi((const char*)swap_wght->inOutStruct);
00908 } else {
00909 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00910 "msiDigestMonStat: Unsupported input swap_wght type %s",
00911 swap_wght->type);
00912 return (rei->status);
00913 }
00914
00915 if ( strcmp (runq_wght->type, STR_MS_T) == 0 ) {
00916 weight[3] = atoi((const char*)runq_wght->inOutStruct);
00917 } else {
00918 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00919 "msiDigestMonStat: Unsupported input runq_wght type %s",
00920 runq_wght->type);
00921 return (rei->status);
00922 }
00923
00924 if ( strcmp (disk_wght->type, STR_MS_T) == 0 ) {
00925 weight[4] = atoi((const char*)disk_wght->inOutStruct);
00926 } else {
00927 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00928 "msiDigestMonStat: Unsupported input disk_wght type %s",
00929 disk_wght->type);
00930 return (rei->status);
00931 }
00932
00933 if ( strcmp (netin_wght->type, STR_MS_T) == 0 ) {
00934 weight[5] = atoi((const char*)netin_wght->inOutStruct);
00935 } else {
00936 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00937 "msiDigestMonStat: Unsupported input netin_wght type %s",
00938 netin_wght->type);
00939 return (rei->status);
00940 }
00941
00942 if ( strcmp (netout_wght->type, STR_MS_T) == 0 ) {
00943 weight[6] = atoi((const char*)netout_wght->inOutStruct);
00944 } else {
00945 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00946 "msiDigestMonStat: Unsupported input netout_wght type %s",
00947 netout_wght->type);
00948 return (rei->status);
00949 }
00950
00951 totalWeight = 0;
00952 for (i=0; i<NRESULT; i++){
00953 totalWeight += weight[i];
00954 }
00955
00956 memset(&genQueryInp, 0, sizeof (genQueryInp));
00957 addInxIval(&genQueryInp.selectInp, COL_SL_RESC_NAME, 1);
00958 addInxIval(&genQueryInp.selectInp, COL_SL_CREATE_TIME, SELECT_MAX);
00959 genQueryInp.maxRows = MAX_SQL_ROWS;
00960 status = rsGenQuery(rsComm, &genQueryInp, &genQueryOut);
00961 if( NULL == genQueryOut ) {
00962 rodsLog( LOG_ERROR, "msiDigestMonStat :: &genQueryOut is NULL" );
00963 return rei->status;
00964 }
00965 if ( status == 0 ) {
00966 nresc = genQueryOut->rowCnt;
00967 for (i=0; i<genQueryOut->attriCnt; i++) {
00968 for (j=0; j<nresc; j++) {
00969 tResult = genQueryOut->sqlResult[i].value;
00970 tResult += j*genQueryOut->sqlResult[i].len;
00971 if ( i == 0 ) {
00972 rstrcpy(rescList[j], tResult, genQueryOut->sqlResult[i].len);
00973 }
00974 if ( i == 1 ) {
00975 rstrcpy(timeList[j], tResult, genQueryOut->sqlResult[i].len);
00976 }
00977 }
00978 }
00979 }
00980 else {
00981 rodsLog(LOG_ERROR, "msiDigestMonStat: Unable to retrieve information \
00982 from R_SERVER_LOAD");
00983 return(rei->status);
00984 }
00985
00986 memset(&genQueryInp, 0, sizeof (genQueryInp));
00987 addInxIval(&genQueryInp.selectInp, COL_SL_CPU_USED, 1);
00988 addInxIval(&genQueryInp.selectInp, COL_SL_MEM_USED, 1);
00989 addInxIval(&genQueryInp.selectInp, COL_SL_SWAP_USED, 1);
00990 addInxIval(&genQueryInp.selectInp, COL_SL_RUNQ_LOAD, 1);
00991 addInxIval(&genQueryInp.selectInp, COL_SL_DISK_SPACE, 1);
00992 addInxIval(&genQueryInp.selectInp, COL_SL_NET_INPUT, 1);
00993 addInxIval(&genQueryInp.selectInp, COL_SL_NET_OUTPUT, 1);
00994 genQueryInp.maxRows = 1;
00995 generalRowInsertInp.tableName = "serverloaddigest";
00996 for (i=0; i<nresc; i++) {
00997 memset(&genQueryInp.sqlCondInp, 0, sizeof (genQueryInp.sqlCondInp));
00998 snprintf(condStr1, MAX_NAME_LEN, "= '%s'", rescList[i]);
00999 addInxVal(&genQueryInp.sqlCondInp, COL_SL_RESC_NAME, condStr1);
01000 snprintf(condStr2, MAX_NAME_LEN, "= '%s'", timeList[i]);
01001 addInxVal(&genQueryInp.sqlCondInp, COL_SL_CREATE_TIME, condStr2);
01002 status = rsGenQuery(rsComm, &genQueryInp, &genQueryOut);
01003 if ( status == 0 ) {
01004 loadFactor = 0;
01005 for (j=0; j<genQueryOut->attriCnt; j++) {
01006 tResult = genQueryOut->sqlResult[j].value;
01007 loadFactor += atoi(tResult) * weight[j];
01008 }
01009 loadFactor = loadFactor / totalWeight;
01010 generalRowInsertInp.arg1 = rescList[i];
01011 snprintf(loadStr, MAX_NAME_LEN, "%i", loadFactor);
01012 generalRowInsertInp.arg2 = loadStr;
01013 rc = rsGeneralRowInsert(rsComm, &generalRowInsertInp);
01014 if ( rc != 0 ) {
01015 rodsLog(LOG_ERROR, "msiDigestMonStat: Unable to ingest\
01016 information into from R_SERVER_LOAD_DIGEST table");
01017 }
01018 }
01019 }
01020
01021 clearGenQueryInp(&genQueryInp);
01022 freeGenQueryOut(&genQueryOut);
01023
01024 return(rei->status);
01025 }