常用shell脚本&数据处理&轻量级长连接
watch.sh(生产环境日志异常监控shell脚本)build.sh(jenkins持续集成脚本)手机号、身份证号脱敏mysql事务未释放导致的锁数据解锁logstash推送钉钉机器人配置服务端推送常用实现方式1.WebSocket2.Server-Sent Events(简称SSE,石墨文档的推送实现,text/event-stream)3.Spring的DeferredResult类长连接异
·
watch.sh(生产环境日志异常监控shell脚本)
#!/bin/bash
#--------------------------------------------
# 监听程序异常日志, 如果出现异常则发送通知
# author:RUNTU
# weixin: classmisschen
#--------------------------------------------
# 切换到脚本执行的目录
cd `dirname $0`
# 监听的日志文件
logfile='error.log'
# 当前机器标识
environment="〓〓〓 PMS异常监控:XXX.XXX.XXX.XXX 〓〓〓\n"
# 获取日志当前的行号
beforelinenum=`sed -n '$=' $logfile`
# 监听间隔时间时间
secondspan=15
while true
do
afterlinenum=`sed -n '$=' $logfile`
line=$(( $afterlinenum - $beforelinenum ))
#echo "line:"$line
beforelinenum=$afterlinenum
content=`tail -n $line $logfile`
if [ -n "$content" ]; then
curl 'https://oapi.dingtalk.com/robot/send?access_token=1626fcbcddddddddddwd' \
-H 'Content-Type: application/json' -d "{\"msgtype\": \"text\",\"text\": { \"content\": \"$environment$content\" }}"
fi
sleep $secondspan
done
echo "====exit====="
exit
build.sh(jenkins持续集成脚本)
#!/bin/bash
PROFILE=/home/opt/code/taskcenter-V2.0
CODEFILE=/home/opt/code
if [ -d $PROFILE ]; then
rm -rf $PROFILE
echo "清除上一次构建文件"
fi
cd $CODEFILE
svn export --username 用户名 --password '密码' https://111.22.3.444/svn/xxx/dsp-v2.0/code/tags/taskcenter-V2.0
echo "svn拉取代码"
cd $PROFILE
mvn clean
mvn package -P common -U
pid=`netstat -nptl| grep 8088 | awk '{print $7}'|awk -F '/' '{print $1}'`
if [ -n "$pid" ]
then
echo "kill掉上一次运行的工程-pid:" $pid
kill -9 $pid
fi
echo "复制最新的jar到指定目录"
cp /home/opt/code/taskcenter-V2.0/web/target/taskcenter-web-2.0.0.jar /home/opt/tomcat/taskcenter/taskcenter-web-2.0.0.jar
# 如果没有配置全局不中断请使用BUILD_ID=dontKillMe,例如:nohup /home/deploy-mq/startup.sh &
java -jar /home/opt/tomcat/taskcenter/taskcenter-web-2.0.0.jar &
echo "项目启动"
手机号、身份证号脱敏
//JAVA
public static void main(String[] args) {
String phone = "18888888888";
String phoneNumber = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
System.out.println("phone总长度:" + phone.length());
System.out.println("phone前3位:" + phone.substring(0, 3));
System.out.println("phone中4位:" + phone.substring(3, 7));
System.out.println("phone后4位:" + phone.substring(7, phone.length()));
System.out.println("phone中4*:" + phoneNumber);
}
//SQL
SELECT
INSERT ( 18888888888, 4, 4, '****' ) AS phone ,
INSERT ( '37292219960101555X', 5, 10, '****' ) AS ID
FROM
DUAL;
mysql事务未释放导致的锁数据解锁
SELECT * FROM information_schema.innodb_trx;
SELECT * FROM information_schema.innodb_locks;
show PROCESSLIST;
SELECT * FROM information_schema.PROCESSLIST WHERE command NOT IN ( 'Sleep' ) AND USER NOT IN ( 'mydba', 'event_scheduler', 'repl', 'binlogbak', 'system user' )
KILL 41501
-- 执行下面SQL可以确定是什么事务运行阻塞了什么事务运行
select l.* from
(
select 'Blocker' role, p.id, p.user, left(p.host, locate(':', p.host) - 1) host, tx.trx_id, tx.trx_state, tx.trx_started, timestampdiff(second, tx.trx_started, now()) duration, lo.lock_mode, lo.lock_type, lo.lock_table, lo.lock_index, tx.trx_query, lw.requesting_trx_id Blockee_id, lw.requesting_trx_id Blockee_trx
from information_schema.innodb_trx tx,
information_schema.innodb_lock_waits lw,
information_schema.innodb_locks lo,
information_schema.processlist p
where lw.blocking_trx_id = tx.trx_id and p.id = tx.trx_mysql_thread_id and lo.lock_id = lw.blocking_lock_id
union
select 'Blockee' role, p.id, p.user, left(p.host, locate(':', p.host) - 1) host, tx.trx_id, tx.trx_state, tx.trx_started, timestampdiff(second, tx.trx_started, now()) duration, lo.lock_mode, lo.lock_type, lo.lock_table, lo.lock_index, tx.trx_query, null, null
from information_schema.innodb_trx tx,
information_schema.innodb_lock_waits lw,
information_schema.innodb_locks lo,
information_schema.processlist p
where lw.requesting_trx_id = tx.trx_id and p.id = tx.trx_mysql_thread_id and lo.lock_id = lw.requested_lock_id
) l order by role desc, trx_state desc;
logstash推送钉钉机器人配置
input {
file {
path => ["/home/opt/logs/taskcenter/error.log"]
type => "taskcenter"
stat_interval => 45
codec=> multiline {
pattern => "^[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3} \[ERROR\]"
negate => true
what => "previous"
auto_flush_interval => 10
# max_lines => 20
# max_bytes => "2KiB"
}
}
}
filter {
grok{
match => {
"message" => "(?<temMsg>([\s\S]{1200}))"
}
}
mutate {
rename => {"temMsg" => "message"}
}
}
output {
if [type] == "taskcenter" {
stdout {}
http {
url => "钩子地址"
http_method => "post"
content_type => "application/json; charset=utf-8"
format => "message"
pool_max => 3
pool_max_per_route => 10
request_timeout => 10
retry_failed => false
socket_timeout => 6
message => '{"msgtype":"text","text":{"content":"%{[message]}"}}'
}
}
}
服务端推送常用实现方式
1.WebSocket
2.Server-Sent Events(简称SSE,石墨文档的推送实现,text/event-stream)
3.Spring的DeferredResult类长连接异步返回(apollo客户端会像服务端发送长轮训来检测配置变更)
更多推荐
已为社区贡献6条内容
所有评论(0)