脚本启动慢_Linux 常用运维脚本,建议收藏

3d4f2b4d9a5f2efbad33cc6a2088d578.png

同步本机时间

ntpdate 210.72.145.44

清除系统缓存,空出更多内存

free && sync && echo 3 > /proc/sys/vm/drop_caches && free

杀掉僵尸进程

kill $(ps -A -ostat,ppid | awk '/[zZ]/ && !a[$2]++ {print $2}')

显示全部arp解析

tcpdump 'arp' -e -i eth0 -n -p -t |grep is-ateth0对应要换成你的显步名称

监看本机网卡端口情况

tcpdump -n -vv tcp port $1 -i em1em1为对应的网卡名称。

检查本机连接数

netstat -nat |awk '{print $6}'|sort|uniq -c|sort -nr

查看tomcat日志中的异常

tail -F
/var/log/tomcat8/catalina.out |grep -E 'Exception|at' |grep -v WARN这里tomcat8要对应成你的相应版本

删除5天以前的tomcat日志

sudo find /var/lib/tomcat8/logs/ -mtime +5 -exec rm {} \;

清空 memcache 缓存

以下存成脚本,

#!/bin/sh
#实现通过主机名,端口清相应的memcache缓存

if(($#<2));then
echo "usage:$0 host port";
exit 1;
fi
#如果参数缺失,退出程序,返回状态1

exec 6<>/dev/tcp/$1/$2 2>/dev/null;
#打开host的port 可读写的socket连接,与文件描述符6连接

if(($?!=0));then
echo "open $1 $2 error!";
exit 1;
fi
#如果打开失败,$?返回不为0,终止程序

echo -e "flush_all">&6;
echo -e "quit">&6;
#将HEAD 信息,发送给socket连接

cat6;
#从socket读取返回信息,显示为标准输出

exec 6exec 6>&-;
#关闭socket的输入,输出

exit 0;

修改VirtualBox虚拟机的内存分配

保存脚本,第一个参数为虚拟机的名称,第二个为内存大小,如2G

#!/bin/bash
VM=$1
VBoxManage controlvm $VM poweroff
VBoxManage modifyvm $VM --memory $2
VBoxManage startvm $VM --type headless

为VirtualBox 虚拟机加磁盘

#!/bin/sh

#machine=phptest
machine=$1

VBoxManage controlvm "$machine" poweroff

disk=/home/xwx/VirtualBox\ VMs/$machine/${machine}_swap.vdi

#VBoxManage createhd --filename "$disk" --size 1024

#VBoxManage storageattach "$machine" --storagectl "IDE" --port 1 --type hdd --medium $disk
#VBoxManage storageattach "$machine" --storagectl SATA --port 1 --type hdd --medium $disk
VBoxManage storageattach "$machine" --storagectl "SATA 控制器" --port 1 --type hdd --medium "$disk"

修改克隆虚拟机的ip地址

虚拟机克隆之前,第一次启动时需要修改ip才能远程控制:

#!/bin/bash
# set modify
ip=/etc/network/interfaces
hn=/etc/hostname
netmask=255.255.255.0
network=192.168.20.0
broadcast=192.168.20.255
gateway=192.168.20.1
# mod ip、mask、gw、dns、hostname
cp $ip /etc/network/interfaces.bak
sed -ri 's/(iface eth0 inet).*/\iface eth0 inet static/' /etc/network/interfaces
echo "Please input IP:"
read ipadd
if [ -n "$ipadd" ]; then
echo "address $ipadd" >> $ip
echo "Modify Completed "
else
echo "Not Modified"
fi
echo "netmask $netmask" >> $ip
echo "Netmask Modify Completed "
echo "network $network" >> $ip
echo "Network Modify Completed "
echo "broadcast $broadcast" >> $ip
echo "Broadcast Modify Completed "
echo "gateway $gateway" >> $ip
echo "Gateway Modify Completed "
echo "Please input hostname:"
read hostname
if [ -n "$hostname" ]; then
echo "$hostname" > $hn
echo "Modify Completed "
else
echo "Default Hostname"
fi
echo "All modification completion"
read -n1 -p "Whether restart network [Y/N]?"
case $REPLY in
Y|y) echo
/etc/init.d/networking restart;;
N|n) echo
echo "Network needs to restart to take effect!!!!!!";;
esac
exit

实时统计nginx日志

使用goaccess软件,可能用apt install goaccess或yum install goaccess安装。

sudo goaccess /var/log/nginx/access.log --log-format='%h %^[%d:%t %^] "%r" %s %b "%R" "%u" "-" "%v"' --date-format='%d/%b/%Y' --time-format='%H:%M:%S'

