Exemplo n.º 1
0
    def _initMonitoring(cls, serviceName, fullUrl):
        """
      Initialize the monitoring specific to this handler
      This has to be called only by :py:meth:`.__initializeService`
      to ensure thread safety and unicity of the call.

      :param serviceName: relative URL ``/<System>/<Component>``
      :param fullUrl: full URl like ``https://<host>:<port>/<System>/<Component>``
    """

        # Init extra bits of monitoring

        cls._monitor = MonitoringClient()
        cls._monitor.setComponentType(MonitoringClient.COMPONENT_WEB)

        cls._monitor.initialize()

        if tornado.process.task_id() is None:  # Single process mode
            cls._monitor.setComponentName('Tornado/%s' % serviceName)
        else:
            cls._monitor.setComponentName(
                'Tornado/CPU%d/%s' % (tornado.process.task_id(), serviceName))

        cls._monitor.setComponentLocation(fullUrl)

        cls._monitor.registerActivity("Queries", "Queries served", "Framework",
                                      "queries", MonitoringClient.OP_RATE)

        cls._monitor.setComponentExtraParam('DIRACVersion', DIRAC.version)
        cls._monitor.setComponentExtraParam('platform', DIRAC.getPlatform())
        cls._monitor.setComponentExtraParam('startTime', datetime.utcnow())

        cls._stats = {'requests': 0, 'monitorLastStatsUpdate': time.time()}

        return S_OK()
Exemplo n.º 2
0
    def __init__(self, serviceData):
        """
      Init the variables for the service

      :param serviceData: dict with modName, standalone, loadName, moduleObj, classObj. e.g.:
        {'modName': 'Framework/serviceName',
        'standalone': True,
        'loadName': 'Framework/serviceName',
        'moduleObj': <module 'serviceNameHandler' from '/home/DIRAC/FrameworkSystem/Service/serviceNameHandler.pyo'>,
        'classObj': <class 'serviceNameHandler.serviceHandler'>}

        Standalone is true if there is only one service started
        If it's false, every service is linked to a different MonitoringClient
    """
        self._svcData = serviceData
        self._name = serviceData['modName']
        self._startTime = Time.dateTime()
        self._validNames = [serviceData['modName']]
        if serviceData['loadName'] not in self._validNames:
            self._validNames.append(serviceData['loadName'])
        self._cfg = ServiceConfiguration(list(self._validNames))
        if serviceData['standalone']:
            self._monitor = gMonitor
        else:
            self._monitor = MonitoringClient()
        self.__monitorLastStatsUpdate = time.time()
        self._stats = {'queries': 0, 'connections': 0}
        self._authMgr = AuthManager(
            "%s/Authorization" %
            PathFinder.getServiceSection(serviceData['loadName']))
        self._transportPool = getGlobalTransportPool()
        self.__cloneId = 0
        self.__maxFD = 0
Exemplo n.º 3
0
 def __init__( self, serviceName ):
   self._name = serviceName
   self._startTime = Time.dateTime()
   self._cfg = ServiceConfiguration( serviceName )
   self._validNames = [ self._name ]
   self._monitor = MonitoringClient()
   self.__monitorLastStatsUpdate = time.time()
   self._stats = { 'queries' : 0, 'connections' : 0 }
   self._authMgr = AuthManager( "%s/Authorization" % self._cfg.getServicePath() )
   self._transportPool = getGlobalTransportPool()
   self.__cloneId = 0
Exemplo n.º 4
0
    def __init__(self, services=None, port=None):
        """

    :param list services: (default None) List of service handlers to load. If ``None``, loads all
    :param int port: Port to listen to. If None, the port is resolved following the logic
       described in the class documentation
    """

        if port is None:
            port = gConfig.getValue(
                "/Systems/Tornado/%s/Port" %
                PathFinder.getSystemInstance('Tornado'), 8443)

        if services and not isinstance(services, list):
            services = [services]

        # URLs for services.
        # Contains Tornado :py:class:`tornado.web.url` object
        self.urls = []
        # Other infos
        self.port = port
        self.handlerManager = HandlerManager()

        # Monitoring attributes

        self._monitor = MonitoringClient()
        # temp value for computation, used by the monitoring
        self.__report = None
        # Last update time stamp
        self.__monitorLastStatsUpdate = None
        self.__monitoringLoopDelay = 60  # In secs

        # If services are defined, load only these ones (useful for debug purpose or specific services)
        if services:
            retVal = self.handlerManager.loadHandlersByServiceName(services)
            if not retVal['OK']:
                sLog.error(retVal['Message'])
                raise ImportError(
                    "Some services can't be loaded, check the service names and configuration."
                )

        # if no service list is given, load services from configuration
        handlerDict = self.handlerManager.getHandlersDict()
        for item in handlerDict.items():
            # handlerDict[key].initializeService(key)
            self.urls.append(url(item[0], item[1]))
        # If there is no services loaded:
        if not self.urls:
            raise ImportError(
                "There is no services loaded, please check your configuration")
