[Erp5-report] r26880 - in /erp5/trunk/utils/mymonitor: ./ mymonitor.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Thu May 7 15:08:06 CEST 2009
Author: vincent
Date: Thu May 7 15:08:02 2009
New Revision: 26880
URL: http://svn.erp5.org?rev=26880&view=rev
Log:
Initial import of mysql monitoring script.
Added:
erp5/trunk/utils/mymonitor/
erp5/trunk/utils/mymonitor/mymonitor.py (with props)
Added: erp5/trunk/utils/mymonitor/mymonitor.py
URL: http://svn.erp5.org/erp5/trunk/utils/mymonitor/mymonitor.py?rev=26880&view=auto
==============================================================================
--- erp5/trunk/utils/mymonitor/mymonitor.py (added)
+++ erp5/trunk/utils/mymonitor/mymonitor.py [utf8] Thu May 7 15:08:02 2009
@@ -1,0 +1,160 @@
+#!/usr/bin/python
+##############################################################################
+#
+# Copyright (c) 2009 Nexedi SARL and Contributors. All Rights Reserved.
+# Vincent Pelletier <vincent at nexedi.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsability of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# garantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# 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; either version 2
+# of the License, or (at your option) any later version.
+#
+# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+##############################################################################
+# Inspired by http://faemalia.net/mysqlUtils/dbmon.pl
+
+import sys
+import MySQLdb
+try:
+ import smtplib
+except ImportError:
+ smtplib = None
+import platform
+from cStringIO import StringIO
+from optparse import OptionParser
+
+PROCESS_QUERY = 'SHOW FULL PROCESSLIST'
+PROCESS_DETAIL_LIST = ['Id', 'User', 'Host', 'db', 'Command', 'Time', 'State',
+ 'Info']
+
+def main():
+ hostname = platform.node()
+ if hostname == '':
+ hostname = 'localhost'
+ parser = OptionParser()
+ parser.add_option('-v', '--verbosee', dest='verbose', action='store_true',
+ help='Display what is happening (never sent by mail).')
+ # Functionality options
+ parser.add_option('-t', '--threshold', default=600, type='float',
+ dest='threshold',
+ help='Duration, in seconds, above which a query will '\
+ 'be reported. [default: %default]')
+ # Database options
+ parser.add_option('-u', '--user', default='root', dest='db_user',
+ help='Database user login. [default: %default]')
+ parser.add_option('-p', '--password', dest='db_password',
+ help='Database user password. [default: %default]')
+ parser.add_option('-H', '--host', default='localhost',
+ dest='db_host',
+ help='Database host. [default: %default]')
+ if smtplib is not None:
+ # Mail options
+ parser.add_option('-m', '--mail-to', dest='mail_to',
+ help='Send mail to given address instead of printing '\
+ 'on stdout.')
+ parser.add_option('-f', '--from', default='mymonitor@%s' % (hostname, ),
+ dest='mail_from',
+ help='Mail "From" address (requires -m). '\
+ '[default: %default]')
+ parser.add_option('-M', '--mail-server', default='localhost:25',
+ dest='mail_host',
+ help='Mail server to contact (requires -m). '\
+ '[default: %default]')
+ parser.add_option('-F', '--force-send-mail', dest='mail_force',
+ action='store_true',
+ help='Send a mail even if no error is detected. '\
+ '(requires -m)')
+ parser.add_option('-s', '--subject', dest='mail_subject',
+ default='mymonitor on %s: ' % (hostname, ),
+ help='Mail subject. Appended with "OK" or "ERROR" ' \
+ 'depending on detection outcome. (requires -m)'\
+ ' [default: %default]')
+ (options, args) = parser.parse_args()
+ assert len(args) == 0, repr(args)
+ if options.mail_to is None:
+ out = sys.stdout
+ else:
+ out = StringIO()
+
+ # Connect to MySQL, list runing processes and disconnect.
+ mysql_arg_dict = {'user': options.db_user,
+ 'host': options.db_host}
+ if options.db_password is not None:
+ mysql_arg_dict['passwd'] = options.db_password
+ if options.verbose:
+ print >>sys.stderr, 'Connecting to MySQL...'
+ sql_connection = MySQLdb.Connect(**mysql_arg_dict)
+ cursor = sql_connection.cursor(cursorclass=MySQLdb.cursors.DictCursor)
+ if options.verbose:
+ print >>sys.stderr, 'Sending query...'
+ cursor.execute(PROCESS_QUERY)
+ if options.verbose:
+ print >>sys.stderr, 'Fetching results...'
+ process_list = cursor.fetchall()
+ if options.verbose:
+ print >>sys.stderr, 'Disconnecting from MySQL...'
+ cursor.close()
+ sql_connection.close()
+
+ # Iterate on processes
+ found = False
+ for process in process_list:
+ if process['Time'] < options.threshold or \
+ process['Command'] in ('Sleep', 'Connect') or \
+ process['User'] in ('System User', ) or \
+ process['Info'] in ('undef', PROCESS_QUERY):
+ continue
+ found = True
+
+ print >>out, '\n '.join(['%s: %r' % (x, process[x]) \
+ for x in PROCESS_DETAIL_LIST])
+
+ if options.mail_to is not None and (found or options.mail_force):
+ # Send result by mail
+ if found:
+ subject = options.mail_subject + 'ERROR'
+ else:
+ subject = options.mail_subject + 'OK'
+ if options.verbose:
+ print >>sys.stderr, 'Connecting to mail server...'
+ if ':' in options.mail_host:
+ host, port = options.mail_host.split(':', 1)
+ smtp_arg_dict = {'host': host, 'port': port}
+ else:
+ smtp_arg_dict = {'host': options.mail_host}
+ smtp_server = smtplib.SMTP(**smtp_arg_dict)
+ if options.verbose:
+ print >>sys.stderr, 'Sending mail...'
+ headers = 'From: %s\nTo: %s\nSubject: %s\n\n' % (options.mail_from,
+ options.mail_to,
+ subject)
+ smtp_server.sendmail(options.mail_from, options.mail_to,
+ headers + out.getvalue())
+ if options.verbose:
+ print >>sys.stderr, 'Disconnecting from mail server...'
+ smtp_server.quit()
+
+ if options.verbose:
+ print >>sys.stderr, 'Exiting...'
+
+ if found:
+ sys.exit(1)
+
+if __name__ == '__main__':
+ main()
+
Propchange: erp5/trunk/utils/mymonitor/mymonitor.py
------------------------------------------------------------------------------
svn:executable = *
More information about the Erp5-report
mailing list