| /trunk/rrd_feeder_http.py r19 | /trunk/rrd_feeder_http.py r20 | ||
| 1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
|---|---|---|---|
| 2 | # Copyright (c) 2008 Corey Goldberg (corey@goldb.org) | 2 | # Copyright (c) 2008 Corey Goldberg (corey@goldb.org) |
| 3 | # | 3 | # |
| 4 | # RRDTool HTTP Performance Meter (Data Feeder/Grapher) | 4 | # RRDTool HTTP Performance Meter (Data Feeder/Grapher) |
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | import rrd | 7 | import rrd |
| 8 | import time | 8 | import time |
| 9 | import sys | 9 | import sys |
| 10 | import httplib | 10 | import httplib |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | host = 'www.python.org' | 13 | host = 'www.python.org' |
| 14 | path = '/' | 14 | path = '/' |
| 15 | use_ssl = False | 15 | use_ssl = False |
| 16 | 16 | ||
| 17 | interval = 10 | 17 | interval = 10 |
| 18 | rrd_file = 'test.rrd' | 18 | rrd_file = 'test.rrd' |
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | def main(): | 21 | def main(): |
| 22 | my_rrd = rrd.RRD(rrd_file, 'Response Time') | 22 | my_rrd = rrd.RRD(rrd_file, 'Response Time') |
| 23 | while True: | 23 | while True: |
| 24 | start_time = time.clock() | 24 | start_time = time.clock() |
| 25 | try: | 25 | try: |
| 26 | send(host) | 26 | send(host) |
| 27 | except: | 27 | except: |
| 28 | print 'failed request' | 28 | print 'failed request' |
| 29 | end_time = time.clock() | 29 | end_time = time.clock() |
| 30 | raw_latency = (end_time - start_time) | 30 | raw_latency = (end_time - start_time) |
| 31 | expire_time = (interval - raw_latency) | 31 | expire_time = (interval - raw_latency) |
| 32 | latency = ('%.3f' % raw_latency) | 32 | latency = ('%.3f' % raw_latency) |
| 33 | try: | 33 | my_rrd.update(latency) |
| 34 | my_rrd.update(latency) | ||
| 35 | except: | ||
| 36 | print 'error updating RRD' | ||
| 37 | sys.exit() | ||
| 38 | my_rrd.graph(60) | 34 | my_rrd.graph(60) |
| 39 | print latency | 35 | print latency |
| 40 | if expire_time > 0: | 36 | if expire_time > 0: |
| 41 | time.sleep(expire_time) | 37 | time.sleep(expire_time) |
| 42 | 38 | ||
| 43 | 39 | ||
| 44 | def send(host): | 40 | def send(host): |
| 45 | if use_ssl: | 41 | if use_ssl: |
| 46 | conn = httplib.HTTPSConnection(host) | 42 | conn = httplib.HTTPSConnection(host) |
| 47 | else: | 43 | else: |
| 48 | conn = httplib.HTTPConnection(host) | 44 | conn = httplib.HTTPConnection(host) |
| 49 | try: | 45 | try: |
| 50 | conn.request('GET', path) | 46 | conn.request('GET', path) |
| 51 | conn.getresponse().read() | 47 | conn.getresponse().read() |
| 52 | except: | 48 | except: |
| 53 | raise | 49 | raise |
| 54 | 50 | ||
| 55 | 51 | ||
| 56 | if __name__ == '__main__': | 52 | if __name__ == '__main__': |
| 57 | main() | 53 | main() |
| 58 | 54 | ||