Exemplo n.º 5
0
 def __init__( self, serviceData ):
   self._svcData = serviceData
   self._name = serviceData[ 'loadName' ]
   self._startTime = Time.dateTime()
   self._validNames = [ serviceData[ 'modName' ]  ]
   if serviceData[ 'loadName' ] not in self._validNames:
     self._validNames.append( serviceData[ 'loadName' ] )
   self._cfg = ServiceConfiguration( list( self._validNames ) )
   if serviceData[ 'standalone' ]:
     self._monitor = gMonitor
   else:
     self._monitor = MonitoringClient()
   self.__monitorLastStatsUpdate = time.time()
   self._stats = { 'queries' : 0, 'connections' : 0 }
   self._authMgr = AuthManager( "%s/Authorization" % PathFinder.getServiceSection( serviceData[ 'loadName' ] ) )
   self._transportPool = getGlobalTransportPool()
   self.__cloneId = 0
   self.__maxFD = 0
Exemplo n.º 6
0
 def __initializeMonitor(self):
     """
 Initialize the system monitoring.
 """
     # This flag is used to activate ES based monitoring
     # if the "EnableActivityMonitoring" flag in "yes" or "true" in the cfg file.
     self.activityMonitoring = (
         Operations().getValue("EnableActivityMonitoring", False)
         or self.am_getOption("EnableActivityMonitoring", False))
     if self.activityMonitoring:
         # The import needs to be here because of the CS must be initialized before importing
         # this class (see https://github.com/DIRACGrid/DIRAC/issues/4793)
         from DIRAC.MonitoringSystem.Client.MonitoringReporter import MonitoringReporter
         self.activityMonitoringReporter = MonitoringReporter(
             monitoringType="ComponentMonitoring")
         # With the help of this periodic task we commit the data to ES at an interval of 100 seconds.
         gThreadScheduler.addPeriodicTask(
             100, self.__activityMonitoringReporting)
     else:
         if self.__moduleProperties['standalone']:
             self.monitor = gMonitor
         else:
             self.monitor = MonitoringClient()
         self.monitor.setComponentType(self.monitor.COMPONENT_AGENT)
         self.monitor.setComponentName(self.__moduleProperties['fullName'])
         self.monitor.initialize()
         self.monitor.registerActivity('CPU', "CPU Usage", 'Framework',
                                       "CPU,%", self.monitor.OP_MEAN, 600)
         self.monitor.registerActivity('MEM', "Memory Usage", 'Framework',
                                       'Memory,MB', self.monitor.OP_MEAN,
                                       600)
         # Component monitor
         for field in ('version', 'DIRACVersion', 'description',
                       'platform'):
             self.monitor.setComponentExtraParam(
                 field, self.__codeProperties[field])
         self.monitor.setComponentExtraParam('startTime', Time.dateTime())
         self.monitor.setComponentExtraParam('cycles', 0)
         self.monitor.disable()
         self.__monitorLastStatsUpdate = time.time()
Exemplo n.º 7
0
 def __initializeMonitor(self):
     """
 Initialize the system monitor client
 """
     if self.__moduleProperties['standalone']:
         self.monitor = gMonitor
     else:
         self.monitor = MonitoringClient()
     self.monitor.setComponentType(self.monitor.COMPONENT_AGENT)
     self.monitor.setComponentName(self.__moduleProperties['fullName'])
     self.monitor.initialize()
     self.monitor.registerActivity('CPU', "CPU Usage", 'Framework', "CPU,%",
                                   self.monitor.OP_MEAN, 600)
     self.monitor.registerActivity('MEM', "Memory Usage", 'Framework',
                                   'Memory,MB', self.monitor.OP_MEAN, 600)
     #Component monitor
     for field in ('version', 'DIRACVersion', 'description', 'platform'):
         self.monitor.setComponentExtraParam(field,
                                             self.__codeProperties[field])
     self.monitor.setComponentExtraParam('startTime', Time.dateTime())
     self.monitor.setComponentExtraParam('cycles', 0)
     self.monitor.disable()
     self.__monitorLastStatsUpdate = time.time()