备份nginx配置文件

nginx会频繁修改,改之前最好备份一下:

###################################################################
#######mysqldump###################################################
#!/bin/sh
# -----------------------------
# the directory for story your backup file.
backup_dir="/home/your/backup"

# date format for backup file (dd-mm-yyyy)
time="$(date +"%Y%m%d")"

MKDIR="$(which mkdir)"
RM="$(which rm)"
MV="$(which mv)"
TAR="$(which tar)"
GZIP="$(which gzip)"

#针对不同系统,如果环境变量都有。可以去掉

# check the directory for store backup is writeable
test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0

# the directory for story the newest backup
test ! -d "$backup_dir" && $MKDIR "$backup_dir"


$TAR -zcPf $backup_dir/$HOSTNAME.nginx.$time.tar.gz /etc/nginx
$TAR -zcPf $backup_dir/$HOSTNAME.cron_daily.$time.tar.gz /etc/cron.daily

#delete the oldest backup 30 days ago
find $backup_dir -name "*.gz" -mtime +30 |xargs rm -rf

exit 0;

nginx 自动筛选出访问量过大的ip进行屏避

#!/bin/bash
nginx_home=/etc/nginx
log_path=/var/log/nginx
tail -n10000 $log_path/access.log \
|awk '{print $1,$12}' \
|grep -i -v -E "google|yahoo|baidu|msnbot|FeedSky|sogou" \
| grep -v '223.223.198.231' \
|awk '{print $1}'|sort|uniq -c|sort -rn \
|awk '{if($1>50)print "deny "$2";"}' >>./blockips.conf

sort ./blockips.conf |uniq -u >./blockips_new.conf
mv ./blockips.conf ./blockips_old.conf
mv ./blockips_new.conf ./blockips.conf
cat ./blockips.conf
#service nginx reload

监控各网站首页

#!/bin/sh

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color


function test_domain {
local domain=$1
status=`curl -s -o /dev/null -I -w "%{http_code}" $domain`

if [ $status -eq '404' ]
then
printf "${domain}${RED} ${status}${NC}\n"
else
printf "$domain$GREEN $status$NC\n"
fi

}


domain_list=$'bixuebihui.cn\nwww.bixuebihui.cn\ndev.bixuebihui.cn\nblog.bixuebihui.cn\nbixuebihui.com\nwww.bixuebihui.com'

while read -r domain; do
# echo "... $domain ..."
test_domain "http://$domain"
test_domain "https://$domain"

done <<< "$domain_list"

从mysql日志里过滤慢sql

#!/usr/bin/perl
#
# Nathanial Hendler
# http://retards.org/
#
# 2001-06-26 v1.0
#
# This perl script parses a MySQL slow_queries log file
# ignoring all queries less than $min_time and prints
# out how many times a query was greater than $min_time
# with the seconds it took each time to run. The queries
# are sorted by number of times it took; the most often
# query appearing at the bottom of the output.
#
# Usage: mysql_slow_log_parser logfile
#
# ------------------------
# SOMETHING TO THINK ABOUT (aka: how to read output)
# ------------------------
#
# Also, it does to regex substitutions to normalize
# the queries...
#
# $query_string =~ s/\d+/XXX/g;
# $query_string =~ s/([\'\"]).+?([\'\"])/$1XXX$2/g;
#
# These replace numbers with XXX and strings found in
# quotes with XXX so that the same select statement
# with different WHERE clauses will be considered
# as the same query.
#
# so these...
#
# SELECT * FROM offices WHERE office_id = 3;
# SELECT * FROM offices WHERE office_id = 19;
#
# become...
#
# SELECT * FROM offices WHERE office_id = XXX;
#
#
# And these...
#
# SELECT * FROM photos WHERE camera_model LIKE 'Nikon%';
# SELECT * FROM photos WHERE camera_model LIKE '%Olympus';
#
# become...
#
# SELECT * FROM photos WHERE camera_model LIKE 'XXX';
#
#
# ---------------------
# THIS MAY BE IMPORTANT (aka: Probably Not)
# ---------------------
#
# *SO* if you use numbers in your table names, or column
# names, you might get some oddities, but I doubt it.
# I mean, how different should the following queries be
# considered?
#
# SELECT car1 FROM autos_10;
# SELECT car54 FROM autos_11;
#
# I don't think so.
#


$min_time = 0; # Skip queries less than $min_time
$min_rows = 0;
$max_display = 10; # Truncate display if more than $max_display occurances of a query

print "\n Starting... \n";

