Created
July 7, 2014 09:01
-
-
Save sabcio/e09ba01899602e34d957 to your computer and use it in GitHub Desktop.
whisper to influxdb - using graphite-api to render graphs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# based on https://github.com/damaex17/whisper-to-influxDB | |
# adjusted to graphite-api data format | |
from influxdb import InfluxDBClient | |
import argparse | |
import whisper | |
import os | |
import time | |
import subprocess | |
DEFAULT_HOST="localhost" | |
DEFAULT_PORT="8086" | |
DEFAULT_USER="graphite" | |
DEFAULT_PASSWORD="graphite" | |
DEFAULT_DB="graphite" | |
def search(pwd): | |
whisper_files = [] | |
for path,directory,filenames in os.walk(pwd): | |
for files in filenames: | |
whisper_files.append('%s/%s' % (path,files)) | |
return whisper_files | |
def lame_whisper_read(whisper_file): | |
linux_cmd = subprocess.Popen(["/usr/local/bin/whisper-fetch.py", "--from=0", whisper_file, ], stdout=subprocess.PIPE,) | |
stdout = linux_cmd.communicate()[0] | |
data = {} | |
for line in stdout.split('\n'): | |
try: time, value = line.split()[0], line.split()[1] | |
except: | |
continue | |
if value != 'None': | |
data[time] = value | |
return data | |
def normalize_path_to_timeseries(path, root_path): | |
timeseries = path.replace(root_path, '',1) | |
timeseries = timeseries.replace('.wsp', '',1) | |
timeseries = timeseries.replace('/', '.') | |
return ".".join(timeseries.split(".")[0:-1]) | |
def main(): | |
parser = argparse.ArgumentParser(description='whisper file to influxDB migration script') | |
parser.add_argument('path', help='path to whispers') | |
parser.add_argument('-host', default=DEFAULT_HOST, metavar="host", help="influxDB host") | |
parser.add_argument('-port', default=DEFAULT_PORT, metavar="port", help="influxDB port") | |
parser.add_argument('-user', default=DEFAULT_USER, metavar="user", help="influxDB user") | |
parser.add_argument('-password', default=DEFAULT_PASSWORD, metavar="password", help="influxDB password") | |
parser.add_argument('-db', default=DEFAULT_DB, metavar="db", help="influxDB db!") | |
args = parser.parse_args() | |
client = InfluxDBClient(args.host, args.port, args.user, args.password, args.db) | |
try: client.create_database(args.db) | |
except: pass | |
for whisper_file in search(args.path): | |
print whisper_file | |
data = lame_whisper_read(whisper_file) | |
value = whisper_file.split('/')[-1].split('.')[0] | |
time_series = normalize_path_to_timeseries(whisper_file, args.path) | |
time_series = '{0}.{1}'.format(time_series, value) | |
for key in data.iterkeys(): | |
time = float(key) | |
write_data = [{ | |
"name":time_series, | |
"columns":["time", "sequence_number","value"], | |
"points":[[time, 1, float(data[key])]] | |
}] | |
client.write_points(write_data) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment