Exemple #1
0
    def run(self):
        import datetime, calendar
        '''
        Daemon start to run here.
        '''

        icount = 0
        jcount = 0
        days = settings.ReportSetting().getReportSetting()['report_interval']
        # Run while there is no stop request.
        while os.path.exists(self.runfile):
            try:
                if icount == 0 and calendar.monthrange(
                        datetime.datetime.now().year,
                        datetime.datetime.now().month
                )[1] == datetime.datetime.now().day:
                    genPDF(mode=self.conf['mode'],
                           conf=self.conf['conf'],
                           filename=self.conf['filename'])
                    icount += 1

                genMonitor(mode='alert')

                if jcount == 0 and datetime.datetime.now().hour in days:
                    genMonitor(conf=None, mode='daily')
                    jcount += 1

            except Exception as err:
                if self.debug:
                    raise
                else:
                    sys.stderr.write('%s\n' % (err))
            finally:
                #print("hello")
                if calendar.monthrange(datetime.datetime.now().year,
                                       datetime.datetime.now().month
                                       )[1] != datetime.datetime.now().day:
                    icount = 0

                if datetime.datetime.now().hour not in days:
                    jcount = 0

                self.wait(timeout=self.looptimeout)
Exemple #2
0
    def main(self):
        #i = 0
        import datetime, calendar
        '''
        Daemon start to run here.
        '''

        icount = 0
        jcount = 0
        days = settings.ReportSetting().getReportSetting()['report_interval']

        while self.isAlive:
            # Run while there is no stop request.
            try:
                if icount == 0 and calendar.monthrange(
                        datetime.datetime.now().year,
                        datetime.datetime.now().month
                )[1] == datetime.datetime.now().day:
                    genPDF()
                    icount += 1

                genMonitor(mode='alert')

                if jcount == 0 and datetime.datetime.now().hour in days:
                    genMonitor(conf=None, mode='daily')
                    jcount += 1

            except Exception as err:
                sys.stderr.write('%s\n' % (err))
            finally:
                #print("hello")
                if calendar.monthrange(datetime.datetime.now().year,
                                       datetime.datetime.now().month
                                       )[1] != datetime.datetime.now().day:
                    icount = 0

                if datetime.datetime.now().hour not in days:
                    jcount = 0

                time.sleep(LOOP_SLEEP)
Exemple #3
0
def genPDF(mode=None, conf=None, filename=None):
    #from config import settings
    from report import oraclerpt
    from utils import wechat, mail

    rptsettings = settings.ReportSetting(conf).getReportSetting()

    # 附件文件
    if filename is not None:
        report_filename = filename
    else:
        report_filename = rptsettings['report_filename']

    # 邮件设置信息
    mail_host = rptsettings['mail_host']
    mail_user = rptsettings['mail_user']
    mail_pass = rptsettings['mail_pass']
    mail_sender = rptsettings['mail_sender']
    mail_receiver = rptsettings['mail_receiver']
    mail_subject = rptsettings['mail_subject']
    mail_content = rptsettings['mail_content']

    # 微信企业号文件共享
    corpid = rptsettings['corpid']
    corpsecret = rptsettings['corpsecret']
    #     token = wechat.get_token(corpid, corpsecret)
    tag = rptsettings['tag']
    touser = rptsettings['touser']
    toparty = rptsettings['toparty']
    agentid = rptsettings['agentid']
    subject = rptsettings['subject']
    content = rptsettings['content']

    content_error = rptsettings['content_error']

    status = 0

    try:
        rpt = oraclerpt.OracleReport(settings.ReportSetting(conf),
                                     report_filename)
        if mode is not None:
            rpt.run(mode)
        else:
            rpt.run()
    except Exception as e:
        status = 1
        print(e)
        print('生成PDF巡检报告失败!')

    # 发送信息及附件
    try:
        if status == 1:
            wechat.send_msg(corpid, corpsecret, tag, touser, toparty, agentid,
                            subject, content_error)
        else:
            wechat.send_msg(corpid, corpsecret, tag, touser, toparty, agentid,
                            subject, content)
            wechat.send_file(corpid, corpsecret, tag, touser, toparty, agentid,
                             report_filename)
    except Exception as e:
        print(e)
        print('微信消息及附件发送失败,您可能现在无法连接到互联网!')

    print(status)

    try:
        if status == 1:
            mail.sendMail(mail_host, mail_user, mail_pass, mail_sender,
                          mail_receiver, mail_subject, content_error,
                          report_filename)
        else:
            mail.sendMail(mail_host, mail_user, mail_pass, mail_sender,
                          mail_receiver, mail_subject, mail_content,
                          report_filename)
    except Exception as e:
        print(e)
        print('邮件消息及附件发送失败,您可能现在无法连接到互联网!')
