懒人Shell脚本》之四——日志条数动态实时统计

简介: 本文是《懒人Shell脚本》之四——日志条数动态实时统计详解

1、需求点

1)输入:日志实时更新:当前日志表以秒级更新日志,每秒有多条日志更新。格式如下:

2016-08-11 11:02:09
2016-08-11 11:02:09
2016-08-11 11:02:09
2016-08-11 11:02:09
2016-08-11 11:02:10
2016-08-11 11:02:10
2016-08-11 11:02:10
2016-08-11 11:02:10
2016-08-11 11:02:10
2016-08-11 11:02:10
2016-08-11 11:02:10
2016-08-11 11:02:10
2016-08-11 11:02:11
2016-08-11 11:02:11
2016-08-11 11:02:11

2)实时输出:每个时刻值对应的2016-08-11 11:06:56 47

2016-08-11 11:06:57 18 
2016-08-11 11:06:58 44 
2016-08-11 11:06:59 22 
2016-08-11 11:07:00 42 
2016-08-11 11:07:01 44 
2016-08-11 11:07:02 12 
2016-08-11 11:07:03 37 
2016-08-11 11:07:04 18 
2016-08-11 11:07:05 18 
2016-08-11 11:07:06 38 
2016-08-11 11:07:07 48 
2016-08-11 11:07:08 38 
2016-08-11 11:07:09 21 
2016-08-11 11:07:10 31 
2016-08-11 11:07:11 18 
2016-08-11 11:07:12 20 
2016-08-11 11:07:13 3 
2016-08-11 11:07:14 43

要求:一行脚本完成统计。

2、脚本实现原理

1)循环产生时间值,并写入文件。

时刻值的产生需要注意时间和时间戳的#时间形式打印

[root@laoyang zq_testing]# tcurtime=date "+%F %T"
[root@laoyang zq_testing]# echo $tcurtime
2016-08-11 11:15:06

2)时间转化为时间戳

[root@laoyang zq_testing]# scurtime=date -d "$tcurtime" +%s
[root@laoyang zq_testing]# echo $scurtime
1470885306

3)时间戳转化为时间

[root@laoyang zq_testing]# tcurtime=date -d @$scurtime "+%F %T"
[root@laoyang zq_testing]# echo $tcurtime
2016-08-11 11:15:06

4)循环环读取文件,使用awk的词频统计功能完成频率统计。

3、脚本实现

#构造时间脚本
[root@laoyang zq_testing]# cat build_time.sh
#!/bin/sh
#generate random values
function random()
{
  min=$1;
  max=$2
  randomval=$((RANDOM%$max+$min))
  echo $randomval
}

#generate time values
function build_conn_time()
{
  rm -rf ./output.log
  touch ./output.log
  cat /dev/null > ./output.log

  tflag=0
  nextsecond=0
  while :
  do
  if [[ $tflag -eq 0 ]];then
  tcurtime=`date "+%F %T"`
  echo "xtcurtime="$tcurtime
  tflag=1
  else
  #时间戳转化为时间
  tcurtime=`date -d @$nextsecond "+%F %T"`
  echo "next tcurtime="$tcurtime
  fi

  #时间格式化 为时间戳
  scurtime=`date -d "$tcurtime" +%s`

  #产生2-50之间的随机数
  cyccnt=$(random 2 50);
  if [[ -z $cyccnt ]]; then
  cyccnt=10
  fi
  echo "cyccnt="$cyccnt
  i=0
  while (( $i<$cyccnt))
  do
  echo $tcurtime >> output.log
  i=$(($i+1))
  done

  #更新下一秒 ,时间戳可以求和操作
  nextsecond=$(($scurtime+1))
  sleep 1
  done
}

build_conn_time;

一行指令完成统计

root@laoyang zq_testing]# while : ; do cat output.log | sort | awk '{ count[$0]++ }\
END { printf("%-14s %s\n","curtime","Count");\
for(ind in count)\
{ printf("%-14s %d\n",ind,count[ind]); } }' | sort ; sleep 1 ; done

作者:铭毅天下
转载请标明出处,原文地址:
http://blog.csdn.net/laoyang360/article/details/52188384

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
1月前
|
Linux Shell
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
246 1
|
2月前
|
监控 安全 Shell
防止员工泄密的措施:在Linux环境下使用Bash脚本实现日志监控
在Linux环境下,为防止员工泄密,本文提出使用Bash脚本进行日志监控。脚本会定期检查系统日志文件,搜索敏感关键词(如&quot;password&quot;、&quot;confidential&quot;、&quot;secret&quot;),并将匹配项记录到临时日志文件。当检测到可疑活动时,脚本通过curl自动将数据POST到公司内部网站进行分析处理,增强信息安全防护。
115 0
|
2月前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 查看系统中的UUCP日志文件 uulog命令 使用指南
【Shell 命令集合 网络通讯 】Linux 查看系统中的UUCP日志文件 uulog命令 使用指南
35 0
|
2月前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
58 0
|
15天前
|
存储 弹性计算 运维
统计/var/log 有多少个文件
【4月更文挑战第29天】
23 1
|
2月前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 实时监控日志文件 swatch命令 使用指南
【Shell 命令集合 系统管理 】Linux 实时监控日志文件 swatch命令 使用指南
41 1
|
3月前
|
Java Shell Perl
使用shell脚本给日志文件瘦身
使用shell脚本给日志文件瘦身
|
4月前
|
Linux Shell
开源日志平台GrayLog5.1.10 CentOS7一键安装脚本
开源日志平台GrayLog5.1.10 CentOS7一键安装脚本
112 0
|
7月前
|
Shell Linux
Linux使用Shell脚本定时清理日志
Linux使用Shell脚本定时清理日志
114 1
|
9月前
|
监控 Shell Linux
linux实现shell脚本监控磁盘内存达到阈值时清理catalina.out日志
linux实现shell脚本监控磁盘内存达到阈值时清理catalina.out日志
251 0