Ejemplo n.º 1
0
def saveLogFile(z=False):
 if z:
  with tools.init_session() as session:
    try:
      log_date = schema.LogFileDetail(log_file_date=getFileOneDate())
      session.add(log_date)
    except Exception as e:
      message = "General Error: %s" % str(e)
      print message
 else:
   rows =list()
   with tools.init_session() as session:
     rows = session.query(schema.LogFileDetail).all()
     rows[0].log_file_date=getFileOneDate()
Ejemplo n.º 2
0
def setLogPosition(p,z=False):
 #print '!!!p=',p,' z=',z
 if z:
  with tools.init_session() as session:
    try:
      last_p = schema.LogFileDetail(last_position=p)
      session.add(last_p)
    except Exception as e:
      message = "General Error: %s" % str(e)
      print message
 else:
  rows=list()
  with tools.init_session() as session:
    rows = session.query(schema.LogFileDetail).all()
    rows[0].last_position=p
Ejemplo n.º 3
0
 def local_auth(self):
  with tools.init_session() as session:
   rows= session.query(schema.User).filter_by(name=self.name).filter(schema.User.paw == hashlib.sha1(self.passwd).hexdigest()).all()
   if rows == []:
    return False
   else:
    return True
Ejemplo n.º 4
0
def get_statistic(start_time=None, end_time=None):
    statistics = list()
    stat_data = dict()
    log_type = 'F5'
    global data

    if start_time is None or end_time is None:
        today = True
    else:
        start_time = datetime.strptime(start_time, "%m/%d/%Y")
        end_time = datetime.strptime(end_time, "%m/%d/%Y")
        today = False

        if end_time < start_time:
            today = True

    with tools.init_session() as session:
        if today:
            all_rows = session.query(schema.LogData)\
                .filter_by(log_type=log_type)\
                .filter(
                    schema.LogData.date_time >= date.today(),schema.LogData.detail.contains('PPP tunnel'))
        else:
            all_rows = session.query(schema.LogData)\
                .filter_by(log_type=log_type)\
                .filter(schema.LogData.date_time.between(
                    start_time, end_time),schema.LogData.detail.contains('PPP tunnel'))

        for row in all_rows:
            if row.f5_session_id not in stat_data:
                stat_data[row.f5_session_id] = get_default_f5_statistic()
                stat_data[row.f5_session_id]['sessionId'] = row.f5_session_id

            if row.f5_user is not None:
                stat_data[row.f5_session_id]['user'] = row.f5_user

            if row.f5_user_action is not None:
                if row.f5_user_action == 'User login':
                    stat_data[row.f5_session_id]['loginTime'] = str(
                        row.date_time)
                elif row.f5_user_action == 'User logout':
                    stat_data[row.f5_session_id]['logoutTime'] = str(
                        row.date_time)

            if row.f5_user_mac is not None:
                stat_data[row.f5_session_id]['loginMac'] = row.f5_user_mac

            if row.f5_login_ip is not None:
                stat_data[row.f5_session_id]['loginIp'] = row.f5_login_ip

            if row.f5_client_ip is not None:
                stat_data[row.f5_session_id]['clientIp'] = row.f5_client_ip

        for session_id, value in stat_data.items():
            statistics.append(value)

    index = uuid.uuid1().hex
    data[index] = json.dumps(statistics)

    return index
Ejemplo n.º 5
0
def get_users():
    user = request.cookies.get('USER', default=None, type=str)

    with tools.init_session() as session:
        if request.method == "POST":
            form = UserCreateForm(request.form)
            name = form.user_id.data
            paw = form.user_paw.data
            creator = form.creator.data

            rows = list()
            rows = session.query(schema.User).filter_by(name=name).all()
            if rows == []:
                try:
                    user_data = schema.User(name=name,
                                            paw=hashlib.sha1(paw).hexdigest(),
                                            creator=creator)
                    session.add(user_data)

                except Exception as e:
                    message = "General Error: %s" % str(e)
                    print message
            else:
                rows[0].paw = hashlib.sha1(paw).hexdigest()

    index = get_user()
    data = {'data_id': index}

    return render_template('users.html', user=user, index=data)
