| 1 |
#!/usr/bin/env python |
| 2 |
import sys |
| 3 |
import os |
| 4 |
import fnmatch |
| 5 |
import shlex |
| 6 |
from subprocess import call |
| 7 |
from optparse import OptionParser |
| 8 |
from distutils.spawn import find_executable |
| 9 |
from os.path import basename |
| 10 |
from six.moves import input |
| 11 |
|
| 12 |
# On Debian systems whisper-resize.py is available as whisper-resize |
| 13 |
whisperResizeExecutable = find_executable("whisper-resize.py") |
| 14 |
if whisperResizeExecutable is None: |
| 15 |
whisperResizeExecutable = find_executable("whisper-resize") |
| 16 |
if whisperResizeExecutable is None: |
| 17 |
# Probably will fail later, set it nevertheless |
| 18 |
whisperResizeExecutable = "whisper-resize.py" |
| 19 |
|
| 20 |
option_parser = OptionParser( |
| 21 |
usage='''%prog storagePath configPath |
| 22 |
|
| 23 |
storagePath the Path to the directory containing whisper files (CAN NOT BE A |
| 24 |
SUBDIR, use --subdir for that) |
| 25 |
configPath the path to your carbon config files |
| 26 |
''', version="%prog 0.1") |
| 27 |
|
| 28 |
option_parser.add_option( |
| 29 |
'--doit', default=False, action='store_true', |
| 30 |
help="This is not a drill, lets do it") |
| 31 |
option_parser.add_option( |
| 32 |
'-q', '--quiet', default=False, action='store_true', |
| 33 |
help="Display extra debugging info") |
| 34 |
option_parser.add_option( |
| 35 |
'--subdir', default=None, |
| 36 |
type='string', help="only process a subdir of whisper files") |
| 37 |
option_parser.add_option( |
| 38 |
'--carbonlib', default=None, |
| 39 |
type='string', help="folder where the carbon lib files are if its not in your path already") |
| 40 |
option_parser.add_option( |
| 41 |
'--whisperlib', default=None, |
| 42 |
type='string', help="folder where the whisper lib files are if its not in your path already") |
| 43 |
option_parser.add_option( |
| 44 |
'--confirm', default=False, action='store_true', |
| 45 |
help="ask for comfirmation prior to resizing a whisper file") |
| 46 |
option_parser.add_option( |
| 47 |
'-x', '--extra_args', default='', type='string', |
| 48 |
help="pass any additional arguments to the %s script" % |
| 49 |
basename(whisperResizeExecutable)) |
| 50 |
|
| 51 |
(options, args) = option_parser.parse_args() |
| 52 |
|
| 53 |
if len(args) < 2: |
| 54 |
option_parser.print_help() |
| 55 |
sys.exit(1) |
| 56 |
|
| 57 |
storagePath = args[0] |
| 58 |
configPath = args[1] |
| 59 |
|
| 60 |
# check to see if we are processing a subfolder |
| 61 |
# we need to have a separate config option for this since |
| 62 |
# otherwise the metric test thinks the metric is at the root |
| 63 |
# of the storage path and can match schemas incorrectly |
| 64 |
if options.subdir is None: |
| 65 |
processPath = args[0] |
| 66 |
else: |
| 67 |
processPath = options.subdir |
| 68 |
|
| 69 |
# Injecting the Whisper Lib Path if needed |
| 70 |
if options.whisperlib is not None: |
| 71 |
sys.path.insert(0, options.whisperlib) |
| 72 |
|
| 73 |
try: |
| 74 |
import whisper |
| 75 |
except ImportError: |
| 76 |
raise SystemExit('[ERROR] Can\'t find the whisper module, try using ' |
| 77 |
'--whisperlib to explicitly include the path') |
| 78 |
|
| 79 |
# Injecting the Carbon Lib Path if needed |
| 80 |
if options.carbonlib is not None: |
| 81 |
sys.path.insert(0, options.carbonlib) |
| 82 |
|
| 83 |
try: |
| 84 |
from carbon.conf import settings |
| 85 |
except ImportError: |
| 86 |
raise SystemExit('[ERROR] Can\'t find the carbon module, try using ' |
| 87 |
'--carbonlib to explicitly include the path') |
| 88 |
|
| 89 |
# carbon.conf not seeing the config files so give it a nudge |
| 90 |
settings.CONF_DIR = configPath |
| 91 |
settings.LOCAL_DATA_DIR = storagePath |
| 92 |
|
| 93 |
# import these once we have the settings figured out |
| 94 |
from carbon.storage import loadStorageSchemas, loadAggregationSchemas |
| 95 |
|
| 96 |
# Load the Defined Schemas from our config files |
| 97 |
schemas = loadStorageSchemas() |
| 98 |
agg_schemas = loadAggregationSchemas() |
| 99 |
|
| 100 |
|
| 101 |
# check to see if a metric needs to be resized based on the current config |
| 102 |
def processMetric(fullPath, schemas, agg_schemas): |
| 103 |
""" |
| 104 |
method to process a given metric, and resize it if necessary |
| 105 |
|
| 106 |
Parameters: |
| 107 |
fullPath - full path to the metric whisper file |
| 108 |
schemas - carbon storage schemas loaded from config |
| 109 |
agg_schemas - carbon storage aggregation schemas load from confg |
| 110 |
|
| 111 |
""" |
| 112 |
schema_config_args = '' |
| 113 |
schema_file_args = '' |
| 114 |
rebuild = False |
| 115 |
messages = '' |
| 116 |
|
| 117 |
# get archive info from whisper file |
| 118 |
info = whisper.info(fullPath) |
| 119 |
|
| 120 |
# get graphite metric name from fullPath |
| 121 |
metric = getMetricFromPath(fullPath) |
| 122 |
|
| 123 |
# loop the carbon-storage schemas |
| 124 |
for schema in schemas: |
| 125 |
if schema.matches(metric): |
| 126 |
# returns secondsPerPoint and points for this schema in tuple format |
| 127 |
archive_config = [archive.getTuple() for archive in schema.archives] |
| 128 |
break |
| 129 |
|
| 130 |
# loop through the carbon-aggregation schemas |
| 131 |
for agg_schema in agg_schemas: |
| 132 |
if agg_schema.matches(metric): |
| 133 |
xFilesFactor, aggregationMethod = agg_schema.archives |
| 134 |
break |
| 135 |
|
| 136 |
if xFilesFactor is None: |
| 137 |
xFilesFactor = 0.5 |
| 138 |
if aggregationMethod is None: |
| 139 |
aggregationMethod = 'average' |
| 140 |
|
| 141 |
# loop through the bucket tuples and convert to string format for resizing |
| 142 |
for retention in archive_config: |
| 143 |
current_schema = '%s:%s ' % (retention[0], retention[1]) |
| 144 |
schema_config_args += current_schema |
| 145 |
|
| 146 |
# loop through the current files bucket sizes and convert to string format |
| 147 |
# to compare for resizing |
| 148 |
for fileRetention in info['archives']: |
| 149 |
current_schema = '%s:%s ' % (fileRetention['secondsPerPoint'], fileRetention['points']) |
| 150 |
schema_file_args += current_schema |
| 151 |
|
| 152 |
# check to see if the current and configured schemas are the same or rebuild |
| 153 |
if (schema_config_args != schema_file_args): |
| 154 |
rebuild = True |
| 155 |
messages += 'updating Retentions from: %s to: %s \n' % \ |
| 156 |
(schema_file_args, schema_config_args) |
| 157 |
|
| 158 |
# only care about the first two decimals in the comparison since there is |
| 159 |
# floaty stuff going on. |
| 160 |
info_xFilesFactor = "{0:.2f}".format(info['xFilesFactor']) |
| 161 |
str_xFilesFactor = "{0:.2f}".format(xFilesFactor) |
| 162 |
|
| 163 |
# check to see if the current and configured xFilesFactor are the same |
| 164 |
if (str_xFilesFactor != info_xFilesFactor): |
| 165 |
rebuild = True |
| 166 |
messages += '%s xFilesFactor differs real: %s should be: %s \n' % \ |
| 167 |
(metric, info_xFilesFactor, str_xFilesFactor) |
| 168 |
# check to see if the current and configured aggregationMethods are the same |
| 169 |
if (aggregationMethod != info['aggregationMethod']): |
| 170 |
rebuild = True |
| 171 |
messages += '%s aggregation schema differs real: %s should be: %s \n' % \ |
| 172 |
(metric, info['aggregationMethod'], aggregationMethod) |
| 173 |
|
| 174 |
# if we need to rebuild, lets do it. |
| 175 |
if rebuild is True: |
| 176 |
cmd = [whisperResizeExecutable, fullPath] |
| 177 |
for x in shlex.split(options.extra_args): |
| 178 |
cmd.append(x) |
| 179 |
cmd.append('--xFilesFactor=' + str(xFilesFactor)) |
| 180 |
cmd.append('--aggregationMethod=' + str(aggregationMethod)) |
| 181 |
for x in shlex.split(schema_config_args): |
| 182 |
cmd.append(x) |
| 183 |
|
| 184 |
if options.quiet is not True or options.confirm is True: |
| 185 |
print(messages) |
| 186 |
print(cmd) |
| 187 |
|
| 188 |
if options.confirm is True: |
| 189 |
options.doit = confirm("Would you like to run this command? [y/n]: ") |
| 190 |
if options.doit is False: |
| 191 |
print("Skipping command \n") |
| 192 |
|
| 193 |
if options.doit is True: |
| 194 |
exitcode = call(cmd) |
| 195 |
# if the command failed lets bail so we can take a look before proceeding |
| 196 |
if (exitcode > 0): |
| 197 |
print('Error running: %s' % (cmd)) |
| 198 |
sys.exit(1) |
| 199 |
|
| 200 |
|
| 201 |
def getMetricFromPath(filePath): |
| 202 |
""" |
| 203 |
this method takes the full file path of a whisper file an converts it |
| 204 |
to a gaphite metric name |
| 205 |
|
| 206 |
Parameters: |
| 207 |
filePath - full file path to a whisper file |
| 208 |
|
| 209 |
Returns a string representing the metric name |
| 210 |
""" |
| 211 |
# sanitize directory since we may get a trailing slash or not, and if we |
| 212 |
# don't it creates a leading '.' |
| 213 |
data_dir = os.path.normpath(settings.LOCAL_DATA_DIR) + os.sep |
| 214 |
|
| 215 |
# pull the data dir off and convert to the graphite metric name |
| 216 |
metric_name = filePath.replace(data_dir, '') |
| 217 |
metric_name = metric_name.replace('.wsp', '') |
| 218 |
metric_name = metric_name.replace('/', '.') |
| 219 |
return metric_name |
| 220 |
|
| 221 |
|
| 222 |
def confirm(question, error_response='Valid options : yes or no'): |
| 223 |
""" |
| 224 |
ask the user if they would like to perform the action |
| 225 |
|
| 226 |
Parameters: |
| 227 |
question - the question you would like to ask the user to confirm. |
| 228 |
error_response - the message to display if an invalid option is given. |
| 229 |
""" |
| 230 |
while True: |
| 231 |
answer = input(question).lower() |
| 232 |
if answer in ('y', 'yes'): |
| 233 |
return True |
| 234 |
if answer in ('n', 'no'): |
| 235 |
return False |
| 236 |
print(error_response) |
| 237 |
|
| 238 |
|
| 239 |
if os.path.isfile(processPath) and processPath.endswith('.wsp'): |
| 240 |
processMetric(processPath, schemas, agg_schemas) |
| 241 |
else: |
| 242 |
for root, _, files in os.walk(processPath): |
| 243 |
# we only want to deal with non-hidden whisper files |
| 244 |
for f in fnmatch.filter(files, '*.wsp'): |
| 245 |
fullpath = os.path.join(root, f) |
| 246 |
processMetric(fullpath, schemas, agg_schemas) |