summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Brown <ben.brown@codethink.co.uk>2014-01-21 11:38:48 +0000
committerBen Brown <ben.brown@codethink.co.uk>2014-01-21 11:40:39 +0000
commit451ee498abbf5a4b7986954a561e4bda94955e72 (patch)
tree0c488aabfbc85b981364276417f90a4a9eb0dca8
downloadtrove-overseer-451ee498abbf5a4b7986954a561e4bda94955e72.tar.gz
Initial commit for trove-overseer project
-rw-r--r--.gitignore2
-rw-r--r--overseerlib/__init__.py23
-rw-r--r--overseerlib/app.py83
-rw-r--r--overseerlib/mailnotify.py44
-rw-r--r--overseerlib/parseyaml.py43
-rw-r--r--overseerlib/plugins/__init__.py0
-rw-r--r--overseerlib/plugins/diskfree_plugin.py48
-rw-r--r--overseerlib/plugins/ramfree_plugin.py53
-rwxr-xr-xtrove-overseer21
9 files changed, 317 insertions, 0 deletions
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('<!DOCTYPE html>\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</%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
--- /dev/null
+++ b/overseerlib/plugins/__init__.py
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()