etcd_volume.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """A health check for OpenShift clusters."""
  2. from openshift_checks import OpenShiftCheck, OpenShiftCheckException, get_var
  3. class EtcdVolume(OpenShiftCheck):
  4. """Ensures etcd storage usage does not exceed a given threshold."""
  5. name = "etcd_volume"
  6. tags = ["etcd", "health"]
  7. # Default device usage threshold. Value should be in the range [0, 100].
  8. default_threshold_percent = 90
  9. # Where to find ectd data, higher priority first.
  10. supported_mount_paths = ["/var/lib/etcd", "/var/lib", "/var", "/"]
  11. @classmethod
  12. def is_active(cls, task_vars):
  13. etcd_hosts = get_var(task_vars, "groups", "etcd", default=[]) or get_var(task_vars, "groups", "masters",
  14. default=[]) or []
  15. is_etcd_host = get_var(task_vars, "ansible_ssh_host") in etcd_hosts
  16. return super(EtcdVolume, cls).is_active(task_vars) and is_etcd_host
  17. def run(self, tmp, task_vars):
  18. mount_info = self._etcd_mount_info(task_vars)
  19. available = mount_info["size_available"]
  20. total = mount_info["size_total"]
  21. used = total - available
  22. threshold = get_var(
  23. task_vars,
  24. "etcd_device_usage_threshold_percent",
  25. default=self.default_threshold_percent
  26. )
  27. used_percent = 100.0 * used / total
  28. if used_percent > threshold:
  29. device = mount_info.get("device", "unknown")
  30. mount = mount_info.get("mount", "unknown")
  31. msg = "etcd storage usage ({:.1f}%) is above threshold ({:.1f}%). Device: {}, mount: {}.".format(
  32. used_percent, threshold, device, mount
  33. )
  34. return {"failed": True, "msg": msg}
  35. return {"changed": False}
  36. def _etcd_mount_info(self, task_vars):
  37. ansible_mounts = get_var(task_vars, "ansible_mounts")
  38. mounts = {mnt.get("mount"): mnt for mnt in ansible_mounts}
  39. for path in self.supported_mount_paths:
  40. if path in mounts:
  41. return mounts[path]
  42. paths = ', '.join(sorted(mounts)) or 'none'
  43. msg = "Unable to find etcd storage mount point. Paths mounted: {}.".format(paths)
  44. raise OpenShiftCheckException(msg)