当前位置:网站首页>Shell script implements application service log warehousing MySQL
Shell script implements application service log warehousing MySQL
2022-07-04 22:22:00 【Wu_ Candy】
Today I'd like to share a shell Scripting tools , adopt shell Script vs mysql The combination of , Store the error output log of a specific service into the specified mysql In the table , So as to facilitate the positioning and analysis of error problems .
On a daily basis , Often need and linux The system deals with , for example : Service deployment 、 Log and service status viewing , and shell Scripts are and linux A common method of interaction . The following code example is in shell Embedded in the script mysql The configuration information drops the error log information into the database .
shell The script is as follows :
#!/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"
# Into the dead cycle
while [[ 1 ]]; do
# Loop de ergodic base Folder under directory ,-vE The last two are the items to be excluded
for name in `ls $base | grep -vE "default|genesis"`;
do
#echo $base/$name
# Determine if it's a directory
if [[ -d $base/$name ]]; then
# Define the previous minute of the current time , It is used to filter and match the log content
before=`date -d "-1 minute" "+%Y-%m-%d %H:%M:"`
# echo "Watching log $name in $before"
# Filter and obtain catalina.out Last minute in the file Exception and Error journal
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' `
# If result If it is zero, skip this cycle
if [[ -z "$result" ]]; then
continue;
fi
echo "-------------------------->"
# Splicing insert sql sentence
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
# Start execution sql sentence
mysql -h$hostName -P$dbPort -u$dbUser -p$dbPassword $dbName -e "source tmp.sql"
echo "************end************"
fi
done
# Input sleep 60s
echo -e "sleep 60 seconds\n"
sleep 60s
done
above shell The key information in the script is marked with corresponding comments , If you need to practice , Can be mysql The corresponding configuration information and the monitored log service path can be changed and replaced with the target information .
end
边栏推荐
- Interview question 01.01 Determine whether the character is unique
- i. Mx6ull driver development | 24 - platform based driver model lights LED
- php短视频源码,点赞时会有大拇指动画飘起
- [advanced C language] array & pointer & array written test questions
- 能源势动:电力行业的碳中和该如何实现?
- Scala download and configuration
- Convolutional neural network model -- lenet network structure and code implementation
- 阿里推出新品牌“瓴羊”,致力成为“数字化领头羊”
- Service online governance
- 卷积神经网络模型之——LeNet网络结构与代码实现
猜你喜欢
随机推荐
UML图记忆技巧
Energy momentum: how to achieve carbon neutralization in the power industry?
1807. Replace the parentheses in the string
短视频系统源码,点击屏幕空白处键盘不自动收起
面试题 01.08. 零矩阵
开户哪家券商比较好?网上开户安全吗
idea中pom.xml依赖无法导入
电话加密,中间4为****代替
New intersectionobserver usage notes
机器人相关课程考核材料归档实施细则2022版本
KDD2022 | 什么特征进行交互才是有效的?
QT - plot other problems
Machine learning notes mutual information
PostgreSQL JOIN实践及原理
Tiktok actual combat ~ the number of comments is updated synchronously
Why should garment enterprises talk about informatization?
Is it safe to open an account in the stock of Caicai college? Can you only open an account by digging money?
1807. 替换字符串中的括号内容
el-tree结合el-table,树形添加修改操作
Bookmark









