]>
Commit | Line | Data |
---|---|---|
81675874 | 1 | #!/usr/bin/python |
2 | ||
befc2e59 | 3 | import simplejson |
3add293a MT |
4 | import subprocess |
5 | ||
81675874 | 6 | def size(s): |
7 | suffixes = ["B", "K", "M", "G", "T",] | |
8 | ||
9 | idx = 0 | |
10 | while s >= 1024 and idx < len(suffixes): | |
11 | s /= 1024 | |
12 | idx += 1 | |
8ccd2ff0 | 13 | |
db8a98eb | 14 | return "%.0f%s" % (s, suffixes[idx]) |
befc2e59 | 15 | |
3add293a MT |
16 | def ping(host, count=5, wait=10): |
17 | cmd = subprocess.Popen( | |
18 | ["ping", "-c%d" % count, "-w%d" % wait, host], | |
19 | stdout = subprocess.PIPE, | |
20 | stderr = subprocess.PIPE, | |
21 | ) | |
22 | ||
23 | latency = None | |
24 | ||
25 | out, error = cmd.communicate() | |
26 | ||
27 | for line in out.split("\n"): | |
28 | if not line.startswith("rtt"): | |
29 | continue | |
30 | ||
31 | line = line.split() | |
32 | if len(line) < 4: | |
33 | break | |
34 | ||
35 | rtts = line[3].split("/") | |
36 | if len(rtts) < 4: | |
37 | break | |
38 | ||
39 | latency = "%.1f" % float(rtts[1]) | |
40 | ||
41 | return latency |