您的位置:首页 > 运维架构 > Linux

LINUX常用的日志分析命令

2015-10-12 17:36 1036 查看
对于已经在线的系统来说,常常会遇到各种恶意攻击行为,其中比较常见的便是HTTP flood,也称之为CC攻击。如何快速的定位到攻击,并迅速响应,便成为开发运维人员必备的技能。定位问题最快的方法就是登陆到相应的应用,查看相应的攻击来源,及时进行处理。有时候一些简单的linux命令就可以帮助我们快速分析日志,及时响应。

本文就是总结了自己在工作中常用到的日志分析命令,希望对大家有所帮助。

首先,我们需要生成一些测试命令的访问日志。通过下面的脚本可以生成10000条访问日志

<?php
/**
* 模拟生成访问日志
*
* log_format '[$time_local] $remote_addr $request_time_usec $request_method '
'$host$request_uri $http_referer $status $body_bytes_sent';
*
* @author risingsun <haizhuo@126.com>
* @version 0.0.1
* @Date 2015-10-12
*/
function accessLog() {
$time = array(  '[12/Oct/2015:00:00:00 +0800]',
'[12/Oct/2015:00:00:01 +0800]',
'[12/Oct/2015:00:00:02 +0800]',
'[12/Oct/2015:00:00:03 +0800]',
'[12/Oct/2015:00:00:04 +0800]',
'[12/Oct/2015:00:00:05 +0800]',
'[12/Oct/2015:00:00:06 +0800]',
'[12/Oct/2015:00:00:07 +0800]',
'[12/Oct/2015:00:00:08 +0800]',
'[12/Oct/2015:00:00:09 +0800]',
'[12/Oct/2015:00:00:10 +0800]',
);
$remote_add = array();
for ($i=0; $i < 10; $i++) {
$remote_add[] = rand_ip();
}
$rt_time = array('21', '436', '67', '599', '12', '740', '260', '124', '23', '312', '44');
$method  = array('GET', 'POST');
$url     = array(
'www.xxxx.com/index.html',
'www.xxxx.com/list.html',
'www.xxxx.com/detail.html',
'www.xxxx.com/userinfo.html',
'www.xxxx.com/publish.html',
'www.xxxx.com/user.html',
'www.xxxx.com/friend.html',
'www.xxxx.com/home.html',
'www.xxxx.com/plugin.html',
'www.xxxx.com/edit.html',
'www.xxxx.com/news.html',
);
$refer   = array('www.google.com', 'www.baidu.com', 'www.bing.com', 'www.yahoo.com');
$status  = array('200', '301', '302', '404', '500');
$send_bytes = array('33556', '2223', '56542', '5445', '520', '464654', '455211', '11121', '1231', '544556', '455550');

$handle = fopen('/tmp/access.log', 'w');
for ($i=0; $i < 10000; $i++) {
$access = $time[array_rand($time,1)]
. ' ' .$remote_add[array_rand($remote_add, 1)]
. ' ' . $rt_time[array_rand($rt_time, 1)]
. ' ' . $method[array_rand($method, 1)]
. ' ' . $url[array_rand($url, 1)]
. ' ' . $refer[array_rand($refer, 1)]
. ' ' . $status[array_rand($status, 1)]
. ' ' . $send_bytes[array_rand($send_bytes, 1)]
. "\n";
fwrite($handle, $access);
}
fclose($handle);

}

/**
* 获取国内随机IP地址
* 注:适用于32位操作系统
*/
function rand_ip(){
$ip_long = array(
array('607649792', '608174079'), //36.56.0.0-36.63.255.255
array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255
array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255
array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255
array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255
array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255
array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255
array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255
array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255
array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255
);
$rand_key = mt_rand(0, 9);
$ip       = long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
return $ip;
}

// 生成日志
accessLog();


生成完日志之后,我们需要对日志进行排序来模拟正常的nginx访问请求,在终端执行

#对日志文件按照时间排序
sort access.log >access2.log && echo 'y' | mv access2.log access.log


下面就是一些常用命令已经演示截图

1. 查询访问量前10的IP地址

cat /tmp/access.log |cut -f 3 -d ' '| sort | uniq -c |sort -k 1 -n -r |head -10



2. 查询访问量前10的url

cat /tmp/access.log |cut -f 6 -d ' '|sort |uniq -c |sort -k 1 -n -r |head -10




3. 查询最耗时的页面

cat /tmp/access.log |sort -k 4 -n -r |head -10



4. 统计404请求的占比

export total_line=`wc -l access.log |cut -f 1 -d ' '` && export not_found_line=`awk '{if($8=="404")print $6}' access.log|wc -l` && expr $not_found_line \* 100 / $total_line



5. 统计访问量前十QPS的时间点

cat /tmp/access.log |cut -f 1 -d ' '|uniq -c |sort -k 1 -n -r |head -10


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: