python如何计算节点创建cgroups绑定虚拟核心
发表于:2025-02-14 作者:千家信息网编辑
千家信息网最后更新 2025年02月14日,这篇文章给大家分享的是有关python如何计算节点创建cgroups绑定虚拟核心的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。 import MySQLdbimport o
千家信息网最后更新 2025年02月14日python如何计算节点创建cgroups绑定虚拟核心
这篇文章给大家分享的是有关python如何计算节点创建cgroups绑定虚拟核心的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
import MySQLdbimport osimport signalimport shleximport subprocessfrom eventlet.green import subprocess as green_subprocessfrom eventlet import greenthreadimport loggingimport stringimport timemysql_host = "10.160.0.120"mysql_db = "nova"mysql_user = "nova"mysql_passwd = "87da3417bb3a42ee"mysql_port = 3306mysql_charset = "utf8"cgroups_hierarchy = "/home/cgroups/cpu"LOG = logging.getLogger(__name__)def create_process(cmd, root_helper=None, addl_env=None): if root_helper: cmd = shlex.split(root_helper) + cmd cmd = map(str, cmd) LOG.debug(("Running command: %s"), cmd) env = os.environ.copy() if addl_env: env.update(addl_env) obj = subprocess_popen(cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) return obj, cmddef execute(cmd, root_helper=None, process_input=None, addl_env=None, check_exit_code=True, return_stderr=False): try: obj, cmd = create_process(cmd, root_helper=root_helper, addl_env=addl_env) _stdout, _stderr = (process_input and obj.communicate(process_input) or obj.communicate()) obj.stdin.close() m = ("\nCommand: %(cmd)s\nExit code: %(code)s\nStdout: %(stdout)r\n" "Stderr: %(stderr)r") % {'cmd': cmd, 'code': obj.returncode, 'stdout': _stdout, 'stderr': _stderr} LOG.debug(m) if obj.returncode and check_exit_code: raise RuntimeError(m) finally: greenthread.sleep(0) return return_stderr and (_stdout, _stderr) or _stdoutdef _subprocess_setup(): signal.signal(signal.SIGPIPE, signal.SIG_DFL)def subprocess_popen(args, stdin=None, stdout=None, stderr=None, shell=False, env=None): return green_subprocess.Popen(args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr, preexec_fn=_subprocess_setup, close_fds=True, env=env)def query_db_of_local_vms(): """ query controller node db for information about vms running on local host """ conn = None try: conn = MySQLdb.connect(host=mysql_host, user=mysql_user, passwd=mysql_passwd, db=mysql_db, port=mysql_port, charset=mysql_charset) except Exception as e: #LOG.error("Fail to connect mysql .") raise e else: LOG.info("Connect to mysql .") local_compute_name = get_host() sql = "SELECT vcpus,uuid FROM instances WHERE host='%s' AND deleted=0"%local_compute_name vms = {} try: cursor = conn.cursor() cursor.execute(sql) result = cursor.fetchall() cursor.close() conn.close() for item in result: vms.update({item[1]:item[0]}) except Exception as ex: #LOG.error("Exception happens while querying mysql.") raise ex else: return vmsdef get_host(): return os.environ['HOSTNAME']def get_ip(): """ parse /etc/hosts to get local ip""" df = open("/etc/hosts") hosts = df.readlines() hostname = get_host() host_rec = [line for line in hosts if hostname in line and "#" not in line][0] return host_rec.split()[0].strip()def check_cglib(): """ check required cglibs""" check_pkgs = "rpm -qa" cglib_kw = "libcgroup" cmd = check_pkgs.split() pkgs = execute(cmd,root_helper=None) cglibs = [pkg for pkg in pkgs.split("\n") if cglib_kw in pkg] if len(cglibs)==0: print "libcgroup-x.xx-x.xx.x86_64 not installed.Exit." exit(1)def init_cgroups(): """ensure cgrouplib installed""" check_cglib() """ create cgroups base architecture """ """clear old cgroups settings mounted on cgroups_hierarchy """ cg_clear() """create base cgroups hierarchy""" create_hierarchy = ("mkdir -p %s"%cgroups_hierarchy).split() execute(create_hierarchy,root_helper=None) """mount target subsystem,that's cpuset,to established hierarchy.""" mount_cg = ("mount -t cgroup -o cpuset cpuset2015 %s"%cgroups_hierarchy).split() execute(mount_cg,root_helper=None)def cpuinfo(): """ get cpu counts and memory nodes """ cpuinfo = "lscpu" cmd = cpuinfo.split() retv = execute(cmd,root_helper=None) memNodes = retv.count("NUMA node")-1 cpus = string.atoi([line for line in retv.split("\n") if "CPU(s)" in line][0].split(":")[1].strip()) return {"cpu_count":cpus,"memNodes":memNodes}def assign_cores(): """ cal out schemes of core binding for instances running on local host""" vmDetails = query_db_of_local_vms() cpuinfos = cpuinfo() def assign(require,all): tmp = {} i=0 for k,v in require.iteritems(): cores = [p%all for p in xrange(i,i+v)] i=i+v tmp.update({k:{"cores":cores,"memNodes":"0-%d"%(cpuinfos["memNodes"]-1)}}) return tmp vmDetails = assign(vmDetails,cpuinfos["cpu_count"]) return vmDetailsdef get_pids(vmDetails): query_pids = "ps aux" cmd = query_pids.split() retv = execute(cmd,root_helper=None) qemu_kvm_processes =[p for p in retv.split("\n") if "qemu-kvm" in p and len(p)>100] tmp = {} for vmDetail in vmDetails: tmp.update({vmDetail:vmDetails[vmDetail]}) for qkp in qemu_kvm_processes: if vmDetail not in qkp: continue else: pid = string.atoi(qkp.split()[1]) tmp[vmDetail]["pid"] = pid return tmp def create_cgs_for_instances(vmDetails): for item in vmDetails: detail = vmDetails[item] if "pid" not in detail or "cores" not in detail or "memNodes" not in detail: print "Instance %s invalid"%item continue else: """ create private group for each instance""" instance_cg = "%s/%s"%(cgroups_hierarchy,item) create_cg_for_instance = ("mkdir -p %s"%instance_cg).split() execute(create_cg_for_instance,root_helper=None) setcpu_txt = "%s"%detail["cores"][0] if len(detail["cores"]) > 1: for core_num in detail["cores"][1:]: setcpu_txt = "%s,%s"%(setcpu_txt,core_num) setmem_txt = detail["memNodes"] fd_setcpu = open("%s/cpuset.cpus"%instance_cg,"w") fd_setcpu.write(setcpu_txt) fd_setcpu.close() fd_setmem = open("%s/cpuset.mems"%instance_cg,"w") fd_setmem.write(setmem_txt) fd_setmem.close() print detail["pid"] fd_task = open("%s/tasks"%instance_cg,"w") fd_task.write("%s"%detail["pid"]) fd_task.close() def cg_clear(): """ destroy existing cgroups """ cgclear = "umount -t cgroup %s"%cgroups_hierarchy cmd = cgclear.split() try: execute(cmd,root_helper=None) except Exception as e: passdef periodic_task(): init_cgroups() hosted_vms = assign_cores() hosted_vms = get_pids(hosted_vms) create_cgs_for_instances(hosted_vms) if __name__ == "__main__": periodic_task() while True: time.sleep(3*60) print "Loop Job." periodic_task()
该脚本作用是:连接数据库查询出运行在当前宿主下的客户机的cpu配额,然后再根据当前宿主的CPU信息,做出虚拟核心的分配;创建cgroups 绑定虚拟核心,实现资源隔离.
该脚本后台运行:
nohup python /path/to/this/scripy &
感谢各位的阅读!关于"python如何计算节点创建cgroups绑定虚拟核心"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
核心
节点
内容
宿主
更多
篇文章
脚本
运行
不错
实用
作用
信息
后台
客户
客户机
数据
数据库
文章
看吧
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
3868数据库应用0005
大型数据库服务器怎样散热的
游戏服务器电源设计
网络安全技术全套教学视频
银行做网络安全
RUP软件开发的四个阶段
天翼云服务器卡顿
网络安全工作整改报告
大学软件开发考试
2019网络安全就业
软件开发些啥语言
服务器不识别硬盘
灵山奇缘有几个服务器
为什么世界要关注网络安全
小学网络安全自查总结
滦州数据网络技术售后保障
买云服务器好还是
小学生网络安全课件ppt
高速收费软件开发 招聘
网络安全 涉密
服务器管理系统芯片有哪些
国家网络安全周文登
青岛速驰网络技术有限公司
网络安全属于信息安全的分支
数据库服务器 要求
贵阳万商网络技术公司
方舟服务器服主可以干嘛
数据库可视化系统设计
我国的计算机网络技术
波峰炉初始数据库失败