Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for directories and checking for free space #51385

Merged
merged 4 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 92 additions & 37 deletions salt/states/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@

# Import salt libs
from salt.ext.six import string_types
from os import path

__monitor__ = [
'status',
]
'status',
]


def _validate_int(name, value, limits=(), strip='%'):
Expand All @@ -74,12 +75,88 @@ def _validate_int(name, value, limits=(), strip='%'):
return value, comment


def status(name, maximum=None, minimum=None, absolute=False):
def _status_mount(name, ret, minimum, maximum, absolute, free, data):
# Get used space
if absolute:
used = int(data[name]['used'])
available = int(data[name]['available'])
else:
# POSIX-compliant df output reports percent used as 'capacity'
used = int(data[name]['capacity'].strip('%'))
available = 100 - used

# Collect return information
ret['data'] = data[name]
return _check_min_max(absolute, free, available, used, maximum, minimum, ret)


def _status_path(directory, ret, minimum, maximum, absolute, free):
if path.isdir(directory) is False:
ret['result'] = False
ret['comment'] += ('Directory {0} does not exist or is not a directory'.format(directory))
return ret

data = __salt__['status.diskusage'](directory)

if absolute:
used = int(data[directory]['total']) - int(data[directory]['available'])
available = int(data[directory]['available'])
else:
if int(data[directory]['total']) == 0:
used = 0
available = 0
else:
used = round(float(int(data[directory]['total']) - int(data[directory]['available'])) /
int(data[directory]['total']) * 100, 1)
available = round(float(data[directory]['available']) / int(data[directory]['total']) * 100, 1)

ret['data'] = data
return _check_min_max(absolute, free, available, used, maximum, minimum, ret)


def _check_min_max(absolute, free, available, used, maximum, minimum, ret):
unit = 'KB' if absolute else '%'
if minimum is not None:
if free:
if available < minimum:
ret['comment'] = ('Disk available space is below minimum'
' of {0} {2} at {1} {2}'
''.format(minimum, available, unit)
)
return ret
else:
if used < minimum:
ret['comment'] = ('Disk used space is below minimum'
' of {0} {2} at {1} {2}'
''.format(minimum, used, unit)
)
return ret
if maximum is not None:
if free:
if available > maximum:
ret['comment'] = ('Disk available space is above maximum'
' of {0} {2} at {1} {2}'
''.format(maximum, available, unit)
)
return ret
else:
if used > maximum:
ret['comment'] = ('Disk used space is above maximum'
' of {0} {2} at {1} {2}'
''.format(maximum, used, unit)
)
return ret
ret['comment'] = 'Disk used space in acceptable range'
ret['result'] = True
return ret


def status(name, maximum=None, minimum=None, absolute=False, free=False):
'''
Return the current disk usage stats for the named mount point

name
Disk mount with which to check used space
Disk mount or directory for which to check used space

maximum
The maximum disk utilization
Expand All @@ -92,6 +169,10 @@ def status(name, maximum=None, minimum=None, absolute=False):
the `absolute` flag to use kilobytes.

.. versionadded:: 2016.11.0

free
By default, `minimum` & `maximum` refer to the amount of used space.
Set to `True` to evaluate the free space instead.
'''
# Monitoring state, no changes will be made so no test interface needed
ret = {'name': name,
Expand All @@ -100,13 +181,6 @@ def status(name, maximum=None, minimum=None, absolute=False):
'changes': {},
'data': {}} # Data field for monitoring state

data = __salt__['disk.usage']()

# Validate name
if name not in data:
ret['result'] = False
ret['comment'] += 'Named disk mount not present '
return ret
# Validate extrema
if maximum is not None:
if not absolute:
Expand All @@ -126,30 +200,11 @@ def status(name, maximum=None, minimum=None, absolute=False):
if ret['comment']:
return ret

# Get used space
if absolute:
used = int(data[name]['used'])
else:
# POSIX-compliant df output reports percent used as 'capacity'
used = int(data[name]['capacity'].strip('%'))
data = __salt__['disk.usage']()

# Collect return information
ret['data'] = data[name]
unit = 'KB' if absolute else '%'
if minimum is not None:
if used < minimum:
ret['comment'] = ('Disk used space is below minimum'
' of {0} {2} at {1} {2}'
''.format(minimum, used, unit)
)
return ret
if maximum is not None:
if used > maximum:
ret['comment'] = ('Disk used space is above maximum'
' of {0} {2} at {1} {2}'
''.format(maximum, used, unit)
)
return ret
ret['comment'] = 'Disk used space in acceptable range'
ret['result'] = True
return ret
# Validate name
if name not in data:
ret['comment'] += ('Disk mount {0} not present. '.format(name))
return _status_path(name, ret, minimum, maximum, absolute, free)
else:
return _status_mount(name, ret, minimum, maximum, absolute, free, data)
Loading