From 451ee498abbf5a4b7986954a561e4bda94955e72 Mon Sep 17 00:00:00 2001 From: Ben Brown Date: Tue, 21 Jan 2014 11:38:48 +0000 Subject: Initial commit for trove-overseer project --- .gitignore | 2 + overseerlib/__init__.py | 23 ++++++++++ overseerlib/app.py | 83 ++++++++++++++++++++++++++++++++++ overseerlib/mailnotify.py | 44 ++++++++++++++++++ overseerlib/parseyaml.py | 43 ++++++++++++++++++ overseerlib/plugins/__init__.py | 0 overseerlib/plugins/diskfree_plugin.py | 48 ++++++++++++++++++++ overseerlib/plugins/ramfree_plugin.py | 53 ++++++++++++++++++++++ trove-overseer | 21 +++++++++ 9 files changed, 317 insertions(+) create mode 100644 .gitignore create mode 100644 overseerlib/__init__.py create mode 100644 overseerlib/app.py create mode 100644 overseerlib/mailnotify.py create mode 100644 overseerlib/parseyaml.py create mode 100644 overseerlib/plugins/__init__.py create mode 100644 overseerlib/plugins/diskfree_plugin.py create mode 100644 overseerlib/plugins/ramfree_plugin.py create mode 100755 trove-overseer diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6add511 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +*~ \ No newline at end of file diff --git a/overseerlib/__init__.py b/overseerlib/__init__.py new file mode 100644 index 0000000..1abf511 --- /dev/null +++ b/overseerlib/__init__.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +# +# Copyright (C) 2014 Codethink Limited +# +# This program 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; version 2 of the License. +# +# This program 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import cliapp + +import mailnotify + +import app diff --git a/overseerlib/app.py b/overseerlib/app.py new file mode 100644 index 0000000..13fb461 --- /dev/null +++ b/overseerlib/app.py @@ -0,0 +1,83 @@ +#!/usr/bin/python +# +# Copyright (C) 2014 Codethink Limited +# +# This program 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; version 2 of the License. +# +# This program 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import os +import smtplib +import getpass + +import cliapp + +import overseerlib + + +class TroveOverseer(cliapp.Application): + + def add_settings(self): + self.settings.string(['config-path'], 'path to configuration file', + metavar='FILE', default='/etc/monitoring.conf') + + def process_args(self, args): +# self.rungit(['remote', 'update', 'origin']) +# self.rungit(['reset', '--hard', 'origin/master']) +# self.rungit(['clean', '-fdx']) + cliapp.Application.process_args(self, args) + + def human_to_bytes(self, threshold): + symbols = ('B', 'K', 'M', 'G', 'T') + s = threshold + letter = s[-1:].strip().upper() + num = s[:-1] + if letter not in symbols: + raise cliapp.AppException('Valid symbols: B, K, M, G, T') + num = float(num) + prefix = {symbols[0]:1} + for i, s in enumerate(symbols[1:]): + prefix[s] = 1 << (i+1)*10 + return int(num * prefix[letter]) + + def rungit(self, args): + self.runcmd(['git']+args, cwd='/src/morph') + + def write_to_html(self, html_file): + if html_file: + try: + with open(self.html_file + '.new', 'w') as ofh: + ofh.write('\n') + ofh.write(self.gen_html()) + ofh.write('\n') + target = self.filename + os.rename(self.filename + '.new', target) + except: + os.unlink(self.filename + '.new') + raise + + def gen_footer(self): + curtime = format_time(time.time()) + return self.tag('div', Class='footer', content= + 'Generated by Trove Monitoring at ' + curtime) + + + def tag(self, tagname, content=None, gap=False, **kwargs): + tagval = ' '.join([tagname] + + ['%s=%r' % (k.lower(), v) + for k, v in kwargs.iteritems()]) + gap = '\n' if gap else '' + if content is None: + return '<%s />' % tagval + else: + return '<%s>%s%s%s' % (tagval, gap, content, gap, tagname) diff --git a/overseerlib/mailnotify.py b/overseerlib/mailnotify.py new file mode 100644 index 0000000..1be0132 --- /dev/null +++ b/overseerlib/mailnotify.py @@ -0,0 +1,44 @@ +#!/usr/bin/python +# +# Copyright (C) 2014 Codethink Limited +# +# This program 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; version 2 of the License. +# +# This program 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import smtplib +import getpass + +import cliapp + + +class MailNotify(object): + + def send_mail(self, content): + sender = 'ben.brown@codethink.co.uk' + username = 'benbrown' + password = getpass.getpass() + recipients = ['ben.brown@codethink.co.uk'] + composed_message = '''\ +From: %s +To: %s +Subject: Test + +%s + ''' % (sender, ', '.join(recipients), content) + server = smtplib.SMTP_SSL('imap.codethink.co.uk') + server.login(username, password) + try: + server.sendmail(sender, recipients, composed_message) + finally: + server.close diff --git a/overseerlib/parseyaml.py b/overseerlib/parseyaml.py new file mode 100644 index 0000000..664f265 --- /dev/null +++ b/overseerlib/parseyaml.py @@ -0,0 +1,43 @@ +#!/usr/bin/python +# +# Copyright (C) 2014 Codethink Limited +# +# This program 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; version 2 of the License. +# +# This program 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import yaml + +import cliapp + + +class ParseYAML(object): + + def parse_yaml(self, self.settings['config-name']): + sender = 'ben.brown@codethink.co.uk' + username = 'benbrown' + password = getpass.getpass() + recipients = ['ben.brown@codethink.co.uk'] + composed_message = '''\ +From: %s +To: %s +Subject: Test + +%s + ''' % (sender, ', '.join(recipients), content) + server = smtplib.SMTP_SSL('imap.codethink.co.uk') + server.login(username, password) + try: + server.sendmail(sender, recipients, composed_message) + finally: + server.close diff --git a/overseerlib/plugins/__init__.py b/overseerlib/plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/overseerlib/plugins/diskfree_plugin.py b/overseerlib/plugins/diskfree_plugin.py new file mode 100644 index 0000000..d0de752 --- /dev/null +++ b/overseerlib/plugins/diskfree_plugin.py @@ -0,0 +1,48 @@ +#!/usr/bin/python +# +# Copyright (C) 2014 Codethink Limited +# +# This program 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; version 2 of the License. +# +# This program 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import os + +import cliapp + +import overseerlib + + +class DiskFreePlugin(cliapp.Plugin): + + def enable(self): + self.app.add_subcommand('diskfree', self.diskfree, + arg_synopsis='PARTITION THRESHOLD') + + def disable(self): + pass + + def diskfree(self, args): + if len(args) < 2: + raise cliapp.AppException('diskfree needs a partition and a ' + 'threshold to be specified') + partition = args[0] + threshold = args[1] + threshold_type = args[2] if len(args) == 3 else 'high' + + if threshold.isdigit() == False: + threshold = self.app.human_to_bytes(threshold) + + st = os.statvfs(partition) + free = st.f_bavail * st.f_frsize + print free diff --git a/overseerlib/plugins/ramfree_plugin.py b/overseerlib/plugins/ramfree_plugin.py new file mode 100644 index 0000000..b1161ee --- /dev/null +++ b/overseerlib/plugins/ramfree_plugin.py @@ -0,0 +1,53 @@ +#!/usr/bin/python +# +# Copyright (C) 2014 Codethink Limited +# +# This program 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; version 2 of the License. +# +# This program 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import cliapp + +import overseerlib + + +class RamFreePlugin(cliapp.Plugin): + + def enable(self): + self.app.add_subcommand('ramfree', self.ramfree, + arg_synopsis='THRESHOLD') + + def disable(self): + pass + + def ramfree(self, args): + if len(args) < 1: + raise cliapp.AppException('ramfree requires a threshold to be ' + 'specified') + threshold = args[0] + threshold_type = args[1] if len(args) == 2 else 'high' + + if threshold.isdigit() == False: + threshold = overseerlib.app.human_to_bytes(threshold) + + with open('/proc/meminfo') as f: + for line in f: + if line.startswith('MemFree'): + free = line.split()[1] + free = int(free) * 1024 + + message = '' + if free >= threshold: + message+=('%s bytes of memory free\n' % free) + if message != '': + print message diff --git a/trove-overseer b/trove-overseer new file mode 100755 index 0000000..681e18c --- /dev/null +++ b/trove-overseer @@ -0,0 +1,21 @@ +#!/usr/bin/python +# +# Copyright (C) 2014 Codethink Limited +# +# This program 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; version 2 of the License. +# +# This program 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import overseerlib + +overseerlib.app.TroveOverseer().run() -- cgit v1.2.1