Add scripts to create myops-getqueryview:
[myops.git] / web / collect / server / load_couch.py
diff --git a/web/collect/server/load_couch.py b/web/collect/server/load_couch.py
new file mode 100755 (executable)
index 0000000..85969fe
--- /dev/null
@@ -0,0 +1,54 @@
+#!/usr/bin/python
+
+import couchdb
+import sys
+from txt2dict import *
+import os
+import traceback
+import time
+
+couch = couchdb.Server("http://IPADDR:5984/")
+db = couch['myops']
+#db.resource.http.add_credentials('admin', 'ssorcmor')
+
+def set_or_update(id, data):
+    doc = db.get(id) 
+    if doc is None: 
+        db[id] = data
+    else:
+        # NOTE: how to update type?
+        del data['type']
+        doc.update(data)
+        print db.update([doc])
+
+def get_hist_id(data):
+    return data['ts'] + "-" + data['hostname']
+
+def get_single_id(data):
+    return data['hostname'] + "-" + data['type']
+
+for filename in sys.argv[1:]:
+    print filename
+    try:
+        data_hist = file_to_dict(filename)
+        data_single = data_hist.copy()
+    except:
+        traceback.print_exc()
+        continue
+
+    # history document
+    if 'ts' not in data_hist or 'hostname' not in data_hist or 'type' not in data_hist:
+        print "data missing fields", data_hist
+        continue
+
+    #hist_id = get_hist_id(data_hist)
+    #set_or_update(hist_id, data_hist)
+
+    # single document
+    single_id = get_single_id(data_single) 
+    data_single['type'] += "-single"
+    set_or_update(single_id, data_single)
+
+    os.system("echo %s >> /root/load.log" % filename)
+
+