纠正一下之前在<关于《rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>》的反思>中说到的PHP+MySQL太慢,这里只是说我技术不好,没有技术可以修改这个开源的php日志程序罢了,当然,在做这个的时候,也是菜鸟一个,只是想自己尝试一下。高手可以直接跳过.....

  首先,写在前面,因为上班空闲时间不多,只有忙里偷闲或自己回家的时间弄下,所以这个前后的时间就比较久了。

  之前在上篇写到,是准备使用apache+django来搭建的,但是真的,我折腾了几次apache+wsgi的,我始终没有成功,无耐,我就没有再使用这个了。最终改为使用nginx+django来进行环境的搭建,这个搭建的相关的教程,如有意向的同学可以看这个.<nginx+uwsgi<django web环境的搭建>,个人感觉使用nginx+uwsgi,搭建的速度更快,而且几乎不用考虑什么版本的问题。真心为自己之前准备使用apache+wsgi而心痛自己。但是不得不说,有之前的经历所以看到这个nginx+uwsgi的思路,也一下子就清楚需要怎么操作了。

  然后其它的就是一些代码相关的了,这里也当是记录下自己的搭建的步骤吧!大晚上的写得详细些吧!

首先是settings.py的文件:<导入log,还有最底下面的静态路径的设置,这里稍微注意一下>

 """
Django settings for searchlog project. Generated by 'django-admin startproject' using Django 1.8.7. For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/ For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
""" # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-qsjwm5iy%(yu+rq*u$3t4o+q(-sxn#o=0qt=mpb)8efto%lxd' # SECURITY WARNING: don't run with debug turned on in production!
import socket
if socket.gethostname() == 'samcao-Lenovo-IdeaPad-Y470':
DEBUG = TEMPLATE_DEBUG = True
else:
DEBUG = TEMPLATE_DEBUG = False #DEBUG = TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['*'] # Application definition INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'log',
) MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
) ROOT_URLCONF = 'searchlog.urls' TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(__file__),'templates').replace('\\','/'),],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
] WSGI_APPLICATION = 'searchlog.wsgi.application' # Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), }
} # Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
HERE = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(HERE,'static')
MEDIA_URL = ''
ADMIN_MEDIA_PREFIX = '/static/admin/'
#STATIC_ROOT = '/home/samcao/caodjango/searchlog/searchlog/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)

settings.py

urls.py<就几个url,没有什么特殊的>,好吧,我不得不说,那个The Django Book 我只看了下前面的7章,而且还不认真.<http://djangobook.py3k.cn/2.0>

from django.conf.urls import include, url
from django.contrib import admin
from log import views
from django.conf import settings urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$',views.login),
url(r'^login/$',views.login),
url(r'^loginsuccess/$',views.loginsuccess),
url(r'^logout/$',views.logout),
]

urls.py

log/views.py--------现在目前还没有加上一些判断之类的.比较菜.只是简单的.

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponse,Http404
from django import forms
from django.contrib import auth
from django.http import HttpResponseRedirect
import datetime
from django.contrib.admin import widgets
import subprocess
import os.path
import fupy
#From Post
#from django.template import RequestContext class loginForm(forms.Form):
username = forms.CharField(max_length=30)
password = forms.CharField(widget=forms.PasswordInput) class searchForm(forms.Form):
searchform = forms.CharField(label="Please Enter Search Content",min_length=1)
day = forms.DateTimeField(label='Please Enter Log Date',widget=widgets.AdminDateWidget()) def login(request):
if request.user.is_authenticated():
return HttpResponseRedirect("/loginsuccess/")
error = False
if request.method == 'POST':
form = loginForm(request.POST)
if form.is_valid():
username = request.POST.get('username','')
password = request.POST.get('password','')
user = auth.authenticate(username=username,password=password)
if user is not None and user.is_active:
auth.login(request,user)
return HttpResponseRedirect("/loginsuccess/")
else:
error=True
#return HttpResponse("not user")
else:
form = loginForm()
return render_to_response('login.html',{'form':form,'error':error}) def loginsuccess(request):
if request.user.is_authenticated():
if request.method == 'POST':
searchform = searchForm(request.POST)
if searchform.is_valid():
searchtext = searchform.cleaned_data['searchform']
today = searchform.cleaned_data['day']
today_day = str(today.day).zfill(2)
#print today_day
#print searchtext
if '|' in searchtext:
searchtext_error = True
return render_to_response('search.html',{'form':searchform,'searchtext_error':searchtext_error,'uname':request.user})
logpath = os.path.join('/home/var/log/',str(today.year),str(today.month),today_day)
if not os.path.exists(logpath):
file_error = True
return render_to_response('search.html',{'form':searchform,'file_error':file_error,'uname':request.user})
logpath = os.path.join(logpath,'*')
cmd = "sudo grep '%s' %s"%(searchtext,logpath)
print cmd
showlog = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
text = showlog.communicate()
textshow = text[0].split('\n')
textshow2 = fupy.returnShowLog(textshow)
return render_to_response('search.html',{'showlog':textshow2,'form':searchform,'uname':request.user})
else:
searchform = searchForm(
initial={'day':datetime.datetime.now().strftime("%Y-%m-%d")}
)
return render_to_response('search.html',{'form':searchform,'uname':request.user})
else:
return HttpResponseRedirect("/login/") def logout(request):
auth.logout(request)
return render_to_response("logout.html")

