OpenStack Invoke API for cloud host IO control, CGroup policy

Source: Internet
Author: User

# vim:tabstop=4 shiftwidth=4 softtabstop=4# Copyright (c) X.commerce, a business unit of EBay inc.# copyright U nited states government as represented by the# Administrator of the National Aeronautics and Space administration.# all Ri Ghts reserved.## Licensed under the Apache License, Version 2.0 (the "License"); You may# don't use this file except in compliance with the License. obtain# a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## unless required by a Pplicable law or agreed to in writing, software# distributed under the License are distributed on a "as is" BASIS, with out# 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 Virt UAL 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,jsonfrom Nova import log as Loggingfrom Nova import Flagsfrom Nova.periodic.blkio Import configfrom Nova import utilsfrom Nova.periodic.blkio import utils as Blkio_utilsflags = flags. Flagslog = 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 VMS port, return to" "" pid = List () Try:cmds = "" "Ps-ef |grep uuid|grep-v grep |awk ' {print $} '" "" Pid=os.popen (CMDS). Readlin ES () 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, with 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 (' Ce                Ntos ', 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-] Er Ror%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 $} '" "%name). Readli                                         NES () 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 (sel        f): "" "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 in 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 * + * + w_size = self.limitwriter * + * 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.WR Ite_bps_device "%vmtaskpath, Process_input ="%s%s "% (diskdevicenum,w_size), RU N_as_root=true) Log.info (_ ("" "[-william-] echo '%s,%s,%s,%s,%s ' Write OK" ""% (Self.pat h, device, Diskdevicenum, W_size, r_size)) Utils.execute (' Tee ', '%s/tasks '%vmtaskpath, Proce ss_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 ()


This article from "Welcome comments, Welcome to like" blog, please be sure to keep this source http://swq499809608.blog.51cto.com/797714/1412022

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.