Пример #1
0
 def cb_validate(self, tctx, kp, newval):
     global maapisock
     log.debug("==> kp=%s newval=%s" % (kp, newval))
     rv = _confd.CONFD_OK
     threshold = 100
     try:
         maapi.attach(maapisock, maapi_example_ns.ns.hash, tctx)
         mc = maapi.init_cursor(maapisock, tctx.th, items_keypath_string)
         val_sum = 0
         keys = maapi.get_next(mc)
         while keys:
             if maapi.exists(
                     maapisock, tctx.th,
                     "%s{%s}/value" % (items_keypath_string, keys[0])):
                 log.debug("value element exists")
                 val = maapi.get_elem(
                     maapisock, tctx.th,
                     "%s{%s}/value" % (items_keypath_string, keys[0]))
                 log.debug("val=%d", val)
                 val_sum += int(val)
             keys = maapi.get_next(mc)
         maapi.destroy_cursor(mc)
         maapi.detach(maapisock, tctx)
         if val_sum > threshold:
             text = "Sum of value elements in %s is %u," \
                    " which is greater than %u!" % (
                        items_keypath_string, val_sum, threshold)
             dp.trans_seterr(tctx, text)
             log.warn(text)
             rv = _confd.CONFD_ERR
     except Exception as e:
         log.exception(e)
         rv = _confd.CONFD_ERR
     log.debug("<== rv=%d" % rv)
     return rv
Пример #2
0
    def cb_command(self, uinfo, path, argv):
        global maapisock
        log.debug("==> path %s argv=%r" % (path, argv))
        rv = _confd.CONFD_OK
        try:
            maapi.attach2(maapisock, maapi_example_ns.ns.hash, uinfo.usid,
                          uinfo.actx_thandle)
            mc = maapi.init_cursor(maapisock, uinfo.actx_thandle,
                                   start_log_keypath_string)
            count = 0
            keys = maapi.get_next(mc)
            while keys:
                if (maapi.exists(
                        maapisock, uinfo.actx_thandle,
                        "%s{%s}" % (start_log_keypath_string, str(keys[0])))):
                    count += 1
                log.debug("Value element count=%d" % count)
                keys = maapi.get_next(mc)
            maapi.destroy_cursor(mc)
            maapi.detach2(maapisock, uinfo.actx_thandle)
            log.debug("count=%i" % count)
            maapi.cli_write(maapisock, uinfo.usid,
                            "\nApplication startup count %d\n" % count)
        except Exception as e:
            maapi.cli_write(maapisock, uinfo.usid,
                            "Cannot determine application startup count")
            log.exception(e)
            rv = _confd.CONFD_ERR

        log.debug("<== rv=%d" % rv)
        return rv
Пример #3
0
 def cb_get_next(self, tctx, kp, next):
     global maapisock
     log.debug("==> kp=%s next=%d" % (kp, next))
     rv = _confd.CONFD_OK
     try:
         maapi.attach(maapisock, maapi_example_ns.ns.hash, tctx)
         if next == -1:  # first call
             next = 0
         n = 0
         mc = maapi.init_cursor(maapisock, tctx.th, items_keypath_string)
         keys = maapi.get_next(mc)
         while keys and n != next:
             log.debug("n=%d" % n)
             keys = maapi.get_next(mc)
             n += 1
         if not keys:
             log.debug("No more item entry, element not found.")
             dp.data_reply_next_key(tctx, None, -1)
         else:
             next += 1
             dp.data_reply_next_key(tctx, keys, next)
         maapi.destroy_cursor(mc)
         maapi.detach(maapisock, tctx)
     except Exception as e:
         log.exception(e)
         rv = _confd.CONFD_ERR
     log.debug("<== rv=%d" % rv)
     return rv
Пример #4
0
            def _get_unused_user_id():
                """ Return "free" (hopefully) user-id that can be used for
                    new user in the "user-storage.yang" configuration.
                """
                cursor = maapi.init_cursor(maapi_socket, helper.th, USER_PATH)
                keys = maapi.get_next(cursor)

                last_user_id = init_user_id
                while keys:
                    last_user_id = int(keys[0])
                    keys = maapi.get_next(cursor)
                maapi.destroy_cursor(cursor)
                return last_user_id + 1
Пример #5
0
 def _get_list_cursor(next_counter, path, cursors):
     """ Return MAAPI cursor for iterating the YANG list
         depending on the "next" value and request keypath.
         CDB path string is used as cursor identifier to simplify API.
     """
     if -1 == next_counter:
         if path in cursors:
             maapi.destroy_cursor(cursors[path])
         cursors[path] = maapi.init_cursor(maapi_socket, helper.th,
                                           path)
     else:
         if path not in cursors:
             raise ValueError("Jumping into uninitialized iteration!")
     return cursors[path]
Пример #6
0
    def exec_totals(self, uinfo):
        print('exec totals')

        key = get_thread_key(uinfo)
        print('[thr# ' + key + '] watch thread for abort request')
        thr = self.thread_map[key]

        # import pdb; pdb.set_trace()

        # open own MAAPI connection for reading values
        msock = socket.socket()
        maapi.connect(msock, CONFD_ADDR, _confd.CONFD_PORT)
        th = maapi.start_trans2(msock, _confd.RUNNING, _confd.READ, uinfo.usid)

        path = "/model:dm/proc"

        mc = maapi.init_cursor(msock, th, path)

        # gather data from ConfD
        cpu_sum = 0
        cpu_cnt = 0
        keys = maapi.get_next(mc)
        while not thr.stop_flag and keys is not False:
            cpu = maapi.get_elem(msock, th,
                                 path + '{' + str(keys[0]) + '}/cpu')
            cpu_sum += int(cpu)
            cpu_cnt += 1
            keys = maapi.get_next(mc)

        # clean the used MAAPI session
        maapi.destroy_cursor(mc)
        maapi.close(msock)

        # and dispatch result to ConfD
        result = [
            make_tag_value(ns.hash, ns.model_num_procs, int(cpu_cnt),
                           _confd.C_UINT32),
            make_tag_value(ns.hash, ns.model_total_cpu, int(cpu_sum),
                           _confd.C_UINT32)
        ]

        dp.action_reply_values(uinfo, result)
        return _confd.OK