1.分布式ELK平台
1.1ELK简介
1.1.1elk是什么?
1.1.2elk能做什么?
2.Elasticsearch
2.1elasticsearch介绍
2.2elasticsearch特点
2.3elasticsearch没有什么?
2.4elasticsearch相关概念
2.5elasticsearch与关系型数据库的对比
2.5elasticsearch架构图
在Elasticsearch使用增删改查时使用http的方法
3.ES集群安装
3.1 问题
本案例要求:
准备1台虚拟机
部署elasticsearch第一个节点
访问9200端口查看是否安装成功
3.2 方案
1)ELK是日志分析平台,不是一款软件,而是一整套解决方案,是三个软件产品的首字母缩写,ELK分别代表:
Elasticsearch:负责日志检索和储存
Logstash:负责日志的收集和分析、处理
Kibana:负责日志的可视化
2) ELK组件在海量日志系统的运维中,可用于解决分布式日志数据集中式查询和管理系统监控等,故障排查,安全信息和事件管理,报表功能.
部署Elasticsearch分布式集群安装,Kibana作为可视化平台,实时总结流量和数据的图表,Logstash用来收集处理日志
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:先准备一台虚拟机
1)更改主机名,配置IP,搭建第三方yum源(之前已经搭建过几次,这里不再赘述)
[root@room9pc01 04]# cd /linux-soft/04/elk
[root@room9pc01 elk]# ls
accounts.json.gz filebeat-1.2.3-x86_64.rpm
bigdesk-master.zip kibana-4.5.2-1.x86_64.rpm
elasticsearch-2.3.4.rpm logs.jsonl.gz
elasticsearch-head-master.zip logstash-2.3.4-1.noarch.rpm
elasticsearch-kopf-master.zip shakespeare.json.gz
[root@se1 ~]# echoes1 > /etc/hostname
[root@se1 ~]# vim /etc/sysconfig/network-scripts
/ifcfg-eth0
#Generated by dracut initrd
DEVICE="eth0"
ONBOOT="yes"
IPV6INIT="no"
IPV4_FAILURE_FATAL="no"
NM_CONTROLLED="no"
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR=192.168.1.51
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
[root@se1 ~]# systemctl restart network
[root@room9pc01 elk]# mkfir /var/ftp/elk 创建elk文件夹
[root@room9pc01 elk]# cp * /var/ftp/elk
[root@room9pc01 elk]# ls /var/ftp/elk
accounts.json.gz filebeat-1.2.3-x86_64.rpm
bigdesk-master.zip kibana-4.5.2-1.x86_64.rpm
elasticsearch-2.3.4.rpm logs.jsonl.gz
elasticsearch-head-master.zip logstash-2.3.4-1.noarch.rpm
elasticsearch-kopf-master.zip shakespeare.json.gz
[root@room9pc01 elk]# createrepo /var/ftp/elk
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 1 pkgs
Spawning worker 2 with 1 pkgs
Spawning worker 3 with 1 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@room9pc01elk]# ls
accounts.json.gz kibana-4.5.2-1.x86_64.rpm
bigdesk-master.zip logs.jsonl.gz
elasticsearch-2.3.4.rpm logstash-2.3.4-1.noarch.rpm
elasticsearch-head-master.zip //repodata//
elasticsearch-kopf-master.zip shakespeare.json.gz
filebeat-1.2.3-x86_64.rpm
[root@elk ~ ]# vim /etc/yum.repos.d/local.repo
[local_repo]
name=CentOS-$releasever - Base
baseurl=“ftp://192.168.1.254/system”
enabled=1
gpgcheck=0
[local_elk]
name=local_elk
baseurl=“ftp://192.168.1.254/elk”
enabled=1
gpgcheck=0
[root@es1 ~]# yum clean all
[root@es1 ~]# yum repolist
repo id repo name status
local_elk local_elk 4
local_repo CentOS-7 - Base 9,911
repolist: 9,915
2)部署elasticsearch第一个节点
[root@se1 ~]# vim /etc/hosts
192.168.1.51 es1
[root@es1 ~]# yum -y install java-1.8.0-openjdk.x86_64
[root@es1 ~]# java -version
openjdk version “1.8.0_161”
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)
[root@es1 ~]# sestatus 查看selinux状态
SELinux status: disabled
[root@es1 ~]# yum -y install elasticsearch
[root@es1 ~]# vim /etc/elasticsearch/elasticsearch.yml 去掉空格
+54 network.host: 0.0.0.0 // 0.0.0.0(监听所有地址)
[root@es1 ~]# systemctl restart elasticsearch
[root@es1 ~]# systemctl enable elasticsearch
[root@es1 ~]# ss -antup | grep 9200
tcp LISTEN 0 50 :::9200 ::? users:((“java”,pid=23114,fd=109))
[root@es1 ~]# ss -antup | grep 9300
tcp LISTEN 0 50 :::9300 ::? users:((“java”,pid=23114,fd=92))
3)访问9200端口查看是否安装成功,
[root@es1 ~]# curl 192.168.1.51:9200
{
"name" : "Azazel",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.3.4",
"build_hash" : "e455fd0c13dceca8dbbdbb1665d068ae55dabe3f",
"build_timestamp" : "2016-06-30T11:24:31Z",
"build_snapshot" : false,
"lucene_version" : "5.5.0"
},
"tagline" : "You Know, for Search"
}
4.ES集群安装配置
4.1 问题
本案例要求:
一共安装5台虚拟机
在所有机器中部署ES
启动服务查看验证集群状态
4.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:安装elasticsearch和java-1.8.0-openjdk,同步配置文件
备注:在步骤一已经安装了一台elasticsearch,这里只需再准备四台即可
1)更改对应的主机名、 ip地址以及搭建yum源(以案例1为例子)
[root@es2 ~]# vim /etc/hosts
#::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.1.51 es1
192.168.1.52 es2
192.168.1.53 es3
192.168.1.54 es4
192.168.1.55 es5
第一种方法:
2)安装elasticsearch四台主机同样操作(以es2为例子,es1已全部修改完毕)
[root@es2 ~]# yum -y install java-1.8.0-openjdk.x86_64
[root@es2 ~]# yum -y install elasticsearch
3)同步配置/etc/elasticsearch/elasticsearch.yml, 修改node.name字段(以es1为例子)
[root@es2 ~]# vim /etc/elasticsearch/elasticsearch.yml
+17 cluster.name:nsd1906 //配置集群名字
+23 node.name: es2 //当前主机名称 ,另外三台修改为对应es3,es4,es5
+54 network.host: 0.0.0.0 // 0.0.0.0(监听所有地址)
+68 discovery.zen.ping.unicast.hosts: [“es1”, “es2”, “es3”]
[root@es2 ~]# systemctl restart elasticsearch
[root@es2 ~]# systemctl enable elasticsearch
[root@es2 ~]# for i in {53…55} ; do scp /etc/elasticsearch/elasticsearch.yml 192.168.1.$i:/etc /elasticsearch/elasticsearch.yml; done
4)访问测试,如图-2所示:
可以访问61-65的任意一台主机, 集群的节点都是5台,若先启动的是es4或es5,这两个会自动成为各自的集群,解决办法,先启动集群里的es1或es2或es3其中的一台,或者把es4和es5重启,es4和es5会自动加进去.如果把所有的集群机器都声明进去,如果其中一台机器故障,所有的集群机器都必须修改配置文件,比较复杂,但是如果只声明一个机器,容易有单点故障
ES 集群验证:返回字段解析:
”status”: ”green“ 集群状态:绿色为正常、黄色表示有问题但不是很严重、红色表示严重故障
”number_of_nodes”: 5, 表示集群中节点的数量
[root@es1 ~]# curl 192.168.1.51:9200/_cluster/health?pretty
验证配置,_cluster/health为es内置字段
{
"cluster_name" : "nsd1906",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 5,
"number_of_data_nodes" : 5,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
b—2)利用ansible-playbook完成其他四台机器的部署(以es6,es7为例,仅用于测试es.yml是否配置正确)
[root@es6 ~]# vim es.yml
---
- hosts: es
tasks:
- name: change yum file
copy:
src: /etc/yum.repos.d/local.repo
dest: /etc/yum.repos.d/
- name: install elasticsearch
yum:
name: java-1.8.0-openjdk,elasticsearch
state: installed
- name: change file
template:
src: /etc/elasticsearch/elasticsearch.yml
dest: /etc/elasticsearch/elasticsearch.yml
notify:
- restart service
handlers:
- name: restart service
service:
name: elasticsearch
state: restarted
[root@es6 .ssh]# ansible-playbook es.yml
PLAY [es] **********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [es7]
TASK [change yum file] *********************************************************
changed: [es7]
TASK [install elasticsearch] ***************************************************
changed: [es7]
TASK [change file] *************************************************************
changed: [es7]
RUNNING HANDLER [restart service] **********************************************
changed: [es7]
PLAY RECAP *********************************************************************
es7 : ok=5 changed=4 unreachable=0 failed=0
[root@es7 ~]# vim /etc/elasticsearch/elasticsearch.yml
+23 node.name: es7 //当前主机名称
[root@es6 ~]# curl 192.168.1.137:9200/_cluster/health?pretty
{
"cluster_name" : "myes",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 2,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
5.ES插件
5.1ES常用插件
5.2 问题
本案例要求:
在其中一台机器上部署插件
使用bigdesk查看集群状态
使用head创建index
使用kopf查看数据
5.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:部署插件
插件装在哪一台机器上,只能在哪台机器上使用(这里安装在es5机器上面)
1)安装插件
[root@room9pc01 ~]# cd /linux-soft/04/elk
[root@room9pc01 elk]# ls
accounts.json.gz filebeat-1.2.3-x86_64.rpm
bigdesk-master.zip kibana-4.5.2-1.x86_64.rpm
elasticsearch-2.3.4.rpm logs.jsonl.gz
elasticsearch-head-master.zip logstash-2.3.4-1.noarch.rpm
elasticsearch-kopf-master.zip shakespeare.json.gz
[root@room9pc01 elk]# scp bigdesk-master.zip elasticsearch-head-master.zip elasticsearch-kopf-master.zip root@192.168.1.55:/usr/share/elasticsearch/bin/
[root@es5 ~]#cd /usr/share/elasticsearch/bin
[root@es5 bin]# ./plugin list
Installed plugins in /usr/share/elasticsearch/plugins:
- No plugin detected
[root@es5 bin]# ./plugin install ftp://192.168.1.254/elk/elasticsearch-head-master.zip
//安装head插件 如果这些文件在本地的话(如/tmp下),必须使用ftp:///tmp/xxx进行下载
[root@es5 bin]# ./plugin install ftp://192.168.1.254/elk/elasticsearch-kopf-master.zip
//安装kopf插件
[root@es5 bin]# [root@se5 bin]# ./plugin install ftp://192.168.1.254/elk/bigdesk-master.zip
//安装bigdesk插件
[root@es5 bin]# ./plugin list 查看安装的插件
Installed plugins in /usr/share/elasticsearch/plugins:
- head
- kopf
- bigdesk
2)访问head插件,如图所示:
[student@room9pc01 ~]$ firefox http://192.168.1.55:9200/_plugin/head
3)访问kopf插件,如图所示:
[student@room9pc01 ~]$ firefox http://192.168.1.55:9200/_plugin/kopf
4)访问bigdesk插件,如图所示:
[student@room9pc01 ~]$ firefox http://192.168.1.55:9200/_plugin/bigdesk
6.扩展插件
6.1RESTful API
6.1.1HTTP协议简介
6.1.2curl命令
-i可以探测网站服务器类型,-A修改客户端访问服务器时客户端的系统类型
[root@es5 ~]# curl http://139.159.158.210/info.php
<pre>
Array
(
[id] => 3114
[REMOTE_ADDR] => 223.255.15.21
[REQUEST_METHOD] => GET //默认为GET请求
[HTTP_USER_AGENT] => wanghailun
[REQUEST_URI] => /info.php
)
[root@es5 ~]# curl -XPOST http://139.159.158.210/info.php
<pre>
Array
(
[id] => 7191
[REMOTE_ADDR] => 223.255.15.21
[REQUEST_METHOD] => POST //改为POST修改
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /info.php
)
[root@es5 ~]# curl -XPOST -A “Windows IE/99.0” http://139.159.158.210/info.php
<pre>
Array
(
[id] => 6956
[REMOTE_ADDR] => 223.255.15.21
[REQUEST_METHOD] => POST
[HTTP_USER_AGENT] => Windows IE/99.0 //将客户端访问网站的系统命令掩盖,修改为Windows IE/99.0
[REQUEST_URI] => /info.php
)
[root@es5 ~]# curl -XPOST -A “吴元吉” http://139.159.158.210/info.php
<pre>
Array
(
[id] => 2593
[REMOTE_ADDR] => 223.255.15.21
[REQUEST_METHOD] => POST
[HTTP_USER_AGENT] => 吴元吉
[REQUEST_URI] => /info.php
)
[root@es5 ~]# curl http://es5:9200/_cat 查看选项
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
[root@es5 ~]# curl http://es5:9200/_cat/master 查看主机信息
1SKekpvBTsOd4YSW2PzbCg 192.168.1.54 192.168.1.54 es4
[root@es5 ~]# curl http://es5:9200/_cat/master?v 查看详细主机信息
id host ip node
1SKekpvBTsOd4YSW2PzbCg 192.168.1.54 192.168.1.54 es4
[root@es5 ~]# curl http://es5:9200/_cat/nodes 查看节点信息
192.168.1.53 192.168.1.53 14 54 0.00 d m es3
192.168.1.52 192.168.1.52 15 54 0.00 d m es2
192.168.1.55 192.168.1.55 13 34 0.00 d m es5
192.168.1.51 192.168.1.51 5 48 0.00 d m es1
192.168.1.54 192.168.1.54 4 33 0.00 d * es4
[root@es5 ~]# curl http://es5:9200/_cat/nodes?v 查看详细节点信息
host ip heap.percent ram.percent load node.role master name
192.168.1.53 192.168.1.53 14 54 0.00 d m es3
192.168.1.52 192.168.1.52 15 54 0.00 d m es2
192.168.1.55 192.168.1.55 13 35 0.00 d m es5
192.168.1.51 192.168.1.51 5 48 0.00 d m es1
192.168.1.54 192.168.1.54 4 33 0.00 d * es4
[root@es5 ~]# curl http://es5:9200/_cat/shards 查看分片信息
new 4 r STARTED 0 159b 192.168.1.55 es5
new 4 p STARTED 0 159b 192.168.1.51 es1
new 2 p STARTED 0 159b 192.168.1.54 es4
new 2 r STARTED 0 159b 192.168.1.53 es3
new 1 p STARTED 0 159b 192.168.1.52 es2
new 1 r STARTED 0 159b 192.168.1.54 es4
new 3 p STARTED 0 159b 192.168.1.53 es3
new 3 r STARTED 0 159b 192.168.1.51 es1
new 0 p STARTED 0 159b 192.168.1.55 es5
new 0 r STARTED 0 159b 192.168.1.52 es2
[root@es5 ~]# curl http://es5:9200/_cat/shards?v 查看详细分片信息
index shard prirep state docs store ip node
new 4 r STARTED 0 159b 192.168.1.55 es5
new 4 p STARTED 0 159b 192.168.1.51 es1
new 2 p STARTED 0 159b 192.168.1.54 es4
new 2 r STARTED 0 159b 192.168.1.53 es3
new 1 p STARTED 0 159b 192.168.1.52 es2
new 1 r STARTED 0 159b 192.168.1.54 es4
new 3 p STARTED 0 159b 192.168.1.53 es3
new 3 r STARTED 0 159b 192.168.1.51 es1
new 0 p STARTED 0 159b 192.168.1.55 es5
new 0 r STARTED 0 159b 192.168.1.52 es2
7.插入,增加,删除查询数据
7.1 问题
本案例要求:
使用curl命令连接使用ES数据库
使用PUT方法增加数据
使用POST修改数据
使用GET查询数据
使用DELETE删除数据
7.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:插入数据
[root@es5 bin]# curl -X PUT “http://192.168.1.55:9200/index” -d ’
{
“settings”:{
“index”:{
“number_of_shards”:5, //分片数
“number_of_replicas”:1 //副本数
}
}
}’
{“acknowledged”:true}
[root@room9pc01 ~]# firefox http://192.168.1.55:9200/_plugin/head
[root@es5 ~]# curl -XPUT http://192.168.1.55:9200/tedu/teacher/1 -d ’
{
“姓名”: “犇犇”,
“年龄”: “28”,
“爱好”: “大锤”
}
’
{"_index":“tedu”,"_type":“teacher”,"_id":“1”,"_version":1,"_shards":{“total”:2,“successful”:2,“failed”:0},“created”:true}
[root@room9pc01 ~]# firefox http://192.168.1.55:9200/_plugin/head
[root@es5 ~]# curl -XPUT http://192.168.1.55:9200/tedu/teacher/2 -d ’
{
“姓名”: “凯子”,
“年龄”: “48”,
“爱好”: “香蕉”
}’
{"_index":“tedu”,"_type":“teacher”,"_id":“2”,"_version":1,"_shards":{“total”:2,“successful”:2,“failed”:0},“created”:true}
[root@room9pc01 ~]# firefox http://192.168.1.55:9200/_plugin/head
步骤二:修改数据
[root@es5 ~]# curl -XPOST http://192.168.1.55:9200/tedu/teacher/1/_update -d ’
{
“doc”: {
“年龄”: “15”
}
}’
{"_index":“tedu”,"_type":“teacher”,"_id":“1”,"_version":2,"_shards":{“total”:2,“successful”:2,“failed”:0}}
[root@room9pc01 ~]# firefox http://192.168.1.55:9200/_plugin/head
步骤三:查询数据
[root@es5 ~]# curl http://192.168.1.55:9200/tedu/teacher/1 查询数据,默认是GET
{"_index":"tedu","_type":"teacher","_id":"1","_version":2,"found":true,"_source":{"姓名":"犇犇","年龄":"15","爱好":"大锤"}}
步骤四:删除数据
[root@es5 ~]# curl -XDELETE http://192.168.1.55:9200/tedu/teacher/1 删除数据
{"found":true,"_index":"tedu","_type":"teacher","_id":"1","_version":3,"_shards":{"total":2,"successful":2,"failed":0}}
[root@room9pc01 ~]# firefox http://192.168.1.55:9200/_plugin/head
步骤五:删除索引
[root@es5 ~]# curl -XDELETE http://192.168.1.55:9200/tedu 删除单个索引
{"acknowledged":true}
[root@room9pc01 ~]# firefox http://192.168.1.55:9200/_plugin/head
[root@es5 bin]# curl -X DELETE http://192.168.1.65:9200/* 删除所有索引
{"acknowledged":true}
[root@room9pc01 ~]# firefox http://192.168.1.55:9200/_plugin/head
7.批量导入数据
7.1 问题
本案例要求批量导入数据:
使用 curl 命名为集群批量导入数据,并查看
7.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:导入数据
使用POST方式批量导入数据,数据格式为json,url 编码使用data-binary导入含有index配置的json文件
[root@room9pc01 ~]# cd /linux-soft/04/elk
[root@room9pc01 elk]# ls
accounts.json.gz filebeat-1.2.3-x86_64.rpm
bigdesk-master.zip kibana-4.5.2-1.x86_64.rpm
elasticsearch-2.3.4.rpm logs.jsonl.gz
elasticsearch-head-master.zip logstash-2.3.4-1.noarch.rpm
elasticsearch-kopf-master.zip shakespeare.json.gz
[root@room9pc01 elk]# scp accounts.json.gz logs.jsonl.gz shakespeare.json.gz root@192.168.1.55:/root
accounts.json.gz 100% 56KB 2.1MB/s 00:00
logs.jsonl.gz 100% 8502KB 48.2MB/s 00:00
shakespeare.json.gz 100% 3513KB 36.9MB/s 00:00
[root@es5 ~]# gzip -d logs.jsonl.gz
[root@es5 ~]# curl -X POST “http://192.168.1.51:9200/_bulk” --data-binary @logs.jsonl
[root@es5 ~]# gzip -d accounts.json.gz
[root@es5 ~]# curl -X POST “http://192.168.1.51:9200/_bulk” --data-binary @laccounts.json
[root@es5 ~]# gzip -d shakespeare.json.gz
[root@es5 ~]# curl -X POST “http://192.168.1.51:9200/_bulk” --data-binary @shakespeare.json
[root@room9pc01 ~]# firefox http://192.168.1.55:9200/_plugin/head
拓展:httpd服务的日志格式 金步国作品集
[root@es5 ~]#yum -y install httpd
[root@es5 ~]# echo 123 > /var/www/html/index.html
[root@es5 ~]# systemctl start httpd
[root@es5 ~]# curl 192.168.1.55
123
[root@es5 ~]# vim /etc/httpd/conf/httpd.conf
LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” commbined
日志格式,%h代表的是远程主机;%I代表的是远端登录名((由identd而来,如果支持的话),除非IdentityCheck设为"On",否则将得到一个"-");%u代表的是远程用户名(根据验证信息而来,如果返回status(%s)为401,可能是假的);%t代表的是时间,用普通日志时间格式(标准英语格式);整个日志记录中最有用的信息,它告诉我们服务器收到的是一个什么样的请求,该项信息的典型格式是"METHOD RESOURCE PROTOCOL",即"方法 资源 协议",RESOURCE是指浏览者向服务器请求的文档,或URL,METHOD是GET,其他经常可能出现的METHOD还有POST和HEAD,此外还有不少可能出现的合法METHOD,但主要就是这三种,PROTOCOL通常是HTTP,后面再加上版本号;状态代码200,它表示服务器已经成功地响应浏览器的 请求,一切正常;客户端的总字节数,它告诉我们传输是否被打断(即,该数值是否和文件的大小相同);客户在提出请求时所在的目录或URL;客户端的详细信息.
[root@es5 ~]# cd /var/log/httpd
[root@es5 httpd]# ls
access_log error_log
[root@es5 httpd]# cat access_log
192.168.1.55 - - [15/Oct/2019:19:06:05 +0800]
%h远程主机 %I远端登录名 %u远程用户名 %t时间,+0800代表东八区
" GET / HTTP/1.1" 200 4
请求 资源 协议 状态代码200 发送给客户端的总字节数
“-” “curl/7.29.0”
客户在提出请求时所在的目录或URL 客户端的详细信息
格式字符串 | 描述 |
---|---|
%% | 百分号(Apache2.0.44或更高的版本) |
%a | 远端IP地址 |
%A | 本机IP地址 |
%B | 除HTTP头以外传送的字节数 |
%b | 以CLF格式显示的除HTTP头以外传送的字节数,也就是当没有字节传送时显示’-'而不是0。 |
%{Foobar}C | 在请求中传送给服务端的cookieFoobar的内容。 |
%D | 服务器处理本请求所用时间,以微为单位。 |
%{FOOBAR}e | 环境变量FOOBAR的值 |
%f | 文件名 |
%h | 远端主机 |
%H | 请求使用的协议 |
%{Foobar}i | 发送到服务器的请求头Foobar:的内容。 |
%l | 远端登录名(由identd而来,如果支持的话),除非IdentityCheck设为"On",否则将得到一个"-"。 |
%m | 请求的方法 |
%{Foobar}n | 来自另一个模块的注解Foobar的内容。 |
%{Foobar}o | 应答头Foobar:的内容。 |
%p | 服务器服务于该请求的标准端口。 |
%P | 为本请求提供服务的子进程的PID。 |
%{format}P | 服务于该请求的PID或TID(线程ID),format的取值范围为:pid和tid(2.0.46及以后版本)以及hextid(需要APR1.2.0及以上版本) |
%q | 查询字符串(若存在则由一个"?"引导,否则返回空串) |
%r | 请求的第一行 |
%s | 状态。对于内部重定向的请求,这个状态指的是原始请求的状态,—%>s则指的是最后请求的状态。 |
%t | 时间,用普通日志时间格式(标准英语格式) |
%{format}t | 时间,用strftime(3)指定的格式表示的时间。(默认情况下按本地化格式) |
%T | 处理完请求所花时间,以秒为单位。 |
%u | 远程用户名(根据验证信息而来;如果返回status(%s)为401,可能是假的) |
%U | 请求的URL路径,不包含查询字符串。 |
%v | 对该请求提供服务的标准ServerName。 |
%V | 根据UseCanonicalName指令设定的服务器名称。 |
%X | 请求完成时的连接状态: |
X= | 连接在应答完成前中断。 |
+= | 应答传送完后继续保持连接。 |
-= | 应答传送完后关闭连接(在1.3以后的版本中,这个指令是%c,但这样就和过去的SSL语法:%{var}c冲突了) |
%I | 接收的字节数,包括请求头的数据,并且不能为零。要使用这个指令你必须启用mod_logio模块。 |
%O | 发送的字节数,包括请求头的数据,并且不能为零。要使用这个指令你必须启用mod_logio模块。 |
日志的缺省格式有如下几种
LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”" combined
LogFormat “%h %l %u %t “%r” %>s %b” common #common为日志格式名称
LogFormat “%{Referer}i -> %U” referer
LogFormat “%{User-agent}i” agent
CustomLog logs/access_log common