日志
统计独立 IP 数量
-
awk '{print $1}' access.log | sort -n | uniq | wc -l
查看某一时间段的 IP 访问量
-
grep "05/Apr/2019:0[1-9]" access.log | awk '{print $1}' | sort | uniq -c| sort -nr | wc -l
查看访问最频繁的前 100 个 IP
-
awk '{print $1}' access.log | sort -n | uniq -c | sort -rn | head -n 100
查看访问 100 次以上的 IP
-
awk '{print $1}' access.log | sort -n | uniq -c | awk '{if($1 > 100) print $0}' | sort -rn
查询某个 IP 的详细访问情况,按访问频率排序
-
grep '127.0.0.1' access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head -n 100
统计 URL 访问量排行
-
awk '{url[$7]++} END {for (k in url) {print url[k],k}}' nginx.access.log | sort -rn
使用
awk
从
Nginx
日志中逐行统计
URL
访问计数,然后使用
sort
对结果进行排名
访问最频繁的 URL
-
awk '{print $7}' access.log | sort | uniq -c | sort -rn | head -n 100
或者
-
awk '{url[$7]++} END {for (k in url) {print url[k],k}}' access.log | sort -rn | head -n 100
除了 .php 以外,访问最频繁的 URL
-
grep -v ".php" access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head -n 100
URL 访问次数超过 100 次的页面
-
awk '{print $7}' access.log | sort -n | uniq -c | sort -rn | head -n 100
查看最近 1000 条记录,访问量最高的 URL
-
tail -1000 access.log | awk '{print $7}' | sort | uniq -c | sort -rn | less
统计每秒的请求数,TOP100 的时间点 (精确到秒)
-
awk '{print $4}' access.log | cut -c 14-21 | sort | uniq -c | sort -rn | head -n 100
统计每小时的请求数,TOP100 的时间点 (精确到小时)
-
awk '{print $4}' access.log | cut -c 14-15 | sort | uniq -c | sort -rn | head -n 100
列出传输时间超过 3 秒的页面,并统计其出现的次数,显示前 20 条
在
Nginx log最后一个字段加入
$request_time
-
cat access.log | awk '($NF > 3){print $7}' | sort -n | uniq -c | sort -rn | head -20
列出 PHP 页面请求时间超过 3 秒的页面,并统计其出现的次数,显示前 100 条
在
Nginx log最后一个字段加入
$request_time
-
cat access.log | awk '($NF > 1 && $7~/\.php/){print $7}' | sort -n | uniq -c | sort -rn | head -100
文件
列出当前目录下的所有文件(包括隐藏文件)的绝对路径
-
find $PWD -maxdepth 1 | xargs ls -ld
递归列出当前目录下的所有文件(包括隐藏文件)的绝对路径
-
find $PWD | xargs ls -ld
在每行记录的开头加上当前路径
-
ls | sed "s:^:`pwd`/:"
删除指定时间之前的文件
-
find /path/to/dir -mtime +30 -type f | xargs rm -f
-
/path/to/dir
设置查找的目录
-
--mtime +30
设置时间为 30 天前
-
-type f
指定查找的类型为文件
删除文件前 / 后 N 行
删除了前 2 行。先用 tail 把从第 3 行开始的所有内容输出到新文件,然后再重命名文件。
-
tail -n +3 old_file > new_file
-
mv new_file old_file
仅保留最后 3 行。
-
tail -n -3 old_file > new_file
-
mv new_file old_file
如果写定时任务,那可放置到一行。
-
0 0 * * * tail -n -3 old_file > new_file && mv -f new_file old_file
网络
统计网卡的流量数据
-
sar -n DEV 1 5
-
平均时间: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
-
平均时间: lo 2.21 2.21 0.18 0.18 0.00 0.00 0.00
-
平均时间: eth0 4.62 3.82 0.37 1.90 0.00 0.00 0.00
命令中 1 5 表示每一秒钟取 1 次值,一共取 5 次。
命令执行后会列出每个网卡这 5 次取值的平均数据,根据实际情况来确定带宽跑满的网卡名称,默认情况下
eth0
为内网网卡,
eth1
为外网网卡。
查询占用端口的进程 / 程序
-
netstat -tunlp | grep ':80'
-
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26655/nginx
或者使用
lsof
命令:
-
lsof -i :80
查看流量占用情况
-
iftop -P
查看程序流量排行
-
nethogs
进程 / 程序
grep 程序并杀死
-
ps -ef | grep process_name | grep -v grep | cut -c 9-15 | xargs kill -s 9
查看指定进程的具体占用内存
-
cat /proc/[pid]/status
-
Name: memcached
-
State: S (sleeping)
-
Tgid: 1954
-
Pid: 1954
-
PPid: 1
-
TracerPid: 0
-
Uid: 500 500 500 500
-
Gid: 500 500 500 500
-
Utrace: 0
-
FDSize: 128
-
Groups:
-
VmPeak: 413792 kB
-
VmSize: 360544 kB
-
VmLck: 0 kB
-
VmHWM: 29704 kB
-
VmRSS: 29376 kB
-
VmData: 341768 kB
-
VmStk: 2132 kB
-
VmExe: 80 kB
-
VmLib: 2152 kB
-
VmPTE: 164 kB
-
VmSwap: 0 kB
-
Threads: 6
-
...
其中,
VmRSS
项表示实际占用内存值。
或者,用
ps
命令
-
ps aux | grep <pid>
-
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
-
jxcdn 1954 0.0 0.1 360544 29376 ? Ssl Apr13 7:56 memcached -m 128 -p 11211
其中
RSS
列表示实际使用内存 (单位:
KB
)。可以看出,与
/proc/[pid]/status
的值是一致的。
本站支持QQ、百度一键登录,更多一键登录可私信客服添加
遇到了请不要着急可私信联系客服,客服会在第一时间补发给您
除去睡觉、午休和运动,每日可及时处理时段约为: 9:00—12:00 14:00—19:00 21:00—22:30