|
| 1 | +# Copyright (c) 2013 dotCloud, Inc. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | + |
| 16 | +import multiprocessing |
| 17 | +import os |
| 18 | + |
| 19 | +from oslo_config import cfg |
| 20 | +import psutil |
| 21 | + |
| 22 | +CONF = cfg.CONF |
| 23 | + |
| 24 | + |
| 25 | +def statvfs(): |
| 26 | + docker_path = CONF.docker.root_directory |
| 27 | + if not os.path.exists(docker_path): |
| 28 | + docker_path = '/' |
| 29 | + return psutil.disk_usage(docker_path) |
| 30 | + |
| 31 | + |
| 32 | +def get_disk_usage(): |
| 33 | + # This is the location where Docker stores its containers. It's currently |
| 34 | + # hardcoded in Docker so it's not configurable yet. |
| 35 | + st = statvfs() |
| 36 | + return { |
| 37 | + 'total': st.total, |
| 38 | + 'available': st.free, |
| 39 | + 'used': st.used |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +def get_total_vcpus(): |
| 44 | + return multiprocessing.cpu_count() |
| 45 | + |
| 46 | + |
| 47 | +def get_vcpus_used(containers): |
| 48 | + total_vcpus_used = 0 |
| 49 | + for container in containers: |
| 50 | + if isinstance(container, dict): |
| 51 | + total_vcpus_used += container.get('Config', {}).get( |
| 52 | + 'CpuShares', 0) / 1024 |
| 53 | + |
| 54 | + return total_vcpus_used |
| 55 | + |
| 56 | + |
| 57 | +def get_memory_usage(): |
| 58 | + vmem = psutil.virtual_memory() |
| 59 | + return { |
| 60 | + 'total': vmem.total, |
| 61 | + 'used': vmem.used |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | +def get_mounts(): |
| 66 | + with open('/proc/mounts') as f: |
| 67 | + return f.readlines() |
| 68 | + |
| 69 | + |
| 70 | +def get_cgroup_devices_path(): |
| 71 | + for ln in get_mounts(): |
| 72 | + fields = ln.split(' ') |
| 73 | + if fields[2] == 'cgroup' and 'devices' in fields[3].split(','): |
| 74 | + return fields[1] |
0 commit comments