同步本机时间

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));thenecho "usage:$0 host port";exit 1;
fi
#如果参数缺失,退出程序,返回状态1exec 6<>/dev/tcp/$1/$2 2>/dev/null;
#打开host的port 可读写的socket连接,与文件描述符6连接if(($?!=0));thenecho "open $1 $2 error!";exit 1;
fi
#如果打开失败,$?返回不为0,终止程序echo -e "flush_all">&6;
echo -e "quit">&6;
#将HEAD 信息,发送给socket连接cat<&6;
#从socket读取返回信息,显示为标准输出exec 6<&-;
exec 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=$1VBoxManage controlvm "$machine" poweroffdisk=/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 ipaddif [ -n "$ipadd" ]; thenecho "address $ipadd" >> $ipecho "Modify Completed "elseecho "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" ]; thenecho "$hostname" > $hnecho "Modify Completed "
elseecho "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) echoecho "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 -rfexit 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.confsort ./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/shRED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Colorfunction test_domain {local domain=$1status=`curl -s -o /dev/null -I -w "%{http_code}" $domain`if [ $status -eq '404' ]thenprintf "${domain}${RED}  ${status}${NC}\n"elseprintf "$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 queryprint "\n Starting... \n";$query_string   = '';
$time           = 0;
$new_sql        = 0;##############################################
# Loop Through The Logfile
##############################################while (<>) {# Skip Bogus Linesnext 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 requestsapp = 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'] = titlelink['text'] = status + ': ' + descriptionlink['messageUrl'] = generatorURLlink['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)

Linux学习总结(55)——Linux 运维常用脚本相关推荐

  1. oracle 运营维护_Oracle数据库日常运维常用脚本

    大 中 小 Oracle数据库日常运维常用脚本 1 查看所有数据文件 select file_name from dba_data_files union select file_name from ...

  2. 2022年最新运维常用脚本学习

    以下是常用脚本,由简单到复杂,赶紧收藏起来. 目录 一.日志备份 二.监控内存和磁盘容量,小于给定值时报警 三.检测当前用户权限 四.自动创建相应的账户及配置密码 五.输入三个数并进行升序排序 六.石 ...

  3. python大数据运维常用脚本_大数据岗位要求之大数据运维

    继续介绍大数据系列岗位要求,大数据运维可能是"技术含量最高"的职位之一,这里说的大数据运维主要是指hadoop生态体系方面的运维,在一些小公司或者传统行业的大公司也会使用oracl ...

  4. python大数据运维常用脚本_python大数据运维

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  5. 很实用的Linux 系统运维常用命令及常识(超实用)

    很实用的Linux 系统运维常用命令及常识(超实用) 作为Linux运维,需要了解Linux操作系统的基本使用和管理知识,下面脚本之家小编给大家介绍下Linux运维需要掌握的命令,想成为Linux运维 ...

  6. linux清除历史命令记录_Linux运维常用维护命令记录,不是长篇理论,干的实用...

    运维常用命令,希望学习Linux同学更快地熟练掌握,没有太多的理论统统是干活,以下内容来自网络,需要的同学拿去慢慢消化吧. 嵌入式开发直播课 - 走进linux底层驱动 - 创客学院直播室​www.m ...

  7. 阿里云官方学习课程推荐-Linux运维学习路线 从事云计算运维相关工作必备技能

    阿里云官方学习课程推荐-Linux运维学习路线 从事云计算运维相关工作必备技能 目前越来越多的企业需要依赖于IT技术发布产品与服务,尤其是电子商务最为明显,它凸显了IT技术在现代企业中的重要性.当企业 ...

  8. linux运维常用服务器软件整理和介绍

    本文介绍Linux运维常用的服务器软件,中间件,运维工具等. 目录 代理服务器 nginx 运行环境 php python c++/g++ java go 数据库/中间件 redis mysql po ...

  9. linux运维常用培训

    linux运维常用培训 2021年2月 目录 一. 系统监控 1.free命令使用 2.ulimit命令使用 3.top命令使用 4.du命令使用 5.ps命令使用 6. crontab 命令 二. ...

最新文章

  1. Qt控件如何随着界面自适应变化
  2. C# 中对WinForm窗体中的控件快速设置TableIndex次序
  3. Entity Framework-02
  4. linux排序语言,Go语言排序sort的使用
  5. 汇编 cmp_汇编复习
  6. python课程设计矩阵对角线之和,为每个python numpy用不同的值填充矩阵对角线
  7. android程序怎么导出数据库,将android程序中的数据库导出到SD卡
  8. 服务器虚拟化的培训,VMware服务器虚拟化及桌面虚拟化培训.pptx
  9. ValidateAntiForgeryToken的用途,解释和示例
  10. OEMAddressTable介绍(转)
  11. 广发基金总经理林传辉:深耕资产管理本源做投资者的资产配置专家
  12. 【数字通信】Matlab实现16QAM调制与解调,判决,误码率计算
  13. java大华监控_JAVA实现大华摄像头WEB方式实时显示视频,H5界面展示方式思路。
  14. python numpy 计算标准差
  15. 苹果恢复出厂设置系统也会还原吗_手机经常恢复出厂设置会怎么样?对手机有害处吗?这下终于清楚了...
  16. 零基础转行学编程技术难吗?
  17. atm机编程java_JAVA语言实现的简单ATM提款机系统_亢先生02070708的博客-CSDN博客
  18. 微信公众号获取AccessToken
  19. 3DMAX高级光照相关的名词解释
  20. css实现水平垂直居中的七种方式

热门文章

  1. python generator长度_Python 高级特性之:生成器(generator)和迭代器(Iterator)
  2. python单行动态刷新不了_python 解决tqdm模块不能单行显示的问题
  3. clock函数的时间单位_PAT B1026:程序运行时间
  4. python网页请求超时_python爬虫多次请求超时的几种重试方法(6种)
  5. vue如何和PHP交互,VUE中如何使用Vue-resource完成交互
  6. os x php,OS X 安装php7.0
  7. 远程连接linux的mysql_【Linux开启mysql远程连接的设置步骤】 mysql开启远程连接
  8. python做动画的库_用matplotlib动画库制作等分法动画
  9. mysql返回前2行_取得前一次MySQL操作所影响的记录行数
  10. mybatis resultType resultMap