root / collector / management / commands / fetch_pm_points.py @ 9958f8ef
History | View | Annotate | Download (2.4 kB)
1 |
# -*- coding: utf-8 -*- vim:encoding=utf-8:
|
---|---|
2 |
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
|
3 |
|
4 |
# Copyright 2012 Leonidas Poulopoulos
|
5 |
#
|
6 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
# you may not use this file except in compliance with the License.
|
8 |
# You may obtain a copy of the License at
|
9 |
#
|
10 |
# http://www.apache.org/licenses/LICENSE-2.0
|
11 |
#
|
12 |
# Unless required by applicable law or agreed to in writing, software
|
13 |
# distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
# See the License for the specific language governing permissions and
|
16 |
# limitations under the License.
|
17 |
|
18 |
from django.core.management.base import BaseCommand, CommandError |
19 |
import urllib2 |
20 |
import pycurl |
21 |
from django.conf import settings |
22 |
from dcapp.models import * |
23 |
import csv |
24 |
import cStringIO |
25 |
import time, datetime |
26 |
|
27 |
|
28 |
class Command(BaseCommand): |
29 |
help = "Populates the devices' series measurements"
|
30 |
args = ""
|
31 |
label = ''
|
32 |
|
33 |
def getUserData(self, dev_id): |
34 |
response = cStringIO.StringIO() |
35 |
c = pycurl.Curl() |
36 |
past = datetime.datetime.now() + datetime.timedelta(minutes=-30)
|
37 |
start = time.mktime(past.timetuple()) |
38 |
c.setopt(pycurl.URL, '%s?_=%s&device=%s&absolute=%s&tstart=%s&tfinish=2147483647&series=%s' %(settings.POWERMETER_DATA_URI, int(time.time()),dev_id, settings.POWERMETER_ABSOLUTE_MEASSUREMENT, start, ','.join(map(str, range(40))) )) |
39 |
c.setopt(pycurl.HTTPHEADER, ['Accept: text/plain, */*'])
|
40 |
c.setopt(pycurl.VERBOSE, 0)
|
41 |
c.setopt(pycurl.CONNECTTIMEOUT, 30)
|
42 |
c.setopt(pycurl.TIMEOUT, 30)
|
43 |
c.setopt(pycurl.USERPWD, '%s:%s' %(settings.POWERMETER_USER,settings.POWERMETER_PASS))
|
44 |
c.setopt(c.WRITEFUNCTION, response.write) |
45 |
c.perform() |
46 |
return response
|
47 |
|
48 |
|
49 |
def handle(self, *args, **options): |
50 |
devices = Device.objects.all() |
51 |
for device in devices: |
52 |
print "Now parsing: %s\n" %device |
53 |
data = self.getUserData(device.mapped_id)
|
54 |
d = data.getvalue() |
55 |
html_text = urllib2.unquote(d) |
56 |
f = cStringIO.StringIO(html_text) |
57 |
reader = csv.DictReader(f) |
58 |
monitoring_list = [] |
59 |
for i in reader: |
60 |
monitoring_list.append(i) |
61 |
device.graphCounters(monitoring_list) |
62 |
print "DONE with device...\n\n" |
63 |
|
64 |
|
65 |
|