| 1 |
# This file is part of Ansible |
| 2 |
# |
| 3 |
# Ansible is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# Ansible is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
from __future__ import annotations |
| 17 |
|
| 18 |
import fcntl |
| 19 |
import os |
| 20 |
|
| 21 |
|
| 22 |
def get_file_content(path, default=None, strip=True): |
| 23 |
""" |
| 24 |
Return the contents of a given file path |
| 25 |
|
| 26 |
:args path: path to file to return contents from |
| 27 |
:args default: value to return if we could not read file |
| 28 |
:args strip: controls if we strip whitespace from the result or not |
| 29 |
|
| 30 |
:returns: String with file contents (optionally stripped) or 'default' value |
| 31 |
""" |
| 32 |
data = default |
| 33 |
if os.path.exists(path) and os.access(path, os.R_OK): |
| 34 |
datafile = None |
| 35 |
try: |
| 36 |
datafile = open(path) |
| 37 |
try: |
| 38 |
# try to not enter kernel 'block' mode, which prevents timeouts |
| 39 |
fd = datafile.fileno() |
| 40 |
flag = fcntl.fcntl(fd, fcntl.F_GETFL) |
| 41 |
fcntl.fcntl(fd, fcntl.F_SETFL, flag | os.O_NONBLOCK) |
| 42 |
except Exception: |
| 43 |
pass # not required to operate, but would have been nice! |
| 44 |
|
| 45 |
# actually read the data |
| 46 |
data = datafile.read() |
| 47 |
|
| 48 |
if strip: |
| 49 |
data = data.strip() |
| 50 |
|
| 51 |
if len(data) == 0: |
| 52 |
data = default |
| 53 |
|
| 54 |
except Exception: |
| 55 |
# ignore errors as some jails/containers might have readable permissions but not allow reads |
| 56 |
pass |
| 57 |
finally: |
| 58 |
if datafile is not None: |
| 59 |
datafile.close() |
| 60 |
|
| 61 |
return data |
| 62 |
|
| 63 |
|
| 64 |
def get_file_lines(path, strip=True, line_sep=None): |
| 65 |
"""get list of lines from file""" |
| 66 |
data = get_file_content(path, strip=strip) |
| 67 |
if data: |
| 68 |
if line_sep is None: |
| 69 |
ret = data.splitlines() |
| 70 |
else: |
| 71 |
if len(line_sep) == 1: |
| 72 |
ret = data.rstrip(line_sep).split(line_sep) |
| 73 |
else: |
| 74 |
ret = data.split(line_sep) |
| 75 |
else: |
| 76 |
ret = [] |
| 77 |
return ret |
| 78 |
|
| 79 |
|
| 80 |
def get_mount_size(mountpoint): |
| 81 |
mount_size = {} |
| 82 |
|
| 83 |
try: |
| 84 |
statvfs_result = os.statvfs(mountpoint) |
| 85 |
mount_size['size_total'] = statvfs_result.f_frsize * statvfs_result.f_blocks |
| 86 |
mount_size['size_available'] = statvfs_result.f_frsize * (statvfs_result.f_bavail) |
| 87 |
|
| 88 |
# Block total/available/used |
| 89 |
mount_size['block_size'] = statvfs_result.f_bsize |
| 90 |
mount_size['block_total'] = statvfs_result.f_blocks |
| 91 |
mount_size['block_available'] = statvfs_result.f_bavail |
| 92 |
mount_size['block_used'] = mount_size['block_total'] - mount_size['block_available'] |
| 93 |
|
| 94 |
# Inode total/available/used |
| 95 |
mount_size['inode_total'] = statvfs_result.f_files |
| 96 |
mount_size['inode_available'] = statvfs_result.f_favail |
| 97 |
mount_size['inode_used'] = mount_size['inode_total'] - mount_size['inode_available'] |
| 98 |
except OSError: |
| 99 |
pass |
| 100 |
|
| 101 |
return mount_size |