-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_copy.py
More file actions
executable file
·50 lines (42 loc) · 1.48 KB
/
stats_copy.py
File metadata and controls
executable file
·50 lines (42 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# usage: python stats_copy.py <source_server> <target_server> <target_database> <delay>
# example store localhosts _stats on localhost:5984/stats every 60 seconds:
# $ python stats_copy.py localhost localhost stats 60
import httplib
import time
from datetime import datetime
import threading
import sys
import simplejson as json
class Couch:
"""Basic wrapper class for operations on a couchDB"""
def __init__(self, host, port=5984, options=None):
self.host = host
self.port = port
def connect(self):
return httplib.HTTPConnection(self.host, self.port) # No close()
def put(self, uri, body):
c = self.connect()
headers = {"Content-type": "application/json"}
c.request("PUT", uri, body, headers)
return c.getresponse()
def stats(self, range):
c = self.connect()
c.request("GET","/_stats?range=%s" % range, "",{})
return c.getresponse()
source_srv = Couch(sys.argv[1])
target_srv = Couch(sys.argv[2])
target_db = sys.argv[3]
# beware couchdb only supports fixed delays of 60, 300 and 900
# anything else will not work
delay = int(sys.argv[4])
def stats(delay):
stats = json.loads(source_srv.stats(delay).read())
now = datetime.now()
# this format can be parsed by javascript's `new Date(str)`
jsonDate = now.strftime("%Y/%m/%d %H:%M:%S +0000")
stats['timestamp'] = jsonDate
path = '/%s/stats-%s' % (target_db, now.strftime("%Y-%m-%d-%H%M%S"))
print target_srv.put(path, json.dumps(stats)).read()
while True:
threading.Thread(target=stats, args=[delay]).start()
time.sleep(delay)