Skip to content

Instantly share code, notes, and snippets.

@danslimmon
Created April 5, 2013 15:34
Show Gist options
  • Save danslimmon/5320247 to your computer and use it in GitHub Desktop.
Save danslimmon/5320247 to your computer and use it in GitHub Desktop.
Parameterizes graphite data points by time. See <link to my blog post>
#!/usr/bin/python
# URL of Graphite server
GRAPHITE_BASEURL = "http://graphite.example.com/"
# Number of days' worth of data you want in the table
WINDOW = 30
# Metric that will appear on the X axis
TARGET_X = "stats.gauges.nconn"
# Metric that will appear on the Y axis
TARGET_Y = "stats.timers.resptime.upper_90"
import subprocess
import time
now_time = int(time.time())
from_time = now_time - (WINDOW * 86400)
url = "%s/render/?target=%s&target=%s&from=%d&rawData" % (
GRAPHITE_BASEURL,
TARGET_X,
TARGET_Y,
from_time
)
p = subprocess.Popen(["curl", "--silent", url], stdout = subprocess.PIPE)
curl_out = p.stdout.readlines()
x_points = curl_out[0].rstrip().split("|")[-1].split(",")
y_points = curl_out[1].rstrip().split("|")[-1].split(",")
# Equalize lengths of X and Y datasets
if len(x_points) < len(y_points):
y_points = y_points[:len(x_points)]
if len(y_points) < len(x_points):
x_points = x_points[:len(y_points)]
# Parameterize by time, skipping any Nones
for i in range(len(x_points)):
if x_points[i] == "None" or y_points[i] == "None":
continue
print "%-20.2f%-20.2f" % (float(x_points[i]), float(y_points[i]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment