My sanity check script |
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Copyright (c) 2006-2012 Axel Rau, [email protected] All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""
IMAP_ACCOUNT_ROOT = '/users'
IMAP_SPAM_DROPS = '"%@%/SPAM drop"'
IMAP_SPAM_ARCHIVE_BASE = '/users/erdb/Archiv/SPAMs'
IMAP_RESPONSE_ARCHIVE_BASE = '/users/erdb/Archiv/SpamCop'
IMAP_SERVER = 'imap.lrau.net'
IMAP_USER = 'erdb'
IMAP_PSWD = '31731706OTXKJfiutiwq60#,%=_h;'
#--------------- imported modules --------------
import imaplib
import codecs
import datetime
import fcntl
import optparse
import subprocess
import sys
import re
year = str(datetime.datetime.today().year)
lockfile = '/var/run/aox_restart_if_died.lock'
#--------------- Class ImapServer --------------
class ImapServer(object):
""" wrapper for an imap server connection, and list of 'SPAM drop' mailboxes"""
imap = None # reference to imap server
mbxs = [] # mailboxes as string
mailBoxList = [] # mailbox index
selectedMbx = '' # selected mailbox
spamList = [] # message index
myArchiveFolder = ''
myMailType = ''
def __init__(self,server,user,pswd,inputFolder,archiveFolder, mailType):
self.myArchiveFolder = archiveFolder + '/' + year
self.myMailType = mailType
self.imap = imaplib.IMAP4(server)
self.imap.starttls()
if options.debug:
self.imap.debug = 4
self.imap.login(user,pswd)
# check if archive folder exists
status, data = self.imap.select(self.myArchiveFolder)
if status != 'OK':
self.imap.create(self.myArchiveFolder)
# obtain list of SPAM drops
status, mbxs = self.imap.list(IMAP_ACCOUNT_ROOT, inputFolder) # exception EOF on 2014-03-222
if options.debug:
print('Status: %s, data: %s' % (status, mbxs))
self.mailBoxList = mbxs
if options.debug:
print(self.mailBoxList)
def __del__(self):
if self.imap:
try:
self.imap.logout()
except: # ignore exceptions while closing
pass
def get_backtrace_of_aox():
cp = subprocess.run(["ps", "-a", "-x", "-l", "-w", "-U 666"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
lines = codecs.decode(cp.stdout).splitlines()
for line in lines:
if 'archiveopteryx' in line:
m = re.search('(\d+)\s+(\d+)', line)
if m and m.group(2): # did we get a pid = did the server run?
pid = m.group(2)
print('[Creating backtrace of archiveopteryx with pid {}]'.format(pid))
subprocess.run(["lldb", "-p", pid, "-s", "/root/.lldbinit"])
def get_coredump_of_aox():
cp = subprocess.run(["ps", "-a", "-x", "-l", "-w", "-U 666"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
lines = codecs.decode(cp.stdout).splitlines()
for line in lines:
if 'archiveopteryx' in line:
m = re.search('(\d+)\s+(\d+)', line)
if m and m.group(2): # did we get a pid = did the server run?
pid = m.group(2)
print('[Creating core dump of archiveopteryx with pid {}]'.format(pid))
subprocess.run(["gcore", "-s", "/usr/local/sbin/archiveopteryx", pid])
subprocess.run(["mv", '/root/core.{}'.format(pid), '/var/coredumps/'])
def restart_aox():
subprocess.run(["ps", "-a", "-x", "-l", "-w", "-U 666"])
subprocess.run(["service", "archiveopteryx", "onerestart"])
subprocess.run(["ps", "-a", "-x", "-l", "-w", "-U 666"])
#--------------- main --------------
parser = optparse.OptionParser(description='Archiveopteryx sanity checker')
parser.add_option('--debug', '-d', dest='debug', action='store_true',
default=False,
help='Turn on debugging.'),
parser.add_option('--no-Restart', '-n', dest='no_restart', action='store_true',
default=False,
help='Do not restart archiveopteryx if stuck.')
parser.add_option('--backtrace', '-b', dest='bt', action='store_true',
default=False,
help='Produce backtrace and restart archiveopteryx.')
(options, args) = parser.parse_args()
if options.debug:
print('[Debugging]')
lock_fd = open(lockfile, 'w')
try:
fcntl.flock(lock_fd, fcntl.LOCK_EX)
except OSError:
print('aox_restart_if_died observed a lock overrun.')
sys.exit(1)
im = None
im_err = True
for c in range(3):
try:
im = ImapServer(IMAP_SERVER, IMAP_USER, IMAP_PSWD,
IMAP_SPAM_DROPS, IMAP_SPAM_ARCHIVE_BASE, 'SPAM(s)')
if im and im.imap:
im.imap.logout()
im_err = False
break
except Exception as e:
print('?Error while connecting to IMAP server and reading root folder, because: ' + str(e))
if options.bt:
im_err = True
if im_err:
print('IMAP server failed.')
##get_coredump_of_aox()
get_backtrace_of_aox()
restart_aox()
fcntl.flock(lock_fd, fcntl.LOCK_UN)
sys.exit(1)
uses and this returns either ?Error while connecting to IMAP server and reading root folder, because: [Errno 61] Connection refused or ?Error while connecting to IMAP server and reading root folder, because: EOF occurred in violation of protocol (_ssl.c:720 Axel
--- PGP-Key:29E99DD6 ☀ computing @ chaos claudius |
