def do_compute(call_no, mode='sleep', runtime=None, n=None):
    started = utcnow()
    process_id = os.getpid()
    thread_id = _thread.get_ident()

    if mode == 'fib':
        res = fib(n)
    elif mode == 'sleep':
        # yes, we do the evil blocking thing here!
        # this is to simulate CPU intensive stuff
        time.sleep(runtime)
        res = None
    else:
        res = random.random()

    ended = utcnow()

    result = {
        'call_no': call_no,
        'started': started,
        'ended': ended,
        'process': process_id,
        'thread': thread_id,
        'result': res
    }
    return result
Example #2
0
   def onConnect(self, connectionRequest):
      protocol, headers = WampCraServerProtocol.onConnect(self, connectionRequest)
      #pprint(connectionRequest.headers)

      ua = connectionRequest.headers.get('user-agent', None)
      origin = connectionRequest.headers.get('origin', None)

      ## Crossbar.io Tracking ID
      ##
      cbtid = None
      if connectionRequest.headers.has_key('cookie'):
         try:
            cookie = Cookie.SimpleCookie()
            cookie.load(str(connectionRequest.headers['cookie']))
         except Cookie.CookieError:
            pass
         else:
            if cookie.has_key('cbtid'):
               _cbtid = cookie['cbtid'].value
               if self.factory.trackingCookies.has_key(_cbtid):
                  cbtid = _cbtid
                  #log.msg("Crossbar.io tracking ID already in cookie: %s" % cbtid)

      if cbtid is None:

         cbtid = newid()
         maxAge = 86400

         cbtData = {'created': utcnow(),
                    'maxAge': maxAge,
                    'sessions': []}

         self.factory.trackingCookies[cbtid] = cbtData

         ## do NOT add the "secure" cookie attribute! "secure" refers to the
         ## scheme of the Web page that triggered the WS, not WS itself!!
         ##
         headers['Set-Cookie'] = 'cbtid=%s;max-age=%d' % (cbtid, maxAge)
         #log.msg("Setting new Crossbar.io tracking ID in cookie: %s" % cbtid)

      self._cbtid = cbtid
      cbSessionData = {'cbtid': cbtid,
                       'ua': ua,
                       'origin': origin,
                       'connected': utcnow()}

      i = len(self.factory.trackingCookies[cbtid]['sessions'])

      self.factory.trackingCookies[cbtid]['sessions'].append(cbSessionData)
      self._cbSession = self.factory.trackingCookies[cbtid]['sessions'][i]

      return (protocol, headers)
Example #3
0
    def onConnect(self, request):
        clients.add(self)
        protocol, headers = None, {}
        self._cbtid = None

        if "cookie" in request.headers:
            try:
                cookie = http.cookies.SimpleCookie()
                cookie.load(str(request.headers["cookie"]))
            except http.cookies.CookieError:
                pass
            else:
                if "cbtid" in cookie:
                    cbtid = cookie["cbtid"].value
                    if cbtid in self.factory._cookies:
                        self._cbtid = cbtid
                        print("Cookie already set: %s" % self._cbtid)

        if self._cbtid is None:

            self._cbtid = newid()
            maxAge = self.factory.session_max_age
            user_index = r.incr("user_index")
            r.hmset(self._cbtid,{"login":"******" % user_index})
            cbtData = {"created": utcnow(),
                       "maxAge": maxAge,
                       "connections": set() }

            self.factory._cookies[self._cbtid] = cbtData
            headers["Set-Cookie"] = "cbtid=%s;max-age=%d" % (self._cbtid, maxAge)
            print("Setting new cookie: %s" % self._cbtid)
        self.factory._cookies[self._cbtid]["connections"].add(self)
        return (protocol, headers)
Example #4
0
 def __init__(self, service = None):
    self.service = service
    self.current = {"timestamp": utcnow(),
                    "packets-in": 0,
                    "bytes-in": 0,
                    "packets-out": 0,
                    "bytes-out": 0}
Example #5
0
    def _compute_challenge(self, user):
        """
        Returns: challenge, signature
        """
        challenge_obj = {
            'authid': self._authid,
            'authrole': self._authrole,
            'authmethod': self._authmethod,
            'authprovider': self._authprovider,
            'session': self._session_details['session'],
            'nonce': util.newid(64),
            'timestamp': util.utcnow()
        }
        challenge: str = json.dumps(challenge_obj, ensure_ascii=False)
        secret = user['secret'].encode('utf8')
        signature = auth.compute_wcs(secret,
                                     challenge.encode('utf8')).decode('ascii')

        # extra data to send to client in CHALLENGE
        extra = {'challenge': challenge}

        # when using salted passwords, provide the client with
        # the salt and then PBKDF2 parameters used
        if 'salt' in user:
            extra['salt'] = user['salt']
            extra['iterations'] = user.get('iterations', 1000)
            extra['keylen'] = user.get('keylen', 32)

        return extra, signature
Example #6
0
    def shutdown(self, restart=False, mode=None, details=None):
        """
        Stop this node.
        """
        if self._shutdown_requested:
            # we're already shutting down .. ignore ..
            return

        self._shutdown_requested = True

        self.log.warn("Shutting down node...")

        # publish management API event
        shutdown_info = {
            'restart': restart,
            'mode': mode,
            'who': details.caller if details else None,
            'when': utcnow()
        }
        yield self.publish('crossbar.node.{}.on_shutdown'.format(
            self._node_id),
                           shutdown_info,
                           options=PublishOptions(
                               exclude=[details.caller] if details else None,
                               acknowledge=True))

        def stop_reactor():
            try:
                self._reactor.stop()
            except ReactorNotRunning:
                pass

        self._reactor.callLater(0, stop_reactor)

        returnValue(shutdown_info)
Example #7
0
 def publish():
     self._heartbeat_time = utcnow()
     self._heartbeat += 1
     try:
         yield self._send_heartbeat()
     except:
         self.log.failure()
Example #8
0
    def poll(self):
        """
        Measure current stats value and return new stats.
        """
        Monitor.poll(self)

        # create new, empty event
        #
        current = {
            # the UTC timestamp when measurement was taken
            u'timestamp': utcnow(),

            # the effective last period in secods
            u'last_period': self._last_period,
        }

        # FIXME: add ratio of ram usage

        with open("/proc/meminfo") as f:
            res = f.read()
            new = res.split()
            new_clean = [x.replace(":", "") for x in new if x != 'kB']
            for i in range(0, len(new_clean), 2):
                k = u'{}'.format(new_clean[i])
                current[k] = int(new_clean[i + 1])

        self._last_value = current

        return succeed(self._last_value)
Example #9
0
    def poll(self):
        """
        Measure current stats value and return new stats.
        """
        Monitor.poll(self)

        # create new, empty event
        #
        current = {
            # the UTC timestamp when measurement was taken
            u'timestamp': utcnow(),

            # the effective last period in secods
            u'last_period': self._last_period,
        }

        # FIXME: add ratio of ram usage

        with open("/proc/meminfo") as f:
            res = f.read()
            new = res.split()
            new_clean = [x.replace(":", "") for x in new if x != 'kB']
            for i in range(0, len(new_clean), 2):
                k = u'{}'.format(new_clean[i])
                current[k] = int( new_clean[i + 1])

        self._last_value = current

        return succeed(self._last_value)
