2017-08-05 12:54:08 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
2016-12-05 19:06:34 -06:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2018-10-19 14:58:30 -05:00
|
|
|
# Copyright © 2010-2018 Lance Edgar
|
2016-12-05 19:06:34 -06:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
2017-07-06 23:38:50 -05:00
|
|
|
# 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.
|
2016-12-05 19:06:34 -06:00
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2017-07-06 23:38:50 -05:00
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
2016-12-05 19:06:34 -06:00
|
|
|
#
|
2017-07-06 23:38:50 -05:00
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-05 19:06:34 -06:00
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
|
|
|
Data models for tempmon
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import unicode_literals, absolute_import
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
2017-02-07 14:47:57 -06:00
|
|
|
import six
|
2016-12-05 19:06:34 -06:00
|
|
|
import sqlalchemy as sa
|
|
|
|
from sqlalchemy import orm
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
|
2018-10-19 14:58:30 -05:00
|
|
|
from rattail import enum
|
2016-12-05 19:06:34 -06:00
|
|
|
from rattail.db.model import uuid_column
|
|
|
|
from rattail.db.model.core import ModelBase
|
|
|
|
|
|
|
|
|
|
|
|
Base = declarative_base(cls=ModelBase)
|
|
|
|
|
|
|
|
|
2018-10-19 17:51:25 -05:00
|
|
|
@six.python_2_unicode_compatible
|
|
|
|
class Appliance(Base):
|
|
|
|
"""
|
|
|
|
Represents an appliance which is monitored by tempmon.
|
|
|
|
"""
|
|
|
|
__tablename__ = 'appliance'
|
|
|
|
__table_args__ = (
|
|
|
|
sa.UniqueConstraint('name', name='appliance_uq_name'),
|
|
|
|
)
|
|
|
|
|
|
|
|
uuid = uuid_column()
|
|
|
|
|
|
|
|
name = sa.Column(sa.String(length=255), nullable=False, doc="""
|
|
|
|
Human-friendly (and unique) name for the appliance.
|
|
|
|
""")
|
|
|
|
|
2018-10-19 19:16:06 -05:00
|
|
|
appliance_type = sa.Column(sa.Integer(), nullable=True, doc="""
|
|
|
|
Code indicating which "type" of appliance this is.
|
|
|
|
""")
|
|
|
|
|
2018-10-19 17:51:25 -05:00
|
|
|
location = sa.Column(sa.String(length=255), nullable=True, doc="""
|
|
|
|
Description of the appliance's physical location.
|
|
|
|
""")
|
|
|
|
|
2018-10-19 19:16:06 -05:00
|
|
|
image_raw = sa.Column(sa.LargeBinary(), nullable=True, doc="""
|
|
|
|
Byte sequence of the raw image, as uploaded.
|
|
|
|
""")
|
|
|
|
|
|
|
|
image_normal = sa.Column(sa.LargeBinary(), nullable=True, doc="""
|
|
|
|
Byte sequence of the normalized image, i.e. "reasonable" size.
|
|
|
|
""")
|
|
|
|
|
|
|
|
image_thumbnail = sa.Column(sa.LargeBinary(), nullable=True, doc="""
|
|
|
|
Byte sequence of the thumbnail image.
|
|
|
|
""")
|
|
|
|
|
2018-10-19 17:51:25 -05:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2017-02-07 14:47:57 -06:00
|
|
|
@six.python_2_unicode_compatible
|
2016-12-05 19:06:34 -06:00
|
|
|
class Client(Base):
|
|
|
|
"""
|
|
|
|
Represents a tempmon client.
|
|
|
|
"""
|
|
|
|
__tablename__ = 'client'
|
|
|
|
__table_args__ = (
|
|
|
|
sa.UniqueConstraint('config_key', name='client_uq_config_key'),
|
|
|
|
)
|
|
|
|
|
|
|
|
uuid = uuid_column()
|
|
|
|
config_key = sa.Column(sa.String(length=50), nullable=False)
|
|
|
|
hostname = sa.Column(sa.String(length=255), nullable=False)
|
|
|
|
location = sa.Column(sa.String(length=255), nullable=True)
|
2018-09-28 19:14:25 -05:00
|
|
|
|
|
|
|
disk_type = sa.Column(sa.Integer(), nullable=True, doc="""
|
|
|
|
Integer code representing the type of hard disk used, if known. The
|
|
|
|
original motivation for this was to keep track of whether each client
|
|
|
|
(aka. Raspberry Pi) was using a SD card or USB drive for the root disk.
|
|
|
|
""")
|
2017-02-07 14:47:57 -06:00
|
|
|
|
|
|
|
delay = sa.Column(sa.Integer(), nullable=True, doc="""
|
|
|
|
Number of seconds to delay between reading / recording temperatures. If
|
|
|
|
not set, a default of 60 seconds will be assumed.
|
|
|
|
""")
|
|
|
|
|
2018-09-28 12:26:49 -05:00
|
|
|
notes = sa.Column(sa.Text(), nullable=True, doc="""
|
|
|
|
Any arbitrary notes for the client.
|
|
|
|
""")
|
|
|
|
|
2018-10-06 17:39:54 -05:00
|
|
|
enabled = sa.Column(sa.Boolean(), nullable=False, default=False, doc="""
|
|
|
|
Whether the client should be considered enabled (active). If set, the
|
|
|
|
client will be expected to take readings (but only for "enabled" probes)
|
|
|
|
and the server will monitor them to ensure they are within the expected
|
|
|
|
range etc.
|
|
|
|
""")
|
|
|
|
|
|
|
|
online = sa.Column(sa.Boolean(), nullable=False, default=False, doc="""
|
|
|
|
Whether the client is known to be online currently. When a client takes
|
|
|
|
readings, it will mark itself as online. If the server notices that the
|
|
|
|
readings have stopped, it will mark the client as *not* online.
|
|
|
|
""")
|
2016-12-05 19:06:34 -06:00
|
|
|
|
2018-09-28 12:21:21 -05:00
|
|
|
archived = sa.Column(sa.Boolean(), nullable=False, default=False, doc="""
|
|
|
|
Flag indicating this client is "archived". This typically means that the
|
2018-10-06 17:39:54 -05:00
|
|
|
client itself no longer exists but that the configuration for it should be
|
|
|
|
retained, to be used as a reference later etc. Note that "archiving" a
|
|
|
|
client is different from "disabling" it; i.e. disabling is temporary and
|
|
|
|
archiving is more for the long term.
|
2018-09-28 12:21:21 -05:00
|
|
|
""")
|
|
|
|
|
2017-02-07 14:47:57 -06:00
|
|
|
def __str__(self):
|
2016-12-05 19:06:34 -06:00
|
|
|
return '{} ({})'.format(self.config_key, self.hostname)
|
|
|
|
|
|
|
|
def enabled_probes(self):
|
|
|
|
return [probe for probe in self.probes if probe.enabled]
|
|
|
|
|
|
|
|
|
2017-02-07 14:47:57 -06:00
|
|
|
@six.python_2_unicode_compatible
|
2016-12-05 19:06:34 -06:00
|
|
|
class Probe(Base):
|
|
|
|
"""
|
|
|
|
Represents a probe connected to a tempmon client.
|
|
|
|
"""
|
|
|
|
__tablename__ = 'probe'
|
|
|
|
__table_args__ = (
|
|
|
|
sa.ForeignKeyConstraint(['client_uuid'], ['client.uuid'], name='probe_fk_client'),
|
2018-10-19 17:51:25 -05:00
|
|
|
sa.ForeignKeyConstraint(['appliance_uuid'], ['appliance.uuid'], name='probe_fk_appliance'),
|
2016-12-05 19:06:34 -06:00
|
|
|
sa.UniqueConstraint('config_key', name='probe_uq_config_key'),
|
|
|
|
)
|
|
|
|
|
|
|
|
uuid = uuid_column()
|
|
|
|
client_uuid = sa.Column(sa.String(length=32), nullable=False)
|
|
|
|
|
|
|
|
client = orm.relationship(
|
|
|
|
Client,
|
|
|
|
doc="""
|
|
|
|
Reference to the tempmon client to which this probe is connected.
|
|
|
|
""",
|
|
|
|
backref=orm.backref(
|
|
|
|
'probes',
|
2017-08-04 15:14:48 -05:00
|
|
|
cascade='all, delete-orphan',
|
2016-12-05 19:06:34 -06:00
|
|
|
doc="""
|
|
|
|
List of probes connected to this client.
|
|
|
|
"""))
|
|
|
|
|
|
|
|
config_key = sa.Column(sa.String(length=50), nullable=False)
|
2018-10-19 17:51:25 -05:00
|
|
|
|
2016-12-05 19:06:34 -06:00
|
|
|
appliance_type = sa.Column(sa.Integer(), nullable=False)
|
2018-10-19 17:51:25 -05:00
|
|
|
|
|
|
|
appliance_uuid = sa.Column(sa.String(length=32), nullable=True)
|
|
|
|
appliance = orm.relationship(
|
|
|
|
Appliance,
|
|
|
|
doc="""
|
|
|
|
Reference to the appliance which this probe monitors.
|
|
|
|
""",
|
|
|
|
backref=orm.backref(
|
|
|
|
'probes',
|
|
|
|
doc="""
|
|
|
|
List of probes which monitor this appliance.
|
|
|
|
"""))
|
|
|
|
|
|
|
|
description = sa.Column(sa.String(length=255), nullable=False, doc="""
|
|
|
|
General human-friendly description for the probe.
|
|
|
|
""")
|
|
|
|
|
|
|
|
location = sa.Column(sa.String(length=255), nullable=True, doc="""
|
|
|
|
Description of the probe's physical location, relative to the appliance.
|
|
|
|
""")
|
|
|
|
|
2016-12-05 19:06:34 -06:00
|
|
|
device_path = sa.Column(sa.String(length=255), nullable=True)
|
|
|
|
enabled = sa.Column(sa.Boolean(), nullable=False, default=True)
|
|
|
|
|
2018-10-19 14:58:30 -05:00
|
|
|
critical_temp_max = sa.Column(sa.Integer(), nullable=False, doc="""
|
|
|
|
Maximum high temperature; when a reading is greater than or equal to this
|
|
|
|
value, the probe's status becomes "critical high temp".
|
|
|
|
""")
|
|
|
|
|
|
|
|
critical_max_started = sa.Column(sa.DateTime(), nullable=True, doc="""
|
|
|
|
Timestamp when the probe readings started to indicate "critical high temp"
|
|
|
|
status. This should be null unless the probe currently has that status.
|
|
|
|
""")
|
|
|
|
|
|
|
|
critical_max_timeout = sa.Column(sa.Integer(), nullable=True, doc="""
|
|
|
|
Number of minutes the probe is allowed to have "critical high temp" status,
|
|
|
|
before the first email alert is sent for that. If empty, there will be no
|
|
|
|
delay and the first email will go out as soon as that status is reached.
|
|
|
|
If set, should probably be a *low* number.
|
|
|
|
""")
|
|
|
|
|
|
|
|
good_temp_max = sa.Column(sa.Integer(), nullable=False, doc="""
|
|
|
|
Maximum good temperature; when a reading is greater than or equal to this
|
|
|
|
value, the probe's status becomes "high temp" (unless the reading also
|
|
|
|
breaches the :attr:`critical_temp_max` threshold).
|
|
|
|
""")
|
|
|
|
|
|
|
|
good_max_timeout = sa.Column(sa.Integer(), nullable=True, doc="""
|
|
|
|
Number of minutes the probe is allowed to have "high temp" status, before
|
|
|
|
the first email alert is sent for that. This is typically meant to account
|
|
|
|
for the length of the defrost cycle, so may be a rather large number.
|
|
|
|
""")
|
|
|
|
|
|
|
|
good_max_started = sa.Column(sa.DateTime(), nullable=True, doc="""
|
|
|
|
Timestamp when the probe readings started to indicate "high temp" status.
|
|
|
|
This should be null unless the probe currently has either "high temp" or
|
|
|
|
"critical high temp" status.
|
|
|
|
""")
|
2018-10-06 17:39:54 -05:00
|
|
|
|
2018-10-19 14:58:30 -05:00
|
|
|
good_temp_min = sa.Column(sa.Integer(), nullable=False, doc="""
|
|
|
|
Minimum good temperature; when a reading is less than or equal to this
|
|
|
|
value, the probe's status becomes "low temp" (unless the reading also
|
|
|
|
breaches the :attr:`critical_temp_min` threshold).
|
|
|
|
""")
|
|
|
|
|
|
|
|
good_min_timeout = sa.Column(sa.Integer(), nullable=True, doc="""
|
|
|
|
Number of minutes the probe is allowed to have "low temp" status, before
|
|
|
|
the first email alert is sent for that.
|
|
|
|
""")
|
|
|
|
|
|
|
|
good_min_started = sa.Column(sa.DateTime(), nullable=True, doc="""
|
|
|
|
Timestamp when the probe readings started to indicate "low temp" status.
|
|
|
|
This should be null unless the probe currently has either "low temp" or
|
|
|
|
"critical low temp" status.
|
|
|
|
""")
|
|
|
|
|
|
|
|
critical_temp_min = sa.Column(sa.Integer(), nullable=False, doc="""
|
|
|
|
Minimum low temperature; when a reading is less than or equal to this
|
|
|
|
value, the probe's status becomes "critical low temp". If empty, there
|
|
|
|
will be no delay and the first email will go out as soon as that status is
|
|
|
|
reached.
|
|
|
|
""")
|
|
|
|
|
|
|
|
critical_min_started = sa.Column(sa.DateTime(), nullable=True, doc="""
|
|
|
|
Timestamp when the probe readings started to indicate "critical low temp"
|
|
|
|
status. This should be null unless the probe currently has that status.
|
|
|
|
""")
|
|
|
|
|
|
|
|
critical_min_timeout = sa.Column(sa.Integer(), nullable=True, doc="""
|
|
|
|
Number of minutes the probe is allowed to have "critical low temp" status,
|
|
|
|
before the first email alert is sent for that. If empty, there will be no
|
|
|
|
delay and the first email will go out as soon as that status is reached.
|
|
|
|
""")
|
|
|
|
|
|
|
|
error_started = sa.Column(sa.DateTime(), nullable=True, doc="""
|
|
|
|
Timestamp when the probe readings started to indicate "error" status. This
|
|
|
|
should be null unless the probe currently has that status.
|
|
|
|
""")
|
|
|
|
|
|
|
|
error_timeout = sa.Column(sa.Integer(), nullable=True, doc="""
|
|
|
|
Number of minutes the probe is allowed to have "error" status, before the
|
|
|
|
first email alert is sent for that. If empty, there will be no delay and
|
|
|
|
the first email will go out as soon as that status is reached.
|
|
|
|
""")
|
|
|
|
|
|
|
|
# TODO: deprecate / remove this
|
2018-10-06 17:39:54 -05:00
|
|
|
therm_status_timeout = sa.Column(sa.Integer(), nullable=False, doc="""
|
2018-10-19 16:47:20 -05:00
|
|
|
NOTE: This field is deprecated; please set the value for High Timeout instead.
|
2018-10-06 17:39:54 -05:00
|
|
|
""")
|
|
|
|
|
|
|
|
status_alert_timeout = sa.Column(sa.Integer(), nullable=False, doc="""
|
2018-10-19 16:47:20 -05:00
|
|
|
Number of minutes between successive status alert emails. These alerts
|
|
|
|
will continue to be sent until the status changes. Note that the *first*
|
|
|
|
alert for a given status will be delayed according to the timeout for that
|
|
|
|
status.
|
2018-10-06 17:39:54 -05:00
|
|
|
""")
|
2016-12-05 19:06:34 -06:00
|
|
|
|
2018-09-28 12:26:49 -05:00
|
|
|
notes = sa.Column(sa.Text(), nullable=True, doc="""
|
|
|
|
Any arbitrary notes for the probe.
|
|
|
|
""")
|
|
|
|
|
2016-12-05 19:06:34 -06:00
|
|
|
status = sa.Column(sa.Integer(), nullable=True)
|
|
|
|
status_changed = sa.Column(sa.DateTime(), nullable=True)
|
|
|
|
status_alert_sent = sa.Column(sa.DateTime(), nullable=True)
|
|
|
|
|
2017-02-07 14:47:57 -06:00
|
|
|
def __str__(self):
|
|
|
|
return self.description
|
2016-12-05 19:06:34 -06:00
|
|
|
|
2018-10-19 14:58:30 -05:00
|
|
|
def start_status(self, status, time):
|
|
|
|
"""
|
|
|
|
Update the "started" timestamp field for the given status. This is
|
|
|
|
used to track e.g. when we cross the "high temp" threshold, as a
|
|
|
|
separate event from when the "critical high temp" threshold is reached.
|
|
|
|
|
|
|
|
Note that in addition to setting the appropriate timestamp field, this
|
|
|
|
also will clear out other timestamp fields, according to the specific
|
|
|
|
(new) status.
|
|
|
|
"""
|
|
|
|
if status in (enum.TEMPMON_PROBE_STATUS_CRITICAL_HIGH_TEMP,
|
|
|
|
enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP):
|
|
|
|
self.critical_max_started = time
|
|
|
|
# note, we don't clear out "high temp" time
|
|
|
|
self.good_min_started = None
|
|
|
|
self.critical_min_started = None
|
|
|
|
self.error_started = None
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_HIGH_TEMP:
|
|
|
|
self.critical_max_started = None
|
|
|
|
self.good_max_started = time
|
|
|
|
self.good_min_started = None
|
|
|
|
self.critical_min_started = None
|
|
|
|
self.error_started = None
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_LOW_TEMP:
|
|
|
|
self.critical_max_started = None
|
|
|
|
self.good_max_started = None
|
|
|
|
self.good_min_started = time
|
|
|
|
self.critical_min_started = None
|
|
|
|
self.error_started = None
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_CRITICAL_LOW_TEMP:
|
|
|
|
self.critical_max_started = None
|
|
|
|
self.good_max_started = None
|
|
|
|
# note, we don't clear out "low temp" time
|
|
|
|
self.critical_min_started = time
|
|
|
|
self.error_started = None
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_ERROR:
|
|
|
|
# note, we don't clear out any other status times
|
|
|
|
self.error_started = time
|
|
|
|
|
|
|
|
def status_started(self, status):
|
|
|
|
"""
|
|
|
|
Return the timestamp indicating when the given status started.
|
|
|
|
"""
|
|
|
|
if status in (enum.TEMPMON_PROBE_STATUS_CRITICAL_HIGH_TEMP,
|
|
|
|
enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP):
|
|
|
|
return self.critical_max_started
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_HIGH_TEMP:
|
|
|
|
return self.good_max_started
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_LOW_TEMP:
|
|
|
|
return self.good_min_started
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_CRITICAL_LOW_TEMP:
|
|
|
|
return self.critical_min_started
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_ERROR:
|
|
|
|
return self.error_started
|
|
|
|
|
|
|
|
def timeout_for_status(self, status):
|
|
|
|
"""
|
|
|
|
Returns the timeout value for the given status. This is be the number
|
|
|
|
of minutes by which we should delay the initial email for the status.
|
|
|
|
"""
|
|
|
|
if status in (enum.TEMPMON_PROBE_STATUS_CRITICAL_HIGH_TEMP,
|
|
|
|
enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP):
|
|
|
|
return self.critical_max_timeout
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_HIGH_TEMP:
|
|
|
|
return self.good_max_timeout or self.therm_status_timeout
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_LOW_TEMP:
|
|
|
|
return self.good_min_timeout or self.therm_status_timeout
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_CRITICAL_LOW_TEMP:
|
|
|
|
return self.critical_min_timeout
|
|
|
|
|
|
|
|
elif status == enum.TEMPMON_PROBE_STATUS_ERROR:
|
|
|
|
return self.error_timeout
|
|
|
|
|
2016-12-05 19:06:34 -06:00
|
|
|
|
2017-02-07 14:47:57 -06:00
|
|
|
@six.python_2_unicode_compatible
|
2016-12-05 19:06:34 -06:00
|
|
|
class Reading(Base):
|
|
|
|
"""
|
2017-04-01 16:37:00 -05:00
|
|
|
Represents a single temperature reading from a tempmon probe.
|
2016-12-05 19:06:34 -06:00
|
|
|
"""
|
|
|
|
__tablename__ = 'reading'
|
|
|
|
__table_args__ = (
|
|
|
|
sa.ForeignKeyConstraint(['client_uuid'], ['client.uuid'], name='reading_fk_client'),
|
|
|
|
sa.ForeignKeyConstraint(['probe_uuid'], ['probe.uuid'], name='reading_fk_probe'),
|
|
|
|
)
|
|
|
|
|
|
|
|
uuid = uuid_column()
|
|
|
|
|
|
|
|
client_uuid = sa.Column(sa.String(length=32), nullable=False)
|
|
|
|
client = orm.relationship(
|
|
|
|
Client,
|
|
|
|
doc="""
|
|
|
|
Reference to the tempmon client which took this reading.
|
2017-08-04 16:07:35 -05:00
|
|
|
""",
|
|
|
|
backref=orm.backref(
|
|
|
|
'readings',
|
|
|
|
cascade='all, delete-orphan'))
|
2016-12-05 19:06:34 -06:00
|
|
|
|
|
|
|
probe_uuid = sa.Column(sa.String(length=32), nullable=False)
|
|
|
|
probe = orm.relationship(
|
|
|
|
Probe,
|
|
|
|
doc="""
|
|
|
|
Reference to the tempmon probe which took this reading.
|
2017-08-04 15:14:48 -05:00
|
|
|
""",
|
|
|
|
backref=orm.backref(
|
|
|
|
'readings',
|
|
|
|
cascade='all, delete-orphan'))
|
2016-12-05 19:06:34 -06:00
|
|
|
|
|
|
|
taken = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow)
|
2017-08-05 12:54:08 -05:00
|
|
|
degrees_f = sa.Column(sa.Numeric(precision=8, scale=4), nullable=False)
|
2016-12-05 19:06:34 -06:00
|
|
|
|
2017-02-07 14:47:57 -06:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.degrees_f)
|