Exemplo n.º 1
0
def sysctl_kernel(key, value=None):
    """(Very) partial implementation of sysctl, for kernel params"""
    if value is not None:
        # write
        utils.write_one_line("/proc/sys/kernel/%s" % key, str(value))
    else:
        # read
        out = utils.read_one_line("/proc/sys/kernel/%s" % key)
        return int(re.search(r"\d+", out).group(0))
Exemplo n.º 2
0
 def get_provisioning_mode(device, host_id):
     """
     Get disk provisioning_mode, value usually is 'writesame_16', depends
     on params for scsi_debug module.
     """
     device_name = os.path.basename(device)
     path = "/sys/block/%s/device/scsi_disk" % device_name
     path += "/%s/provisioning_mode" % host_id
     return utils.read_one_line(path).strip()
Exemplo n.º 3
0
def sysctl_kernel(key, value=None):
    """(Very) partial implementation of sysctl, for kernel params"""
    if value is not None:
        # write
        utils.write_one_line('/proc/sys/kernel/%s' % key, str(value))
    else:
        # read
        out = utils.read_one_line('/proc/sys/kernel/%s' % key)
        return int(re.search(r'\d+', out).group(0))
Exemplo n.º 4
0
 def get_provisioning_mode(device, host_id):
     """
     Get disk provisioning_mode, value usually is 'writesame_16', depends
     on params for scsi_debug module.
     """
     device_name = os.path.basename(device)
     path = "/sys/block/%s/device/scsi_disk" % device_name
     path += "/%s/provisioning_mode" % host_id
     return utils.read_one_line(path).strip()
Exemplo n.º 5
0
 def get_allocation_bitmap():
     """
     Get block allocation bitmap
     """
     path = "/sys/bus/pseudo/drivers/scsi_debug/map"
     try:
         return utils.read_one_line(path).strip()
     except IOError:
         logging.warn("block allocation bitmap not exists")
     return ""
Exemplo n.º 6
0
 def get_allocation_bitmap():
     """
     Get block allocation bitmap
     """
     path = "/sys/bus/pseudo/drivers/scsi_debug/map"
     try:
         return utils.read_one_line(path).strip()
     except IOError:
         logging.warn("block allocation bitmap not exists")
     return ""
Exemplo n.º 7
0
def sysctl(key, value=None):
    """Generic implementation of sysctl, to read and write.

    @param key: A location under /proc/sys
    @param value: If not None, a value to write into the sysctl.

    @return The single-line sysctl value as a string.
    """
    path = '/proc/sys/%s' % key
    if value is not None:
        utils.write_one_line(path, str(value))
    return utils.read_one_line(path)
Exemplo n.º 8
0
def sysctl(key, value=None):
    """Generic implementation of sysctl, to read and write.

    :param key: A location under /proc/sys
    :param value: If not None, a value to write into the sysctl.

    :return: The single-line sysctl value as a string.
    """
    path = "/proc/sys/%s" % key
    if value is not None:
        utils.write_one_line(path, str(value))
    return utils.read_one_line(path)
Exemplo n.º 9
0
def cpuload(cpu_num, prev_total=0, prev_idle=0):
    """
    Calculate average CPU usage.

    :param prev_total: previous total value (default to 0).
    :param prev_idle: previous idle value (default to 0).

    :return: float number of CPU usage.
    """
    cpu_stat = utils.read_one_line('/proc/stat').lstrip('cpu')
    cpu_stat = [int(_) for _ in cpu_stat.split()]
    total, idle = (0, cpu_stat[3])
    for val in cpu_stat:
        total += val
    diff_total = total - prev_total
    diff_idle = idle - prev_idle
    load = float((1000 * (diff_total - diff_idle) / diff_total + 5) / 10)
    load = load / cpu_num
    return (total, idle, load)
Exemplo n.º 10
0
 def test_strips_read_lines(self):
     self._create_test_file("abc   \n")
     self.assertEqual("abc   ", utils.read_one_line("filename"))
     self.god.check_playback()
Exemplo n.º 11
0
 def test_preserves_leading_whitespace(self):
     self._create_test_file("   has leading whitespace")
     self.assertEqual("   has leading whitespace",
                      utils.read_one_line("filename"))
Exemplo n.º 12
0
 def test_works_on_file_with_no_newlines(self):
     self._create_test_file("line but no newline")
     self.assertEqual("line but no newline",
                      utils.read_one_line("filename"))
     self.god.check_playback()
Exemplo n.º 13
0
 def test_works_on_file_with_no_newlines(self):
     self._create_test_file("line but no newline")
     self.assertEqual("line but no newline",
                      utils.read_one_line("filename"))
     self.god.check_playback()
Exemplo n.º 14
0
 def test_strips_read_lines(self):
     self._create_test_file("abc   \n")
     self.assertEqual("abc   ", utils.read_one_line("filename"))
     self.god.check_playback()
Exemplo n.º 15
0
 def test_drops_extra_lines(self):
     self._create_test_file("line 1\nline 2\nline 3\n")
     self.assertEqual("line 1", utils.read_one_line("filename"))
     self.god.check_playback()
Exemplo n.º 16
0
 def test_works_on_empty_file(self):
     self._create_test_file("")
     self.assertEqual("", utils.read_one_line("filename"))
     self.god.check_playback()
Exemplo n.º 17
0
 def test_drops_extra_lines(self):
     self._create_test_file("line 1\nline 2\nline 3\n")
     self.assertEqual("line 1", utils.read_one_line("filename"))
     self.god.check_playback()
Exemplo n.º 18
0
 def test_preserves_leading_whitespace(self):
     self._create_test_file("   has leading whitespace")
     self.assertEqual("   has leading whitespace",
                      utils.read_one_line("filename"))
Exemplo n.º 19
0
 def test_works_on_empty_file(self):
     self._create_test_file("")
     self.assertEqual("", utils.read_one_line("filename"))
     self.god.check_playback()