Example #10
0
    def create(self):
        """
      Create a new cookie, returning the cookie ID and cookie header value.
      """
        if self.debug:
            log.msg("CookieStore.create()")

        ## http://tools.ietf.org/html/rfc6265#page-20
        ## 0: delete cookie
        ## -1: preserve cookie until browser is closed

        id = util.newid(self._cookie_id_field_length)

        cbtData = {
            'created': util.utcnow(),
            'authid': None,
            'authrole': None,
            'authmethod': None,
            'max_age': self._cookie_max_age,
            'connections': set()
        }

        self._cookies[id] = cbtData

        ## do NOT add the "secure" cookie attribute! "secure" refers to the
        ## scheme of the Web page that triggered the WS, not WS itself!!
        ##
        return id, '%s=%s;max-age=%d' % (self._cookie_id_field, id,
                                         cbtData['max_age'])
Example #11
0
   def onAuthenticated(self, authKey, perms):
      """
      WAMP session authenticated. Register PubSub topics and RPC handlers.
      """
      self.authenticatedAs = authKey
      self.cookies = perms.get('cookies', None)

      self.sessionInfo.authenticatedAs = authKey

      self._cbSession['authenticated'] = utcnow()
      self._cbSession['href'] = perms.get('href', None)
      self._cbSession['referrer'] = perms.get('referrer', None)

      self.registerForPubSubFromPermissions(perms['permissions'])

      for remoter, key, method in [("restremoter", "rest", RestRemoter.remoteCall),
                                   ("extdirectremoter", "extdirect", ExtDirectRemoter.remoteCall),
                                   ("hanaremoter", "hana", HanaRemoter.remoteCall),
                                   ("pgremoter", "pg", PgRemoter.remoteCall),
                                   ("oraremoter", "ora", OraRemoter.remoteCall),
                                   ]:
         if self.factory.services.has_key(remoter):
            r = perms[key]
            for uri in r:
               self.registerHandlerMethodForRpc(uri,
                                                self.factory.services[remoter],
                                                method,
                                                r[uri])

      self.factory.dispatch("http://analytics.tavendo.de#enter", self._cbSession)
Example #12
0
    def __init__(self, uri, ordered=False, extra=None):
        """

        :param uri: The URI (or URI pattern) for this observation.
        :type uri: unicode
        """
        # URI (or URI pattern) this observation is created for
        self.uri = uri

        # flag indicating whether observers should be maintained
        # in an ordered set or a regular, unordered set
        self.ordered = ordered

        # arbitrary, opaque extra data attached to the observation
        self.extra = extra

        # generate a new ID for the observation
        self.id = util.id()

        # UTC timestamp this observation was created
        self.created = util.utcnow()

        # set of observers
        if self.ordered:
            self.observers = OrderedSet()
        else:
            self.observers = set()
Example #13
0
    def __init__(self, uri, ordered=False, extra=None):
        """

        :param uri: The URI (or URI pattern) for this observation.
        :type uri: unicode
        """
        # URI (or URI pattern) this observation is created for
        self.uri = uri

        # flag indicating whether observers should be maintained
        # in an ordered set or a regular, unordered set
        self.ordered = ordered

        # arbitrary, opaque extra data attached to the observation
        self.extra = extra

        # generate a new ID for the observation
        self.id = util.id()

        # UTC timestamp this observation was created
        self.created = util.utcnow()

        # set of observers
        if self.ordered:
            self.observers = OrderedSet()
        else:
            self.observers = set()
