Ejemplo n.º 1
0
def view_hostname(request):
    return_dict = {}
    try:
        template = 'logged_in_error.html'
        hostname, err = networking.get_hostname()
        if err:
            raise Exception(err)
        domain_name, err = networking.get_domain_name()
        if err:
            raise Exception(err)

        if not "error" in return_dict:
            if "ack" in request.GET:
                if request.GET["ack"] == "saved":
                    return_dict[
                        'ack_message'] = "Hostname information successfully updated"
            return_dict['domain_name'] = domain_name
            return_dict['hostname'] = hostname
            template = "view_hostname.html"
        return django.shortcuts.render_to_response(
            template,
            return_dict,
            context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        return_dict['base_template'] = "networking_base.html"
        return_dict["page_title"] = 'View system hostname'
        return_dict['tab'] = 'view_hostname_tab'
        return_dict["error"] = 'Error loading system hostname'
        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.º 2
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.º 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 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.º 6
0
def update_hostname(request):
    return_dict = {}
    try:

        hostname = socket.gethostname()
        if request.method == "GET":
            hostname, err = networking.get_hostname()
            if err:
                raise Exception(err)
            domain_name, err = networking.get_domain_name()
            if err:
                raise Exception(err)

            initial = {}
            initial['hostname'] = hostname
            initial['domain_name'] = domain_name

            form = networking_forms.EditHostnameForm(initial=initial)
            return_dict['form'] = form
            return django.shortcuts.render_to_response(
                "update_hostname.html",
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
        else:
            form = networking_forms.EditHostnameForm(request.POST)
            return_dict['form'] = form
            if not form.is_valid():
                return django.shortcuts.render_to_response(
                    "update_hostname.html",
                    return_dict,
                    context_instance=django.template.context.RequestContext(
                        request))
            cd = form.cleaned_data
            result_str = ""
            domain_name = None
            if 'domain_name' in cd:
                domain_name = cd['domain_name']
            result, err = networking.update_hostname(cd['hostname'],
                                                     domain_name)
            if not result:
                if err:
                    raise Exception(err)
                else:
                    raise Exception('Error setting hostname')
            result, err = networking.update_domain_name(domain_name)
            if not result:
                if err:
                    raise Exception(err)
                else:
                    raise Exception('Error setting domain name')
            python_scripts_path, err = config.get_python_scripts_path()
            if err:
                raise Exception(err)
            common_python_scripts_path, err = config.get_common_python_scripts_path(
            )
            if err:
                raise Exception(err)
            ss_path, err = config.get_system_status_path()
            if err:
                raise Exception(err)

            ret, err = command.get_command_output(
                "python %s/generate_manifest.py %s" %
                (common_python_scripts_path, ss_path))
            if err:
                raise Exception(err)

            ret, err = command.get_command_output(
                "python %s/generate_status.py %s" %
                (common_python_scripts_path, ss_path))

            audit_str = "Hostname set to %s." % cd['hostname']
            if 'domain_name' in cd:
                audit_str += 'Domain name set to %s' % cd['domain_name']
            ret, err = audit.audit("edit_hostname", audit_str, request)
            if err:
                raise Exception(err)

            return django.http.HttpResponseRedirect(
                '/view_hostname?result=saved')
    except Exception, e:
        return_dict['base_template'] = "networking_base.html"
        return_dict["page_title"] = 'Modify system hostname'
        return_dict['tab'] = 'view_hostname_tab'
        return_dict["error"] = 'Error modifying system hostname'
        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))