| # Gaudi Framework, version v21r7 |

Home |  | Generated: 22 Jan 2010

# ApMon.cpp File Reference

This file contains the implementations of the methods from the ApMon class. More...

`#include "ApMon.h"`
`#include "utils.h"`
`#include "proc_utils.h"`
`#include "monitor_utils.h"`
`#include <math.h>`

Include dependency graph for ApMon.cpp:

Go to the source code of this file.

## Defines
#define | RECHECK_CONF 0
#define | SYS_INFO_SEND 1
#define | JOB_INFO_SEND 2
## Functions
void * | bkTask (void *param)
| Performs background actions like rechecking the configuration file and the URLs and sending monitoring information.
## Variables
char | boolStrings [][10] = {"false", "true"}

## Detailed Description

This file contains the implementations of the methods from the ApMon class.

Definition in file ApMon.cpp.

## Define Documentation

#define JOB_INFO_SEND 2

Definition at line 50 of file ApMon.cpp.

#define RECHECK_CONF 0

Definition at line 48 of file ApMon.cpp.

#define SYS_INFO_SEND 1

Definition at line 49 of file ApMon.cpp.

## Function Documentation

void* bkTask | ( | void * | param | ) |

Performs background actions like rechecking the configuration file and the URLs and sending monitoring information.

This function is executed in a background thread and has two roles: it automatically sends the system/job monitoring parameters (if the user requested) and it checks the configuration file/URLs for changes.

(this is done in a separate thread).