$query_string = '';
$time = 0;
$new_sql = 0;


##############################################
# Loop Through The Logfile
##############################################

while (<>) {

# Skip Bogus Lines

next if ( m|/.*mysqld, Version:.+ started with:| );
next if ( m|Tcp port: \d+ Unix socket: .*mysql.sock| );
next if ( m|Time\s+Id\s+Command\s+Argument| );
next if ( m|administrator\s+command:| );


# print $_;
# if ( /Query_time:\s+(.*)\s+Lock_time:\s+(.*)\s/ ) {
#if ( /Query_time:\s+(.*)\s+Lock_time:\s+(.*)\s+Rows_examined:\s+(\d+)/ ) {
if ( /Query_time:\s+(.*)\s+Lock_time:\s+(.*)\s+Rows_examined:\s+(.*)/ ) {

$time = $1;
$rows = $3;
$new_sql = 1;
# print "found $1 $3\n";
next;

}


if ( /^\#/ && $query_string ) {

if (($time > $min_time) && ($rows >= $min_rows)) {
$orig_query = $query_string;

$query_string =~ s/\d+/XXX/g;
$query_string =~ s/'([^'\\]*(\\.[^'\\]*)*)'/'XXX'/g;
$query_string =~ s/"([^"\\]*(\\.[^"\\]*)*)"/"XXX"/g;
#$query_string =~ s/([\'\"]).+?([\'\"])/$1XXX$2/g;
#$query_string =~ s/\s+/ /g;
#$query_string =~ s/\n+/\n/g;

push @{$queries{$query_string}}, $time;
push @{$queries_rows{$query_string}}, $rows;
$queries_tot{$query_string} += $time;
$queries_orig{$query_string} = $orig_query;
$query_string = '';

}

} else {

if ($new_sql) {
$query_string = $_;
$new_sql = 0;
} else {
$query_string .= $_;
}
}

}


##############################################
# Display Output
##############################################

foreach my $query ( sort { $queries_tot{$b} <=> $queries_tot{$a} } keys %queries_tot ) {
my $total = 0;
my $cnt = 0;
my @seconds = sort { $a <=> $b } @{$queries{$query}};
my @rows = sort { $a <=> $b } @{$queries_rows{$query}};
($total+=$_) for @seconds;
($cnt++) for @seconds;

print "### " . @{$queries{$query}} . " Quer" . ((@{$queries{$query}} > 1)?"ies ":"y ") . "\n";
print "### Total time: " . $total .", Average time: ".($total/$cnt)."\n";
print "### Taking ";
print @seconds > $max_display ? "$seconds[0] to $seconds[-1]" : sec_joiner(\@seconds);
print " seconds to complete\n";
print "### Rows analyzed ";
print @rows > $max_display ? "$rows[0] - $rows[-1]": sec_joiner(\@rows);
print "\n";

print "$query\n";
print $queries_orig{$query}."\n\n";
}


sub sec_joiner {
my ($seconds) = @_;
$string = join(", ", @{$seconds});
$string =~ s/, (\d+)$/ and $1/;
return $string;
}

exit(0);

本机路由表

ip route add 5.6.13.192/26 dev em1 src 5.6.13.218 table 10
ip route add default via 5.6.13.254 table 10
ip route add 5.6.13.192/26 dev em2 src 5.6.13.217 table 20
ip route add default via 5.6.13.254 table 20
ip route add 5.6.13.192/26 dev em1 src 5.6.13.218
ip route add 5.6.13.192/26 dev em2 src 5.6.13.217
ip route add default via 5.6.13.254
ip rule add from 5.6.13.218 table 10
ip rule add from 5.6.13.217 table 20
ip route flush cache

出现异常时,用钉钉dingtalk报警

#!/bin/python
# -*- coding: utf-8 -*-

from flask import Flask
from flask import request
import json
import requests

app = Flask(__name__)

def transform(text):
textMap = json.loads(text)

nodePorturl = 'http://192.168.10.182:3672'
externalURL = textMap['externalURL']
print(externalURL)
links =[]
for alert in textMap['alerts']:
print('-------------')
time = alert['startsAt'] + ' -- ' + alert['endsAt']
generatorURL = alert['generatorURL'];
generatorURL = nodePorturl+generatorURL[generatorURL.index('graph'):]
summary = alert['annotations']['summary']
description = alert['annotations']['description']
status = alert['status']
title = alert['labels']['alertname']
link = {}
link['title'] = title
link['text'] = status + ': ' + description
link['messageUrl'] = generatorURL
link['picUrl'] = ''
links.append(link)
return links

@app.route('/',methods=['POST'])
def send():
if request.method == 'POST':
post_data = request.get_data()
alert_data(post_data)
return "hello"

def alert_data(data):
url = 'https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN'
headers = {'Content-Type': 'application/json'}
for link in transform(data):
send_data = {"msgtype": "link", "link": link}
print(send_data)
r = requests.post(url, data=json.dumps(send_data), headers=headers)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=1111)

喜欢本号请点击上方关注!

部分图片、文字来源于网络,如有版权请联系删除!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/427374.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

eclipse maven项目 class类部署不到tomcat下_Servlet tomcat部署

网上搜到的很多利用eclipse结合tomcat开发servlet的教程都要修改server.xml感觉这种方式太粗暴了&#xff0c;server.xml最好是与应用无关的&#xff0c;这里比较推荐export war的方式进行部署先记录一下环境配置过程我的系统是ubuntu 14.04&#xff1b;下载eclipse for javaee…

tensorflow 指定cpu 但是还会运行 gpu_PyTorch VS TensorFlow 谁最强?这是标星 15000+ Transformers 库的运行结果...

点击上方“AI有道”&#xff0c;选择“星标”公众号重磅干货&#xff0c;第一时间送达作者 | Lysandre Debut译者 | 陆离出品 | AI科技大本营(ID: rgznai100)【导语】自然语言处理预训练模型库 Transformers 实现了几种用于 NLP 任务的最先进的 Transformer 架构&#xff0c;如…

after不显示_web前端入门到实战:css实现单行、多行文本超出显示省略号

前言&#xff1a;项目中我们经常遇到这种需求&#xff0c;需要对单行、多行文本超出显示为省略号。这篇文章主要总结了小编解决此问题的方法&#xff0c;有不足之处欢迎大家指正。单行文本省略.ellipsis-line {border: 1px solid #f70505;padding: 8px;width: 400px;overflow: …

linux下查看内存使用情况

基本内存术语解读 1> free -m 同样是做为缓存&#xff0c;buffers和cache又有啥区别呢&#xff1f; 于是又查了些资料&#xff0c;发现buffers实际应该是叫“缓冲”&#xff0c;其英文解释是&#xff1a;A buffer is something that has yet to be "written" to d…

redistemplate使用_如何使用 SpringBoot + Redis 优雅的解决接口幂等性问题

# 前言在实际的开发项目中,一个对外暴露的接口往往会面临很多次请求&#xff0c;我们来解释一下幂等的概念&#xff1a;任意多次执行所产生的影响均与一次执行的影响相同。按照这个含义&#xff0c;最终的含义就是 对数据库的影响只能是一次性的&#xff0c;不能重复处理。如何…

jdbctemplate 开启事务_SpringBoot 系列教程之事务隔离级别知识点小结

上一篇博文介绍了声明式事务Transactional的简单使用姿势&#xff0c;最文章的最后给出了这个注解的多个属性&#xff0c;本文将着重放在事务隔离级别的知识点上&#xff0c;并通过实例演示不同的事务隔离级别下&#xff0c;脏读、不可重复读、幻读的具体场景I. 基础知识在进入…

东风小康为什么是dfsk_助力地摊经济瑞驰纯电动物流车和东风小康微型货车厚积薄发...

核心提示&#xff1a;小康集团旗下瑞驰纯电动物流车及东风小康微型货车深耕多年&#xff0c;其中瑞驰纯电动物流车2015年上市&#xff0c;连续多年销量位列中国行业第一。近日&#xff0c;地摊经济成为热门&#xff0c;疫情常态化下&#xff0c;地摊经济、小店经济对于快速恢复…

链表中删除选定结点的优雅操作!

一般我们在进行单向链表链表的结点删除操作时,都是通过相应的结构体指针进行链表的遍历,然后找 到需要删除的节点,为了完成删除操作,我们需要在寻找该节点时,不断地记录下这个节点前面的节点 (prev),来保证当特定结点被删除后,我们还可以将断开的链表重新连起来,下面给出一段…

python抢货程序_Python自动化xpath实现自动抢票抢货代码示例

本篇文章小编给大家分享一下Python自动化xpath实现自动抢票抢货代码示例&#xff0c;文章代码介绍的很详细&#xff0c;小编觉得挺不错的&#xff0c;现在分享给大家供大家参考&#xff0c;有需要的小伙伴们可以来看看。 总代码&#xff1a; for i in range(51,56): driver.imp…

Ubuntu安装pycharm并且激活

下载pycharm: https://www.jetbrains.com/pycharm/download/#sectionlinux 选择专业版下载&#xff0c;然后提取&#xff0c;也就是解压 进入解压后目录&#xff0c;再进入bin目录&#xff0c;打开终端执行命令 ./pycharm.sh pycharm就启动了 激活Pycharm: 编辑hosts文件&am…

二阶矩阵转置怎么求_矩阵求导术(下)

点击上方“Datawhale”&#xff0c;选择“星标”公众号第一时间获取价值内容本文承接上篇 https://zhuanlan.zhihu.com/p/24709748&#xff0c;来讲矩阵对矩阵的求导术。使用小写字母x表示标量&#xff0c;粗体小写字母表示列向量&#xff0c;大写字母X表示矩阵。矩阵对矩阵的求…

c语言中x的n次方怎么表示_线性代数的本质及其在人工智能中的应用

线性代数是 AI 专家必须掌握的知识&#xff0c;这已不再是个秘密。如果不掌握应用数学这个领域&#xff0c;你永远就只能是「门外汉」。当然&#xff0c;学习线性代数道阻且长。数学&#xff0c;尤其是线性代数常与枯燥、复杂和毫无意义的事物联系起来。不过你还可以另辟蹊径。…

django 实现电子支付功能

思路&#xff1a;调用第三方支付 API 接口实现支付功能。本来想用支付宝来实现第三方网站的支付功能的&#xff0c;但是在实际操作中发现支付宝没有 Python 接口&#xff0c;网上虽然有他人二次封装的的 Python 接口&#xff0c;但是对我这个小白白来说上手还是有点难度&#x…

android中怎么保存checkbox中的checked属性_第二十四天HTML中的form表单

form表单用于收集用户信息&#xff0c;如&#xff1a;登录、注册等场景&#xff1b;所有要提交的数据都必须放在form标签中action&#xff1a;提交地址、动作&#xff0c;与input标签中typy标签的submit属性相关联。 &#xff0c;提交地址是action的地址method:提交方法&#x…

Python中曲率与弯曲的转换_1000R曲率更具沉浸感!三星T55曲面显示器评测

在曲面屏的设计上&#xff0c;三星一直在突破极限&#xff0c;比如在2017年推出的49英寸超宽带鱼屏C49HG90&#xff0c;引来众人围观&#xff0c;非常震撼。而在曲率方面&#xff0c;我们常见的有1800R和1500R&#xff0c;但是三星并不满足&#xff0c;于日前推出了一款曲率达到…

opencv resize_opencv-python库基础操作(一)

点赞再看&#xff0c;养成习惯&#xff01;点赞再看&#xff0c;养成习惯&#xff01;点赞再看&#xff0c;养成习惯&#xff01;opencv-python库基础操作0.安装opencv-pythonpip install opencv-python进行下载并安装不过在python中导入opencv库的时候需要"import cv2&qu…

百旺智能编码_【百旺】票字版开票软件操作指南已为您备好,请查阅!

为确保小规模纳税人继续享受税收优惠政策&#xff0c;请广大用户及时对开票软件进行升级~按照国家税务总局要求&#xff0c;从增值税发票税控开票软件五月补丁开始&#xff0c;将停止对增值税发票税控开票软件(税控盘版)“税”字版(以下简称“税”字版)的软件升级更新服务&…

mysql binlog 备份_做好mysql运维,必须熟练掌握备份和恢复,实战一次不行多来几次...

原文&#xff1a;https://www.toutiao.com/i6855460778359816715/平台&#xff1a;头条作者&#xff1a;程序员不就是0和1一、 备份恢复策略进行备份或恢复操作时需要考虑一些因素&#xff1a;1、确定要备份的表的存储引擎是事务型还是非事务型&#xff0c;两种不同的存储引擎备…

在过程中要正式批准可交付成果_干货!软考高项项目管理知识体系5大过程组47个过程...

现在应该很多小伙伴都在紧张的复习软考中&#xff0c;为了让大家更加高效的复习&#xff0c;今天给大家分享软考高级信息系统项目管理师的考试重点&#xff0c;项目管理知识体系的5大过程组47个过程。考高项的朋友都知道&#xff0c;47个过程是非常重要的&#xff0c;必须要理解…

android 组件化_Android 组件化路由框架设计(仿Arouter)

前言在组件化开发中一个必须要面对的问题就是组件间页面跳转&#xff0c;实现的方法有很多&#xff0c;简单的可以通过反射获取&#xff0c;但是比较耗费性能&#xff0c;也可以通过隐式跳转&#xff0c;但是随着页面的增多&#xff0c;过滤条件会随之增多&#xff0c;后期维护…