Ejemplo n.º 1
0
def raise_alert(msg_list):
    """Raise one alert for the passed msg list."""
    try:
        t = int(time.time())
        filename, err = _get_alerts_file_path()
        if err:
            raise Exception(err)
        with open(filename, "a") as f:
            fcntl.flock(f, fcntl.LOCK_EX)
            for msg in msg_list:
                f.write("\n%-13d %s\n" % (t, msg))
            fcntl.flock(f, fcntl.LOCK_UN)
            f.close()
        d, err = mail.load_email_settings()
        if err:
            raise Exception(err)
        if d:
            if d["email_alerts"]:
                ret, err = mail.send_mail(d["server"], d["port"], d["username"], d["pswd"], d["tls"], d["rcpt_list"],
                                          "Alerts from IntegralStor " + networking.get_hostname()[0], '\n'.join(msg_list))
                if err:
                    with open(filename, "a") as f:
                        fcntl.flock(f, fcntl.LOCK_EX)
                        f.write("\n%-13d %s\n" %
                                (t, "Error sending email alert %s" % err))
                        fcntl.flock(f, fcntl.LOCK_UN)
                        f.close()
                    raise Exception(err)
    except Exception, e:
        print "Error raising alert : %s" % str(e)
        with open(filename, "a") as f:
            fcntl.flock(f, fcntl.LOCK_EX)
            f.write("\n%-13d %s\n" % (t, "Error raising alert : %s" % str(e)))
            fcntl.flock(f, fcntl.LOCK_UN)
            f.close()
