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
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
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
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
def _get_next_user(user_cursor): """ Retrieve next "/folder-user{}" from the "/user-storage/user". """ keys = maapi.get_next(user_cursor) if keys: v_username = maapi.get_elem(maapi_socket, helper.th, kp_user(keys[0]) + '/username') return v_username return None
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
def _get_next_folder(keypath, next_counter, folder_cursor): """ Retrieve next "/folder-user{}/managed-folder{}" record that belongs to the user from the "/user-storage/ownership{}". """ v_username = keypath[-2][0] v_user_id = helper.get_user_id_value(v_username) if -1 == next_counter: keys = maapi.find_next(mc=folder_cursor, type=confd.FIND_NEXT, inkeys=[v_user_id]) else: keys = maapi.get_next(folder_cursor) if keys: # second key of the "ownership" list is our storage name v_storage_id = keys[1] if is_users_storage(v_user_id, v_storage_id): return parse_folder_id_value(v_storage_id) return None