Example yaml
[rrdaggregate.git] / mpsubmit.py
1 #!/usr/bin/python
2
3 import base64
4 import requests
5 import json
6 import os
7 import xmltodict
8 import pdb
9 import glob
10
11 def track(event, properties=None):
12         if properties == None:
13                 properties = {}
14         token = "2504af1a1d3eee3f1744a764807d89b0"
15         if "token" not in properties:
16                 properties["token"] = token
17
18         params = {"event": event, "properties": properties}
19         data = base64.b64encode(json.dumps(params))
20         request = "http://api.mixpanel.com/track/"
21         result = requests.get(request,params={'data':data})
22         return result
23
24 cdata = os.popen('/etc/rrdaggregate/rrdaggregate.py /etc/rrdaggregate/mp.yaml').read()
25 d = xmltodict.parse(cdata)
26 legend = d['xport']['meta']['legend']['entry']
27 data = d['xport']['data']['row'][-3]['v'] 
28 time = d['xport']['data']['row'][-3]['t'] 
29 pdata = []
30
31 for e in data:
32         pdata.append(float(e))
33   
34 if (len(legend)!=len(data)):
35         'Mismatch between legend and data'
36 else:
37         prop_list = zip(legend,pdata)
38         props = dict(prop_list)
39         slices = glob.glob('/vservers/*')
40         slices = map(lambda s:s[10:],slices)
41         props['hostname']=os.popen('hostname').read().rstrip()
42         props['kernel']=os.popen('uname -r').read().rstrip()
43         try:
44                 props['cpu']=100 - int(100*props['cpu']/12)/100.0;
45                 props['memory']/=(1024*1024)
46         except:
47                 pass 
48         props['ip']=os.popen('hostname -i').read().rstrip()
49         props['slices']=slices
50         
51         try:
52                 last_sent = open('/tmp/lst_mp_hb').read()
53         except:
54                 last_sent = '0'
55
56         if (last_sent!=time):
57                 print 'Sending'
58                 track('heartbeat',props)
59                 open('/tmp/lst_mp_hb','w').write('%s'%time)
60