Initial commit

This commit is contained in:
Lance Edgar 2016-12-05 19:06:34 -06:00
commit 7550e63940
19 changed files with 1654 additions and 0 deletions

View file

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2016 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Rattail Temperature Monitoring
"""
from ._version import __version__

View file

@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
__version__ = u'0.1.0'

131
rattail_tempmon/client.py Normal file
View file

@ -0,0 +1,131 @@
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2016 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
TempMon client daemon
"""
from __future__ import unicode_literals, absolute_import
import time
import datetime
import socket
import logging
from rattail.daemon import Daemon
from rattail_tempmon.db import Session, model as tempmon
log = logging.getLogger(__name__)
class TempmonClient(Daemon):
"""
Linux daemon implementation of Tempmon client
"""
def run(self):
"""
This method is invoked upon daemon startup. It is meant to run/loop
"forever" or until daemon stop.
"""
# figure out which client we are
session = Session()
client = session.query(tempmon.Client)\
.filter_by(hostname=socket.gethostname())\
.one()
client_uuid = client.uuid
session.close()
# main loop: take readings, pause, repeat
while True:
session = Session()
client = session.query(tempmon.Client).get(client_uuid)
if client.enabled:
try:
for probe in client.enabled_probes():
self.take_reading(session, probe)
except:
log.exception("Failed to read/record temperature data")
session.rollback()
raise
else:
# make sure we show as being online
if not client.online:
client.online = True
session.commit()
finally:
session.close()
else:
session.close()
# TODO: make this configurable
time.sleep(60)
def take_reading(self, session, probe):
"""
Take a single reading and add to Rattail database.
"""
reading = tempmon.Reading()
reading.client = probe.client
reading.probe = probe
reading.degrees_f = self.read_temp(probe)
reading.taken = datetime.datetime.utcnow()
session.add(reading)
return reading
def read_temp(self, probe):
"""
Check for good reading, then format temperature to our liking
"""
lines = self.read_temp_raw(probe)
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = self.read_temp_raw(probe)
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return round(temp_f,4)
def read_temp_raw(self, probe):
"""
Function that gets the raw temp data
"""
with open(probe.device_path, 'rt') as therm_file:
return therm_file.readlines()
def make_daemon(config, pidfile=None):
"""
Returns a tempmon client daemon instance.
"""
if not pidfile:
pidfile = config.get('rattail.tempmon', 'client.pid_path',
default='/var/run/rattail/tempmon-client.pid')
return TempmonClient(pidfile, config=config)

View file

