1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
# Copyright (c) 2024, Thomas Goirand <zigo@debian.org>
# Copyright (c) 2024, Philippe Serafin <philippe.serafin@infomaniak.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This script check a mounted device is full, and disable
# its matching rsync module if that is the case. The disk
# full limit is the first argument.
import argparse
import configparser
import io
import os
import shutil
import sys
from six.moves.configparser import ConfigParser
from swift.common.utils import config_true_value, ismount, get_logger
GiB = 1024 * 1024 * 1024
# Params for this function:
# logger: ref to the logger
# cp: config parser object containing the rsyncd.conf representation
# srvnode_dir: name of the drive we're inspecting (for example: sdb)
# free: bytes available in the current srvnode_dir that we're inspecting
# sec_name: name of the rsyncd.conf section we may need to patch
# rs: bytes reserved space in srvnode_dir
# mc: "normal" max connections (ie: when partition isn't full)
# for the given dir entry
def _patch_rsyncdconf_entry(logger, cp, srvnode_dir, free, sec_name, rs, mc):
# Calculate section name (ie: replace '{}' by drive name if present)
if '{}' in sec_name:
search_str = sec_name.format(srvnode_dir).strip('"')
else:
search_str = sec_name.strip('"')
# If referenced in the rsyncd.conf
# In old setup (Python 2), calling config_parser['something']
# raises an exception if the something section is not present
# in the config file. Which is why we must do try/except.
# I believe this try/except can be removed on more recent
# Python3 based setups.
try:
if cp[search_str]:
# If partition is full (ie: current_free_space < reserved_space),
# set 'max connections' to -1 to disable rsync
if free < rs:
cm = -1
else:
cm = mc
if int(cp[search_str]['max connections']) != cm:
if cm == -1:
logger.info('Disabling ' + search_str)
else:
logger.info('Enabling ' + search_str)
cp[search_str]['max connections'] = str(cm)
except KeyError:
pass
return cp
def configure_rsyncd_conf(account_rs, account_mc, account_secname,
container_rs, container_mc, container_secname,
object_rs, object_mc, object_secname,
storage_p, rsyncd_p, logger, sf=None):
# Load the rsyncd.conf file, adding
# a fake global section.
fake_section = '[fake_section_to_please_configobj]\n'
source_file = sf if sf else rsyncd_p
try:
with open(source_file, 'r') as f:
file_content = fake_section + f.read()
except Exception as err:
print("Unexpected error reading {}: {}".format(source_file, err))
return 1
cp = configparser.RawConfigParser()
if sys.version_info[0] == 2:
cp.read_string(file_content.decode('unicode-escape'))
else:
cp.read_string(file_content)
# For all dirs in /srv/node
for srvnode_dir in os.listdir(storage_p):
dirpath = os.path.join(storage_p, srvnode_dir)
# If the dir is mounted
if ismount(dirpath):
# Get free space of the partition
# shutil.disk_usage can be mocked in tests.
if sys.version_info[0] == 2:
space = os.statvfs(dirpath)
free = (space.f_bsize * space.f_bavail)
else:
free = shutil.disk_usage(dirpath).free
# Patch all 3 types of rsync module (a+c+o)
cp = _patch_rsyncdconf_entry(logger, cp, srvnode_dir, free,
account_secname, account_rs,
account_mc)
cp = _patch_rsyncdconf_entry(logger, cp, srvnode_dir, free,
container_secname, container_rs,
container_mc)
cp = _patch_rsyncdconf_entry(logger, cp, srvnode_dir, free,
object_secname, object_rs,
object_mc)
# Prepare our rsyncd.conf file before writing
iow = io.StringIO()
cp.write(iow)
file_out = iow.getvalue().replace(fake_section, '')
try:
with open(rsyncd_p, 'w') as f:
f.write(file_out)
except Exception as err:
logger.error("Unexpected error {}".format(err))
return 1
def main():
# Cli OPT parsing
parser = argparse.ArgumentParser(prog='swift-drive-full-checker',
description='Check if the drives of a '
'swift node are full, and '
'switches /etc/rsyncd.conf '
'"max connections" '
'accordingly.',
epilog='(c) 2024, Thomas Goirand, '
'Philippe Serafin & Infomaniak '
'Networks.')
parser.add_argument('-c', '--config-file',
default='/etc/swift/drive-full-checker.conf',
help='Path to the drive-full-checker.conf. Default to '
'/etc/swift/drive-full-checker.conf')
parser.add_argument('-s', '--source-file',
default='/etc/rsyncd.conf',
help='Path to the source file. Default to '
'/etc/rsyncd.conf')
args = parser.parse_args()
# disk-full-checker config file parsing
c = ConfigParser()
if not c.read(args.config_file):
print("Unable to read config file %s" % args.conf_path)
sys.exit(1)
CONF = dict(c.items('drive-full-checker'))
device_dir = CONF.get('device_dir', '/srv/node')
rsyncd_conf_path = CONF.get('rsyncd_conf_path', '/etc/rsyncd.conf')
account_max_connections = int(CONF.get('account_max_connections', 8))
account_reserved_space = int(CONF.get('account_reserved_space', 100)) * GiB
account_rsyncd_section_name = CONF.get('account_rsyncd_section_name',
' account_{} ')
container_max_connections = int(CONF.get('container_max_connections', 8))
container_reserved_space = (int(CONF.get('container_reserved_space', 100))
* GiB)
container_rsyncd_section_name = CONF.get('container_rsyncd_section_name',
' container_{} ')
object_max_connections = int(CONF.get('object_max_connections', 8))
object_reserved_space = int(CONF.get('object_reserved_space', 100)) * GiB
object_rsyncd_section_name = CONF.get('object_rsyncd_section_name',
' object_{} ')
# logging facility setup
log_to_console = config_true_value(CONF.get('log_to_console', False))
CONF['log_name'] = CONF.get('log_name', 'drive-full-checker')
logger = get_logger(CONF, log_to_console=log_to_console,
log_route='drive-full-checker')
return configure_rsyncd_conf(account_reserved_space,
account_max_connections,
account_rsyncd_section_name,
container_reserved_space,
container_max_connections,
container_rsyncd_section_name,
object_reserved_space,
object_max_connections,
object_rsyncd_section_name,
device_dir,
rsyncd_conf_path,
logger,
args.source_file)
if __name__ == "__main__":
sys.exit(main())
|