设为首页 收藏本站
查看: 907|回复: 0

[经验分享] openstack 调用API 实现云主机的IO 控制,CGroup 策略

[复制链接]

尚未签到

发表于 2018-6-2 09:58:43 | 显示全部楼层 |阅读模式
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#    Single virtual machine io limit , reader 70/MB , writer 50/MB
"""
curl -d '{"auth": {"tenantName": "admin", "passwordCredentials": {"username": "admin", "password": "admin"}}}' -H "Content-type: application/json" http://127.0.0.1:35357/v2.0/tokens | python -m json.tool
curl -v -d '{"ioctlAction": {"instance_name":"instance-0000000c","limit_reader":"40MB","limit_writer":"60MB"}}' -i http://127.0.0.1:8774/v2/ba5a6a86a8224a70ba0ffde747d8a724/servers/e6b9cae7-8732-405d-9aaf-575643b4bbef/action -X POST -H "X-Auth-Project-Id: ba5a6a86a8224a70ba0ffde747d8a724" -H "Accept: application/json" -H "X-Auth-Token: 6f0c37595f384fd4a18766825cf49013" -H "Content-Type: application/json"
"""
import time,sys,os,libvirt,traceback,re,subprocess,json
from nova import log as logging
from nova import flags
from nova.periodic.blkio  import config
from nova import utils
from nova.periodic.blkio import utils as blkio_utils
FLAGS = flags.FLAGS
LOG = logging.getLogger(__name__)
class ActionIoctl(object):
def __init__(self,instance_name,limit_reader,limit_writer):
self.conn = libvirt.open(None)
self.limitReader = int(limit_reader[0:-2])
self.limitWriter = int(limit_writer[0:-2])
self.name = instance_name
self.path = FLAGS.instances_path
def diskDeviceNum(self,disk):
""" Func info:  Returns the number of hard disk,other is 8:0 """
if disk == '/dev/sda1' or disk == '/dev/hda1' or disk[:-1] == '/dev/sda' or disk[:-1] == '/dev/hda':
return '8:0'
elif disk == '/dev/sdb1' or disk == '/dev/hdb1' or disk[:-1] == '/dev/sdb' or disk[:-1] == '/dev/hdb':
return '8:16'
elif disk == '/dev/sdc1' or disk == '/dev/hdc1' or disk[:-1] == '/dev/sdc' or disk[:-1] == '/dev/hdc':
return '8:32'
elif disk == '/dev/sdd1' or disk == '/dev/hdd1' or disk[:-1] == '/dev/sdd' or disk[:-1] == '/dev/hdd':
return '8:48'
else:
return '8:16'
def vmsPIDall(self):
""" Getting all vm port, return to """
pid = list()
try:
cmds = """ps -ef |grep uuid|grep -v grep |awk '{print $2}'"""
pid=os.popen(cmds).readlines()
return pid
except Exception,e:
LOG.error(_('[-william-] Error %s'%e))
return
def cgroupPath(self):
'''
Func info : Return Cgroup path ,Because the system is not the same,
the Cgroup path is different also.
By default, have RedHat, Centos, Ubuntu  linux  ...
osType : The current system version , And return to ...
Cpath  : Cgroup path
'''
osType = open('/etc/issue').readlines()
for i in osType:
if re.match('Ubuntu',i):
Cpath = '/sys/fs/cgroup/'
return Cpath
elif re.match('CentOS',i):
Cpath = '/cgroup/'
return Cpath
elif re.match('Red Hat',i):
Cpath = '/cgroup/'
return Cpath
else:
return '/sys/fs/cgroup/'
def exeCmd(self,cmds):
''' Perform system command '''
LOG.info(_('exeCmd  %s'%cmds))
try:
exec_process = subprocess.Popen(cmds,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell = True)
exec_process.wait()
rest = exec_process.stdout.read()
err = exec_process.stderr.read()
if err == '':
return rest
else:
LOG.error(_('[-william-] return values is null'))
except Exception,e:
LOG.error(_('[-william-] Error %s') %e)
return
''' Geting vmName name  and pid '''
def get_vm_pid(self,name):
pid = self.conn.listDomainsID()
try:
pid = os.popen("""ps -ef |grep %s |grep -v grep |awk '{print $2}'""" %name).readlines()
return int(pid[0])
except:
LOG.error(_('[-william-] Error %s' \
%traceback.format_exc()))
return
def _vm_pids(self):
try:
return os.popen('pidof kvm').read().split()
except:
return
def work(self):
"""
Func info :  working func ................
Cpath : is _Cpath function return values ...
path :  The current virtual machine IMG file path
vm_count: All of the current node number of virtual machines
size :  Limit size
mountInfo : mounting point
"""
Cpath = self.cgroupPath()
vm_pid = self.get_vm_pid(self.name)
LOG.info(_('[- william -] %s , %s' %(self.name,vm_pid)))
r_size = self.limitReader * 1000 * 1000
w_size = self.limitWriter * 1000 * 1000
device = blkio_utils.instance_path(self.path)
diskDeviceNum = self.diskDeviceNum(device)
vmTaskPath = "%s/blkio/%s"%(Cpath,self.name)
utils.execute("mkdir","-p",vmTaskPath,run_as_root=True)
utils.execute("tee" , "%s/blkio.throttle.read_bps_device"%vmTaskPath,\
process_input = "%s %s"%(diskDeviceNum,r_size),\
run_as_root=True)
utils.execute("tee" , "%s/blkio.throttle.write_bps_device"%vmTaskPath, \
process_input = "%s %s"%(diskDeviceNum,w_size) ,\
run_as_root=True)
LOG.info(_(""" [-william-] echo  '%s, %s, %s , %s , %s ' write oK """
%(self.path , device, diskDeviceNum, w_size, r_size)))
utils.execute('tee', '%s/tasks'%vmTaskPath,\
process_input= str(vm_pid) ,\
run_as_root=True)
LOG.info(_('[- william-] append pid %s in task' %vm_pid))
if __name__ == "__main__":
sc = ActionIoctl('instance-0000000c','40MB','30MB')
sc.work()  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-506045-1-1.html 上篇帖子: openstack内存复用处理方法 下篇帖子: Install and Configure OpenStack Orchestration Service (Heat)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表