当前位置:网站首页>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
边栏推荐
- 服装企业为什么要谈信息化?
- Nat. Commun.| Machine learning jointly optimizes the affinity and specificity of mutagenic therapeutic antibodies
- Radio and television Wuzhou signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
- Implementation rules for archiving assessment materials of robot related courses 2022 version
- B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条
- 高中物理:直线运动
- Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game
- Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
- Solana链上应用Crema因黑客攻击停运
- You don't have to run away to delete the library! Detailed MySQL data recovery
猜你喜欢
Éducation à la transmission du savoir | Comment passer à un test logiciel pour l'un des postes les mieux rémunérés sur Internet? (joindre la Feuille de route pour l'apprentissage des tests logiciels)
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
Deveco device tool 3.0 release brings five capability upgrades to make intelligent device development more efficient
Cloudcompare & open3d DBSCAN clustering (non plug-in)
Why do you have to be familiar with industry and enterprise business when doing Bi development?
Kdd2022 | what features are effective for interaction?
El tree combined with El table, tree adding and modifying operations
Machine learning notes mutual information
Scala download and configuration
30余家机构联合发起数字藏品行业倡议,未来会如何前进?
随机推荐
能源势动:电力行业的碳中和该如何实现?
淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口
虚拟人产业面临的挑战
达梦数据凭什么被称为国产数据库“第一股”?
常用的开源无代码测试工具
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条
TLA+ 入门教程(1):形式化方法简介
MySQL存储数据加密
HDU - 1078 FatMouse and Cheese(记忆化搜索DP)
力扣2_1480. 一维数组的动态和
1807. Replace the parentheses in the string
HDU - 1078 fatmouse and cheese (memory search DP)
PostgreSQL基本结构——表
服务线上治理
保证接口数据安全的10种方案
The use of complex numbers in number theory and geometry - Cao Zexian
Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development
File read write
Force buckle_ Palindrome number
现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类