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;
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 rsGenQuery(rsComm, &genQueryInp, &genQueryOut);
00181 if ( genQueryOut->rowCnt > 0 ) {
00182 l = 0;
00183 for (i=0; i<genQueryOut->attriCnt; i++) {
00184 for (j=0; j<genQueryOut->rowCnt; j++) {
00185 char *tResult;
00186 tResult = genQueryOut->sqlResult[i].value;
00187 tResult += j*genQueryOut->sqlResult[i].len;
00188 switch (i) {
00189 case 0:
00190 if ( nservers >= 0 ) {
00191 for (k=0; k<nservers; k++) {
00192 if ( strcmp(serverList[k], tResult) == 0 ) {
00193 index[j] = l;
00194 l++;
00195 }
00196 }
00197 }
00198 else {
00199 index[j] = l;
00200 l++;
00201 }
00202 if ( index[j] != -1 ) {
00203 rstrcpy(monList[index[j]].serverName, tResult, LONG_NAME_LEN);
00204 }
00205 break;
00206 case 1:
00207 if ( index[j] != -1 ) {
00208 rstrcpy(monList[index[j]].rescName, tResult, MAX_NAME_LEN);
00209 }
00210 break;
00211 case 2:
00212 if ( index[j] != -1 ) {
00213 rstrcpy(monList[index[j]].rescType, tResult, LONG_NAME_LEN);
00214 }
00215 break;
00216 case 3:
00217 if ( index[j] != -1 ) {
00218 rstrcpy(monList[index[j]].vaultPath, tResult, LONG_NAME_LEN);
00219 }
00220 break;
00221 }
00222 }
00223 }
00224 (*nlist) = l;
00225 clearGenQueryInp(&genQueryInp);
00226 freeGenQueryOut(&genQueryOut);
00227 return (0);
00228 }
00229 return (-1);
00230 }
00231
00232 void *startMonScript(void *arg) {
00233
00234
00235
00236
00237
00238 char *output;
00239 msParam_t msp1, msp2, msp3, msp4, msp5, msout;
00240 int thrid, status;
00241 int retval;
00242
00243 thrInp_t *tinput = (thrInp_t*)arg;
00244 #ifndef windows_platform
00245 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
00246 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
00247 #endif
00248 fillStrInMsParam(&msp1, tinput->cmd);
00249 fillStrInMsParam(&msp2, tinput->cmdArgv);
00250 fillStrInMsParam(&msp3, tinput->execAddr);
00251 fillStrInMsParam(&msp4, tinput->hintPath);
00252 fillIntInMsParam(&msp5, tinput->addPathToArgv);
00253 thrid = tinput->threadId;
00254
00255 threadIsAlive[thrid] = 0;
00256 status = msiExecCmd(&msp1, &msp2, &msp3, &msp4, &msp5, &msout, &(tinput->rei));
00257
00258 if (status < 0) {
00259 char noanswer[MAXSTR] = MON_OUTPUT_NO_ANSWER;
00260 rodsLogError (LOG_ERROR, status, "Call to msiExecCmd failed in msiServerMonPerf. ");
00261 rodsMonPerfLog(tinput->execAddr, tinput->rescName, noanswer, &(tinput->rei));
00262 threadIsAlive[thrid] = 1;
00263 retval = -1;
00264 #ifndef windows_platform
00265 pthread_exit((void *)&retval);
00266 #endif
00267 }
00268
00269
00270
00271 if ( (char *) (*((execCmdOut_t *) msout.inOutStruct)).stdoutBuf.buf != NULL) {
00272 output = (char *) (*((execCmdOut_t *) msout.inOutStruct)).stdoutBuf.buf;
00273 rodsMonPerfLog(tinput->execAddr, tinput->rescName, output, &(tinput->rei));
00274 }
00275 else {
00276 char noanswer[MAXSTR] = MON_OUTPUT_NO_ANSWER;
00277 rodsLog(LOG_ERROR, "Server monitoring: no output for the server %s, status = %i \n", tinput->execAddr, status);
00278 rodsMonPerfLog(tinput->execAddr, tinput->rescName, noanswer, &(tinput->rei));
00279 threadIsAlive[thrid] = 1;
00280 retval = -1;
00281 #ifndef windows_platform
00282 pthread_exit((void *)&retval);
00283 #endif
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 threadIsAlive[thrid] = 1;
00299
00300 retval = 0;
00301 #ifndef windows_platform
00302 pthread_exit((void *)&retval);
00303 #endif
00304 }
00305
00306 int checkIPaddress(char *IP, unsigned char IPcomp[IPV4]) {
00307
00308 const char *delimIP = ".";
00309 char *eltstrIP, IPclone[MAX_NAME_LEN];
00310 int i, nelt = 0;
00311
00312 strcpy(IPclone, IP);
00313 i = atoi(strtok(IPclone, delimIP));
00314 if ( i < 0 || i > 255 ) return -1;
00315 IPcomp[0] = i;
00316 while ( (eltstrIP = strtok(NULL, delimIP)) ) {
00317 nelt++;
00318 i = atoi(eltstrIP);
00319 if ( i < 0 || i > 255 ) return -1;
00320 IPcomp[nelt] = i;
00321 }
00322 if ( (nelt + 1) != IPV4 ) return -1;
00323 return (0);
00324
00325 }
00326
00327 int checkHostAccessControl (char *username, char *hostclient, char *groupsname)
00328 {
00329
00330 char *configDir, hostControlAccessFile[LONG_NAME_LEN];
00331 char grouplist[MAX_SQL_ROWS][MAXSTR];
00332 const char *delim = " \t\n";
00333 int groupok, i, indxc, iok, nelt;
00334 char line[MAXLEN], *eltstr, tempArr[NFIELDS][MAXLEN];
00335 unsigned char result, IPEntry[IPV4], subnetEntry[IPV4], visitorIP[IPV4];
00336 FILE *fp;
00337
00338
00339 configDir = getConfigDir ();
00340 snprintf (hostControlAccessFile, LONG_NAME_LEN, "%s/%s", configDir,
00341 HOST_ACCESS_CONTROL_FILE);
00342 fp = fopen(hostControlAccessFile, "r");
00343 if (fp == NULL) {
00344 rodsLog (LOG_NOTICE,
00345 "hostAuthCheck: can't open HostControlAccess file %s", hostControlAccessFile);
00346 return (UNIX_FILE_OPEN_ERR - errno);
00347 }
00348
00349 nelt = 0;
00350 strncpy(grouplist[0], strtok(groupsname, delim), MAXSTR);
00351 while ( (eltstr = strtok(NULL, delim)) ) {
00352 nelt++;
00353 strncpy(grouplist[nelt], eltstr, MAXSTR);
00354 }
00355
00356 while ( !feof(fp) ) {
00357 indxc = 0;
00358 if( fgets(line, MAXLEN, fp) ) {
00359 if ( line[0] != '#' && line[0] != '\n' ) {
00360 eltstr = strtok(line, delim);
00361 strncpy(tempArr[indxc], eltstr, MAXSTR);
00362 while ( (eltstr = strtok(NULL, delim)) ) {
00363 indxc++;
00364 strncpy(tempArr[indxc], eltstr, MAXSTR);
00365 }
00366 if ( (indxc+1) == NFIELDS && checkIPaddress(tempArr[2], IPEntry) == 0 &&
00367 checkIPaddress(tempArr[3], subnetEntry) == 0 &&
00368 checkIPaddress(hostclient, visitorIP) == 0 ) {
00369
00370 groupok = 1;
00371 for ( i = 0; i <= nelt; i++ ) {
00372 if ( strcmp(tempArr[1], grouplist[i]) == 0 ) {
00373 groupok = 0;
00374 break;
00375 }
00376 }
00377 if ( strcmp(tempArr[1], "all") == 0 || groupok == 0 ) {
00378 if ( strcmp(tempArr[0], "all") == 0 || strcmp(tempArr[0], username) == 0 ) {
00379 iok = 1;
00380
00381
00382 for ( i = 0; i < IPV4; i++ ) {
00383 result = ~( visitorIP[i] ^ IPEntry[i] ) | subnetEntry[i];
00384 if ( result != 255 ) {
00385 iok = 0;
00386 }
00387 }
00388 if ( iok == 1 ) { fclose( fp ); return (0); }
00389 }
00390 }
00391 }
00392 }
00393 }
00394 }
00395 fclose( fp );
00396 return (-1);
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
00433
00434
00435 int msiCheckHostAccessControl (ruleExecInfo_t *rei) {
00436 char group[MAX_NAME_LEN], *hostclient, *result, *username;
00437 char condstr[MAX_NAME_LEN];
00438 int i, rc, status;
00439 genQueryInp_t genQueryInp;
00440 genQueryOut_t *genQueryOut = NULL;
00441 rsComm_t *rsComm;
00442
00443 RE_TEST_MACRO (" Calling msiCheckHostAccessControl")
00444
00445
00446 group[0] = '\0';
00447 rsComm = rei->rsComm;
00448
00449
00450 username = rsComm->clientUser.userName;
00451
00452 hostclient = inet_ntoa(rsComm->remoteAddr.sin_addr);
00453
00454 memset(&genQueryInp, 0, sizeof (genQueryInp));
00455 snprintf(condstr, MAX_NAME_LEN, "= '%s'", username);
00456 addInxVal(&genQueryInp.sqlCondInp, COL_USER_NAME, condstr);
00457 addInxIval(&genQueryInp.selectInp, COL_USER_GROUP_NAME, 1);
00458 genQueryInp.maxRows = MAX_SQL_ROWS;
00459 status = rsGenQuery(rsComm, &genQueryInp, &genQueryOut);
00460 if ( status >= 0 ) {
00461 for (i=0; i<genQueryOut->rowCnt; i++) {
00462 result = genQueryOut->sqlResult[0].value;
00463 result += i*genQueryOut->sqlResult[0].len;
00464 strcat(group, result);
00465 strcat(group, " ");
00466 }
00467 } else {
00468 rstrcpy(group, "all", MAX_NAME_LEN);
00469 }
00470 clearGenQueryInp(&genQueryInp);
00471 freeGenQueryOut(&genQueryOut);
00472
00473 rc = checkHostAccessControl(username, hostclient, group);
00474 if ( rc < 0 ) {
00475 rodsLog (LOG_NOTICE, "Access to user %s from host %s has been refused.\n", username, hostclient);
00476 }
00477
00478 return (rei->status);
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
00526
00527
00528 int msiServerMonPerf (msParam_t *verb, msParam_t *ptime, ruleExecInfo_t *rei) {
00529 char buffer[MAX_NAME_LEN], line[MAX_VALUE], splchain[MAX_VALUE][MAX_NAME_LEN], *verbosity;
00530 char serverList[MAX_VALUE][MAX_NAME_LEN];
00531 char cmd[MAX_NAME_LEN];
00532 char probtime[LEN_SECONDS], measTime[LEN_SECONDS];
00533 FILE *filein;
00534 const char *delim = " \n";
00535 char valinit[MAX_NAME_LEN] = "";
00536 char val[MAX_NAME_LEN] = "";
00537 int check, i, indx, j, looptime, maxtime, nresc, nservers, rc, thrCount, threadsNotfinished;
00538 const char *probtimeDef = "10";
00539
00540 rsComm_t *rsComm;
00541 monInfo_t rescList[MAX_NSERVERS];
00542 thrInp_t *thrInput;
00543 int addPathToArgv = 0;
00544 char *hintPath = "";
00545
00546 RE_TEST_MACRO (" Calling msiServerMonPerf")
00547
00548
00549
00550 rsComm = rei->rsComm;
00551
00552 if (verb->inOutStruct != NULL) {
00553 verbosity = (char *) verb->inOutStruct;
00554 if (strcmp(verbosity, "verbose") == 0 ) {
00555 strcat(valinit, "-v ");
00556 }
00557 }
00558
00559 strcat(valinit, " -t ");
00560
00561 strncpy(probtime, (char *) ptime->inOutStruct, LEN_SECONDS);
00562 if( atoi(probtime) > 0) {
00563 strcat(valinit, probtime);
00564 strncpy(measTime, probtime, LEN_SECONDS);
00565 }
00566 else {
00567 strcat(valinit, probtimeDef);
00568 strncpy(measTime, probtimeDef, LEN_SECONDS);
00569 }
00570
00571 rstrcpy(val, "", MAX_NAME_LEN);
00572
00573
00574 nresc = 0;
00575
00576 nservers = -1;
00577
00578 if((filein = fopen(MON_CFG_FILE, "r")) != NULL) {
00579 i = 0;
00580 while(fgets(line, sizeof line, filein) != NULL) {
00581
00582 if(line[0] != '#') {
00583 rstrcpy(buffer, strdup(line), MAX_NAME_LEN);
00584 strSplit(buffer, delim, splchain);
00585 rstrcpy(serverList[i], splchain[0], MAX_NAME_LEN);
00586 i++;
00587 }
00588 }
00589
00590 nservers = i;
00591
00592 fclose(filein);
00593 }
00594 getListOfResc(rsComm, serverList, nservers, rescList, &nresc);
00595
00596 strcpy(cmd, MON_PERF_SCRIPT);
00597 #ifndef windows_platform
00598 pthread_t *threads = (pthread_t*)malloc(sizeof(pthread_t) * nresc);
00599 pthread_mutex_init(&my_mutex, NULL);
00600 #endif
00601 thrInput = (thrInp_t*)malloc(sizeof(thrInp_t) * nresc);
00602 thrCount = 0;
00603
00604 for (i = 0; i < nresc; i++) {
00605
00606
00607
00608 check = 0;
00609 for (j = 0; j < thrCount; j++) {
00610 if ( strcmp(thrInput[j].execAddr, rescList[i].serverName) == 0 ) {
00611 indx = j;
00612 check = 1;
00613 }
00614 }
00615 if ( check == 0 ) {
00616 strcpy(thrInput[thrCount].cmdArgv, valinit);
00617 strcat(thrInput[thrCount].cmdArgv, " -fs ");
00618 if ( strcmp(rescList[thrCount].rescType, "unix file system") == 0 ) {
00619 strcat(thrInput[thrCount].cmdArgv, rescList[i].vaultPath);
00620 } else {
00621 strcat(thrInput[thrCount].cmdArgv, "none");
00622 }
00623 rstrcpy(thrInput[thrCount].cmd, cmd, LONG_NAME_LEN);
00624 rstrcpy(thrInput[thrCount].execAddr, rescList[i].serverName, LONG_NAME_LEN);
00625 rstrcpy(thrInput[thrCount].hintPath, hintPath, MAX_NAME_LEN);
00626 thrInput[thrCount].addPathToArgv = addPathToArgv;
00627 thrInput[thrCount].threadId = thrCount;
00628 rstrcpy(thrInput[thrCount].rescName, rescList[i].rescName, MAX_NAME_LEN);
00629 memcpy(&(thrInput[thrCount].rei), rei, sizeof(ruleExecInfo_t));
00630 thrCount += 1;
00631 }
00632 else {
00633 rstrcat(thrInput[indx].rescName, ",", MAX_NAME_LEN);
00634 rstrcat(thrInput[indx].rescName, rescList[i].rescName, MAX_NAME_LEN);
00635 if ( strcmp(rescList[i].rescType, "unix file system") == 0 ) {
00636 strcat(thrInput[indx].cmdArgv, ",");
00637 strcat(thrInput[indx].cmdArgv, rescList[i].vaultPath);
00638 } else {
00639 strcat(thrInput[indx].cmdArgv, ",none");
00640 }
00641 }
00642 rstrcpy(val, "", MAX_NAME_LEN);
00643 }
00644
00645 for ( i = 0; i < thrCount; i++ ) {
00646 #ifndef windows_platform
00647 if ( pthread_create(&threads[i], NULL, *startMonScript, (void *) &thrInput[i]) < 0) {
00648 rodsLog(LOG_ERROR, "msiServerMonPerf: pthread_create error\n");
00649 exit(1);
00650 }
00651 #endif
00652 }
00653
00654 maxtime = atoi(measTime) + TIMEOUT;
00655 looptime = 0;
00656 while (1) {
00657 sleep(1);
00658 looptime += 1;
00659 if ( looptime >= maxtime ) {
00660 for (i = 0; i < thrCount; i++) {
00661 if ( !threadIsAlive[i] ) {
00662 #ifndef windows_platform
00663 rc = pthread_cancel(threads[i]);
00664 #endif
00665 if ( rc == 0 ) {
00666 char noanswer[MAXSTR] = MON_OUTPUT_NO_ANSWER;
00667 threadIsAlive[i] = 1;
00668 rodsMonPerfLog(thrInput[i].execAddr,
00669 thrInput[i].rescName,
00670 noanswer,
00671 &(thrInput[i].rei));
00672 }
00673 }
00674 }
00675 }
00676 threadsNotfinished = 1;
00677 for (i = 0; i < thrCount; i++) {
00678 if ( threadIsAlive[i] == 0 ) {
00679 threadsNotfinished = 0;
00680 }
00681 }
00682 if ( threadsNotfinished ) {
00683 break;
00684 }
00685 }
00686
00687 #ifndef windows_platform
00688 free(threads);
00689 #endif
00690 free(thrInput);
00691
00692 return (rei->status);
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
00732
00733
00734 int msiFlushMonStat (msParam_t *inpParam1, msParam_t *inpParam2, ruleExecInfo_t *rei) {
00735 int elapseTime, defaultTimespan, rc;
00736 char secAgo[MAXLEN], *tablename, *timespan;
00737 generalRowPurgeInp_t generalRowPurgeInp;
00738 rsComm_t *rsComm;
00739
00740 RE_TEST_MACRO (" Calling msiFlushMonStat");
00741
00742 defaultTimespan = 24;
00743
00744 if (rei == NULL || rei->rsComm == NULL) {
00745 rodsLog (LOG_ERROR, "msiFlushMonStat: input rei or rsComm is NULL");
00746 return (SYS_INTERNAL_NULL_INPUT_ERR);
00747 }
00748
00749 rsComm = rei->rsComm;
00750
00751 if ( inpParam1 == NULL ) {
00752 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00753 "msiFlushMonStat: input Param1 is NULL");
00754 return (rei->status);
00755 }
00756
00757 if ( strcmp (inpParam1->type, STR_MS_T) == 0 ) {
00758 timespan = (char *) inpParam1->inOutStruct;
00759 } else {
00760 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00761 "msiFlushMonStat: Unsupported input Param1 type %s",
00762 inpParam1->type);
00763 return (rei->status);
00764 }
00765
00766 if ( inpParam2 == NULL ) {
00767 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00768 "msiFlushMonStat: input Param2 is NULL");
00769 return (rei->status);
00770 }
00771
00772 if ( strcmp (inpParam2->type, STR_MS_T) == 0 ) {
00773 tablename = (char *) inpParam2->inOutStruct;
00774 } else {
00775 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00776 "msiFlushMonStat: Unsupported input Param2 type %s",
00777 inpParam2->type);
00778 return (rei->status);
00779 }
00780
00781 if ( atoi(timespan) > 0 ) {
00782 elapseTime = atoi(timespan) * 3600;
00783 }
00784 else {
00785 elapseTime = defaultTimespan * 3600;
00786 }
00787
00788 if ( strcmp(tablename, "serverload") != 0 &&
00789 strcmp(tablename, "serverloaddigest") != 0 ) {
00790 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00791 "msiFlushMonStat: table %s does not exist", tablename);
00792 return (rei->status);
00793 }
00794
00795 generalRowPurgeInp.tableName = tablename;
00796 snprintf (secAgo, MAXLEN, "%i", elapseTime);
00797 generalRowPurgeInp.secondsAgo = secAgo;
00798 rc = rsGeneralRowPurge(rsComm, &generalRowPurgeInp);
00799
00800 if ( rc != 0 && rc != CAT_SUCCESS_BUT_WITH_NO_INFO ) {
00801 rodsLog (LOG_ERROR, "msiFlushMonStat failed, error %i", rc);
00802 }
00803
00804 return (rei->status);
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
00860
00861
00862 int msiDigestMonStat(msParam_t *cpu_wght, msParam_t *mem_wght, msParam_t *swap_wght, msParam_t *runq_wght,
00863 msParam_t *disk_wght, msParam_t *netin_wght, msParam_t *netout_wght,
00864 ruleExecInfo_t *rei) {
00865 char rescList[MAX_NSERVERS][MAX_NAME_LEN], *tResult,
00866 timeList[MAX_NSERVERS][MAX_NAME_LEN];
00867 char condStr1[MAX_NAME_LEN], condStr2[MAX_NAME_LEN], loadStr[MAX_NAME_LEN];
00868 int i, j, loadFactor, nresc, rc, status, totalWeight, weight[NRESULT];
00869 rsComm_t *rsComm;
00870 generalRowInsertInp_t generalRowInsertInp;
00871 genQueryInp_t genQueryInp;
00872 genQueryOut_t *genQueryOut = NULL;
00873
00874 RE_TEST_MACRO (" Calling msiDigestMonStat");
00875
00876 if (rei == NULL || rei->rsComm == NULL) {
00877 rodsLog (LOG_ERROR,
00878 "msiDigestMonStat: input rei or rsComm is NULL");
00879 return (SYS_INTERNAL_NULL_INPUT_ERR);
00880 }
00881
00882 rsComm = rei->rsComm;
00883
00884 if ( cpu_wght == NULL || mem_wght == NULL || swap_wght == NULL || runq_wght == NULL
00885 || disk_wght == NULL || netin_wght == NULL || netout_wght == NULL ) {
00886 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00887 "msiDigestMonStat: at least one of the input param is NULL");
00888 return (rei->status);
00889 }
00890
00891 if ( strcmp (cpu_wght->type, STR_MS_T) == 0 ) {
00892 weight[0] = atoi((const char*)cpu_wght->inOutStruct);
00893 } else {
00894 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00895 "msiDigestMonStat: Unsupported input cpu_wght type %s",
00896 cpu_wght->type);
00897 return (rei->status);
00898 }
00899
00900 if ( strcmp (mem_wght->type, STR_MS_T) == 0 ) {
00901 weight[1] = atoi((const char*)mem_wght->inOutStruct);
00902 } else {
00903 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00904 "msiDigestMonStat: Unsupported input mem_wght type %s",
00905 mem_wght->type);
00906 return (rei->status);
00907 }
00908
00909 if ( strcmp (swap_wght->type, STR_MS_T) == 0 ) {
00910 weight[2] = atoi((const char*)swap_wght->inOutStruct);
00911 } else {
00912 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00913 "msiDigestMonStat: Unsupported input swap_wght type %s",
00914 swap_wght->type);
00915 return (rei->status);
00916 }
00917
00918 if ( strcmp (runq_wght->type, STR_MS_T) == 0 ) {
00919 weight[3] = atoi((const char*)runq_wght->inOutStruct);
00920 } else {
00921 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00922 "msiDigestMonStat: Unsupported input runq_wght type %s",
00923 runq_wght->type);
00924 return (rei->status);
00925 }
00926
00927 if ( strcmp (disk_wght->type, STR_MS_T) == 0 ) {
00928 weight[4] = atoi((const char*)disk_wght->inOutStruct);
00929 } else {
00930 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00931 "msiDigestMonStat: Unsupported input disk_wght type %s",
00932 disk_wght->type);
00933 return (rei->status);
00934 }
00935
00936 if ( strcmp (netin_wght->type, STR_MS_T) == 0 ) {
00937 weight[5] = atoi((const char*)netin_wght->inOutStruct);
00938 } else {
00939 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00940 "msiDigestMonStat: Unsupported input netin_wght type %s",
00941 netin_wght->type);
00942 return (rei->status);
00943 }
00944
00945 if ( strcmp (netout_wght->type, STR_MS_T) == 0 ) {
00946 weight[6] = atoi((const char*)netout_wght->inOutStruct);
00947 } else {
00948 rodsLogAndErrorMsg (LOG_ERROR, &rsComm->rError, rei->status,
00949 "msiDigestMonStat: Unsupported input netout_wght type %s",
00950 netout_wght->type);
00951 return (rei->status);
00952 }
00953
00954 totalWeight = 0;
00955 for (i=0; i<NRESULT; i++){
00956 totalWeight += weight[i];
00957 }
00958
00959 memset(&genQueryInp, 0, sizeof (genQueryInp));
00960 addInxIval(&genQueryInp.selectInp, COL_SL_RESC_NAME, 1);
00961 addInxIval(&genQueryInp.selectInp, COL_SL_CREATE_TIME, SELECT_MAX);
00962 genQueryInp.maxRows = MAX_SQL_ROWS;
00963 status = rsGenQuery(rsComm, &genQueryInp, &genQueryOut);
00964 if( NULL == genQueryOut ) {
00965 rodsLog( LOG_ERROR, "msiDigestMonStat :: &genQueryOut is NULL" );
00966 return rei->status;
00967 }
00968 if ( status == 0 ) {
00969 nresc = genQueryOut->rowCnt;
00970 for (i=0; i<genQueryOut->attriCnt; i++) {
00971 for (j=0; j<nresc; j++) {
00972 tResult = genQueryOut->sqlResult[i].value;
00973 tResult += j*genQueryOut->sqlResult[i].len;
00974 if ( i == 0 ) {
00975 rstrcpy(rescList[j], tResult, genQueryOut->sqlResult[i].len);
00976 }
00977 if ( i == 1 ) {
00978 rstrcpy(timeList[j], tResult, genQueryOut->sqlResult[i].len);
00979 }
00980 }
00981 }
00982 }
00983 else {
00984 rodsLog(LOG_ERROR, "msiDigestMonStat: Unable to retrieve information \
00985 from R_SERVER_LOAD");
00986 return(rei->status);
00987 }
00988
00989 memset(&genQueryInp, 0, sizeof (genQueryInp));
00990 addInxIval(&genQueryInp.selectInp, COL_SL_CPU_USED, 1);
00991 addInxIval(&genQueryInp.selectInp, COL_SL_MEM_USED, 1);
00992 addInxIval(&genQueryInp.selectInp, COL_SL_SWAP_USED, 1);
00993 addInxIval(&genQueryInp.selectInp, COL_SL_RUNQ_LOAD, 1);
00994 addInxIval(&genQueryInp.selectInp, COL_SL_DISK_SPACE, 1);
00995 addInxIval(&genQueryInp.selectInp, COL_SL_NET_INPUT, 1);
00996 addInxIval(&genQueryInp.selectInp, COL_SL_NET_OUTPUT, 1);
00997 genQueryInp.maxRows = 1;
00998 generalRowInsertInp.tableName = "serverloaddigest";
00999 for (i=0; i<nresc; i++) {
01000 memset(&genQueryInp.sqlCondInp, 0, sizeof (genQueryInp.sqlCondInp));
01001 snprintf(condStr1, MAX_NAME_LEN, "= '%s'", rescList[i]);
01002 addInxVal(&genQueryInp.sqlCondInp, COL_SL_RESC_NAME, condStr1);
01003 snprintf(condStr2, MAX_NAME_LEN, "= '%s'", timeList[i]);
01004 addInxVal(&genQueryInp.sqlCondInp, COL_SL_CREATE_TIME, condStr2);
01005 status = rsGenQuery(rsComm, &genQueryInp, &genQueryOut);
01006 if ( status == 0 ) {
01007 loadFactor = 0;
01008 for (j=0; j<genQueryOut->attriCnt; j++) {
01009 tResult = genQueryOut->sqlResult[j].value;
01010 loadFactor += atoi(tResult) * weight[j];
01011 }
01012 loadFactor = loadFactor / totalWeight;
01013 generalRowInsertInp.arg1 = rescList[i];
01014 snprintf(loadStr, MAX_NAME_LEN, "%i", loadFactor);
01015 generalRowInsertInp.arg2 = loadStr;
01016 rc = rsGeneralRowInsert(rsComm, &generalRowInsertInp);
01017 if ( rc != 0 ) {
01018 rodsLog(LOG_ERROR, "msiDigestMonStat: Unable to ingest\
01019 information into from R_SERVER_LOAD_DIGEST table");
01020 }
01021 }
01022 }
01023
01024 clearGenQueryInp(&genQueryInp);
01025 freeGenQueryOut(&genQueryOut);
01026
01027 return(rei->status);
01028 }