log/views.py

log/templates/base.html-----html模板文件,这里主要是导入bootstrap文件.其它的就没有什么了.

 {% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
{% block title %}<title>Searchlog</title>{% endblock %}
<!-- <script src="{% static 'bootstrap/js/jquery.ui-1.9.0.min.js' %}"></script> -->
<script src="{% static 'bootstrap/js/jquery.min.js' %}"></script>
<!-- 引入 Bootstrap -->
<link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
<script src="{% static 'bootstrap/js/html5shiv.js' %}"></script>
<script src="{% static 'bootstrap/js/respond.min.js' %}"></script>
<script type="text/javascript" src="{% static 'bootstrap/js/bootstrap.js' %}"></script>
<style type="text/css" src="{% static 'bootstrap/css/bootstrap.css' %}"></style>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>

log/templates/base.html

log/templates/login.html-------登陆窗口页面

 {% extends "base.html" %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
{% if error %}
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<strong>提示:</strong>您的账号或者密码有误,请重新进行登陆,谢谢!
</div>
{% endif %}
<div class="jumbotron">
<form class="form-signin" role="form" method="POST" action="">{% csrf_token %}
<h2 class="form-signin-heading">Please sign in</h2>
<input class="form-control" placeholder="{{ form.username.name }}" required="" autofocus="" type="{{ form.username.name }}" name="{{ form.username.name }}">
<input class="form-control" placeholder="Password" required="" type="password" name="{{ form.password.name }}">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
</div>
</div>
</div>
{% endblock %}

log/templates/login.html

log/templates/search.html----查询窗口页面

 {% extends "base.html" %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-12 col-md-offset-0">
<h3>Hello {{ uname.username }}</h3>
<form method="POST" action="">
{% if form.searchform.errors %}
<div class="alert alert-success" role="alert">
<strong>警告!</strong> 请输入需要搜索的日志内容!<br>
</div>
{% endif %}
{% if form.day.errors %}
<div class="alert alert-success" role="alert">
<strong>警告!</strong> 请输入搜索日志时间!
</div>
{% endif %}
{% if searchtext_error %}
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<strong>Warning!</strong> 请搜索正确的内容!不要在搜索内容中带有“|”符号,谢谢!
</div>
<p style="color: red;"></p>
{% endif %}
{% if file_error %}
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<strong>Warning!</strong> 您搜索的日期没有日志,请输入正确的日期,谢谢!格式如:2016-10-10
</div>
{% endif %}
<input class="form-control" placeholder="{{ form.searchform.label }}" type="text" name="{{ form.searchform.name }}" style="width:800px;">
<input class="form-control" type="text" name="{{ form.day.name }}" size="" value="{{ form.day.value }}" style="width:130px;">
<!-- {{form.as_p}}--> <!-- <input type="submit" value="searchlog"> -->
<button type="submit" class="btn btn-primary">Search</button>
<a href="/logout/">注销登陆</a><br/>
<hr>
<table class="table table-hover">
<thead>
<th>序列</th><th>日志内容</th>
</thead>
{% for logline in showlog %}
<tr>
<td>{{ forloop.counter }}</td><td>{{ logline }}</td>
</tr>
{% endfor %}
</table>
</form>
</div> </div>
</div>
{% endblock %}

log/templates/search.html

恩,好吧,大概就是这样一个页面的东西了,没有什么其它的东西了!回想起来还是非常简单的,最麿人的事情是在apache+wsgi的时候,好了!那都是后话了,这样试了下,每天大概有几百M的日志,这样查没有什么问题,响应速度还是不错的。好吧,是好垃圾个,暂时时间与能力有限,先这样,后面再慢慢的进行修改优化下!!!

最新修改,为了方便日志的查看,在搜索的时候会将搜索的内容进行高亮显示,如上图。这个底层返回纠结了下子,因为要可以有星号。

django+nginx+xshell简易日志查询,接上<关于《rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>》的反思>的更多相关文章

  1. 关于《rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>》的反思

    关于<rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>>的反思--链接--http://www.cnblogs.com/drgcaosheng/p/ ...

  2. rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>

    大概思路如下: 使用Linux自带的rsyslog服务来做底层,然后再使用mysql与rsyslog的模板来存储文件,并且以web来进行显示出来.<模板的存储以日期的树形结构来存储,并且以服务器 ...

  3. CentOS 6.5下的lamp环境rsyslog+MySQL+loganalyzer实现日志集中分析管理

    前言 rsyslog系统日志,在CentOS5上叫syslog,而在CentOS6上叫rsyslog,是增强版的syslog,CentOS5上的配置文件在/etc/syslog.conf下,而Cent ...

  4. rsyslog+mariadb+loganalyzer实现日志服务器搭建

    rsyslog+mariadb+loganalyzer实现日志服务器搭建 一.概述 Linux的日志记录了用户在系统上一切操作,包括系统自身运作产生的日志,这些日志是应使用者了解服务器的情况最好的资料 ...

  5. Linux 之 rsyslog+mysql+LogAnalyzer 日志收集系统

     作者:邓聪聪 LogAnalyzer 是一个 syslog 和其他网络事件数据的 Web 前端工具,提供简单易用的日志浏览.搜索和基本分析以及图表显示 由于公司部分项目需求使用日志记录系统,随笔记录 ...

  6. 2020年,手把手教你如何在CentOS7上一步一步搭建LDAP服务器的最新教程

    同步滚动:关 什么是LDAP 什么是LDAP? 要想知道一个概念,最简单的办法就是wikipedia,当然也可以百科. LDAP全称是轻型目录访问协议(Lightweight Directory Ac ...

  7. 在阿里云主机上基于CentOS用vsftpd搭建FTP服务器

    最近需要在一台阿里云的云服务器上搭建FTP服务器,在这篇博文中分享一下我们根据实际需求进行的一些配置. ftp软件用的是vsftpd. vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...

  8. 菜鸟宝典之Windows Server 2012 R2上PHP、MySQL环境搭建

    原文来自:https://www.jb51.net/article/59280.htm 上车准备一.准备工具服务器操作系统:Windows Server 2012PHP版本:5.6.9(根据自己需要) ...

  9. php-fpm 慢日志查询

    虽然可以通过 nginx 的 accesslog 日志查询到用户访问接口或网页消耗的时间,但是不能清晰的追踪到哪个文件或函数慢, 可以通过 php-fpm 慢日志查询检测 php 脚本运行状态,哪些 ...

随机推荐

  1. Yaf框架下类的自动加载

    前面两篇博客分别讲述了PHP自带的类加载和composer中类的自动加载,其实Yaf框架也实现了基于PSR0和PSR4的类的自动加载.根据我对Yaf下类的自动加载方式的理解写下这篇博客.由于接触Yaf ...

  2. 浅述python中argsort()函数的用法

    由于想使用python用训练好的caffemodel来对很多图片进行批处理分类,学习过程中,碰到了argsort函数,因此去查了相关文献,也自己在python环境下进行了测试,大概了解了其相关的用处, ...

  3. mybatis支持属性使用驼峰的命名

    数据库字段,我们一般都用下划线分隔 但是Model中的属性,一般用驼峰命名 如果需要自动映射,则配置mybatis-config.xml文件 <settings> <setting ...

  4. redis 主从同步

    修改redis.conf配置文件 vi redis.conf 在编辑模式下 输入  /slaveof 来搜索 将slaveof启用 即 将#删除 依次配置所有 slave 并将进程 kill 掉 重启 ...

  5. MSSQL日志传送出现“LSN 太晚,无法应用到数据库”

    一个月之前配置了日志传送的数据库,在今天早上收到作业警报:"LSRestore_ServerName_Databasename"运行失败,到历史记录中查看,错误信息如下 消息 20 ...

  6. 第二次C语言作业

    实验一:判断成绩等级. 给定一百分制成绩,要求输出成绩的等级.90以上为A,80-89为B,70-79为C,60-69为D,60分以下为E,输入大于100或小于0时输出"输入数据错误&quo ...

  7. JavaScript编程总结

    1.   JS加载放在底部 2.   JS和CSS合并,一个页面加载的JS和CSS越少越好 3.   尽量使用变量,页非全局变量. 4.   脚本和DOM交互越少越好,尽量批量修改. 5.   批量修 ...

  8. Flex 中画图工具(drawTool)失效

    做项目的时候画图工具突然失效,解决了半天都不行,最后将画图结束的函数map_drawEndHandler写在方法里面的时候,运行却能够画图了,不知道是什么原理,比较头疼,左思右想,都感觉有点怪怪的,虽 ...

  9. js中原型继承的三种方式

  10. Count(*)或者Count(1)或者Count([列]) 区别

    在SQL 中Count(*)或者Count(1)或者Count([列])或许是最常用的聚合函数.很多人其实对这三者之间是区分不清的.本文会阐述这三者的作用,关系以及背后的原理. 往常我经常会看到一些所 ...