Example #14
0
 def _isActivated(self, txn):
     if True:
         return {
             'license-id': '',
             'type': 'BETA',
             'connected-cap': 0,
             'tls-enabled': True,
             'valid-from': '1970-01-01T00:00:00:00Z',
             'valid-to': '2020-01-01T00:00:00:00Z'
         }
     else:
         now = utcnow()
         txn.execute(
             "SELECT license_id, license_type, connection_cap, tls_enabled, valid_from, valid_to FROM license WHERE enabled = 1 AND valid_from <= ? AND valid_to > ?",
             [now, now])
         res = txn.fetchone()
         if res:
             return {
                 'license-id': res[0],
                 'type': res[1],
                 'connected-cap': res[2],
                 'tls-enabled': True if res[3] != 0 else False,
                 'valid-from': res[4],
                 'valid-to': res[5]
             }
         else:
             return None
   def _login(self, txn, authResponse, stayLoggedIn):

      if self.authChallenge is None:
         raise Exception(URI_ERROR + "login-without-previous-request", "Login attempt without previous login request.")

      txn.execute("SELECT value FROM config WHERE key = ?", ['admin-password'])
      res = txn.fetchone()
      if res:
         pw = str(json_loads(res[0]))
         h = hmac.new(pw, self.authChallenge, hashlib.sha256)
         v = binascii.b2a_base64(h.digest()).strip()
         if v == str(authResponse):
            self.authUser = self.authChallengeUser
            if stayLoggedIn:
               cookie = ''.join([random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_") for i in xrange(64)])
               now = utcnow()
               txn.execute("INSERT INTO cookie (created, username, value) VALUES (?, ?, ?)", [now, "admin", cookie])
               self.authCookie = cookie
               res = cookie
            else:
               res = None
            self.onLogin()
            return res
         else:
            raise Exception(URI_ERROR + "login-failed", "Login failed.")
      else:
         raise Exception(URI_ERROR + "internal-error", "Could not retrieve admin password from database.")
Example #16
0
    def __init__(self, node):
        """
      :param node: The node singleton for this node controller session.
      :type node: obj
      """
        NativeProcessSession.__init__(self)
        #self.debug = node.debug
        self.debug = False
        self.debug_app = False

        ## associated node
        self._node = node
        self._node_id = node._node_id
        self._realm = node._realm

        self.cbdir = self._node._cbdir

        self._created = utcnow()
        self._pid = os.getpid()

        ## map of worker processes: worker_id -> NativeWorkerProcess
        self._workers = {}

        self._management_transport = None

        exit = Deferred()
Example #17
0
    def shutdown(self, restart=False, mode=None, details=None):
        """
        Explicitly stop this node.
        """
        if self._shutdown_requested:
            # we're already shutting down .. ignore ..
            return

        self._shutdown_requested = True

        self.log.info('Node shutdown requested ..')

        # publish management API event
        shutdown_info = {
            u'node_id': self._node._node_id,
            u'restart': restart,
            u'mode': mode,
            u'who': details.caller if details else None,
            u'when': utcnow()
        }
        yield self.publish(u'{}.on_shutdown'.format(self._uri_prefix),
                           shutdown_info,
                           options=PublishOptions(
                               exclude=details.caller if details else None,
                               acknowledge=True))

        def stop_reactor():
            try:
                self._reactor.stop()
            except ReactorNotRunning:
                pass

        self._reactor.callLater(0, stop_reactor)

        returnValue(shutdown_info)
Example #18
0
    def log(self, childFD, data):
        """
      FIXME: line buffering
      """
        assert (childFD in self._log_fds)

        for msg in data.split('\n'):
            msg = msg.strip()
            if msg != "":

                ## log entry used for buffered worker log and/or worker log events
                ##
                if self._log is not None or self._log_topic:
                    log_entry = (self._log_lineno, utcnow(), msg)

                ## maintain buffered worker log
                ##
                if self._log is not None:
                    self._log_lineno += 1
                    self._log.append(log_entry)
                    if self._keeplog > 0 and len(self._log) > self._keeplog:
                        self._log.popleft()

                ## publish worker log event
                ##
                if self._log_topic:
                    self._controller.publish(self._log_topic, log_entry)

                ## log to controller
                ##
                log.msg(msg,
                        system="{:<10} {:>6}".format(self.LOGNAME, self.pid),
                        override_system=True)
Example #19
0
   def onConnect(self, connectionRequest):
      if self.debug:
         log.msg("connection received from %s speaking WebSockets protocol %d - upgrade request for host '%s', path '%s', params %s, origin '%s', protocols %s, headers %s" % (connectionRequest.peerstr, connectionRequest.version, connectionRequest.host, connectionRequest.path, str(connectionRequest.params), connectionRequest.origin, str(connectionRequest.protocols), str(connectionRequest.headers)))

      if connectionRequest.params.has_key("agent"):
         if len(connectionRequest.params["agent"]) > 1:
            raise Exception("multiple agents specified")
         self.caseAgent = connectionRequest.params["agent"][0]
      else:
         #raise Exception("no agent specified")
         self.caseAgent = None

      if connectionRequest.params.has_key("case"):
         if len(connectionRequest.params["case"]) > 1:
            raise Exception("multiple test cases specified")
         try:
            self.case = int(connectionRequest.params["case"][0])
         except:
            raise Exception("invalid test case ID %s" % connectionRequest.params["case"][0])

      if self.case:
         if self.case >= 1 and self.case <= len(self.factory.specCases):
            self.Case = self.factory.CaseSet.CasesById[self.factory.specCases[self.case - 1]]
            if connectionRequest.path == "/runCase":
               self.runCase = self.Case(self)
         else:
            raise Exception("case %s not found" % self.case)

      if connectionRequest.path == "/runCase":
         if not self.runCase:
            raise Exception("need case to run")
         if not self.caseAgent:
            raise Exception("need agent to run case")
         self.caseStarted = utcnow()
         print "Running test case ID %s for agent %s from peer %s" % (self.factory.CaseSet.caseClasstoId(self.Case), self.caseAgent, connectionRequest.peerstr)

      elif connectionRequest.path == "/updateReports":
         if not self.caseAgent:
            raise Exception("need agent to update reports for")
         print "Updating reports, requested by peer %s" % connectionRequest.peerstr

      elif connectionRequest.path == "/getCaseInfo":
         if not self.Case:
            raise Exception("need case to get info")

      elif connectionRequest.path == "/getCaseStatus":
         if not self.Case:
            raise Exception("need case to get status")
         if not self.caseAgent:
            raise Exception("need agent to get status")

      elif connectionRequest.path == "/getCaseCount":
         pass

      else:
         print "Entering direct command mode for peer %s" % connectionRequest.peerstr

      self.path = connectionRequest.path

      return None
Example #20
0
    def create(self):
        """
        Create a new cookie, returning the cookie ID and cookie header value.
        """
        # http://tools.ietf.org/html/rfc6265#page-20
        # 0: delete cookie
        # -1: preserve cookie until browser is closed

        cbtid = util.newid(self._cookie_id_field_length)

        cbtData = {
            "created": util.utcnow(),
            "authid": None,
            "authrole": None,
            "authmethod": None,
            "max_age": self._cookie_max_age,
            "connections": set(),
        }

        self._cookies[cbtid] = cbtData

        self.log.debug("New cookie {cbtid} created", cbtid=cbtid)

        # do NOT add the "secure" cookie attribute! "secure" refers to the
        # scheme of the Web page that triggered the WS, not WS itself!!
        #
        return cbtid, "%s=%s;max-age=%d" % (self._cookie_id_field, cbtid, cbtData["max_age"])
Example #21
0
    def shutdown(self, restart=False, mode=None, details=None):
        """
        Stop this node.
        """
        if self._shutdown_requested:
            # we're already shutting down .. ignore ..
            return

        self._shutdown_requested = True

        self.log.warn("Shutting down node...")

        # publish management API event
        shutdown_info = {
            u'restart': restart,
            u'mode': mode,
            u'who': details.caller if details else None,
            u'when': utcnow()
        }
        yield self.publish(
            'crossbar.node.{}.on_shutdown'.format(self._node_id),
            shutdown_info,
            options=PublishOptions(exclude=details.caller if details else None, acknowledge=True)
        )

        def stop_reactor():
            try:
                self._reactor.stop()
            except ReactorNotRunning:
                pass

        self._reactor.callLater(0, stop_reactor)

        returnValue(shutdown_info)
    def _compute_challenge(self, user):
        """
        Returns: challenge, signature
        """
        challenge_obj = {
            u'authid': self._authid,
            u'authrole': self._authrole,
            u'authmethod': self._authmethod,
            u'authprovider': self._authprovider,
            u'session': self._session_details[u'session'],
            u'nonce': util.newid(64),
            u'timestamp': util.utcnow()
        }
        challenge = json.dumps(challenge_obj, ensure_ascii=False)

        # Sometimes, if it doesn't have to be Unicode, PyPy won't make it
        # Unicode. Make it Unicode, even if it's just ASCII.
        if not isinstance(challenge, six.text_type):
            challenge = challenge.decode('utf8')

        secret = user['secret'].encode('utf8')
        signature = auth.compute_wcs(secret,
                                     challenge.encode('utf8')).decode('ascii')

        # extra data to send to client in CHALLENGE
        extra = {u'challenge': challenge}

        # when using salted passwords, provide the client with
        # the salt and then PBKDF2 parameters used
        if 'salt' in user:
            extra[u'salt'] = user['salt']
            extra[u'iterations'] = user.get('iterations', 1000)
            extra[u'keylen'] = user.get('keylen', 32)

        return extra, signature
Example #23
0
    def __init__(self, session, authid, authrole, authprovider, secret):
        """
        :param session: The WAMP session ID of the session being authenticated.
        :type session: int
        :param authid: The authentication ID of the authenticating principal.
        :type authid: unicode
        :param authrole: The role under which the principal will be authenticated when
           the authentication succeeds.
        :type authrole: unicode
        :param authprovider: Optional authentication provider.
        :type authprovider: unicode or None
        :param secret: The secret of the principal being authenticated. Either a password
           or a salted password.
        :type secret: str
        """
        self.session = session
        self.authmethod = u"wampcra"
        self.authid = authid
        self.authrole = authrole
        self.authprovider = authprovider

        challenge_obj = {
            'authid': self.authid,
            'authrole': self.authrole,
            'authmethod': u'wampcra',
            'authprovider': self.authprovider,
            'session': self.session,
            'nonce': util.newid(),
            'timestamp': util.utcnow()
        }

        # challenge must be bytes
        self.challenge = json.dumps(challenge_obj,
                                    ensure_ascii=False).encode('utf8')
        self.signature = auth.compute_wcs(secret, self.challenge)
Example #24
0
 def publish():
     self._heartbeat_time = utcnow()
     self._heartbeat += 1
     try:
         yield self._send_heartbeat()
     except:
         self.log.failure()
Example #25
0
        def do(txn):
            data = json.dumps(spec,
                              ensure_ascii=False,
                              allow_nan=False,
                              separators=(',', ':'),
                              indent=None)

            now = utcnow()
            id = newid()

            txn.execute(
                "SELECT id, spec FROM testspec WHERE name = ? AND valid_to IS NULL",
                [name])
            res = txn.fetchone()
            op = None

            if res is not None:
                currId, currSpec = res
                if currSpec == data:
                    return (op, currId, name)
                else:
                    beforeId = currId
                    op = 'U'
                    txn.execute(
                        "UPDATE testspec SET valid_to = ? WHERE id = ?",
                        [now, currId])
            else:
                beforeId = None
                op = 'I'

            txn.execute(
                "INSERT INTO testspec (id, before_id, valid_from, name, desc, mode, caseset, spec) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
                [id, beforeId, now, name, desc, mode, caseset, data])
            return (op, id, name)
Example #26
0
    def lineReceived(self, line):
        if self._debug:
            print("Serial RX: {0}".format(line))

        pins = range(8)

        try:
            # parse data received from MCU
            ##
            data = [int(x) for x in line.split(',')]
        except ValueError:
            print('Unable to parse value {0}'.format(line))
        else:
            if not self._last:
                self._last = data
            else:
                changed = False
                for i in range(len(data)):
                    #print i
                    if i in pins:
                        if abs(data[i] - self._last[i]) > 3:
                            changed = True
                            break

                if changed:
                    payload = {
                        u'id': self._id,
                        u'timestamp': utcnow(),
                        u'values': [1023 - data[p] for p in pins]
                    }

                    self._session.publish(u"io.crossbar.demo.wpad.{}.on_change".format(self._wpad_id), payload)
                    self._last_event = payload
                    self._last = data
                    self._id += 1
Example #27
0
    def shutdown(self, details=None):
        """
        Registered under: ``crossbar.worker.<worker_id>.shutdown``
        Event published under: ``crossbar.worker.<worker_id>.on_shutdown_requested``
        """
        if self._is_shutting_down:
            # ignore: we are already shutting down ..
            return
            # raise ApplicationError(u'crossbar.error.operation_in_progress', 'cannot shutdown - the worker is already shutting down')
        else:
            self._is_shutting_down = True

        self.log.info("Shutdown of worker requested!")

        # publish management API event
        #
        yield self.publish(
            u'{}.on_shutdown_requested'.format(self._uri_prefix),
            {
                u'who': details.caller if details else None,
                u'when': utcnow()
            },
            options=PublishOptions(exclude=details.caller if details else None, acknowledge=True)
        )

        # we now call self.leave() to initiate the clean, orderly shutdown of the native worker.
        # the call is scheduled to run on the next reactor iteration only, because we want to first
        # return from the WAMP call when this procedure is called from the node controller
        #
        self._reactor.callLater(0, self.leave)
Example #28
0
    def shutdown(self, details=None):
        """
        Registered under: ``crossbar.worker.<worker_id>.shutdown``
        Event published under: ``crossbar.worker.<worker_id>.on_shutdown_requested``
        """
        if self._is_shutting_down:
            # ignore: we are already shutting down ..
            return
            # raise ApplicationError(u'crossbar.error.operation_in_progress', 'cannot shutdown - the worker is already shutting down')
        else:
            self._is_shutting_down = True

        self.log.info("Shutdown of worker requested!")

        # publish management API event
        #
        yield self.publish(
            u'{}.on_shutdown_requested'.format(self._uri_prefix), {
                u'who': details.caller if details else None,
                u'when': utcnow()
            },
            options=PublishOptions(exclude=details.caller if details else None,
                                   acknowledge=True))

        # we now call self.leave() to initiate the clean, orderly shutdown of the native worker.
        # the call is scheduled to run on the next reactor iteration only, because we want to first
        # return from the WAMP call when this procedure is called from the node controller
        #
        self._reactor.callLater(0, self.leave)
Example #29
0
   def create(self):
      """
      Create a new cookie, returning the cookie ID and cookie header value.
      """
      if self.debug:
         log.msg("CookieStore.create()")

      ## http://tools.ietf.org/html/rfc6265#page-20
      ## 0: delete cookie
      ## -1: preserve cookie until browser is closed

      id = util.newid(self._cookie_id_field_length)

      cbtData = {'created': util.utcnow(),
                 'authid': None,
                 'authrole': None,
                 'authmethod': None,
                 'max_age': self._cookie_max_age,
                 'connections': set()}

      self._cookies[id] = cbtData

      ## do NOT add the "secure" cookie attribute! "secure" refers to the
      ## scheme of the Web page that triggered the WS, not WS itself!!
      ##
      return id, '%s=%s;max-age=%d' % (self._cookie_id_field, id, cbtData['max_age'])
Example #30
0
   def __init__(self, node):
      """
      :param node: The node singleton for this node controller session.
      :type node: obj
      """
      NativeProcessSession.__init__(self)
      #self.debug = node.debug
      self.debug = False
      self.debug_app = False


      ## associated node
      self._node = node
      self._node_id = node._node_id
      self._realm = node._realm

      self.cbdir = self._node._cbdir

      self._created = utcnow()
      self._pid = os.getpid()

      ## map of worker processes: worker_id -> NativeWorkerProcess
      self._workers = {}

      self._management_transport = None

      exit = Deferred()
Example #31
0
    def __init__(self, session, authid, authrole, authprovider, secret):
        """
        :param session: The WAMP session ID of the session being authenticated.
        :type session: int
        :param authid: The authentication ID of the authenticating principal.
        :type authid: unicode
        :param authrole: The role under which the principal will be authenticated when
           the authentication succeeds.
        :type authrole: unicode
        :param authprovider: Optional authentication provider.
        :type authprovider: unicode or None
        :param secret: The secret of the principal being authenticated. Either a password
           or a salted password.
        :type secret: str
        """
        self.session = session
        self.authmethod = u"wampcra"
        self.authid = authid
        self.authrole = authrole
        self.authprovider = authprovider

        challenge_obj = {
            'authid': self.authid,
            'authrole': self.authrole,
            'authmethod': u'wampcra',
            'authprovider': self.authprovider,
            'session': self.session,
            'nonce': util.newid(),
            'timestamp': util.utcnow()
        }

        self.challenge = json.dumps(challenge_obj)
        self.signature = auth.compute_wcs(secret, self.challenge)
Example #32
0
    def store_event(self,
                    publisher_id,
                    publication_id,
                    topic,
                    args=None,
                    kwargs=None):
        """
        Persist the given event to history.

        :param publisher_id: The session ID of the publisher of the event being persisted.
        :type publisher_id: int
        :param publication_id: The publication ID of the event.
        :type publisher_id: int
        :param topic: The topic URI of the event.
        :type topic: unicode
        :param args: The args payload of the event.
        :type args: list or None
        :param kwargs: The kwargs payload of the event.
        :type kwargs: dict or None
        """
        assert (publication_id not in self._event_store)
        evt = {
            'timestamp': utcnow(),
            'publisher': publisher_id,
            'publication': publication_id,
            'topic': topic,
            'args': args,
            'kwargs': kwargs
        }
        self._event_store[publication_id] = evt
        self.log.debug("event {publication_id} persisted",
                       publication_id=publication_id)
Example #33
0
    def log(self, childFD, data):
        """
        FIXME: line buffering
        """
        assert(childFD in self._log_fds)

        for msg in data.split('\n'):
            msg = msg.strip()
            if msg != "":

                # log entry used for buffered worker log and/or worker log events
                #
                if self._log is not None or self._log_topic:
                    log_entry = (self._log_lineno, utcnow(), msg)

                # maintain buffered worker log
                #
                if self._log is not None:
                    self._log_lineno += 1
                    self._log.append(log_entry)
                    if self._keeplog > 0 and len(self._log) > self._keeplog:
                        self._log.popleft()

                # publish worker log event
                #
                if self._log_topic:
                    self._controller.publish(self._log_topic, log_entry)

                # log to controller
                #
                log.msg(msg, system="{:<10} {:>6}".format(self.LOGNAME, self.pid), override_system=True)
Example #34
0
    def store_event(self, publisher_id, publication_id, topic, args=None, kwargs=None):
        """
        Persist the given event to history.

        :param publisher_id: The session ID of the publisher of the event being persisted.
        :type publisher_id: int
        :param publication_id: The publication ID of the event.
        :type publisher_id: int
        :param topic: The topic URI of the event.
        :type topic: unicode
        :param args: The args payload of the event.
        :type args: list or None
        :param kwargs: The kwargs payload of the event.
        :type kwargs: dict or None
        """
        assert(publication_id not in self._event_store)
        evt = {
            u'timestamp': utcnow(),
            u'publisher': publisher_id,
            u'publication': publication_id,
            u'topic': topic,
            u'args': args,
            u'kwargs': kwargs
        }
        self._event_store[publication_id] = evt
        self.log.debug("Event {publication_id} persisted", publication_id=publication_id)
Example #35
0
    def onJoin(self, details):
        self.log.info('Session joined on thread {thread_id}: {details}', thread_id=current_thread().ident, details=details)

        """Callback when the WAMP session has been established and is ready for use."""
        # get the Pi serial number
        self._serial = get_serial()

        # all procedures/events will have this URI prefix
        self._prefix = u'io.crossbar.demo.iotstarterkit.{}.light_sensor'.format(self._serial)

        # print startup infos
        self.log.info("Crossbar.io IoT Starterkit Serial No.: {serial}", serial=self._serial)
        self.log.info("LightSensorComponent connected: {details}", details=details)

        # get component user extra configuration
        cfg = self.config.extra

        # initialize button
        self._light_sensor_pin = cfg['light_sensor_pin']
        GPIO.setwarnings(False)
        config_light_sensor_gpio(self._light_sensor_pin)

        # setup edge detection for the light sensor
        GPIO.add_event_detect(self._light_sensor_pin, GPIO.RISING, callback=self.light_change, bouncetime=1000)

        # # for testing: print HIGH/LOW state (every 100ms)
        # def printit():
        #     threading.Timer(0.1, printit).start()
        #     # print "Hello, World!"
        #     if GPIO.input(self._light_sensor_pin):
        #         print("input is HIGH")
        #     else:
        #         print("input is LOW")
        #
        # printit()






        # remember startup timestamp
        self._started = utcnow()

        # flag indicating if the button is already pressed
        self._is_dark = False

        # register procedures
        for proc in [
            (self.started, u'started'),
            (self._is_dark, u'is_dark'),
            # (self.press, u'press'),
        ]:
            uri = u'{}.{}'.format(self._prefix, proc[1])
            yield self.register(proc[0], uri)
            self.log.info('registered procedure {uri}', uri=uri)

        self._is_ready = True
        self.log.info("LightSensorComponent ready!")
Example #36
0
    def onJoin(self, details):

        self.log.info("Joined realm '{realm}' on node management router", realm=details.realm)

        # When a (native) worker process has connected back to the router of
        # the node controller, the worker will publish this event
        # to signal it's readyness.
        #
        def on_worker_ready(res):
            id = res['id']
            if id in self._workers:
                ready = self._workers[id].ready
                if not ready.called:
                    # fire the Deferred previously stored for
                    # signaling "worker ready"
                    ready.callback(id)
                else:
                    self.log.error("Internal error: on_worker_ready() fired for process {process}, but already called earlier",
                                   process=id)
            else:
                self.log.error("Internal error: on_worker_ready() fired for process {process}, but no process with that ID",
                               process=id)

        self.subscribe(on_worker_ready, 'crossbar.node.{}.on_worker_ready'.format(self._node_id))

        yield NativeProcessSession.onJoin(self, details)

        # register node controller procedures: 'crossbar.node.<ID>.<PROCEDURE>'
        #
        procs = [
            'shutdown',
            'get_info',
            'get_workers',
            'get_worker_log',
            'start_router',
            'stop_router',
            'start_container',
            'stop_container',
            'start_guest',
            'stop_guest',
            'start_websocket_testee',
            'stop_websocket_testee',
        ]

        dl = []
        for proc in procs:
            uri = '{}.{}'.format(self._uri_prefix, proc)
            self.log.debug("Registering management API procedure {proc}", proc=uri)
            dl.append(self.register(getattr(self, proc), uri, options=RegisterOptions(details_arg='details')))

        regs = yield DeferredList(dl)

        self.log.debug("Registered {cnt} management API procedures", cnt=len(regs))

        self._started = utcnow()

        self.publish(u"crossbar.node.on_ready", self._node_id)

        self.log.debug("Node controller ready")
Example #37
0
 def __init__(self, service = None):
    self.service = service
    self.current = {"timestamp": utcnow(),
                    "mem-used": 0,
                    "mem-free": 0,
                    "cpu-user": 0,
                    "cpu-system": 0,
                    "cpu-idle": 0}
Example #38
0
 def show_fabric(self, verbose=False):
     res = {
         u'version': u'17.01.23',
         u'now': utcnow(),
         u'started': self._started,
         u'tick': self._tick
     }
     return res
Example #39
0
    def _onPoolConnectionCreated(self, conn):
        ## per connection settings
        conn.autocommit = True

        ## get connection info and store that
        backendPid = conn.get_backend_pid()
        serverVersion = conn.server_version
        self.poolConnections.append((backendPid, utcnow(), serverVersion, conn))
Example #40
0
    def test_purge_on_startup(self):

        created_time = util.utcnow()

        original = [{
            "id": "thisIsAnID",
            "created": created_time,
            "max_age": 604800,
            "authid": "example.authid",
            "authrole": "example.authrole",
            "authmethod": "example.authmethod"
        }, {
            "id": "thisIsAnotherID",
            "created": created_time,
            "max_age": 604800,
            "authid": "example.other.authid",
            "authrole": "example.other.authrole",
            "authmethod": "example.other.authmethod"
        }, {
            "id": "thisIsAnID",
            "modified": created_time,
            "max_age": 604800,
            "authid": "example.second.authid",
            "authrole": "example.second.authrole",
            "authmethod": "example.second.authmethod"
        }]

        expected = [{
            "id": "thisIsAnID",
            "created": created_time,
            "max_age": 604800,
            "authid": "example.second.authid",
            "authrole": "example.second.authrole",
            "authmethod": "example.second.authmethod"
        }, {
            "id": "thisIsAnotherID",
            "created": created_time,
            "max_age": 604800,
            "authid": "example.other.authid",
            "authrole": "example.other.authrole",
            "authmethod": "example.other.authmethod"
        }]

        with tempfile.NamedTemporaryFile() as fp:
            self.write_cookies_to_file(original, fp)

            config = {
                'store': {
                    'type': 'file',
                    'filename': fp.name,
                    'purge_on_startup': True
                }
            }

            CookieStoreFileBacked(fp.name, config)

            actual = self.read_cookies_from_file(fp)
            self.assertEqual(actual, expected)
Example #41
0
    def _createAppCred(self, txn, spec):
        """
      Create new application credential, runs in database transaction.
      """
        attrs = {
            "label": (True, [str, unicode], AppCreds.APPCRED_LABEL_MIN_LENGTH,
                      AppCreds.APPCRED_LABEL_MAX_LENGTH, None),
            "key":
            (True, [str, unicode], AppCreds.APPCRED_KEY_MIN_LENGTH,
             AppCreds.APPCRED_KEY_MAX_LENGTH, AppCreds.APPCRED_KEY_PATTERN),
            "secret": (True, [str,
                              unicode], AppCreds.APPCRED_SECRET_MIN_LENGTH,
                       AppCreds.APPCRED_SECRET_MAX_LENGTH,
                       AppCreds.APPCRED_SECRET_PATTERN)
        }

        errcnt, errs = self.proto.checkDictArg("appcred spec", spec, attrs)

        txn.execute("SELECT created FROM appcredential WHERE key = ?",
                    [spec["key"]])
        if txn.fetchone() is not None:
            errs["key"].append(
                (self.proto.shrink(URI_ERROR + "duplicate-value"),
                 "Application key '%s' already exists" % spec["key"]))
            errcnt += 1

        if errcnt:
            raise Exception(
                URI_ERROR + "illegal-argument",
                "one or more illegal arguments (%d errors)" % errcnt, errs)

        id = newid()
        appcred_uri = URI_APPCRED + id
        label = spec["label"].strip()
        now = utcnow()
        txn.execute(
            "INSERT INTO appcredential (id, label, key, created, secret) VALUES (?, ?, ?, ?, ?)",
            [id, label, spec["key"], now, spec["secret"]])

        services = self.proto.factory.services
        if services.has_key("restpusher"):
            services["restpusher"].recache(txn)
        if services.has_key("clientfilter"):
            services["clientfilter"].recache(txn)

        appcred = {
            "uri": appcred_uri,
            "created": now,
            "label": label,
            "key": spec["key"],
            "secret": spec["secret"]
        }

        self.proto.dispatch(URI_EVENT + "on-appcred-created", appcred,
                            [self.proto])

        appcred["uri"] = self.proto.shrink(appcred_uri)
        return appcred
Example #42
0
    def utcnow(self, details=None):
        """
        Return current time as determined from within this process.

        :returns str -- Current time (UTC) in UTC ISO 8601 format.
        """
        self.log.debug("{cls}.utcnow", cls=self.__class__.__name__)

        return utcnow()
Example #43
0
    def _onPoolConnectionCreated(self, conn):
        ## per connection settings
        conn.autocommit = True

        ## get connection info and store that
        backendPid = conn.get_backend_pid()
        serverVersion = conn.server_version
        self.poolConnections.append(
            (backendPid, utcnow(), serverVersion, conn))
Example #44
0
 def startService(self):
    log.msg("Starting %s service .." % self.SERVICENAME)
    self.current = {"timestamp": utcnow(),
                    "packets-in": 0,
                    "bytes-in": 0,
                    "packets-out": 0,
                    "bytes-out": 0}
    self.isRunning = True
    self.emitFake()
Example #45
0
   def processRecord(self, inPackets, inBytes, outPackets, outBytes):

      evt = {"timestamp": utcnow(),
             "packets-in": inPackets,
             "bytes-in": inBytes,
             "packets-out": outPackets,
             "bytes-out": outBytes}
      self.current = evt
      self.dispatchEvent(evt)
Example #46
0
    def onJoin(self, details):

        self.log.info("Joined realm '{realm}' on node management router", realm=details.realm)

        # When a (native) worker process has connected back to the router of
        # the node controller, the worker will publish this event
        # to signal it's readyness.
        #
        def on_worker_ready(res):
            id = res['id']
            if id in self._workers:
                ready = self._workers[id].ready
                if not ready.called:
                    # fire the Deferred previously stored for
                    # signaling "worker ready"
                    ready.callback(id)
                else:
                    self.log.error("Internal error: on_worker_ready() fired for process {process}, but already called earlier",
                                   process=id)
            else:
                self.log.error("Internal error: on_worker_ready() fired for process {process}, but no process with that ID",
                               process=id)

        self.subscribe(on_worker_ready, 'crossbar.node.{}.on_worker_ready'.format(self._node_id))

        yield NativeProcessSession.onJoin(self, details)

        # register node controller procedures: 'crossbar.node.<ID>.<PROCEDURE>'
        #
        procs = [
            'shutdown',
            'get_info',
            'get_workers',
            'get_worker_log',
            'start_router',
            'stop_router',
            'start_container',
            'stop_container',
            'start_guest',
            'stop_guest',
        ]

        dl = []
        for proc in procs:
            uri = '{}.{}'.format(self._uri_prefix, proc)
            self.log.debug("Registering management API procedure {proc}", proc=uri)
            dl.append(self.register(getattr(self, proc), uri, options=RegisterOptions(details_arg='details')))

        regs = yield DeferredList(dl)

        self.log.debug("Registered {cnt} management API procedures", cnt=len(regs))

        self._started = utcnow()

        self.publish(u"crossbar.node.on_ready", self._node_id)

        self.log.debug("Node controller ready")
Example #47
0
   def _createAppCred(self, txn, spec):
      """
      Create new application credential, runs in database transaction.
      """
      attrs = {"label": (True,
                         [str, unicode],
                         AppCreds.APPCRED_LABEL_MIN_LENGTH,
                         AppCreds.APPCRED_LABEL_MAX_LENGTH,
                         None),
               "key": (True,
                       [str, unicode],
                       AppCreds.APPCRED_KEY_MIN_LENGTH,
                       AppCreds.APPCRED_KEY_MAX_LENGTH,
                       AppCreds.APPCRED_KEY_PATTERN),
               "secret": (True,
                          [str, unicode],
                          AppCreds.APPCRED_SECRET_MIN_LENGTH,
                          AppCreds.APPCRED_SECRET_MAX_LENGTH,
                          AppCreds.APPCRED_SECRET_PATTERN)}

      errcnt, errs = self.proto.checkDictArg("appcred spec", spec, attrs)

      txn.execute("SELECT created FROM appcredential WHERE key = ?", [spec["key"]])
      if txn.fetchone() is not None:
         errs["key"].append((self.proto.shrink(URI_ERROR + "duplicate-value"), "Application key '%s' already exists" % spec["key"]))
         errcnt += 1

      if errcnt:
         raise Exception(URI_ERROR + "illegal-argument", "one or more illegal arguments (%d errors)" % errcnt, errs)

      id = newid()
      appcred_uri = URI_APPCRED + id
      label = spec["label"].strip()
      now = utcnow()
      txn.execute("INSERT INTO appcredential (id, label, key, created, secret) VALUES (?, ?, ?, ?, ?)",
                  [id,
                   label,
                   spec["key"],
                   now,
                   spec["secret"]])

      services = self.proto.factory.services
      if services.has_key("restpusher"):
         services["restpusher"].recache(txn)
      if services.has_key("clientfilter"):
         services["clientfilter"].recache(txn)

      appcred = {"uri": appcred_uri,
                 "created": now,
                 "label": label,
                 "key": spec["key"],
                 "secret": spec["secret"]}

      self.proto.dispatch(URI_EVENT + "on-appcred-created", appcred, [self.proto])

      appcred["uri"] = self.proto.shrink(appcred_uri)
      return appcred
Example #48
0
 def processRecord(self, memUsed, memFree, cpuUser, cpuSys, cpuIdle):
    evt = {"timestamp": utcnow(),
           "mem-used": memUsed,
           "mem-free": memFree,
           "cpu-user": cpuUser,
           "cpu-system": cpuSys,
           "cpu-idle": cpuIdle}
    self.current = evt
    self.dispatchEvent(evt)
Example #49
0
 def processRecord(self, memUsed, memFree, cpuUser, cpuSys, cpuIdle):
    evt = {"timestamp": utcnow(),
           "mem-used": memUsed,
           "mem-free": memFree,
           "cpu-user": cpuUser,
           "cpu-system": cpuSys,
           "cpu-idle": cpuIdle}
    self.current = evt
    self.dispatchEvent(evt)
Example #50
0
 def __init__(self, service=None):
     self.service = service
     self.current = {
         "timestamp": utcnow(),
         "packets-in": 0,
         "bytes-in": 0,
         "packets-out": 0,
         "bytes-out": 0
     }
Example #51
0
    def utcnow(self, details=None):
        """
        Return current time as determined from within this process.

        :returns str -- Current time (UTC) in UTC ISO 8601 format.
        """
        self.log.debug("{cls}.utcnow", cls=self.__class__.__name__)

        return utcnow()
Example #52
0
        def do(txn):
            ## verify that testrun exists and is not closed already
            ##
            txn.execute("SELECT started, ended FROM testrun WHERE id = ?",
                        [runId])
            res = txn.fetchone()
            if res is None:
                raise Exception("no such test run")
            if res[1] is not None:
                raise Exception("test run already closed")

            ## save test case results with foreign key to test run
            ##
            id = newid()
            now = utcnow()

            ci = []
            for i in xrange(5):
                if len(testCase.index) > i:
                    ci.append(testCase.index[i])
                else:
                    ci.append(0)

            if saveLog:
                log = result.log
            else:
                log = []
            result.log = None

            resultData = result.serialize()

            txn.execute(
                """
            INSERT INTO testresult
               (id, testrun_id, inserted, testee, c1, c2, c3, c4, c5, duration, passed, result)
                  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """, [
                    id, runId, now, testRun.testee.name, ci[0], ci[1], ci[2],
                    ci[3], ci[4], result.ended - result.started,
                    1 if result.passed else 0, resultData
                ])

            ## save test case log with foreign key to test result
            ##
            if log:
                lineno = 1
                for l in log:
                    txn.execute(
                        """
                  INSERT INTO testlog
                     (testresult_id, lineno, timestamp, sessionidx, sessionid, line)
                        VALUES (?, ?, ?, ?, ?, ?)
                  """, [id, lineno, l[0], l[1], l[2], l[3]])
                    lineno += 1

            return id
Example #53
0
    def onJoin(self, details):

        from autobahn.wamp.types import SubscribeOptions

        self.log.debug("Joined realm '{realm}' on node management router", realm=details.realm)

        # When a (native) worker process has connected back to the router of
        # the node controller, the worker will publish this event
        # to signal it's readyness.
        #
        def on_worker_ready(res):
            worker_id = res['id']
            if worker_id in self._workers:
                ready = self._workers[worker_id].ready
                if not ready.called:
                    # fire the Deferred previously stored for
                    # signaling "worker ready"
                    ready.callback(worker_id)
                else:
                    self.log.error("Internal error: on_worker_ready() fired for process {process}, but already called earlier",
                                   process=worker_id)
            else:
                self.log.error("Internal error: on_worker_ready() fired for process {process}, but no process with that ID",
                               process=worker_id)

        self.subscribe(on_worker_ready, u'crossbar.worker..on_worker_ready', SubscribeOptions(match=u'wildcard'))

        yield NativeProcess.onJoin(self, details)
        # above upcall registers procedures we have marked with @wamp.register(None)

        # we need to catch SIGINT here to properly shutdown the
        # node explicitly (a Twisted system trigger wouldn't allow us to distinguish
        # different reasons/origins of exiting ..)
        def signal_handler(_signal, frame):
            if _signal == signal.SIGINT:
                # CTRL-C'ing Crossbar.io is considered "willful", and hence we want to exit cleanly
                self._shutdown_was_clean = True
            elif _signal == signal.SIGTERM:
                self._shutdown_was_clean = False
            else:
                # FIXME: can we run into others here?
                self._shutdown_was_clean = False

            self.log.warn('Controller received SIGINT [signal={signal}]: shutting down node [shutdown_was_clean={shutdown_was_clean}] ..', signal=_signal, shutdown_was_clean=self._shutdown_was_clean)

            # the following will shutdown the Twisted reactor in the end
            self.shutdown()

        signal.signal(signal.SIGINT, signal_handler)
        self.log.info('Signal handler installed on process {pid} thread {tid}', pid=os.getpid(), tid=threading.get_ident())

        self._started = utcnow()

        self.publish(u"crossbar.on_ready")

        self.log.debug("Node controller ready")
Example #54
0
    def _loop(self, index):
        prefix = self.config.extra['prefix']
        last = None

        while self._running:
            rtts = []

            batch_started_str = utcnow()
            batch_started = time()

            while (time() - batch_started) < self._period:
                ts_req = time_ns()
                res = yield self.call('{}.echo'.format(prefix), ts_req)
                ts_res = time_ns()
                assert res == ts_req
                rtt = ts_res - ts_req
                rtts.append(rtt)

            stats = self._pinfo.get_stats()

            if last:
                batch_duration = (stats['time'] - last['time']) / 10 ** 9
                ctx = round((stats['voluntary'] - last['voluntary']) / batch_duration, 0)

                self.log.info('{logname}: {cpu} cpu, {mem} mem, {ctx} ctx',
                              logname=self._logname,
                              cpu=round(stats['cpu_percent'], 1),
                              mem=round(stats['mem_percent'], 1),
                              ctx=ctx)

                rtts = sorted(rtts)

                sr = WampStatsRecord()
                sr.key = '{}#{}.{}'.format(batch_started_str, self._logname, index)
                sr.count = len(rtts)
                sr.calls_per_sec = int(round(sr.count / batch_duration, 0))

                # all times here are in microseconds:
                sr.avg_rtt = round(1000000. * batch_duration / float(sr.count), 1)
                sr.max_rtt = round(rtts[-1] / 1000, 1)
                sr.q50_rtt = round(rtts[int(sr.count / 2.)] / 1000, 1)
                sr.q99_rtt = round(rtts[int(-(sr.count / 100.))] / 1000, 1)
                sr.q995_rtt = round(rtts[int(-(sr.count / 995.))] / 1000, 1)

                with self._db.begin(write=True) as txn:
                    self._schema.wamp_stats[txn, sr.key] = sr

                print("{}: {} calls, {} calls/sec, RTT (us): q50 {}, avg {}, q99 {}, q995 {}, max {}".format(sr.key,
                                                                                                             sr.count,
                                                                                                             sr.calls_per_sec,
                                                                                                             sr.q50_rtt,
                                                                                                             sr.avg_rtt,
                                                                                                             sr.q99_rtt,
                                                                                                             sr.q995_rtt,
                                                                                                             sr.max_rtt))
            last = stats
Example #55
0
    def onJoin(self, details):

        from autobahn.wamp.types import SubscribeOptions

        self.log.debug("Joined realm '{realm}' on node management router", realm=details.realm)

        # When a (native) worker process has connected back to the router of
        # the node controller, the worker will publish this event
        # to signal it's readyness.
        #
        def on_worker_ready(res):
            worker_id = res['id']
            if worker_id in self._workers:
                ready = self._workers[worker_id].ready
                if not ready.called:
                    # fire the Deferred previously stored for
                    # signaling "worker ready"
                    ready.callback(worker_id)
                else:
                    self.log.error("Internal error: on_worker_ready() fired for process {process}, but already called earlier",
                                   process=worker_id)
            else:
                self.log.error("Internal error: on_worker_ready() fired for process {process}, but no process with that ID",
                               process=worker_id)

        self.subscribe(on_worker_ready, u'crossbar.worker..on_worker_ready', SubscribeOptions(match=u'wildcard'))

        yield NativeProcess.onJoin(self, details)
        # above upcall registers procedures we have marked with @wamp.register(None)

        # we need to catch SIGINT here to properly shutdown the
        # node explicitly (a Twisted system trigger wouldn't allow us to distinguish
        # different reasons/origins of exiting ..)
        def signal_handler(_signal, frame):
            if _signal == signal.SIGINT:
                # CTRL-C'ing Crossbar.io is considered "willful", and hence we want to exit cleanly
                self._shutdown_was_clean = True
            elif _signal == signal.SIGTERM:
                self._shutdown_was_clean = False
            else:
                # FIXME: can we run into others here?
                self._shutdown_was_clean = False

            self.log.warn('Controller received SIGINT [signal={signal}]: shutting down node [shutdown_was_clean={shutdown_was_clean}] ..', signal=_signal, shutdown_was_clean=self._shutdown_was_clean)

            # the following will shutdown the Twisted reactor in the end
            self.shutdown()

        signal.signal(signal.SIGINT, signal_handler)
        self.log.info('Signal handler installed on process {pid} thread {tid}', pid=os.getpid(), tid=threading.get_ident())

        self._started = utcnow()

        self.publish(u"crossbar.on_ready")

        self.log.debug("Node controller ready")
Example #56
0
    def _loop(self, index):
        prefix = self.config.extra['prefix']
        last = None

        while self._running:
            rtts = []

            batch_started_str = utcnow()
            batch_started = time()

            while (time() - batch_started) < self._period:
                ts_req = time_ns()
                res = yield self.call('{}.echo'.format(prefix), ts_req)
                ts_res = time_ns()
                assert res == ts_req
                rtt = ts_res - ts_req
                rtts.append(rtt)

            stats = self._pinfo.get_stats()

            if last:
                batch_duration = (stats['time'] - last['time']) / 10**9
                ctx = round(
                    (stats['voluntary'] - last['voluntary']) / batch_duration,
                    0)

                self.log.info('{logname}: {cpu} cpu, {mem} mem, {ctx} ctx',
                              logname=self._logname,
                              cpu=round(stats['cpu_percent'], 1),
                              mem=round(stats['mem_percent'], 1),
                              ctx=ctx)

                rtts = sorted(rtts)

                sr = WampStatsRecord()
                sr.key = '{}#{}.{}'.format(batch_started_str, self._logname,
                                           index)
                sr.count = len(rtts)
                sr.calls_per_sec = int(round(sr.count / batch_duration, 0))

                # all times here are in microseconds:
                sr.avg_rtt = round(1000000. * batch_duration / float(sr.count),
                                   1)
                sr.max_rtt = round(rtts[-1] / 1000, 1)
                sr.q50_rtt = round(rtts[int(sr.count / 2.)] / 1000, 1)
                sr.q99_rtt = round(rtts[int(-(sr.count / 100.))] / 1000, 1)
                sr.q995_rtt = round(rtts[int(-(sr.count / 995.))] / 1000, 1)

                with self._db.begin(write=True) as txn:
                    self._schema.wamp_stats[txn, sr.key] = sr

                print(
                    "{}: {} calls, {} calls/sec, RTT (us): q50 {}, avg {}, q99 {}, q995 {}, max {}"
                    .format(sr.key, sr.count, sr.calls_per_sec, sr.q50_rtt,
                            sr.avg_rtt, sr.q99_rtt, sr.q995_rtt, sr.max_rtt))
            last = stats
Example #57
0
   def connectionLost(self, reason):
      """
      Client disconnected. Update stats.
      """
      WampCraServerProtocol.connectionLost(self, reason)
      self.factory.onConnectionCountChanged()

      self._cbSession['lost'] = utcnow()

      self.factory.dispatch("http://analytics.tavendo.de#leave", self._cbSession)
Example #58
0
 def stats(self):
     """
     """
     res = {}
     res[u'ts'] = utcnow()
     res[u'cpu'] = self.cpu_stats()
     res[u'mem'] = self.mem_stats()
     res[u'net'] = self.net_stats()
     res[u'disk'] = self.disk_stats()
     return res
Example #59
0
 def stats(self):
     """
  """
     res = {}
     res['ts'] = utcnow()
     res['cpu'] = self.cpu_stats()
     res['mem'] = self.mem_stats()
     res['net'] = self.net_stats()
     res['disk'] = self.disk_stats()
     return res