需求,获取时段内的/var/log/messages文件内出现错误的消息,支持多行的消息,支持天,小时分钟,秒级的区间,可以修改监控的日志对象
#!/bin/bash

if [ $# != 1 ] ; then
echo "USAGE: $0 num[Y|D|H|M|S]"
echo " e.g.: $0 12h"
exit 1;
fi interval=$1
unit=$(echo $interval | tr -d [:blank:] |tr -d [:digit:] |tr -t [a-z] [A-Z])
val=$(echo $interval | tr -d [:blank:] |tr -d [:alpha:] )
#echo $val
#echo $unit case $unit in
Y) unit="years"
;;
D)
unit="days"
;;
H)
unit="hours"
;;
M)
unit="minutes"
;;
S)
unit="seconds"
;;
*)
echo "not"
exit 1;
;;
esac
#echo $unit ts=$(date -d"-${val}${unit}" +"%s")
echo $ts
#date span one year 计算两年之间的秒数
cts=$(date +"%s")
x1=$(date -d"$(date +%Y)-01-01 00:00:00" +"%s")
x2=$(date -d"$(date -d'-1years' +%Y)-01-01 00:00:00" +"%s")
((secs=$x1-$x2)) checkfile=/var/log/messages
#checkfile=/home/student/test.txt #first_line=$(awk -v ts="$ts" '{"date -d \""$1" "$2" "$3"\" +%s 2>/dev/null"|getline dt; if(dt>ts) {print $0; exit;}}' $checkfile)
first_line=$(awk -v secs="$secs" -v ts="$ts" -v cts="$cts" '{"date -d \""$1" "$2" "$3"\" +%s 2>/dev/null"|getline dt;if(dt>cts){dt=dt-secs;}if(dt>ts) {print $0; exit;}}' $checkfile) echo "firstline is $first_line" if [ "$first_line"X == "X" ]; then exit 1; fi basedir=$(cd `dirname $0`;pwd)
#echo "basedir is $basedir" log=$basedir/linux_messages.log
>$log #function to handle one msg
handleOneMsg()
{
msg="$1"
has_error=$(echo "$msg"|grep -oie "\berror\b")
if [ "$has_error"X != "X" ]; then
#first we should encode the < > &
msg_out=$(echo "$msg" |sed 's/&/&/g'| sed 's/</\</g'|sed 's/>/\>/g' )
cat <<EOF >>$log
<msg>
<hostName>`hostname`</hostName>
<product>LINUX</product>
<date>`date +"%Y-%m-%d %T"`</date>
<streamName>messages</streamName>
<output>$msg_out</output>
</msg>
EOF
fi } OLDIFS=IFS
IFS=$'\n'
for line in $(grep -A100000 -F "$first_line" $checkfile)
do
#date ..... one message is start with date
#before another msg is the last msg's body
line_begin=$(echo $line | awk '{print $1,$2,$3}')
#echo "line begin is $line_begin" line_begin_ts=$(date -d "$line_begin" "+%s" 2>/dev/null)
#echo "line begin ts is $line_begin_ts" if [ "${line_begin_ts}"X == "X" -o "$msg"X == "X" ] ; then
msg="${msg}"$'\n'"${line}"
continue;#consume next line
fi handleOneMsg "$msg"
msg=$line
done handleOneMsg "$msg" IFS=$OLDIFS

bash 获取时间段内的日志内容的更多相关文章

  1. Shell 从日志文件中选择时间段内的日志输出到另一个文件

    Shell 从日志文件中选择时间段内的日志输出到另一个文件 情况是这样的,某系统的日志全部写在一个日志文件内,所以这个文件非常大,非常长,每次查阅的时候非常的不方便.所以,相关人员希望能够查询某个时间 ...

  2. 利用webBrowser获取框架内Html页面内容

    原文:利用webBrowser获取框架内Html页面内容 利用webBrowser获取页面比较简单,MSDN下有示例,在这里不必多说. 可是一些 HTML 文档由“框架”构成,或可以存放它们自己独特 ...

  3. SQL 获取时间段内日期列表

    declare @start date,@end date; set @start='2010-01-01'; set @end='2010-02-01'; --获取时间段内日期列表 select [ ...

  4. SVN windows内修改日志内容(错误解决)

    在我的电脑是windows 7,使用TortoiseSVN客户端,选中代码目录,点击右键,选择<显示日志> 显示日志信息 修改原来的日志信息(在需要修改的版本的日志中点击鼠标右键,显示如下 ...

  5. 根据时间段获取时间段内所有时间点(js)

    Date.prototype.format=function (){var s='';s+=this.getFullYear()+'-';// 获取年份.s+=(this.getMonth()+1)+ ...

  6. SQL获取时间段内的所有月份

    select convert(varchar(7),dateadd(month,number,'2010-01-01'),120) AS MONTHfrom master..spt_valueswhe ...

  7. sql获取时间段内的所有日期

    ,'2015-01-01 00:00:00') h INTO vinson_h DELETE vinson_h DECLARE @h int SELECT @h=DATEDIFF(HOUR,'2015 ...

  8. java获取时间段内的所有日期

        public static void main(String[] args) {        SimpleDateFormat dateFormat = new SimpleDateForm ...

  9. JAVA 获取时间段内的每一天

    public class day { public static void main(String[] args) { // TODO Auto-generated method stub Strin ...

随机推荐

  1. PowerShell----Automatic_Variables(预定义变量)

    以下这些变量是由powershell创建和维护的.ls Variable: 可以获取到所有默认的变量, 每个版本的Powershell可能有差异 $$包含会话所收到的最后一行中的最后一个令牌. $? ...

  2. 获取软件的apk包名、查看手机设备名称等

    获取软件的apk包名 1.下载aapt,这里使用的是SDK自带的aapt,找到SDK路径下面的aapt.exe,本机路径:E:\downloads\android-sdk_r23.0.2-window ...

  3. Lumen Carbon 日期及时间处理包

    用到过的方法: 获取当前Y-m-d H:i:s Carbon::now()->toDateTimeString() 把 Y-m-d H:i:s 转 Y-m-d Carbon::parse('Y- ...

  4. Echarts中axislabel文字过长导致显示不全或重叠

    最近在使用Echarts的时候,遇到点问题就是xAxis文字过长导致x轴的文字显示不全. 解决方案如下: 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HT ...

  5. Web端测试

    一.功能测试 1.链接测试      1)所有链接是否按指示的那样,链接正确?      2)所有链接是否存在?      3)保证Web应用系统上没有孤立的页面? 在线链接测试地址:http://v ...

  6. windows10子系统配置vim

    .cmd背景,字体等配置 (1)设置背景颜色44 50 59 (2)设置字体 大小 颜色 Courier New 20 白色 2.ubuntuvim配置(etc/vim/vimrc):(1)配置vim ...

  7. Centos7.0安装mysql5.6

    环境 CentOS 7.0(64-bit system) mysql-5.6.37 一.mysql依赖libaio,所以要先安装libaio # yum search libaio           ...

  8. 字符串与图片的Base64编码转换操作

    //图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = ne ...

  9. authz_core_module

    w https://httpd.apache.org/docs/trunk/mod/mod_authz_core.html codeigniter index.html .htaccess <I ...

  10. 4.1 - FTP文件上传下载

    题目:开发一个支持多用户同时在线的FTP程序要求:1.用户加密认证2.允许同时多用户登录3.每个用户有自己的家目录,且只能访问自己的家目录4.对用户进行磁盘配额,每个用户的可用空间不同5.允许用户在f ...