forked from stfc/artemis
/
artemis_collect.py
executable file
·168 lines (133 loc) · 4.67 KB
/
artemis_collect.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python
# coding=utf8
#
# Copyright Science and Technology Facilities Council, 2009-2012.
#
# This file is part of ARTEMIS.
#
# ARTEMIS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ARTEMIS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ARTEMIS. If not, see <http://www.gnu.org/licenses/>.
#
#Required modules from Python Standard Library
from time import time
time_start = time()
from datetime import datetime
import commands, os, sys
#Try to import rrdtool module
try:
import rrdtool
except:
print("ERROR: Unable to import the rrdtool module, is python-rrdtool installed?")
sys.exit(1)
#Fall back to simplejson for versions of python < 2.5 (simplejson requires seperate install)
try:
import json
except:
try:
import simplejson as json
except:
print("ERROR: Unable to find a usable json module, is simplejson installed?")
sys.exit(1)
#ARTEMIS Components
from artemis_core import *
from plugins import base
#Load config module
from artemis_config import *
# Setup base nodes from store
base_nodes = []
for n in session.query(Node).all():
base_nodes.append(load_plugin(n.plugin)(n.ip, n.username, n.password))
# Setup sensors from store
sensors = {}
for p in session.query(Probe).all():
sensors[p.id] = [p.name, float(p.x), float(p.y), float(p.z), float(p.w), float(p.h), float(p.d)]
#Configuration
this_dir = os.path.dirname(os.path.realpath( __file__ )) + "/"
rrd_dir = this_dir + config.get("rrd","dir")
print("Starting run...")
#Start collection
g = grabData(base_nodes)
snapshot_list = [];
print("---- Data grab complete ----")
for serial, value, units, name, source_node in g:
print(r"%2.3f : Found sensor %s with value %s %s and name %s" % (time(), serial, value, units, name))
rrd = str(rrd_dir + serial + config.get("rrd","ext"))
if not os.path.isfile(rrd):
#create rrd if none exists
print("Creating new RRD " + rrd)
rrdtool.create(
rrd,
"--step", "60",
"DS:val:GAUGE:120:-100:100", # Accept data between -100 and +100 as valid
"RRA:AVERAGE:0.5:1:525600", # A year of minutes
"RRA:AVERAGE:0.5:60:8760", # A year of hours
"RRA:MAX:0.5:60:8760", # A year of hours
"RRA:MIN:0.5:60:8760", # A year of hours
)
#update data
rrdtool.update(rrd, "N:" + str(value))
#store latest values
try:
(n, x, y, z, h, w, d) = sensors[serial]
except:
(n, x, y, z, h, w, d) = ("Auto-detected " + name, 0, 0, 0, 0, 0, 0)
session.add(Probe(serial, n, x, y, z, h, w, d))
# Update timestamp
probe = session.query(Probe).filter(Probe.id == serial).first()
if (probe.name <> n):
print("Mismatch of name against node %s vs %s" % (probe.name, n))
probe.lastcontact = datetime.now()
probe.remote_name = name
probe.node = source_node
row = [serial, value, n, x, y, h, w]
snapshot_list.append(row)
# Commit outside loop
session.commit()
# Prep config
c = dict(config.items("room"))
for i in ['offset_x', 'offset_y', 'offset_z', 'unit_x', 'unit_y', 'unit_z', 'height', 'width']:
c[i] = int(c[i])
for i in ['reverse_x', 'reverse_y', 'reverse_z']:
c[i] = config.getboolean("room", i)
#Dump data
dump_prep = {
"config" : c,
"probes" : snapshot_list,
}
# Write out data dump for gui
try:
file_json_dump = open(this_dir + "web/data/data-dump.json", "w")
json.dump(dump_prep, file_json_dump)
file_json_dump.close()
print("Wrote output to %s" % file_json_dump.name)
except:
print("Error while writing data dump file - %s %s %s" % sys.exc_info())
# Update performance rrd
rrd = str(rrd_dir + "ARTEMIS-STATS-" + c["name"].replace(" ","_") + config.get("rrd","ext"))
if not os.path.isfile(rrd):
#create rrd if none exists
print("Creating new RRD " + rrd)
rrdtool.create(
rrd,
"--step", "60",
"DS:collect:GAUGE:120:0:3600", # Accept data between 0 and 3600 as valid
"DS:nodes:GAUGE:120:0:U", # Number of known nodes
"DS:probes:GAUGE:120:0:U", # Number of known probes
"RRA:AVERAGE:0.5:1:525600", # A year of minutes
"RRA:AVERAGE:0.5:60:8760", # A year of hours
"RRA:MAX:0.5:60:8760", # A year of hours
"RRA:MIN:0.5:60:8760", # A year of hours
)
time_run = time() - time_start
rrdtool.update(rrd, "N:%f:%d:%d" % (time_run, len(base_nodes), len(g)))
print("Collect finished in %0.3f seconds" % (time_run))