Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
help="Output results in JSON form")
(options, args) = option_parser.parse_args()
if len(args) < 1:
option_parser.print_help()
sys.exit(1)
path = args[0]
if len(args) > 1:
field = args[1]
else:
field = None
try:
info = whisper.info(path)
except whisper.WhisperException as exc:
raise SystemExit('[ERROR] %s' % str(exc))
info['fileSize'] = os.stat(path).st_size
if field:
if field not in info:
print('Unknown field "%s". Valid fields are %s' % (field, ','.join(info)))
sys.exit(1)
print(info[field])
sys.exit(0)
if options.json:
print(json.dumps(info, indent=2, separators=(',', ': ')))
else:
archives = info.pop('archives')
if len(args) != 1:
option_parser.print_help()
sys.exit(1)
path = args[0]
from_time = int(options._from)
until_time = int(options.until)
try:
data = whisper.fetch(path, from_time, until_time)
if not data:
raise SystemExit('No data in selected timerange')
(timeInfo, values) = data
except (whisper.WhisperException, IOError) as exc:
raise SystemExit('[ERROR] %s' % str(exc))
if options.drop:
fcn = _DROP_FUNCTIONS.get(options.drop)
values = [x for x in values if fcn(x)]
(start, end, step) = timeInfo
if options.json:
values_json = str(values).replace('None', 'null')
print('''{
"start" : %d,
"end" : %d,
"step" : %d,
"values" : %s
}''' % (start, end, step, values_json))
option_parser.print_help()
sys.exit(1)
path = args[0]
archives = [whisper.parseRetentionDef(retentionDef)
for retentionDef in args[1:]]
if os.path.exists(path) and options.overwrite:
print('Overwriting existing file: %s' % path)
os.unlink(path)
try:
whisper.create(path, archives, xFilesFactor=options.xFilesFactor,
aggregationMethod=options.aggregationMethod, sparse=options.sparse,
useFallocate=options.fallocate)
except whisper.WhisperException as exc:
raise SystemExit('[ERROR] %s' % str(exc))
size = os.stat(path).st_size
print('Created: %s (%d bytes)' % (path, size))
now = int(time.time())
option_parser = argparse.ArgumentParser(
description="Accepts multiple Whisper datapoints on stdin to update a "
"single .wsp file"
)
option_parser.add_argument("filename", nargs=1,
help="path to a .wsp file to update")
args = option_parser.parse_args()
datapoint_strings = [point.replace('N:', '%d:' % now) for point in sys.stdin]
datapoints = [tuple(point.strip().split(':')) for point in datapoint_strings]
try:
whisper.update_many(args.filename, datapoints)
except whisper.WhisperException as exc:
raise SystemExit('[ERROR] %s' % str(exc))
option_parser.print_help()
sys.exit(1)
path = args[0]
datapoint_strings = args[1:]
datapoint_strings = [point.replace('N:', '%d:' % now)
for point in datapoint_strings]
datapoints = [tuple(point.split(':')) for point in datapoint_strings]
try:
if len(datapoints) == 1:
timestamp,value = datapoints[0]
whisper.update(path, value, timestamp)
else:
whisper.update_many(path, datapoints)
except whisper.WhisperException as exc:
raise SystemExit('[ERROR] %s' % str(exc))
"""Set xFilesFactor for existing whisper file"""
parser = argparse.ArgumentParser(
description='Set xFilesFactor for existing whisper file')
parser.add_argument('path', type=str, help='path to whisper file')
parser.add_argument('xff', metavar='xFilesFactor', type=float,
help='new xFilesFactor, a float between 0 and 1')
args = parser.parse_args()
try:
old_xff = whisper.setXFilesFactor(args.path, args.xff)
except IOError:
sys.stderr.write("[ERROR] File '%s' does not exist!\n\n" % args.path)
parser.print_help()
sys.exit(1)
except whisper.WhisperException as exc:
raise SystemExit('[ERROR] %s' % str(exc))
print('Updated xFilesFactor: %s (%s -> %s)' %
(args.path, old_xff, args.xff))
else:
points_re = re.compile(r'^(\d+)([a-z]+)$')
match = points_re.match(points)
if match:
points = int(match.group(1)) * UnitMultipliers[getUnitString(match.group(2))] / precision
else:
raise ValueError("Invalid retention specification '%s'" % points)
return (precision, points)
class WhisperException(Exception):
"""Base class for whisper exceptions."""
class InvalidConfiguration(WhisperException):
"""Invalid configuration."""
class InvalidAggregationMethod(WhisperException):
"""Invalid aggregation method."""
class InvalidTimeInterval(WhisperException):
"""Invalid time interval."""
class TimestampNotCovered(WhisperException):
"""Timestamp not covered by any archives in this database."""
class CorruptWhisperFile(WhisperException):
def __init__(self, error, path):
return (precision, points)
class WhisperException(Exception):
"""Base class for whisper exceptions."""
class InvalidConfiguration(WhisperException):
"""Invalid configuration."""
class InvalidAggregationMethod(WhisperException):
"""Invalid aggregation method."""
class InvalidTimeInterval(WhisperException):
"""Invalid time interval."""
class TimestampNotCovered(WhisperException):
"""Timestamp not covered by any archives in this database."""
class CorruptWhisperFile(WhisperException):
def __init__(self, error, path):
Exception.__init__(self, error)
self.error = error
self.path = path
def __repr__(self):
return "" % (self.path, self.error)
def __str__(self):
def setAggregation(path, mode):
if not mode:
return 0
if not os.path.exists(path):
return 0
try:
whisper.setAggregationMethod(path, mode)
return 1
except whisper.WhisperException as exc:
logging.warning("%s failed (%s)" % (path, str(exc)))
class InvalidConfiguration(WhisperException):
"""Invalid configuration."""
class InvalidAggregationMethod(WhisperException):
"""Invalid aggregation method."""
class InvalidTimeInterval(WhisperException):
"""Invalid time interval."""
class TimestampNotCovered(WhisperException):
"""Timestamp not covered by any archives in this database."""
class CorruptWhisperFile(WhisperException):
def __init__(self, error, path):
Exception.__init__(self, error)
self.error = error
self.path = path
def __repr__(self):
return "" % (self.path, self.error)
def __str__(self):
return "%s (%s)" % (self.error, self.path)
def enableDebug():
global open, debug, startBlock, endBlock
class open(file):
def __init__(self,*args,**kwargs):
file.__init__(self,*args,**kwargs)