@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2016 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Tempmon commands
"""
from __future__ import unicode_literals, absolute_import
from rattail.commands import Subcommand
class TempmonClient(Subcommand):
"""
Manage the tempmon-client daemon
"""
name = 'tempmon-client'
description = __doc__.strip()
def add_parser_args(self, parser):
subparsers = parser.add_subparsers(title='subcommands')
start = subparsers.add_parser('start', help="Start daemon")
start.set_defaults(subcommand='start')
stop = subparsers.add_parser('stop', help="Stop daemon")
stop.set_defaults(subcommand='stop')
parser.add_argument('-p', '--pidfile',
help="Path to PID file.", metavar='PATH')
parser.add_argument('-D', '--daemonize', action='store_true',
help="Daemonize when starting.")
def run(self, args):
from rattail_tempmon.client import make_daemon
daemon = make_daemon(self.config, args.pidfile)
if args.subcommand == 'start':
daemon.start(args.daemonize)
elif args.subcommand == 'stop':
daemon.stop()
class TempmonServer(Subcommand):
"""
Manage the tempmon-server daemon
"""
name = 'tempmon-server'
description = __doc__.strip()
def add_parser_args(self, parser):
subparsers = parser.add_subparsers(title='subcommands')
start = subparsers.add_parser('start', help="Start daemon")
start.set_defaults(subcommand='start')
stop = subparsers.add_parser('stop', help="Stop daemon")
stop.set_defaults(subcommand='stop')
parser.add_argument('-p', '--pidfile',
help="Path to PID file.", metavar='PATH')
parser.add_argument('-D', '--daemonize', action='store_true',
help="Daemonize when starting.")
def run(self, args):
from rattail_tempmon.server import make_daemon
daemon = make_daemon(self.config, args.pidfile)
if args.subcommand == 'start':
daemon.start(args.daemonize)
elif args.subcommand == 'stop':
daemon.stop()

56
rattail_tempmon/config.py Normal file
View file

@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2016 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Tempmon config extension
"""
from __future__ import unicode_literals, absolute_import
from rattail.config import ConfigExtension
from rattail.db.config import get_engines
from rattail_tempmon.db import Session
class TempmonConfigExtension(ConfigExtension):
"""
Config extension for tempmon; adds tempmon DB engine/Session etc. Expects
something like this in your config:
.. code-block:: ini
[rattail_tempmon.db]
default.url = postgresql://localhost/tempmon
Config object will get two new attributes:
* ``tempmon_engines``
* ``tempmon_engine``
Additionally, :class:`Session` will be configured with the default engine.
"""
key = 'tempmon'
def configure(self, config):
config.tempmon_engines = get_engines(config, section='rattail_tempmon.db')
config.tempmon_engine = config.tempmon_engines.get('default')
Session.configure(bind=config.tempmon_engine)

