3 # copy .rrd files from a remote munin master host, sum the 'df' stats from a
4 # list of hosts, use them to estimate a rate-of-change for the past month,
5 # then extrapolate to guess how many weeks/months/years of storage space we
6 # have left, and output it to another munin graph
11 MUNIN_HOST = "munin.allmydata.com"
12 PREFIX = "%s:/var/lib/munin/prodtahoe/" % MUNIN_HOST
13 FILES = [ "prodtahoe%d.allmydata.com-df-_dev_sd%s3-g.rrd" % (a,b)
15 for b in ("a", "b", "c", "d")
17 REMOTEFILES = [ PREFIX + f for f in FILES ]
18 LOCALFILES = ["/var/lib/munin/prodtahoe/" + f for f in FILES ]
19 WEBFILE = "/var/www/tahoe/spacetime.json"
23 # copy the RRD files from your munin master host to a local one
24 cmd = "rsync %s rrds/" % (" ".join(REMOTEFILES))
29 return time.strftime("%b %d %H:%M", time.localtime(t))
31 def predict_future(past_s):
38 d = rrdtool.fetch(fn, "AVERAGE", "-s", "-"+past_s, "-e", "-1hr")
39 # ((start, end, step), (name1, name2, ...), [(data1, data2, ..), ...])
40 (start_time, end_time ,step) = d[0]
41 #print format_time(start_time), " - ", format_time(end_time), step
46 start_space = d[2][0][0]
47 if start_space is None:
49 # I don't know why, but the last few points are always bogus. Running
50 # 'rrdtool fetch' on the command line is usually ok.. I blame the python
52 end_space = d[2][-4][0]
55 end_time = end_time - (4*step)
56 start_df.append(start_space)
57 end_df.append(end_space)
58 durations.append(end_time - start_time)
60 avg_start_df = sum(start_df) / len(start_df)
61 avg_end_df = sum(end_df) / len(end_df)
62 avg_duration = sum(durations) / len(durations)
63 #print avg_start_df, avg_end_df, avg_duration
65 rate = (avg_end_df - avg_start_df) / avg_duration
66 #print "Rate", rate, " %/s"
67 #print "measured over", avg_duration / 86400, "days"
68 remaining = 100 - avg_end_df
69 remaining_seconds = remaining / rate
70 #print "remaining seconds", remaining_seconds
71 remaining_days = remaining_seconds / 86400
72 #print "remaining days", remaining_days
75 def write_to_file(samples):
76 # write a JSON-formatted dictionary
77 f = open(WEBFILE + ".tmp", "w")
79 f.write(", ".join(['"%s": %s' % (k, samples[k])
80 for k in sorted(samples.keys())]))
83 os.rename(WEBFILE + ".tmp", WEBFILE)
85 if len(sys.argv) > 1 and sys.argv[1] == "config":
87 graph_title Tahoe Remaining Space Predictor
88 graph_vlabel days remaining
90 graph_info This graph shows the estimated number of days left until storage space is exhausted
91 days_2wk.label days left (2wk sample)
93 days_4wk.label days left (4wk sample)
94 days_4wk.draw LINE2"""
99 remaining_4wk = predict_future("4wk")
100 if remaining_4wk is not None:
101 print "days_4wk.value", remaining_4wk
102 samples["remaining_4wk"] = remaining_4wk
103 remaining_2wk = predict_future("2wk")
104 if remaining_2wk is not None:
105 print "days_2wk.value", remaining_2wk
106 samples["remaining_2wk"] = remaining_2wk
107 write_to_file(samples)