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