当前位置:网站首页>Shell 脚本实现应用服务日志入库 Mysql
Shell 脚本实现应用服务日志入库 Mysql
2022-07-04 21:58:00 【Wu_Candy】
今天给大家分享一个 shell 脚本工具,通过 shell 脚本与 mysql 的结合,将某个具体服务的错误输出日志入库到指定的 mysql 表中,以便于进行错误问题的定位与分析。
日常工作中,经常需要和 linux 系统打交道,例如:服务部署、日志和服务状态查看等,而 shell 脚本是和 linux 交互的一种常见方法。以下代码实例为在 shell 脚本中嵌入 mysql 配置信息将错误日志信息落入到数据库中。
shell脚本如下:
#!/usr/bin/env bash
hostName="l-beta.io"
dbPort=3306
dbUser="beta"
dbPassword="123456"
dbName="test_log"
tableName="beta_error_log_record"
env="test1"
base="/home/q/www"
#进入死循环
while [[ 1 ]]; do
#循环去遍历base目录下的文件夹,-vE 后面两个是要排除掉的项
for name in `ls $base | grep -vE "default|genesis"`;
do
#echo $base/$name
#判断是否是目录
if [[ -d $base/$name ]]; then
#定义当前时间的前一分钟,用于对日志内容进行过滤匹配使用
before=`date -d "-1 minute" "+%Y-%m-%d %H:%M:"`
# echo "Watching log $name in $before"
#过滤获取对应目录下的catalina.out文件中的上一分钟的Exception and Error日志
result=`grep -C5 "$before" $base/$name/logs/catalina.out | grep -vE "DEBUG|skywalking|10.8.0.37:8800" | grep -C5 -E "Exception|ERROR" | sed $'s/\'/\"/g' `
#如果result为零就跳过本次循环
if [[ -z "$result" ]]; then
continue;
fi
echo "-------------------------->"
#拼接insert sql 语句
insert_sql="insert into $tableName (environment,project_name,content) values('$env','$name','\n\nException:\n\n$result')"
echo $insert_sql > tmp.sql
echo "***********start*************"
echo $insert_sql
#开始执行sql语句
mysql -h$hostName -P$dbPort -u$dbUser -p$dbPassword $dbName -e "source tmp.sql"
echo "************end************"
fi
done
#输入休眠60s
echo -e "sleep 60 seconds\n"
sleep 60s
done
以上 shell 脚本中关键信息都标有对应的注释,如需实操,可将 mysql 对应的配置信息及监控的日志服务路径进行更改和替换为目标信息即可。
end
边栏推荐
- Kdd2022 | what features are effective for interaction?
- 关系型数据库
- new IntersectionObserver 使用笔记
- Common open source codeless testing tools
- Basic structure of PostgreSQL - table
- Nat. Commun.| Machine learning jointly optimizes the affinity and specificity of mutagenic therapeutic antibodies
- 现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类
- 抖音实战~评论数量同步更新
- 1807. Replace the parentheses in the string
- 并发优化总结
猜你喜欢

复数在数论、几何中的用途 - 曹则贤

什么是商业智能(BI),就看这篇文章足够了

Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry

机器学习笔记 - 互信息Mutual Information

MongoDB聚合操作总结

从RepVgg到MobileOne,含mobileone的代码

From repvgg to mobileone, including mobileone code

常用的开源无代码测试工具

Tiktok actual combat ~ the number of comments is updated synchronously
![[acwing] solution of the 58th weekly match](/img/e3/fd2c0ffbc9c7ca8a71875882d6c71b.png)
[acwing] solution of the 58th weekly match
随机推荐
Service online governance
bizchart+slider实现分组柱状图
ACM multimedia 2022 | counterfactual measurement and elimination of social prejudice in visual language pre training model
大厂的广告系统升级,怎能少了大模型的身影
力扣2_1480. 一维数组的动态和
Implementation rules for archiving assessment materials of robot related courses 2022 version
vim 从嫌弃到依赖(23)——最后的闲扯
MySQL存储数据加密
删库不必跑路!详解 MySQL 数据恢复
智洋创新与华为签署合作协议,共同推进昇腾AI产业持续发展
close系统调用分析-性能优化
283. Moving zero-c and language assisted array method
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
Interview question 01.08 Zero matrix
el-tree结合el-table,树形添加修改操作
复数在数论、几何中的用途 - 曹则贤
力扣_回文数
You don't have to run away to delete the library! Detailed MySQL data recovery
Force buckle 2_ 1480. Dynamic sum of one-dimensional array
# 2156. Find the substring of the given hash value - post order traversal