Ejemplo n.º 6
0
def getLogPosition():
 rows =list()
 with tools.init_session() as session:
   rows = session.query(schema.LogFileDetail).all()
   if rows != []:
     if rows[0].last_position != None:
       return rows[0].last_position
     else:
       setLogPosition(p=0)
   else:
     setLogPosition(p=0,z=True)
   return 0
Ejemplo n.º 7
0
def checkLogFile():
 rows =list()
 with tools.init_session() as session:
  rows = session.query(schema.LogFileDetail).all()
  if rows == []:
    saveLogFile(True)
    return False
  else:
    if rows[0].log_file_date == getFileOneDate():
      return False
    else:
      return True
Ejemplo n.º 8
0
def del_users():
    user = request.cookies.get('USER', default=None, type=str)
    form = UserCreateForm(request.form)
    name = form.user_id.data

    if name != 'admin':
        with tools.init_session() as session:
            session.query(schema.User).filter_by(name=name).delete()

    index = get_user()
    data = {'data_id': index}

    return render_template('users.html', user=user, index=data)
Ejemplo n.º 9
0
def get_user():
    users = list()
    global data

    with tools.init_session() as session:
        all_rows = session.query(schema.User)

        for row in all_rows:
            users.append(row.get_dict())

    index = uuid.uuid1().hex
    data[index] = json.dumps(users)

    return index
Ejemplo n.º 10
0
    def save_to_database(self):
        LOG.debug("Save log data to database.")
        with tools.init_session() as session:
            LOG.debug("Starting save F5 log data.")
            for log_data in self.all_f5:
                log_data = schema.LogData(
                    log_level=log_data.get('logLevel'),
                    log_type='F5',
                    f5_session_id=log_data.get('sessionId'),
                    f5_login_ip=log_data.get('loginIp'),
                    f5_client_ip=log_data.get('clientIp'),
                    device=log_data.get('device'),
                    f5_user=log_data.get('user'),
                    f5_user_mac=log_data.get('loginMac'),
                    f5_user_action=log_data.get('clientAction'),
                    detail=log_data.get('detail'),
                    date_time=log_data.get('dateTime'))
               
                if self.checkTime(log_data):
                 session.add(log_data)

            LOG.debug("Starting save FTP log data.")
            for log_data in self.all_ftp:
                log_data = schema.LogData(
                    log_type='FTP',
                    ftp_user_computer=log_data.get('computer'),
                    ftp_user_ip=log_data.get('userIp'),
                    ftp_user_access_file=log_data.get('file'),
                    device=log_data.get('device'),
                    ftp_user=log_data.get('ftpUser'),
                    f5_user=log_data.get('user'),
                    ftp_user_action=log_data.get('action'),
                    detail=log_data.get('detail'),
                    date_time=log_data.get('dateTime'))

                if self.checkTime(log_data):
                 session.add(log_data)

        self.all_f5 = list()
        self.all_ftp = list()
Ejemplo n.º 11
0
def get_log(start_time=None, end_time=None, log_type='F5'):
    logs = list()
    global data

    if start_time is None or end_time is None:
        today = True
    else:
        start_time = datetime.strptime(start_time, "%m/%d/%Y")
        end_time = datetime.strptime(end_time, "%m/%d/%Y")
        today = False

        if end_time < start_time:
            today = True

    with tools.init_session() as session:
        if today:
            print 'Get today data.'
            all_rows = session.query(
                schema.LogData).filter_by(log_type=log_type).filter(
                    schema.LogData.date_time >= date.today(),
                    schema.LogData.detail.contains('PPP tunnel'))

        else:
            print 'Get time range data.'
            all_rows = session.query(schema.LogData)\
                .filter_by(log_type=log_type)\
                .filter(schema.LogData.date_time.between(
                    start_time, end_time),schema.LogData.detail.contains('PPP tunnel'))

        for row in all_rows:
            logs.append(row.get_dict())

    index = uuid.uuid1().hex
    data[index] = json.dumps(logs)

    return index