Ejemplo n.º 2
0
def view_email_settings(request):
    return_dict = {}
    try:
        return_dict['base_template'] = "system_base.html"
        return_dict["page_title"] = 'View Email settings'
        return_dict['tab'] = 'email_settings_tab'
        return_dict["error"] = 'Error loading Email settings'
        d, err = mail.load_email_settings()
        if err:
            raise Exception(err)
        if not d:
            return_dict["email_not_configured"] = True
        else:
            if d["tls"]:
                d["tls"] = True
            else:
                d["tls"] = False
            if d["email_alerts"]:
                d["email_alerts"] = True
            else:
                d["email_alerts"] = False
            return_dict["email_settings"] = d
        ack_msg = ''
        if "ack" in request.GET:
            if request.GET["ack"] == "saved":
                ack_message = "Email settings have been saved. "
        if "err" in request.REQUEST:
            ack_message += 'The following errors were reported : %s' % request.REQUEST["err"]
        return django.shortcuts.render_to_response('view_email_settings.html', return_dict, context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response('logged_in_error.html', return_dict, context_instance=django.template.context.RequestContext(request))
Ejemplo n.º 3
0
def audit(audit_code, audit_str, request, system_initiated=False):
    try:
        db_path, err = config.get_db_path()
        if err:
            raise Exception(err)
        if system_initiated is False:
            ip, err = networking.get_client_ip(request.META)
            if err:
                raise Exception(err)
        tz = pytz.timezone('UTC')
        django.utils.timezone.activate(tz)
        now_utc = datetime.datetime.now(tz)
        now = int(now_utc.strftime('%s'))
        if system_initiated:
            username = '******'
            source_ip = 'System'
        else:
            username = request.user.username
            source_ip, err = networking.get_client_ip(request.META)
            if err:
                raise Exception(err)
        command_list = []
        cmd = [
            'insert into audit(audit_time, username, source_ip, audit_code, audit_str) values (?,?,?,?,?)', (now, username, source_ip, audit_code, audit_str,)]
        command_list.append(cmd)
        ret, err = db.execute_iud(db_path, command_list)
        if err:
            raise Exception(err)

        d, err = mail.load_email_settings()
        if d:
            if d["email_audit"]:
                local_timezone, err = system_date_time.get_current_timezone()
                if err:
                    raise Exception(err)
                if 'timezone_str' not in local_timezone:
                    timezone_str = 'UTC'
                else:
                    timezone_str = local_timezone['timezone_str']
                tz = pytz.timezone(timezone_str)
                now_local = now_utc.astimezone(tz)
                now_local_str = now_local.strftime("%a, %d %b %Y %H:%M:%S")
                mail_mesg = 'Action: %s\n' % audit_str
                mail_mesg += 'Action initiated from: %s\n' % source_ip
                mail_mesg += 'Action performed by: %s\n' % username
                mail_mesg += 'Action performed at: %s\n' % now_local_str
                # print mail_mesg
                hostname, err = networking.get_hostname()
                if err or not hostname:
                    hostname = 'Undetermined hostname'
                ret, err = mail.send_mail(d["server"], d["port"], d["username"], d["pswd"], d["tls"],
                                          d["rcpt_list"], "Audit message from Integralstor " + hostname, mail_mesg)
                if err:
                    raise Exception(err)
    except Exception, e:
        return False, 'Error performing an audit operation : %s' % str(e)
Ejemplo n.º 4
0
def audit(audit_code, audit_str, request, system_initiated=False):
    try:
        db_path, err = config.get_db_path()
        if err:
            raise Exception(err)
        if system_initiated is False:
            ip, err = networking.get_client_ip(request.META)
            if err:
                raise Exception(err)
        tz = pytz.timezone('UTC')
        django.utils.timezone.activate(tz)
        now_utc = datetime.datetime.now(tz)
        now = int(now_utc.strftime('%s'))
        if system_initiated:
            username = '******'
            source_ip = 'System'
        else:
            username = request.user.username
            source_ip, err = networking.get_client_ip(request.META)
            if err:
                raise Exception(err)
        command_list = []
        cmd = [
            'insert into audit(audit_time, username, source_ip, audit_code, audit_str) values (?,?,?,?,?)', (now, username, source_ip, audit_code, audit_str,)]
        command_list.append(cmd)
        ret, err = db.execute_iud(db_path, command_list)
        if err:
            raise Exception(err)

        d, err = mail.load_email_settings()
        if d:
            if d["email_audit"]:
                local_timezone, err = system_date_time.get_current_timezone()
                if err:
                    raise Exception(err)
                if 'timezone_str' not in local_timezone:
                    timezone_str = 'UTC'
                else:
                    timezone_str = local_timezone['timezone_str']
                tz = pytz.timezone(timezone_str)
                now_local = now_utc.astimezone(tz)
                now_local_str = now_local.strftime("%a, %d %b %Y %H:%M:%S")
                mail_mesg = 'Action: %s\n' % audit_str
                mail_mesg += 'Action initiated from: %s\n' % source_ip
                mail_mesg += 'Action performed by: %s\n' % username
                mail_mesg += 'Action performed at: %s\n' % now_local_str
                # print mail_mesg
                hostname, err = networking.get_hostname()
                if err or not hostname:
                    hostname = 'Undetermined hostname'
                ret, err = mail.send_mail(d["server"], d["port"], d["username"], d["pswd"], d["tls"],
                                          d["rcpt_list"], "Audit message from Integralstor " + hostname, mail_mesg)
                if err:
                    raise Exception(err)
    except Exception, e:
        return False, 'Error performing an audit operation : %s' % str(e)
Ejemplo n.º 5
0
def view_email_settings(request):
    return_dict = {}
    try:
        d, err = mail.load_email_settings()
        if err:
            raise Exception(err)
        if not d:
            return_dict["email_not_configured"] = True
        else:
            if d["tls"]:
                d["tls"] = True
            else:
                d["tls"] = False
            if d["email_alerts"]:
                d["email_alerts"] = True
            else:
                d["email_alerts"] = False
            return_dict["email_settings"] = d
        ret, err = django_utils.get_request_parameter_values(
            request, ['ack', 'err', 'sent_mail'])
        if err:
            raise Exception(err)
        if 'ack' in ret and ret['ack'] == 'saved':
            return_dict[
                "ack_message"] = 'Email settings have successfully been updated.'
        if 'err' in ret:
            return_dict["err"] = ret['err']
        if 'sent_mail' in ret:
            return_dict["sent_mail"] = ret['sent_mail']
        return django.shortcuts.render_to_response(
            'view_email_settings.html',
            return_dict,
            context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        return_dict['base_template'] = "system_base.html"
        return_dict["page_title"] = 'View email notification settings'
        return_dict['tab'] = 'email_tab'
        return_dict["error"] = 'Error viewing email notification settings'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            'logged_in_error.html',
            return_dict,
            context_instance=django.template.context.RequestContext(request))
Ejemplo n.º 6
0
def view_email_settings(request):
    return_dict = {}
    try:
        return_dict['base_template'] = "system_base.html"
        return_dict["page_title"] = 'View Email settings'
        return_dict['tab'] = 'email_settings_tab'
        return_dict["error"] = 'Error loading Email settings'
        d, err = mail.load_email_settings()
        if err:
            raise Exception(err)
        if not d:
            return_dict["email_not_configured"] = True
        else:
            if d["tls"]:
                d["tls"] = True
            else:
                d["tls"] = False
            if d["email_alerts"]:
                d["email_alerts"] = True
            else:
                d["email_alerts"] = False
            return_dict["email_settings"] = d
        ack_msg = ''
        if "ack" in request.GET:
            if request.GET["ack"] == "saved":
                ack_message = "Email settings have been saved. "
        if "err" in request.REQUEST:
            ack_message += 'The following errors were reported : %s' % request.REQUEST[
                "err"]
        return django.shortcuts.render_to_response(
            'view_email_settings.html',
            return_dict,
            context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            'logged_in_error.html',
            return_dict,
            context_instance=django.template.context.RequestContext(request))
Ejemplo n.º 7
0
def raise_alert(msg_list):
    """Raise one alert for the passed msg list."""
    try:
        t = int(time.time())
        filename, err = _get_alerts_file_path()
        if err:
            raise Exception(err)
        with open(filename, "a") as f:
            fcntl.flock(f, fcntl.LOCK_EX)
            for msg in msg_list:
                f.write("\n%-13d %s\n" % (t, msg))
            fcntl.flock(f, fcntl.LOCK_UN)
            f.close()
        d, err = mail.load_email_settings()
        if err:
            raise Exception(err)
        if d:
            if d["email_alerts"]:
                ret, err = mail.send_mail(
                    d["server"], d["port"], d["username"], d["pswd"], d["tls"],
                    d["rcpt_list"],
                    "Alerts from IntegralStor " + networking.get_hostname()[0],
                    '\n'.join(msg_list))
                if err:
                    with open(filename, "a") as f:
                        fcntl.flock(f, fcntl.LOCK_EX)
                        f.write("\n%-13d %s\n" %
                                (t, "Error sending email alert %s" % err))
                        fcntl.flock(f, fcntl.LOCK_UN)
                        f.close()
                    raise Exception(err)
    except Exception, e:
        print "Error raising alert : %s" % str(e)
        with open(filename, "a") as f:
            fcntl.flock(f, fcntl.LOCK_EX)
            f.write("\n%-13d %s\n" % (t, "Error raising alert : %s" % str(e)))
            fcntl.flock(f, fcntl.LOCK_UN)
            f.close()
Ejemplo n.º 8
0
def update_email_settings(request):

    try:
        return_dict = {}
        url = "update_email_settings.html"
        if request.method == "GET":
            d, err = mail.load_email_settings()
            if err:
                raise Exception(err)
            if not d:
                form = admin_forms.ConfigureEmailForm()
            else:
                if 'tls' in d and d["tls"]:
                    d["tls"] = True
                else:
                    d["tls"] = False
                if 'email_alerts' in d and d["email_alerts"]:
                    d["email_alerts"] = True
                else:
                    d["email_alerts"] = False
                form = admin_forms.ConfigureEmailForm(
                    initial={
                        'server': d["server"],
                        'port': d["port"],
                        'tls': d["tls"],
                        'username': d["username"],
                        'email_alerts': d["email_alerts"],
                        'email_audit': d['email_audit'],
                        'rcpt_list': d["rcpt_list"],
                        'pswd': ""
                    })
        else:
            form = admin_forms.ConfigureEmailForm(request.POST)
            if form.is_valid():
                cd = form.cleaned_data
                # print "Saving : "
                ret, err = mail.save_email_settings(cd)
                if err:
                    raise Exception(err)

                ret, err = mail.send_mail(
                    cd["server"], cd["port"], cd["username"], cd["pswd"],
                    cd["tls"], cd["rcpt_list"], "Test email from IntegralStor",
                    "This is a test email sent by the IntegralStor system in order to confirm that your email settings are working correctly."
                )
                if err:
                    raise Exception(err)
                if ret:
                    return django.http.HttpResponseRedirect(
                        "/view_email_settings?ack=saved&sent_mail=1")
                else:
                    return django.http.HttpResponseRedirect(
                        "/view_email_settings?ack=saved&err=%s" % err)
        return_dict["form"] = form
        return django.shortcuts.render_to_response(
            url,
            return_dict,
            context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        return_dict['base_template'] = "system_base.html"
        return_dict["page_title"] = 'Change email notification settings'
        return_dict['tab'] = 'email_tab'
        return_dict["error"] = 'Error changing email notification settings'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            'logged_in_error.html',
            return_dict,
            context_instance=django.template.context.RequestContext(request))
Ejemplo n.º 9
0
def configure_email_settings(request):

    return_dict = {}
    try:
        return_dict['base_template'] = "system_base.html"
        return_dict["page_title"] = 'Configure email settings'
        return_dict['tab'] = 'email_settings_tab'
        return_dict["error"] = 'Error configuring email settings'
        if request.method == "GET":
            d, err = mail.load_email_settings()
            if err:
                raise Exception(err)
            if not d:
                form = admin_forms.ConfigureEmailForm()
            else:
                if d["tls"]:
                    d["tls"] = True
                else:
                    d["tls"] = False
                if d["email_alerts"]:
                    d["email_alerts"] = True
                else:
                    d["email_alerts"] = False
                form = admin_forms.ConfigureEmailForm(
                    initial={
                        'email_server': d["server"],
                        'email_server_port': d["port"],
                        'tls': d["tls"],
                        'username': d["username"],
                        'email_alerts': d["email_alerts"],
                        'rcpt_list': d["rcpt_list"],
                        "email_audit": True,
                        "email_quota": True
                    })
        else:
            form = admin_forms.ConfigureEmailForm(request.POST)
            if form.is_valid():
                print 'valid'
                cd = form.cleaned_data
                d = {}
                if "email_alerts" in cd:
                    d["email_alerts"] = cd["email_alerts"]
                else:
                    d["email_alerts"] = False
                d["server"] = cd["email_server"]
                d["port"] = cd["email_server_port"]
                d["username"] = cd["username"]
                d["pswd"] = cd["pswd"]
                d["rcpt_list"] = cd["rcpt_list"]
                if "tls" in cd:
                    d["tls"] = cd["tls"]
                else:
                    d["tls"] = False
                if "email_audit" in cd:
                    d["email_audit"] = cd["email_audit"]
                else:
                    d["email_audit"] = False
                if "email_quota" in cd:
                    d["email_quota"] = cd["email_quota"]
                else:
                    d["email_quota"] = False
                # print "Saving : "
                # print d
                ret, err = mail.save_email_settings(d)
                if err:
                    raise Exception(err)

                ret, err = mail.send_mail(
                    cd["email_server"], cd["email_server_port"],
                    cd["username"], cd["pswd"], cd["tls"], cd["rcpt_list"],
                    "Test email from IntegralStor",
                    "This is a test email sent by the IntegralStor system in order to confirm that your email settings are working correctly."
                )
                print ret, err
                if err:
                    raise Exception(err)
                if ret:
                    return django.http.HttpResponseRedirect(
                        "/view_email_settings?ack=saved")
                else:
                    return django.http.HttpResponseRedirect(
                        "/view_email_settings?ack=saved&err=%s" % err)
            else:
                # print 'invalid form'
                pass
        return_dict["form"] = form
        return django.shortcuts.render_to_response(
            'edit_email_settings.html',
            return_dict,
            context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            'logged_in_error.html',
            return_dict,
            context_instance=django.template.context.RequestContext(request))
Ejemplo n.º 10
0
def configure_email_settings(request):

    return_dict = {}
    try:
        return_dict['base_template'] = "system_base.html"
        return_dict["page_title"] = 'Configure email settings'
        return_dict['tab'] = 'email_settings_tab'
        return_dict["error"] = 'Error configuring email settings'
        if request.method == "GET":
            d, err = mail.load_email_settings()
            if err:
                raise Exception(err)
            if not d:
                form = admin_forms.ConfigureEmailForm()
            else:
                if d["tls"]:
                    d["tls"] = True
                else:
                    d["tls"] = False
                if d["email_alerts"]:
                    d["email_alerts"] = True
                else:
                    d["email_alerts"] = False
                form = admin_forms.ConfigureEmailForm(initial={'email_server': d["server"], 'email_server_port': d["port"], 'tls': d["tls"], 'username': d[
                                                      "username"], 'email_alerts': d["email_alerts"], 'rcpt_list': d["rcpt_list"], "email_audit": True, "email_quota": True})
        else:
            form = admin_forms.ConfigureEmailForm(request.POST)
            if form.is_valid():
                print 'valid'
                cd = form.cleaned_data
                d = {}
                if "email_alerts" in cd:
                    d["email_alerts"] = cd["email_alerts"]
                else:
                    d["email_alerts"] = False
                d["server"] = cd["email_server"]
                d["port"] = cd["email_server_port"]
                d["username"] = cd["username"]
                d["pswd"] = cd["pswd"]
                d["rcpt_list"] = cd["rcpt_list"]
                if "tls" in cd:
                    d["tls"] = cd["tls"]
                else:
                    d["tls"] = False
                if "email_audit" in cd:
                    d["email_audit"] = cd["email_audit"]
                else:
                    d["email_audit"] = False
                if "email_quota" in cd:
                    d["email_quota"] = cd["email_quota"]
                else:
                    d["email_quota"] = False
                # print "Saving : "
                # print d
                ret, err = mail.save_email_settings(d)
                if err:
                    raise Exception(err)

                ret, err = mail.send_mail(cd["email_server"], cd["email_server_port"], cd["username"], cd["pswd"], cd["tls"], cd["rcpt_list"], "Test email from IntegralStor",
                                          "This is a test email sent by the IntegralStor system in order to confirm that your email settings are working correctly.")
                print ret, err
                if err:
                    raise Exception(err)
                if ret:
                    return django.http.HttpResponseRedirect("/view_email_settings?ack=saved")
                else:
                    return django.http.HttpResponseRedirect("/view_email_settings?ack=saved&err=%s" % err)
            else:
                # print 'invalid form'
                pass
        return_dict["form"] = form
        return django.shortcuts.render_to_response('edit_email_settings.html', return_dict, context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response('logged_in_error.html', return_dict, context_instance=django.template.context.RequestContext(request))