Definition at line 913 of file ApMon.cpp.

    00913                           {
    00914 #else
    00915 DWORD WINAPI bkTask(void *param) {
    00916 #endif
    00917   struct stat st;
    00918 #ifndef WIN32
    00919   struct timespec delay;
    00920 #else
    00921   DWORD delay;
    00922 #endif
    00923   bool resourceChanged, haveChange;
    00924   int nextOp = -1, i, ret;
    00925   int generalInfoCount;
    00926   time_t crtTime, timeRemained;
    00927   time_t nextRecheck = 0, nextJobInfoSend = 0, nextSysInfoSend = 0;
    00928   ApMon *apm = (ApMon *)param;
    00929   char logmsg[200];
    00930
    00931   logger(INFO, "[Starting background thread...]");
    00932   apm -> bkThreadStarted = true;
    00933
    00934   crtTime = time(NULL);
    00935
    00936   pthread_mutex_lock(&(apm -> mutexBack));
    00937   if (apm -> confCheck) {
    00938     nextRecheck = crtTime + apm -> crtRecheckInterval;
    00939     //sprintf(logmsg, "###1 crt %ld interv %ld recheck %ld ", crtTime,
    00940     //   apm -> crtRecheckInterval, nextRecheck);
    00941     //logger(FINE, logmsg);
    00942     //fflush(stdout);
    00943   }
    00944   if (apm -> jobMonitoring)
    00945     nextJobInfoSend = crtTime + apm -> jobMonitorInterval;
    00946   if (apm -> sysMonitoring)
    00947     nextSysInfoSend = crtTime + apm -> sysMonitorInterval;
    00948   pthread_mutex_unlock(&(apm -> mutexBack));
    00949
    00950   timeRemained = -1;
    00951   generalInfoCount = 0;
    00952
    00953   while (1) {
    00954     pthread_mutex_lock(&apm -> mutexBack);
    00955     if (apm -> stopBkThread) {
    00956 //      printf("### stopBkThread \n");
    00957       pthread_mutex_unlock(&apm -> mutexBack);
    00958       break;
    00959     }
    00960     pthread_mutex_unlock(&apm -> mutexBack);
    00961
    00962     //sprintf(logmsg, "### 2 recheck %ld sys %ld ", nextRecheck,
    00963     //    nextSysInfoSend);
    00964     //logger(FINE, logmsg);
    00965
    00966     /* determine the next operation that must be performed */
    00967     if (nextRecheck > 0 && (nextJobInfoSend <= 0 ||
    00968                             nextRecheck <= nextJobInfoSend)) {
    00969       if (nextSysInfoSend <= 0 || nextRecheck <= nextSysInfoSend) {
    00970         nextOp = RECHECK_CONF;
    00971         timeRemained = nextRecheck - crtTime;
    00972       } else {
    00973         nextOp = SYS_INFO_SEND;
    00974         timeRemained = nextSysInfoSend - crtTime;
    00975       }
    00976     } else {
    00977       if (nextJobInfoSend > 0 && (nextSysInfoSend <= 0 ||
    00978                                   nextJobInfoSend <= nextSysInfoSend)) {
    00979         nextOp = JOB_INFO_SEND;
    00980         timeRemained = nextJobInfoSend - crtTime;
    00981       } else if (nextSysInfoSend > 0) {
    00982         nextOp = SYS_INFO_SEND;
    00983         timeRemained = nextSysInfoSend - crtTime;
    00984       }
    00985     }
    00986
    00987     if (timeRemained == -1)
    00988       timeRemained = RECHECK_INTERVAL;
    00989
    00990 #ifndef WIN32
    00991     /* the moment when the next operation should be performed */
    00992     delay.tv_sec = crtTime + timeRemained;
    00993     delay.tv_nsec = 0;
    00994 #else
    00995     delay = (/*crtTime +*/ timeRemained) * 1000;  // this is in millis
    00996 #endif
    00997
    00998     pthread_mutex_lock(&(apm -> mutexBack));
    00999
    01000     pthread_mutex_lock(&(apm -> mutexCond));
    01001     /* check for changes in the settings */
    01002     haveChange = false;
    01003     if (apm -> jobMonChanged || apm -> sysMonChanged || apm -> recheckChanged)
    01004       haveChange = true;
    01005     if (apm -> jobMonChanged) {
    01006       if (apm -> jobMonitoring)
    01007         nextJobInfoSend = crtTime + apm -> jobMonitorInterval;
    01008       else
    01009         nextJobInfoSend = -1;
    01010       apm -> jobMonChanged = false;
    01011     }
    01012     if (apm -> sysMonChanged) {
    01013       if (apm -> sysMonitoring)
    01014         nextSysInfoSend = crtTime + apm -> sysMonitorInterval;
    01015       else
    01016         nextSysInfoSend = -1;
    01017       apm -> sysMonChanged = false;
    01018     }
    01019     if (apm -> recheckChanged) {
    01020       if (apm -> confCheck) {
    01021         nextRecheck = crtTime + apm -> crtRecheckInterval;
    01022       }
    01023       else
    01024         nextRecheck = -1;
    01025       apm -> recheckChanged = false;
    01026     }
    01027     pthread_mutex_unlock(&(apm -> mutexBack));
    01028
    01029     if (haveChange) {
    01030       pthread_mutex_unlock(&(apm -> mutexCond));
    01031       continue;
    01032     }
    01033
    01034     /* wait until the next operation should be performed or until
    01035        a change in the settings occurs */
    01036 #ifndef WIN32
    01037     ret = pthread_cond_timedwait(&(apm -> confChangedCond),
    01038                                 &(apm -> mutexCond), &delay);
    01039     pthread_mutex_unlock(&(apm -> mutexCond));
    01040 #else
    01041     pthread_mutex_unlock(&(apm -> mutexCond));
    01042     ret = WaitForSingleObject(apm->confChangedCond, delay);
    01043 #endif
    01044     if (ret == ETIMEDOUT) {
    01045 //      printf("### ret TIMEDOUT\n");
    01046       /* now perform the operation */
    01047       if (nextOp == JOB_INFO_SEND) {
    01048         apm -> sendJobInfo();
    01049         crtTime = time(NULL);
    01050         nextJobInfoSend = crtTime + apm -> getJobMonitorInterval();
    01051       }
    01052
    01053       if (nextOp == SYS_INFO_SEND) {
    01054         apm -> sendSysInfo();
    01055         if (apm -> getGenMonitoring()) {
    01056           if (generalInfoCount <= 1)
    01057             apm -> sendGeneralInfo();
    01058           generalInfoCount = (generalInfoCount + 1) % apm -> genMonitorIntervals;
    01059         }
    01060         crtTime = time(NULL);
    01061         nextSysInfoSend = crtTime + apm -> getSysMonitorInterval();
    01062       }
    01063
    01064       if (nextOp == RECHECK_CONF) {
    01065         //logger(FINE, "### recheck conf");
    01066         resourceChanged = false;
    01067         try {
    01068           if (apm -> initType == FILE_INIT) {
    01069             sprintf(logmsg, "Checking for modifications for file %s ",
    01070                     apm -> initSources[0]);
    01071             logger(INFO, logmsg);
    01072             stat(apm -> initSources[0], &st);
    01073             if (st.st_mtime > apm -> lastModifFile) {
    01074               sprintf(logmsg, "File %s modified ", apm -> initSources[0]);
    01075               logger(INFO, logmsg);
    01076               resourceChanged = true;
    01077             }
    01078           }
    01079
    01080           // check the configuration URLs
    01081           for (i = 0; i < apm -> confURLs.nConfURLs; i++) {
    01082             sprintf(logmsg, "[Checking for modifications for URL %s ] ",
    01083                    apm -> confURLs.vURLs[i]);
    01084             logger(INFO, logmsg);
    01085             if (urlModified(apm -> confURLs.vURLs[i], apm -> confURLs.lastModifURLs[i])) {
    01086               sprintf(logmsg, "URL %s modified ", apm -> confURLs.vURLs[i]);
    01087               logger(INFO, logmsg);
    01088               resourceChanged = true;
    01089               break;
    01090             }
    01091           }
    01092
    01093           if (resourceChanged) {
    01094             logger(INFO, "Reloading configuration...");
    01095             if (apm -> initType == FILE_INIT)
    01096               apm -> initialize(apm -> initSources[0], false);
    01097             else
    01098               apm -> initialize(apm -> nInitSources, apm -> initSources, false);
    01099           }
    01100           apm -> setCrtRecheckInterval(apm -> getRecheckInterval());
    01101         } catch (runtime_error &err) {
    01102           logger(WARNING, err.what());
    01103           logger(WARNING, "Increasing the time interval for reloading the configuration...");
    01104           apm -> setCrtRecheckInterval(apm -> getRecheckInterval() * 5);
    01105         }
    01106         crtTime = time(NULL);
    01107         nextRecheck = crtTime + apm -> getCrtRecheckInterval();
    01108         //sleep(apm -> getCrtRecheckInterval());
    01109       }
    01110     }
    01111
    01112   } // while
    01113
    01114 #ifndef WIN32
    01115   return NULL; // it doesn't matter what we return here
    01116 #else
    01117   return 0;
    01118 #endif
    01119 }

## Variable Documentation

char boolStrings[][10] = {"false", "true"}

Definition at line 52 of file ApMon.cpp.