2011-03-24 15:51:44 -07:00
|
|
|
#!/usr/bin/env python
|
2013-09-20 01:00:54 +08:00
|
|
|
# Copyright (c) 2010-2012 OpenStack Foundation
|
2010-07-12 17:03:45 -05:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import datetime
|
2012-11-19 13:02:34 +00:00
|
|
|
import glob
|
2010-07-12 17:03:45 -05:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2015-07-05 11:49:54 +05:30
|
|
|
|
|
|
|
|
|
|
|
from six.moves.configparser import ConfigParser
|
2010-07-12 17:03:45 -05:00
|
|
|
|
Add new features to swift-drive-audit
This patch adds two new features to swift-drive-audit. The first
is an option in the drive-audit.conf file that allows the operator
to prevent the drives ever being unmounted automatically,
regardless of the amount of errors present. This could be of
benefit in very small systems consisting of only one or two drives
where the operator would like to manually unmount/fix the
particular drive(s) and minimise any potential downtime.
The second is another option in drive-audit.conf that allows the
operator to select a recon directory. This directory will then
have a drive.recon file which will keep an up-to-date record of
the swift drives and any errors associated with them. An example
of the output would be as follows:
{"/srv/node/disk2": "0", "/srv/node/disk3": "25", "/srv/node/disk0": "0",
"/srv/node/disk1": "0", "/srv/node/disk10": "0", "/srv/node/disk7": "0",
"/srv/node/disk4": "137", "/srv/node/disk5": "0", "/srv/node/disk8": "0",
"/srv/node/disk9": "0", "/srv/node/disk6": "0", "/srv/node/disk11": "60"}
This would allow the operator to monitor the errors on the swift
drives without having to spend time searching through logs. Also, if
this is accepted, it should be possible to add an option to
swift-recon that would keep track of this at a system level.
Change-Id: Ib5dacf8622b7363e070c274c7c30c8ead448a055
2014-09-18 17:23:54 +01:00
|
|
|
from swift.common.utils import backward, get_logger, dump_recon_cache, \
|
|
|
|
config_true_value
|
2010-07-12 17:03:45 -05:00
|
|
|
|
2011-09-30 00:17:35 +00:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
def get_devices(device_dir, logger):
|
|
|
|
devices = []
|
|
|
|
for line in open('/proc/mounts').readlines():
|
|
|
|
data = line.strip().split()
|
|
|
|
block_device = data[0]
|
|
|
|
mount_point = data[1]
|
|
|
|
if mount_point.startswith(device_dir):
|
|
|
|
device = {}
|
|
|
|
device['mount_point'] = mount_point
|
|
|
|
device['block_device'] = block_device
|
|
|
|
try:
|
|
|
|
device_num = os.stat(block_device).st_rdev
|
2013-04-28 18:32:12 -07:00
|
|
|
except OSError:
|
2010-07-12 17:03:45 -05:00
|
|
|
# If we can't stat the device, then something weird is going on
|
|
|
|
logger.error("Error: Could not stat %s!" %
|
2012-11-26 18:15:21 -08:00
|
|
|
block_device)
|
2010-07-12 17:03:45 -05:00
|
|
|
continue
|
|
|
|
device['major'] = str(os.major(device_num))
|
|
|
|
device['minor'] = str(os.minor(device_num))
|
|
|
|
devices.append(device)
|
|
|
|
for line in open('/proc/partitions').readlines()[2:]:
|
2011-09-30 00:17:35 +00:00
|
|
|
major, minor, blocks, kernel_device = line.strip().split()
|
2010-07-12 17:03:45 -05:00
|
|
|
device = [d for d in devices
|
2012-11-26 18:15:21 -08:00
|
|
|
if d['major'] == major and d['minor'] == minor]
|
2010-07-12 17:03:45 -05:00
|
|
|
if device:
|
|
|
|
device[0]['kernel_device'] = kernel_device
|
|
|
|
return devices
|
|
|
|
|
2011-09-30 00:17:35 +00:00
|
|
|
|
2014-10-10 12:51:50 +01:00
|
|
|
def get_errors(error_re, log_file_pattern, minutes, logger):
|
2012-11-19 13:02:34 +00:00
|
|
|
# Assuming log rotation is being used, we need to examine
|
2014-02-07 16:06:12 +08:00
|
|
|
# recently rotated files in case the rotation occurred
|
2012-11-19 13:02:34 +00:00
|
|
|
# just before the script is being run - the data we are
|
|
|
|
# looking for may have rotated.
|
2013-06-28 14:51:15 -05:00
|
|
|
#
|
|
|
|
# The globbing used before would not work with all out-of-box
|
|
|
|
# distro setup for logrotate and syslog therefore moving this
|
|
|
|
# to the config where one can set it with the desired
|
|
|
|
# globbing pattern.
|
|
|
|
log_files = [f for f in glob.glob(log_file_pattern)]
|
2014-04-24 11:02:54 -04:00
|
|
|
try:
|
|
|
|
log_files.sort(key=lambda f: os.stat(f).st_mtime, reverse=True)
|
|
|
|
except (IOError, OSError) as exc:
|
|
|
|
logger.error(exc)
|
|
|
|
print(exc)
|
|
|
|
sys.exit(1)
|
2012-11-19 13:02:34 +00:00
|
|
|
|
|
|
|
now_time = datetime.datetime.now()
|
|
|
|
end_time = now_time - datetime.timedelta(minutes=minutes)
|
|
|
|
# kern.log does not contain the year so we need to keep
|
|
|
|
# track of the year and month in case the year recently
|
|
|
|
# ticked over
|
|
|
|
year = now_time.year
|
|
|
|
prev_entry_month = now_time.month
|
2010-07-12 17:03:45 -05:00
|
|
|
errors = {}
|
2012-11-19 13:02:34 +00:00
|
|
|
|
|
|
|
reached_old_logs = False
|
|
|
|
for path in log_files:
|
|
|
|
try:
|
|
|
|
f = open(path)
|
|
|
|
except IOError:
|
|
|
|
logger.error("Error: Unable to open " + path)
|
|
|
|
print("Unable to open " + path)
|
|
|
|
sys.exit(1)
|
|
|
|
for line in backward(f):
|
|
|
|
if '[ 0.000000]' in line \
|
|
|
|
or 'KERNEL supported cpus:' in line \
|
|
|
|
or 'BIOS-provided physical RAM map:' in line:
|
2012-09-11 14:22:11 +00:00
|
|
|
# Ignore anything before the last boot
|
2012-11-19 13:02:34 +00:00
|
|
|
reached_old_logs = True
|
|
|
|
break
|
|
|
|
# Solves the problem with year change - kern.log does not
|
|
|
|
# keep track of the year.
|
|
|
|
log_time_entry = line.split()[:3]
|
|
|
|
if log_time_entry[0] == 'Dec' and prev_entry_month == 'Jan':
|
|
|
|
year -= 1
|
|
|
|
prev_entry_month = log_time_entry[0]
|
|
|
|
log_time_string = '%s %s' % (year, ' '.join(log_time_entry))
|
2013-03-08 16:25:13 +00:00
|
|
|
try:
|
|
|
|
log_time = datetime.datetime.strptime(
|
|
|
|
log_time_string, '%Y %b %d %H:%M:%S')
|
|
|
|
except ValueError:
|
|
|
|
continue
|
2012-11-19 13:02:34 +00:00
|
|
|
if log_time > end_time:
|
2012-09-11 14:22:11 +00:00
|
|
|
for err in error_re:
|
|
|
|
for device in err.findall(line):
|
|
|
|
errors[device] = errors.get(device, 0) + 1
|
2012-11-19 13:02:34 +00:00
|
|
|
else:
|
|
|
|
reached_old_logs = True
|
|
|
|
break
|
|
|
|
if reached_old_logs:
|
|
|
|
break
|
|
|
|
return errors
|
2010-07-12 17:03:45 -05:00
|
|
|
|
2011-09-30 00:17:35 +00:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
def comment_fstab(mount_point):
|
|
|
|
with open('/etc/fstab', 'r') as fstab:
|
|
|
|
with open('/etc/fstab.new', 'w') as new_fstab:
|
|
|
|
for line in fstab:
|
|
|
|
parts = line.split()
|
2015-01-23 20:16:20 +09:00
|
|
|
if len(parts) > 2 \
|
|
|
|
and parts[1] == mount_point \
|
|
|
|
and not line.startswith('#'):
|
2011-09-30 00:17:35 +00:00
|
|
|
new_fstab.write('#' + line)
|
2010-07-12 17:03:45 -05:00
|
|
|
else:
|
|
|
|
new_fstab.write(line)
|
|
|
|
os.rename('/etc/fstab.new', '/etc/fstab')
|
|
|
|
|
2011-09-30 00:17:35 +00:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
if __name__ == '__main__':
|
|
|
|
c = ConfigParser()
|
|
|
|
try:
|
|
|
|
conf_path = sys.argv[1]
|
2011-01-26 14:31:33 -08:00
|
|
|
except Exception:
|
2016-07-07 18:00:05 +00:00
|
|
|
print("Usage: %s CONF_FILE" % sys.argv[0].split('/')[-1])
|
2010-07-12 17:03:45 -05:00
|
|
|
sys.exit(1)
|
|
|
|
if not c.read(conf_path):
|
2016-07-07 18:00:05 +00:00
|
|
|
print("Unable to read config file %s" % conf_path)
|
2010-07-12 17:03:45 -05:00
|
|
|
sys.exit(1)
|
|
|
|
conf = dict(c.items('drive-audit'))
|
|
|
|
device_dir = conf.get('device_dir', '/srv/node')
|
|
|
|
minutes = int(conf.get('minutes', 60))
|
|
|
|
error_limit = int(conf.get('error_limit', 1))
|
Add new features to swift-drive-audit
This patch adds two new features to swift-drive-audit. The first
is an option in the drive-audit.conf file that allows the operator
to prevent the drives ever being unmounted automatically,
regardless of the amount of errors present. This could be of
benefit in very small systems consisting of only one or two drives
where the operator would like to manually unmount/fix the
particular drive(s) and minimise any potential downtime.
The second is another option in drive-audit.conf that allows the
operator to select a recon directory. This directory will then
have a drive.recon file which will keep an up-to-date record of
the swift drives and any errors associated with them. An example
of the output would be as follows:
{"/srv/node/disk2": "0", "/srv/node/disk3": "25", "/srv/node/disk0": "0",
"/srv/node/disk1": "0", "/srv/node/disk10": "0", "/srv/node/disk7": "0",
"/srv/node/disk4": "137", "/srv/node/disk5": "0", "/srv/node/disk8": "0",
"/srv/node/disk9": "0", "/srv/node/disk6": "0", "/srv/node/disk11": "60"}
This would allow the operator to monitor the errors on the swift
drives without having to spend time searching through logs. Also, if
this is accepted, it should be possible to add an option to
swift-recon that would keep track of this at a system level.
Change-Id: Ib5dacf8622b7363e070c274c7c30c8ead448a055
2014-09-18 17:23:54 +01:00
|
|
|
recon_cache_path = conf.get('recon_cache_path', "/var/cache/swift")
|
2013-06-28 14:51:15 -05:00
|
|
|
log_file_pattern = conf.get('log_file_pattern',
|
|
|
|
'/var/log/kern.*[!.][!g][!z]')
|
2014-12-11 13:45:38 +05:30
|
|
|
log_to_console = config_true_value(conf.get('log_to_console', False))
|
2013-06-28 14:51:15 -05:00
|
|
|
error_re = []
|
|
|
|
for conf_key in conf:
|
|
|
|
if conf_key.startswith('regex_pattern_'):
|
|
|
|
error_pattern = conf[conf_key]
|
|
|
|
try:
|
|
|
|
r = re.compile(error_pattern)
|
|
|
|
except re.error:
|
|
|
|
sys.exit('Error: unable to compile regex pattern "%s"' %
|
|
|
|
error_pattern)
|
|
|
|
error_re.append(r)
|
|
|
|
if not error_re:
|
|
|
|
error_re = [
|
|
|
|
re.compile(r'\berror\b.*\b(sd[a-z]{1,2}\d?)\b'),
|
|
|
|
re.compile(r'\b(sd[a-z]{1,2}\d?)\b.*\berror\b'),
|
|
|
|
]
|
2011-02-10 00:01:07 -08:00
|
|
|
conf['log_name'] = conf.get('log_name', 'drive-audit')
|
2014-12-11 13:45:38 +05:30
|
|
|
logger = get_logger(conf, log_to_console=log_to_console,
|
|
|
|
log_route='drive-audit')
|
2010-07-12 17:03:45 -05:00
|
|
|
devices = get_devices(device_dir, logger)
|
|
|
|
logger.debug("Devices found: %s" % str(devices))
|
|
|
|
if not devices:
|
|
|
|
logger.error("Error: No devices found!")
|
Add new features to swift-drive-audit
This patch adds two new features to swift-drive-audit. The first
is an option in the drive-audit.conf file that allows the operator
to prevent the drives ever being unmounted automatically,
regardless of the amount of errors present. This could be of
benefit in very small systems consisting of only one or two drives
where the operator would like to manually unmount/fix the
particular drive(s) and minimise any potential downtime.
The second is another option in drive-audit.conf that allows the
operator to select a recon directory. This directory will then
have a drive.recon file which will keep an up-to-date record of
the swift drives and any errors associated with them. An example
of the output would be as follows:
{"/srv/node/disk2": "0", "/srv/node/disk3": "25", "/srv/node/disk0": "0",
"/srv/node/disk1": "0", "/srv/node/disk10": "0", "/srv/node/disk7": "0",
"/srv/node/disk4": "137", "/srv/node/disk5": "0", "/srv/node/disk8": "0",
"/srv/node/disk9": "0", "/srv/node/disk6": "0", "/srv/node/disk11": "60"}
This would allow the operator to monitor the errors on the swift
drives without having to spend time searching through logs. Also, if
this is accepted, it should be possible to add an option to
swift-recon that would keep track of this at a system level.
Change-Id: Ib5dacf8622b7363e070c274c7c30c8ead448a055
2014-09-18 17:23:54 +01:00
|
|
|
recon_errors = {}
|
2015-03-12 15:40:39 +00:00
|
|
|
total_errors = 0
|
Add new features to swift-drive-audit
This patch adds two new features to swift-drive-audit. The first
is an option in the drive-audit.conf file that allows the operator
to prevent the drives ever being unmounted automatically,
regardless of the amount of errors present. This could be of
benefit in very small systems consisting of only one or two drives
where the operator would like to manually unmount/fix the
particular drive(s) and minimise any potential downtime.
The second is another option in drive-audit.conf that allows the
operator to select a recon directory. This directory will then
have a drive.recon file which will keep an up-to-date record of
the swift drives and any errors associated with them. An example
of the output would be as follows:
{"/srv/node/disk2": "0", "/srv/node/disk3": "25", "/srv/node/disk0": "0",
"/srv/node/disk1": "0", "/srv/node/disk10": "0", "/srv/node/disk7": "0",
"/srv/node/disk4": "137", "/srv/node/disk5": "0", "/srv/node/disk8": "0",
"/srv/node/disk9": "0", "/srv/node/disk6": "0", "/srv/node/disk11": "60"}
This would allow the operator to monitor the errors on the swift
drives without having to spend time searching through logs. Also, if
this is accepted, it should be possible to add an option to
swift-recon that would keep track of this at a system level.
Change-Id: Ib5dacf8622b7363e070c274c7c30c8ead448a055
2014-09-18 17:23:54 +01:00
|
|
|
for device in devices:
|
|
|
|
recon_errors[device['mount_point']] = 0
|
2014-10-10 12:51:50 +01:00
|
|
|
errors = get_errors(error_re, log_file_pattern, minutes, logger)
|
2010-07-12 17:03:45 -05:00
|
|
|
logger.debug("Errors found: %s" % str(errors))
|
|
|
|
unmounts = 0
|
2011-09-30 00:17:35 +00:00
|
|
|
for kernel_device, count in errors.items():
|
2010-07-12 17:03:45 -05:00
|
|
|
if count >= error_limit:
|
2012-05-03 18:33:05 -07:00
|
|
|
device = \
|
|
|
|
[d for d in devices if d['kernel_device'] == kernel_device]
|
2010-07-12 17:03:45 -05:00
|
|
|
if device:
|
|
|
|
mount_point = device[0]['mount_point']
|
2011-09-30 00:17:35 +00:00
|
|
|
if mount_point.startswith(device_dir):
|
Add new features to swift-drive-audit
This patch adds two new features to swift-drive-audit. The first
is an option in the drive-audit.conf file that allows the operator
to prevent the drives ever being unmounted automatically,
regardless of the amount of errors present. This could be of
benefit in very small systems consisting of only one or two drives
where the operator would like to manually unmount/fix the
particular drive(s) and minimise any potential downtime.
The second is another option in drive-audit.conf that allows the
operator to select a recon directory. This directory will then
have a drive.recon file which will keep an up-to-date record of
the swift drives and any errors associated with them. An example
of the output would be as follows:
{"/srv/node/disk2": "0", "/srv/node/disk3": "25", "/srv/node/disk0": "0",
"/srv/node/disk1": "0", "/srv/node/disk10": "0", "/srv/node/disk7": "0",
"/srv/node/disk4": "137", "/srv/node/disk5": "0", "/srv/node/disk8": "0",
"/srv/node/disk9": "0", "/srv/node/disk6": "0", "/srv/node/disk11": "60"}
This would allow the operator to monitor the errors on the swift
drives without having to spend time searching through logs. Also, if
this is accepted, it should be possible to add an option to
swift-recon that would keep track of this at a system level.
Change-Id: Ib5dacf8622b7363e070c274c7c30c8ead448a055
2014-09-18 17:23:54 +01:00
|
|
|
if config_true_value(conf.get('unmount_failed_device',
|
|
|
|
True)):
|
|
|
|
logger.info("Unmounting %s with %d errors" %
|
|
|
|
(mount_point, count))
|
|
|
|
subprocess.call(['umount', '-fl', mount_point])
|
|
|
|
logger.info("Commenting out %s from /etc/fstab" %
|
|
|
|
(mount_point))
|
|
|
|
comment_fstab(mount_point)
|
|
|
|
unmounts += 1
|
2016-01-05 12:32:50 +01:00
|
|
|
else:
|
|
|
|
logger.info("Detected %s with %d errors "
|
|
|
|
"(Device not unmounted)" %
|
|
|
|
(mount_point, count))
|
Add new features to swift-drive-audit
This patch adds two new features to swift-drive-audit. The first
is an option in the drive-audit.conf file that allows the operator
to prevent the drives ever being unmounted automatically,
regardless of the amount of errors present. This could be of
benefit in very small systems consisting of only one or two drives
where the operator would like to manually unmount/fix the
particular drive(s) and minimise any potential downtime.
The second is another option in drive-audit.conf that allows the
operator to select a recon directory. This directory will then
have a drive.recon file which will keep an up-to-date record of
the swift drives and any errors associated with them. An example
of the output would be as follows:
{"/srv/node/disk2": "0", "/srv/node/disk3": "25", "/srv/node/disk0": "0",
"/srv/node/disk1": "0", "/srv/node/disk10": "0", "/srv/node/disk7": "0",
"/srv/node/disk4": "137", "/srv/node/disk5": "0", "/srv/node/disk8": "0",
"/srv/node/disk9": "0", "/srv/node/disk6": "0", "/srv/node/disk11": "60"}
This would allow the operator to monitor the errors on the swift
drives without having to spend time searching through logs. Also, if
this is accepted, it should be possible to add an option to
swift-recon that would keep track of this at a system level.
Change-Id: Ib5dacf8622b7363e070c274c7c30c8ead448a055
2014-09-18 17:23:54 +01:00
|
|
|
recon_errors[mount_point] = count
|
2015-03-12 15:40:39 +00:00
|
|
|
total_errors += count
|
Add new features to swift-drive-audit
This patch adds two new features to swift-drive-audit. The first
is an option in the drive-audit.conf file that allows the operator
to prevent the drives ever being unmounted automatically,
regardless of the amount of errors present. This could be of
benefit in very small systems consisting of only one or two drives
where the operator would like to manually unmount/fix the
particular drive(s) and minimise any potential downtime.
The second is another option in drive-audit.conf that allows the
operator to select a recon directory. This directory will then
have a drive.recon file which will keep an up-to-date record of
the swift drives and any errors associated with them. An example
of the output would be as follows:
{"/srv/node/disk2": "0", "/srv/node/disk3": "25", "/srv/node/disk0": "0",
"/srv/node/disk1": "0", "/srv/node/disk10": "0", "/srv/node/disk7": "0",
"/srv/node/disk4": "137", "/srv/node/disk5": "0", "/srv/node/disk8": "0",
"/srv/node/disk9": "0", "/srv/node/disk6": "0", "/srv/node/disk11": "60"}
This would allow the operator to monitor the errors on the swift
drives without having to spend time searching through logs. Also, if
this is accepted, it should be possible to add an option to
swift-recon that would keep track of this at a system level.
Change-Id: Ib5dacf8622b7363e070c274c7c30c8ead448a055
2014-09-18 17:23:54 +01:00
|
|
|
recon_file = recon_cache_path + "/drive.recon"
|
|
|
|
dump_recon_cache(recon_errors, recon_file, logger)
|
2016-10-17 19:46:57 +02:00
|
|
|
dump_recon_cache({'drive_audit_errors': total_errors}, recon_file, logger,
|
|
|
|
set_owner=conf.get("user", "swift"))
|
Add new features to swift-drive-audit
This patch adds two new features to swift-drive-audit. The first
is an option in the drive-audit.conf file that allows the operator
to prevent the drives ever being unmounted automatically,
regardless of the amount of errors present. This could be of
benefit in very small systems consisting of only one or two drives
where the operator would like to manually unmount/fix the
particular drive(s) and minimise any potential downtime.
The second is another option in drive-audit.conf that allows the
operator to select a recon directory. This directory will then
have a drive.recon file which will keep an up-to-date record of
the swift drives and any errors associated with them. An example
of the output would be as follows:
{"/srv/node/disk2": "0", "/srv/node/disk3": "25", "/srv/node/disk0": "0",
"/srv/node/disk1": "0", "/srv/node/disk10": "0", "/srv/node/disk7": "0",
"/srv/node/disk4": "137", "/srv/node/disk5": "0", "/srv/node/disk8": "0",
"/srv/node/disk9": "0", "/srv/node/disk6": "0", "/srv/node/disk11": "60"}
This would allow the operator to monitor the errors on the swift
drives without having to spend time searching through logs. Also, if
this is accepted, it should be possible to add an option to
swift-recon that would keep track of this at a system level.
Change-Id: Ib5dacf8622b7363e070c274c7c30c8ead448a055
2014-09-18 17:23:54 +01:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
if unmounts == 0:
|
|
|
|
logger.info("No drives were unmounted")
|