Exemple #4
0
def genMonitor(conf=None, mode='alert'):
    from report import monitoring

    from utils import wechat, mail

    rptsettings = settings.ReportSetting(conf).getReportSetting()

    # 邮件设置信息
    mail_host = rptsettings['mail_host']
    mail_user = rptsettings['mail_user']
    mail_pass = rptsettings['mail_pass']
    mail_sender = rptsettings['mail_sender']
    mail_receiver = rptsettings['mail_receiver']
    mail_subject = '数据库监控'
    mail_content = ''

    # 微信企业号文件共享
    corpid = rptsettings['corpid']
    corpsecret = rptsettings['corpsecret']
    #     token = wechat.get_token(corpid, corpsecret)
    tag = rptsettings['tag']
    touser = rptsettings['touser']
    toparty = rptsettings['toparty']
    agentid = rptsettings['agentid']
    subject = rptsettings['subject']
    content = ''

    content_error = ''

    status = 0

    om = monitoring.OracleMonitor().test_connect_oracle()
    for oralist in om:
        content = content + '***********************\n' + '主机名:' + oralist[
            'host'] + '\n' + '实例名称:' + oralist['instance'][
                0] + '\n' + '实例状态:' + oralist['instance'][
                    3] + '\n' + '数据库名:' + oralist['database'][
                        0] + '\n' + '数据状态:' + oralist['database'][
                            2] + '\n' + '可连接性:' + str(
                                oralist['connectability']) + '\n'
        if oralist['instance'][3] != 'OPEN' or oralist['database'][
                2] != 'READ WRITE' or oralist['connectability'] != 1:
            status = 1
            content_error = content_error + '***********************\n' + '主机名:' + oralist[
                'host'] + '\n' + '实例名称:' + oralist['instance'][
                    0] + '\n' + '实例状态:' + oralist['instance'][
                        3] + '\n' + '数据库名:' + oralist['database'][
                            0] + '\n' + '数据状态:' + oralist['database'][
                                2] + '\n' + '可连接性:' + str(
                                    oralist['connectability']) + '\n'

    # 发送信息及附件
    try:
        if mode == 'alert' and status == 1:
            wechat.send_msg(corpid, corpsecret, tag, touser, toparty, agentid,
                            subject, content_error)

        if mode == 'daily':
            wechat.send_msg(corpid, corpsecret, tag, touser, toparty, agentid,
                            subject, content)
    except Exception as e:
        print(e)
        print('微信消息及附件发送失败,您可能现在无法连接到互联网!')

    try:
        if mode == 'alert' and status == 1:
            mail.sendMail(mail_host, mail_user, mail_pass, mail_sender,
                          mail_receiver, mail_subject, content_error)

        if mode == 'daily':
            mail.sendMail(mail_host, mail_user, mail_pass, mail_sender,
                          mail_receiver, mail_subject, content)
    except Exception as e:
        print(e)
        print('邮件消息及附件发送失败,您可能现在无法连接到互联网!')
Exemple #5
0
@email: [email protected]
@created:Mar 14, 2018

@licence: GPL
'''

import sys, getopt, os
import argparse
import time

from utils.rundaemon import Daemon
from config import settings
from utils import rptlogging

logger = rptlogging.rptlogger(
    settings.ReportSetting().getPackageSetting()['logfile'])

# Daemon name. It's only used in messages.
DAEMON_NAME = 'DBReport Service'
# Cleanstop wait time before to kill the process.
DAEMON_STOP_TIMEOUT = 10
# Daemon pid file.
PIDFILE = '/tmp/dbreport_service.pid'
# Deamon run file. "stop" request deletes this file to inform the process and
# waits DAEMON_STOP_TIMEOUT seconds before to send SIGTERM. The process has a
# change to stop cleanly if it's written appropriately.
RUNFILE = '/tmp/dbreport_service.run'
# Sleep time.
LOOP_SLEEP = 300
# Debug mode.
DEBUG = 0
Exemple #6
0
 def __init__(self, conf=settings.ReportSetting()):
     self.dbsettings = conf.getDatabaseSetting()
Exemple #7
0
        )  # The MIMEBase class always adds a Content-Type header (based on _maintype, _subtype, and _params), and a MIME-Version header (always set to 1.0).

        attachment.set_payload(file.read(
        ))  # Set the entire message object’s payload(负载) to payload.
        encode_base64(
            attachment
        )  # Encodes the payload into base64 form and sets the Content-Transfer-Encoding header to base64.

    file.close()
    """
Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。Content-disposition其实可以控制用户请求所得的内容存为一个文件的时候提供一个默认的文件名,
文件直接在浏览器上显示或者在访问时弹出文件下载对话框。 content-disposition = "Content-Disposition" ":" disposition-type *( ";" disposition-parm ) 。    
    """
    attachment.add_header(
        'Content-Disposition',
        'attachment',
        filename=os.path.basename(attachmentFilePath)
    )  # Content-Disposition为属性名 disposition-type是以什么方式下载,如attachment为以附件方式下载 disposition-parm为默认保存时的文件名
    return attachment


if __name__ == '__main__':
    from config import settings

    rptsettings = settings.ReportSetting().getReportSetting()

    sendMail(rptsettings['mail_host'], rptsettings['mail_user'],
             rptsettings['mail_pass'], rptsettings['mail_sender'],
             rptsettings['mail_receiver'], rptsettings['mail_subject'],
             rptsettings['mail_content'], rptsettings['report_filename'])