View file

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2016 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Tempmon database stuff
"""
from __future__ import unicode_literals, absolute_import
from sqlalchemy import orm
Session = orm.sessionmaker()

View file

@ -0,0 +1 @@
Generic single-database configuration.

View file

@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
"""
Alembic Environment Script
"""
from __future__ import unicode_literals, absolute_import
from alembic import context
from rattail.config import make_config
from rattail_tempmon.db import model as tempmon
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use
alembic_config = context.config
# Use same config file for Rattail, as we are for Alembic.
rattail_config = make_config(alembic_config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
target_metadata = tempmon.Base.metadata
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
engine = rattail_config.tempmon_engine
context.configure(
url=engine.url,
target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
engine = rattail_config.tempmon_engine
connection = engine.connect()
context.configure(
connection=connection,
target_metadata=target_metadata)
try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View file

@ -0,0 +1,30 @@
# -*- coding: utf-8; mode: python -*-
# -*- coding: utf-8 -*-
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from __future__ import unicode_literals, absolute_import
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
from alembic import op
import sqlalchemy as sa
import rattail.db.types
${imports if imports else ""}
def upgrade():
${upgrades if upgrades else "pass"}
def downgrade():
${downgrades if downgrades else "pass"}

View file

@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
"""initial tables
Revision ID: 7c7d205787b0
Revises:
Create Date: 2016-12-05 15:14:09.387668
"""
from __future__ import unicode_literals, absolute_import
# revision identifiers, used by Alembic.
revision = '7c7d205787b0'
down_revision = None
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
import rattail.db.types
def upgrade():
# client
op.create_table('client',
sa.Column('uuid', sa.String(length=32), nullable=False),
sa.Column('config_key', sa.String(length=50), nullable=False),
sa.Column('hostname', sa.String(length=255), nullable=False),
sa.Column('location', sa.String(length=255), nullable=True),
sa.Column('enabled', sa.Boolean(), nullable=False),
sa.Column('online', sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint('uuid'),
sa.UniqueConstraint('config_key', name=u'client_uq_config_key')
)
# probe
op.create_table('probe',
sa.Column('uuid', sa.String(length=32), nullable=False),
sa.Column('client_uuid', sa.String(length=32), nullable=False),
sa.Column('config_key', sa.String(length=50), nullable=False),
sa.Column('appliance_type', sa.Integer(), nullable=False),
sa.Column('description', sa.String(length=255), nullable=False),
sa.Column('device_path', sa.String(length=255), nullable=True),
sa.Column('enabled', sa.Boolean(), nullable=False),
sa.Column('good_temp_min', sa.Integer(), nullable=False),
sa.Column('good_temp_max', sa.Integer(), nullable=False),
sa.Column('critical_temp_min', sa.Integer(), nullable=False),
sa.Column('critical_temp_max', sa.Integer(), nullable=False),
sa.Column('therm_status_timeout', sa.Integer(), nullable=False),
sa.Column('status_alert_timeout', sa.Integer(), nullable=False),
sa.Column('status', sa.Integer(), nullable=True),
sa.Column('status_changed', sa.DateTime(), nullable=True),
sa.Column('status_alert_sent', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['client_uuid'], [u'client.uuid'], name=u'probe_fk_client'),
sa.PrimaryKeyConstraint('uuid'),
sa.UniqueConstraint('config_key', name=u'probe_uq_config_key')
)
# tempmon reading
op.create_table('reading',
sa.Column('uuid', sa.String(length=32), nullable=False),
sa.Column('client_uuid', sa.String(length=32), nullable=False),
sa.Column('probe_uuid', sa.String(length=32), nullable=False),
sa.Column('taken', sa.DateTime(), nullable=False),
sa.Column('degrees_f', sa.Numeric(precision=7, scale=4), nullable=False),
sa.ForeignKeyConstraint(['client_uuid'], [u'client.uuid'], name=u'reading_fk_client'),
sa.ForeignKeyConstraint(['probe_uuid'], [u'probe.uuid'], name=u'reading_fk_probe'),
sa.PrimaryKeyConstraint('uuid')
)
def downgrade():
op.drop_table('reading')
op.drop_table('probe')
op.drop_table('client')

140
rattail_tempmon/db/model.py Normal file
View file

@ -0,0 +1,140 @@
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2016 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Data models for tempmon
"""
from __future__ import unicode_literals, absolute_import
import datetime
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base
from rattail.db.model import uuid_column
from rattail.db.model.core import ModelBase
Base = declarative_base(cls=ModelBase)
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)
enabled = sa.Column(sa.Boolean(), nullable=False, default=False)
online = sa.Column(sa.Boolean(), nullable=False, default=False)
def __unicode__(self):
return '{} ({})'.format(self.config_key, self.hostname)
def enabled_probes(self):
return [probe for probe in self.probes if probe.enabled]
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'),
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',
doc="""
List of probes connected to this client.
"""))
config_key = sa.Column(sa.String(length=50), nullable=False)
appliance_type = sa.Column(sa.Integer(), nullable=False)
description = sa.Column(sa.String(length=255), nullable=False)
device_path = sa.Column(sa.String(length=255), nullable=True)
enabled = sa.Column(sa.Boolean(), nullable=False, default=True)
good_temp_min = sa.Column(sa.Integer(), nullable=False)
good_temp_max = sa.Column(sa.Integer(), nullable=False)
critical_temp_min = sa.Column(sa.Integer(), nullable=False)
critical_temp_max = sa.Column(sa.Integer(), nullable=False)
therm_status_timeout = sa.Column(sa.Integer(), nullable=False)
status_alert_timeout = sa.Column(sa.Integer(), nullable=False)
status = sa.Column(sa.Integer(), nullable=True)
status_changed = sa.Column(sa.DateTime(), nullable=True)
status_alert_sent = sa.Column(sa.DateTime(), nullable=True)
def __unicode__(self):
return unicode(self.description or '')
class Reading(Base):
"""
Represents a single tempurate reading from a tempmon probe.
"""
__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.
""")
probe_uuid = sa.Column(sa.String(length=32), nullable=False)
probe = orm.relationship(
Probe,
doc="""
Reference to the tempmon probe which took this reading.
""")
taken = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow)
degrees_f = sa.Column(sa.Numeric(precision=7, scale=4), nullable=False)
def __unicode__(self):
return unicode(self.degrees_f)

161
rattail_tempmon/server.py Normal file
View file

@ -0,0 +1,161 @@
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2016 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Tempmon server daemon
"""
from __future__ import unicode_literals, absolute_import
import time
import datetime
import logging
from rattail.db import Session, api
from rattail_tempmon import Session as TempmonSession
from rattail.daemon import Daemon
from rattail.time import localtime, make_utc
from rattail.mail import send_email
log = logging.getLogger(__name__)
class TempmonServerDaemon(Daemon):
"""
Linux daemon implementation of tempmon server.
"""
timefmt = '%Y-%m-%d %H:%M:%S'
def run(self):
"""
Keeps an eye on tempmon readings and sends alerts as needed.
"""
self.extra_emails = self.config.getlist('rattail.tempmon', 'extra_emails', default=[])
while True:
self.check_readings()
time.sleep(5)
def check_readings(self):
self.now = make_utc()
session = TempmonSession()
probes = session.query(tempmon.Probe)\
.join(tempmon.Client)\
.filter(tempmon.Client.enabled == True)\
.filter(tempmon.Probe.enabled == True)\
.all()
if probes:
cutoff = self.now - datetime.timedelta(seconds=120)
uuids = [probe.uuid for probe in probes]
readings = session.query(tempmon.Reading)\
.filter(tempmon.Reading.probe_uuid.in_(uuids))\
.filter(tempmon.Reading.taken >= cutoff)\
.all()
self.process_readings(probes, readings)
else:
log.warning("found no enabled probes!")
session.commit()
session.close()
# TODO: not sure this is really necessary?
self.set_last_checked()
def process_readings(self, probes, readings):
for probe in probes:
probe_readings = [r for r in readings if r.probe is probe]
if probe_readings:
reading = sorted(probe_readings, key=lambda r: r.taken)[-1]
if (reading.degrees_f <= probe.critical_temp_min or
reading.degrees_f >= probe.critical_temp_max):
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP, reading)
elif reading.degrees_f < probe.good_temp_min:
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP, reading)
elif reading.degrees_f > probe.good_temp_max:
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP, reading)
else: # temp is good
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP, reading)
else: # no readings for probe
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR)
def update_status(self, probe, status, reading=None):
prev_status = probe.status
if probe.status != status:
probe.status = status
probe.status_changed = self.now
probe.status_alert_sent = None
# no email if status is good
if status == self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP:
return
# no email if we already sent one...until timeout is reached
if probe.status_alert_sent:
timeout = datetime.timedelta(minutes=probe.status_alert_timeout)
if (self.now - probe.status_alert_sent) <= timeout:
return
msgtypes = {
self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP : 'tempmon_low_temp',
self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP : 'tempmon_high_temp',
self.enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP : 'tempmon_critical_temp',
self.enum.TEMPMON_PROBE_STATUS_ERROR : 'tempmon_error',
}
data = {
'probe': probe,
'status': self.enum.TEMPMON_PROBE_STATUS[status],
'reading': reading,
'taken': localtime(self.config, make_utc(reading.taken, tzinfo=True)) if reading else None,
'now': localtime(self.config),
}
send_email(self.config, msgtypes[status], data)
# maybe send more emails if config said so
for msgtype in self.extra_emails:
send_email(self.config, msgtype, data)
probe.status_alert_sent = self.now
def set_last_checked(self):
session = Session()
api.save_setting(session, 'tempmon.server.last_checked', self.now.strftime(self.timefmt))
session.commit()
session.close()
def make_daemon(config, pidfile=None):
"""
Returns a tempmon server daemon instance.
"""
if not pidfile:
pidfile = config.get('rattail.tempmon', 'server.pid_path',
default='/var/run/rattail/tempmon-server.pid')
return TempmonServerDaemon(pidfile, config=config)