def aliasdb_show(ctx, oformat, ostyle, table, matchalias, userid): """Show details for user aliases \b Parameters: [<userid>] - username, AoR or SIP URI for user or alias - it can be a list of userids - if not provided then all aliases are shown """ if not userid: ctx.vlog('Showing all records') e = create_engine(ctx.gconfig.get('db', 'rwurl')) res = e.execute('select * from ' + table) ioutils_dbres_print(ctx, oformat, ostyle, res) else: for u in userid: udata = parse_user_spec(ctx, u) e = create_engine(ctx.gconfig.get('db', 'rwurl')) if matchalias: ctx.vlog('Showing records for alias [%s@%s]', udata['username'], udata['domain']) res = e.execute('select * from ' + table + ' where alias_username=%s and alias_domain=%s', udata['username'], udata['domain']) else: ctx.vlog('Showing records for user [%s@%s]', udata['username'], udata['domain']) res = e.execute('select * from ' + table + ' where username=%s and domain=%s', udata['username'], udata['domain']) ioutils_dbres_print(ctx, oformat, ostyle, res)
def speeddial_show(ctx, oformat, ostyle, table, userid, shortdial): """Show details for speed dial records \b Parameters: <userid> - username, AoR or SIP URI for user or alias [<shortdial>] - username, AoR or SIP URI for short dial (optional) """ udata = parse_user_spec(ctx, userid) e = create_engine(ctx.gconfig.get("db", "rwurl")) ctx.vlog( "Showing speed dial records for user [%s@%s]", udata["username"], udata["domain"], ) if not shortdial: res = e.execute( "select * from " + table + " where username=%s and domain=%s", udata["username"], udata["domain"], ) ioutils_dbres_print(ctx, oformat, ostyle, res) else: for s in shortdial: sdata = parse_user_spec(ctx, s) res = e.execute( "select * from " + table + " where username=%s and domain=%s " "and sd_username=%s and sd_domain=%s", udata["username"], udata["domain"], sdata["username"], sdata["domain"], ) ioutils_dbres_print(ctx, oformat, ostyle, res)
def htable_dbshow(ctx, oformat, ostyle, dbtname, colkeyname, keyname): """Show details for records in htable database table \b Parameters: <keyname> - key name to match the record (optional) """ e = create_engine(ctx.gconfig.get("db", "rwurl")) if not keyname: ctx.vlog("Showing all htable database records") res = e.execute( "select * from {0}".format( dbtname.encode("ascii", "ignore").decode() ) ) ioutils_dbres_print(ctx, oformat, ostyle, res) else: ctx.vlog("Showing htable database records for key name") for k in keyname: res = e.execute( "select * from {0} where {1}={2!r}".format( dbtname.encode("ascii", "ignore").decode(), colkeyname.encode("ascii", "ignore").decode(), k.encode("ascii", "ignore").decode(), ) ) ioutils_dbres_print(ctx, oformat, ostyle, res)
def mtree_dbshow(ctx, oformat, ostyle, coltprefix, dbtname, tprefix): """Show details for records in mtree database table \b Parameters: <dbtname> - name of tree database table <tprefix> - tree prefix value to match the record """ e = create_engine(ctx.gconfig.get("db", "rwurl")) if not tprefix: ctx.vlog("Showing all tree database records") res = e.execute( "select * from {0}".format( dbtname.encode("ascii", "ignore").decode() ) ) else: ctx.vlog("Showing tree database records for prefix") res = e.execute( "select * from {0} where {1}={2!r}".format( dbtname.encode("ascii", "ignore").decode(), coltprefix.encode("ascii", "ignore").decode(), tprefix.encode("ascii", "ignore").decode(), ) ) ioutils_dbres_print(ctx, oformat, ostyle, res)
def group_show(ctx, oformat, ostyle, userid): """Show details for subscribers \b Parameters: [<userid>] - username, AoR or SIP URI for subscriber - it can be a list of userids - if not provided then all subscribers are shown """ if not userid: ctx.vlog("Showing all records") e = create_engine(ctx.gconfig.get("db", "rwurl")) res = e.execute("select * from grp") ioutils_dbres_print(ctx, oformat, ostyle, res) else: for u in userid: udata = parse_user_spec(ctx, u) ctx.vlog( "Showing group membership for user [%s@%s]", udata["username"], udata["domain"], ) e = create_engine(ctx.gconfig.get("db", "rwurl")) res = e.execute( "select * from grp where username=%s and domain=%s", udata["username"], udata["domain"], ) ioutils_dbres_print(ctx, oformat, ostyle, res)
def dialog_showdb(ctx, oformat, ostyle): """Show details for records in dialog table \b """ e = create_engine(ctx.gconfig.get('db', 'rwurl')) ctx.vlog('Showing all dispatcher records') res = e.execute('select * from dialog') ioutils_dbres_print(ctx, oformat, ostyle, res)
def dialog_showdb(ctx, oformat, ostyle): """Show details for records in dialog table \b """ e = create_engine(ctx.gconfig.get("db", "rwurl")) ctx.vlog("Showing all dialog records") res = e.execute("select * from dialog") ioutils_dbres_print(ctx, oformat, ostyle, res)
def rtpengine_showdb(ctx, oformat, ostyle): """Show the rtpengine records in database table \b Parameters: none """ e = create_engine(ctx.gconfig.get('db', 'rwurl')) ctx.vlog('Showing all rtpengine database records') res = e.execute('select * from rtpengine') ioutils_dbres_print(ctx, oformat, ostyle, res)
def db_version_get(ctx, vertable, oformat, ostyle, table): """Get the version number for a table structure \b Parameters: <table> - Name of the table to get the version for """ e = create_engine(ctx.gconfig.get("db", "rwurl")) res = e.execute("select * from {0} where table_name={1!r}".format( vertable.encode("ascii", "ignore").decode(), table.encode("ascii", "ignore").decode(), )) ioutils_dbres_print(ctx, oformat, ostyle, res)
def address_showdb(ctx, oformat, ostyle, group): """Show details for records in address db table \b Parameters: <group> - address group """ e = create_engine(ctx.gconfig.get('db', 'rwurl')) if not group: ctx.vlog('Showing all address records') res = e.execute('select * from address') else: ctx.vlog('Showing address records for group') res = e.execute('select * from address where group=%d', group) ioutils_dbres_print(ctx, oformat, ostyle, res)
def mtree_showdb(ctx, oformat, ostyle, coltprefix, dbtable, tprefix): """Show details for records in mtree database table \b Parameters: <dbtname> - name of tree database table <tprefix> - tree prefix value to match the record """ e = create_engine(ctx.gconfig.get('db', 'rwurl')) if not tprefix: ctx.vlog('Showing all tree database records') res = e.execute('select * from {0!r}'.format(dbtname.encode('ascii','ignore'))) else: ctx.vlog('Showing tree database records for prefix') res = e.execute('select * from {0!r} where {1!r}={2!r}'.format(dbtname.encode('ascii','ignore'), coltprefix.encode('ascii','ignore'), tprefix.encode('ascii','ignore'))) ioutils_dbres_print(ctx, oformat, ostyle, res)
def dispatcher_showdb(ctx, oformat, ostyle, dpid): """Show details for records in dialplan \b Parameters: [<dpid>] - dialplan id """ e = create_engine(ctx.gconfig.get('db', 'rwurl')) if not dpid: ctx.vlog('Showing all dialplan records') res = e.execute('select * from dialplan') ioutils_dbres_print(ctx, oformat, ostyle, res) else: for d in dpid: ctx.vlog('Showing dialplan records for set id:' + d) res = e.execute('select * from dialplan where dpid=%d', d) ioutils_dbres_print(ctx, oformat, ostyle, res)
def dispatcher_showdb(ctx, oformat, ostyle, setid): """Show details for records in dispatcher table \b Parameters: [<setid>] - dispatching set id """ e = create_engine(ctx.gconfig.get('db', 'rwurl')) if not setid: ctx.vlog('Showing all dispatcher records') res = e.execute('select * from dispatcher') ioutils_dbres_print(ctx, oformat, ostyle, res) else: for s in setid: ctx.vlog('Showing dispatcher records for set id') res = e.execute('select * from dispatcher where setid=%d', s) ioutils_dbres_print(ctx, oformat, ostyle, res)
def domain_showdb(ctx, oformat, ostyle, domain): """Show details for records in domain table \b Parameters: [<domain>] - domain value (optional) """ e = create_engine(ctx.gconfig.get("db", "rwurl")) if not domain: ctx.vlog("Showing all domain records") res = e.execute("select * from domain") ioutils_dbres_print(ctx, oformat, ostyle, res) else: for d in domain: ctx.vlog("Showing a specific domain record") res = e.execute('select * from domain where domain="%s"', d) ioutils_dbres_print(ctx, oformat, ostyle, res)
def domain_showdb(ctx, oformat, ostyle, domain): """Show details for records in domain table \b Parameters: [<domain>] - domain value (optional) """ e = create_engine(ctx.gconfig.get('db', 'rwurl')) if not domain: ctx.vlog('Showing all domain records') res = e.execute('select * from domain') ioutils_dbres_print(ctx, oformat, ostyle, res) else: for d in domain: ctx.vlog('Showing a specific domain record') res = e.execute('select * from domain where domain="%s"', d) ioutils_dbres_print(ctx, oformat, ostyle, res)
def uacreg_showdb(ctx, oformat, ostyle, l_uuid): """Show details for records in uacreg database table \b Parameters: [<l_uuid>] - local user unique id """ e = create_engine(ctx.gconfig.get("db", "rwurl")) if not l_uuid: ctx.vlog("Showing all uacreg records") res = e.execute("select * from uacreg") ioutils_dbres_print(ctx, oformat, ostyle, res) else: for record in l_uuid: ctx.vlog("Showing uacreg records for l_uuid: " + record) res = e.execute('select * from uacreg where l_uuid="%s"', record) ioutils_dbres_print(ctx, oformat, ostyle, res)
def dialplan_showdb(ctx, oformat, ostyle, dpid): """Show details for records in dialplan \b Parameters: [<dpid>] - dialplan id """ e = create_engine(ctx.gconfig.get("db", "rwurl")) if not dpid: ctx.vlog("Showing all dialplan records") res = e.execute("select * from dialplan") ioutils_dbres_print(ctx, oformat, ostyle, res) else: for d in dpid: ctx.vlog("Showing dialplan records for set id: " + d) res = e.execute("select * from dialplan where dpid=%d", d) ioutils_dbres_print(ctx, oformat, ostyle, res)
def aliasdb_show(ctx, oformat, ostyle, table, matchalias, userid): """Show details for user aliases \b Parameters: [<userid>] - username, AoR or SIP URI for user or alias - it can be a list of userids - if not provided then all aliases are shown """ if not userid: ctx.vlog("Showing all records") e = create_engine(ctx.gconfig.get("db", "rwurl")) res = e.execute("select * from " + table) ioutils_dbres_print(ctx, oformat, ostyle, res) else: for u in userid: udata = parse_user_spec(ctx, u) e = create_engine(ctx.gconfig.get("db", "rwurl")) if matchalias: ctx.vlog( "Showing records for alias [%s@%s]", udata["username"], udata["domain"], ) res = e.execute( "select * from " + table + " where alias_username=%s " "and alias_domain=%s", udata["username"], udata["domain"], ) else: ctx.vlog( "Showing records for user [%s@%s]", udata["username"], udata["domain"], ) res = e.execute( "select * from " + table + " where username=%s and " "domain=%s", udata["username"], udata["domain"], ) ioutils_dbres_print(ctx, oformat, ostyle, res)
def dispatcher_showdb(ctx, oformat, ostyle, coltprefix, dbtable, tprefix): """Show details for records in mtree database table \b Parameters: <dbtname> - name of tree database table <tprefix> - tree prefix value to match the record """ e = create_engine(ctx.gconfig.get('db', 'rwurl')) if not tprefix: ctx.vlog('Showing all tree database records') res = e.execute('select * from {0!r}'.format( dbtname.encode('ascii', 'ignore'))) else: ctx.vlog('Showing tree database records for prefix') res = e.execute('select * from {0!r} where {1!r}={2!r}'.format( dbtname.encode('ascii', 'ignore'), coltprefix.encode('ascii', 'ignore'), tprefix.encode('ascii', 'ignore'))) ioutils_dbres_print(ctx, oformat, ostyle, res)
def ul_showdb(ctx, oformat, ostyle, userid): """Show details for location records in database \b Parameters: [<userid>] - username, AoR or SIP URI for subscriber - it can be a list of userids - if not provided then all records are shown """ if not userid: ctx.vlog('Showing all records') e = create_engine(ctx.gconfig.get('db', 'rwurl')) res = e.execute('select * from location') ioutils_dbres_print(ctx, oformat, ostyle, res) else: for u in userid: udata = parse_user_spec(ctx, u) ctx.vlog('Showing records for [%s@%s]', udata['username'], udata['domain']) e = create_engine(ctx.gconfig.get('db', 'rwurl')) res = e.execute('select * from location where username=%s and domain=%s', udata['username'], udata['domain']) ioutils_dbres_print(ctx, oformat, ostyle, res)
def speeddial_show(ctx, oformat, ostyle, table, userid, shortdial): """Show details for speed dial records \b Parameters: <userid> - username, AoR or SIP URI for user or alias [<shortdial>] - username, AoR or SIP URI for short dial (optional) """ udata = parse_user_spec(ctx, userid) e = create_engine(ctx.gconfig.get('db', 'rwurl')) ctx.vlog('Showing speed dial records for user [%s@%s]', udata['username'], udata['domain']) if not shortdial: res = e.execute('select * from ' + table + ' where username=%s and domain=%s', udata['username'], udata['domain']) ioutils_dbres_print(ctx, oformat, ostyle, res) else: for s in shortdial: sdata = parse_user_spec(ctx, s) res = e.execute('select * from ' + table + ' where username=%s and domain=%s and sd_username=%s and sd_domain=%s', udata['username'], udata['domain'], sdata['username'], sdata['domain']) ioutils_dbres_print(ctx, oformat, ostyle, res)
def db_showcreate(ctx, oformat, ostyle, table): ctx.vlog("Show create of database table [%s]", table) dbtype = ctx.gconfig.get("db", "type") if dbtype == "mysql": e = create_engine(ctx.gconfig.get("db", "rwurl")) res = e.execute("show create table {0}".format(table)) ioutils_dbres_print(ctx, oformat, ostyle, res) elif dbtype == "postgresql": scmd = ('psql "postgresql://{0}:{1}@{2}/{3}" -c "\\d {4} "').format( ctx.gconfig.get("db", "rwuser"), ctx.gconfig.get("db", "rwpassword"), ctx.gconfig.get("db", "host"), ctx.gconfig.get("db", "dbname"), table, ) os.system(scmd) elif dbtype == "sqlite": scmd = ('sqlite3 {0} ".schema {1} "').format( ctx.gconfig.get("db", "dbpath"), table, ) os.system(scmd) else: ctx.log("unsupported database type [%s]", dbtype)
def avp_dbshow( ctx, oformat, ostyle, dbtname, coluuid, colusername, coldomain, colattribute, coltype, colvalue, atype, isuuid, userid, attribute, value, ): """Show AVP records from database table \b Parameters: <userid> - user AVP id (<username>@<domain> or <uuid>) <attribute> - attribute name <value> - associated value for attribute - use '*' to match any value for <userid>, <attribute> or <value> - example - remove all AVPs: kamcli avp db-rm '*' '*' '*' """ ctx.vlog( "Show AVPs from table [%s] - [%s] [%s] => [%s]", dbtname, userid, attribute, value, ) e = create_engine(ctx.gconfig.get("db", "rwurl")) dbname = dbtname.encode("ascii", "ignore").decode() c_uuid = coluuid.encode("ascii", "ignore").decode() c_username = colusername.encode("ascii", "ignore").decode() c_domain = coldomain.encode("ascii", "ignore").decode() c_attribute = colattribute.encode("ascii", "ignore").decode() c_type = coltype.encode("ascii", "ignore").decode() c_value = colvalue.encode("ascii", "ignore").decode() v_userid = userid.encode("ascii", "ignore").decode() v_attribute = attribute.encode("ascii", "ignore").decode() v_value = value.encode("ascii", "ignore").decode() sqlquery = "SELECT * FROM {0}".format(dbname) if atype != -1 or v_userid != "*" or v_userid != "*" or v_userid != "*": sqlquery += " WHERE 1=1" if v_userid != "*": if isuuid: sqlquery += " AND {0}={1!r}".format(c_uuid, v_userid) else: udata = parse_user_spec(ctx, userid) sqlquery += " AND {0}={1!r} AND {2}={3!r}".format( c_username, udata["username"], c_domain, udata["domain"]) if v_attribute != "*": sqlquery += " AND {0}={1!r}".format(c_attribute, v_attribute) if atype != -1: sqlquery += " AND {0}={1}".format(c_type, atype) if v_value != "*": sqlquery += " AND {0}={1!r}".format(c_value, v_value) res = e.execute(sqlquery) ioutils_dbres_print(ctx, oformat, ostyle, res)
def db_query(ctx, oformat, ostyle, query): e = create_engine(ctx.gconfig.get("db", "rwurl")) res = e.execute(query.encode("ascii", "ignore").decode()) ioutils_dbres_print(ctx, oformat, ostyle, res)
def db_show(ctx, oformat, ostyle, table): ctx.vlog("Content of database table [%s]", table) e = create_engine(ctx.gconfig.get("db", "rwurl")) res = e.execute("select * from {0}".format(table)) ioutils_dbres_print(ctx, oformat, ostyle, res)
def db_showcreate(ctx, oformat, ostyle, table): ctx.vlog('Show create of database table [%s]', table) e = create_engine(ctx.gconfig.get('db', 'rwurl')) res = e.execute('show create table {0}'.format(table)) ioutils_dbres_print(ctx, oformat, ostyle, res)