Exemplo n.º 8
0
    def initialize(self):
        # Build the URLs
        self._url = self._cfg.getURL()
        if not self._url:
            return S_ERROR("Could not build service URL for %s" % self._name)
        gLogger.verbose("Service URL is %s" % self._url)
        # Load handler
        result = self._loadHandlerInit()
        if not result["OK"]:
            return result
        self._handler = result["Value"]
        # Initialize lock manager
        self._lockManager = LockManager(self._cfg.getMaxWaitingPetitions())
        self._threadPool = ThreadPoolExecutor(max(0,
                                                  self._cfg.getMaxThreads()))
        self._msgBroker = MessageBroker("%sMSB" % self._name,
                                        threadPool=self._threadPool)
        # Create static dict
        self._serviceInfoDict = {
            "serviceName":
            self._name,
            "serviceSectionPath":
            PathFinder.getServiceSection(self._name),
            "URL":
            self._cfg.getURL(),
            "messageSender":
            MessageSender(self._name, self._msgBroker),
            "validNames":
            self._validNames,
            "csPaths": [
                PathFinder.getServiceSection(svcName)
                for svcName in self._validNames
            ],
        }
        self.securityLogging = Operations().getValue(
            "EnableSecurityLogging", True) and getServiceOption(
                self._serviceInfoDict, "EnableSecurityLogging", True)
        # Initialize Monitoring
        # This is a flag used to check whether "EnableActivityMonitoring" is enabled or not from the config file.
        self.activityMonitoring = Operations().getValue(
            "EnableActivityMonitoring", False) or getServiceOption(
                self._serviceInfoDict, "EnableActivityMonitoring", False)
        if self.activityMonitoring:
            # The import needs to be here because of the CS must be initialized before importing
            # this class (see https://github.com/DIRACGrid/DIRAC/issues/4793)
            from DIRAC.MonitoringSystem.Client.MonitoringReporter import MonitoringReporter

            self.activityMonitoringReporter = MonitoringReporter(
                monitoringType="ComponentMonitoring")
            gThreadScheduler.addPeriodicTask(
                100, self.__activityMonitoringReporting)
        elif self._standalone:
            self._monitor = gMonitor
        else:
            self._monitor = MonitoringClient()
        self._initMonitoring()
        # Call static initialization function
        try:
            if self.activityMonitoring:
                self._handler["class"]._rh__initializeClass(
                    dict(self._serviceInfoDict), self._lockManager,
                    self._msgBroker, self.activityMonitoringReporter)
            else:
                self._handler["class"]._rh__initializeClass(
                    dict(self._serviceInfoDict), self._lockManager,
                    self._msgBroker, self._monitor)
            if self._handler["init"]:
                for initFunc in self._handler["init"]:
                    gLogger.verbose("Executing initialization function")
                    try:
                        result = initFunc(dict(self._serviceInfoDict))
                    except Exception as excp:
                        gLogger.exception(
                            "Exception while calling initialization function",
                            lException=excp)
                        return S_ERROR(
                            "Exception while calling initialization function: %s"
                            % str(excp))
                    if not isReturnStructure(result):
                        return S_ERROR(
                            "Service initialization function %s must return S_OK/S_ERROR"
                            % initFunc)
                    if not result["OK"]:
                        return S_ERROR("Error while initializing %s: %s" %
                                       (self._name, result["Message"]))
        except Exception as e:
            errMsg = "Exception while initializing %s" % self._name
            gLogger.exception(e)
            gLogger.exception(errMsg)
            return S_ERROR(errMsg)

        # Load actions after the handler has initialized itself
        result = self._loadActions()
        if not result["OK"]:
            return result
        self._actions = result["Value"]

        if not self.activityMonitoring:
            gThreadScheduler.addPeriodicTask(30,
                                             self.__reportThreadPoolContents)

        return S_OK()