Add scripts to create myops-getqueryview:
[myops.git] / web / collect / server / load_couch.py
1 #!/usr/bin/python
2
3 import couchdb
4 import sys
5 from txt2dict import *
6 import os
7 import traceback
8 import time
9
10 couch = couchdb.Server("http://IPADDR:5984/")
11 db = couch['myops']
12 #db.resource.http.add_credentials('admin', 'ssorcmor')
13
14 def set_or_update(id, data):
15     doc = db.get(id) 
16     if doc is None: 
17         db[id] = data
18     else:
19         # NOTE: how to update type?
20         del data['type']
21         doc.update(data)
22         print db.update([doc])
23
24 def get_hist_id(data):
25     return data['ts'] + "-" + data['hostname']
26
27 def get_single_id(data):
28     return data['hostname'] + "-" + data['type']
29
30 for filename in sys.argv[1:]:
31     print filename
32     try:
33         data_hist = file_to_dict(filename)
34         data_single = data_hist.copy()
35     except:
36         traceback.print_exc()
37         continue
38
39     # history document
40     if 'ts' not in data_hist or 'hostname' not in data_hist or 'type' not in data_hist:
41         print "data missing fields", data_hist
42         continue
43
44     #hist_id = get_hist_id(data_hist)
45     #set_or_update(hist_id, data_hist)
46
47     # single document
48     single_id = get_single_id(data_single) 
49     data_single['type'] += "-single"
50     set_or_update(single_id, data_single)
51
52     os.system("echo %s >> /root/load.log" % filename)
53
54