• shell脚本


    技巧:

    xshell自动脚本进入日志查看

    cd /data/logs
    file="log_"`date "+%Y-%m-%d"`".txt"
    tail -f $file

     IP筛选

      cat /etc/httpd/logs/access_log |awk '{print $1}'|sort |uniq -c |sort -nr |head -100

    日志删除脚本

    删除创建60天以上的日志

    find ./ -mtime +60|xargs rm -f

    N=7
    find . -type f -mtime +7 | grep -E ".log.20[0-9]{2}-[0-1][0-9]-[0-3][0-9].gz$" |xargs rm -f

    +N表示N天以前
    -N表示N天以内

    atime是指access time,即文件被读取或者执行的时间
    ctime即change time文件状态改变时间,指文件的i结点被修改的时间,如通过chmod修改文件属性
    mtime即modify time,指文件内容被修改的时间。

    find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} ;

    场景脚本

    1.提取系统信息

    2.获取操作系统运行状态

    3.分析应用状态

    4.应用日志分析

    第一章vim编辑器设置(etc/vimrc或者是用户家目录下.vimrc)
    临时设置

    末行模式输入 命令   esc:

    永久设置(修改vimrc文件)

     可用命令:

    syntax off/on  语法高亮

    set number/nonumber 开启或关闭行号

    set autoindent  set cindent  自动缩进设置

    设置文件头(在vimrc中设置)

    autocmd BufNewFile *.py,*.sh, exec ":call SetTitle()"
    let $author_name = "mao"
    let $author_email = "dd@163.com"
    # 命名的函数首字母必须大写
    func SetTitle()    
    if &filetype == 'sh'
    	call setline(1,"####################################################")
    	call append(line("."), "# File Name:".expand("%"))
    	call append(line(".")+1, "# Author:".$author_name)
    	call append(line(".")+2, "# Mail:".$author_email)
    	call append(line(".")+3, "# Created time: ".strftime("%c"))
    	call append(line(".")+4, "# =================================================")
    	call append(line(".")+5, "#!/bin/bash")
    	call append(line(".")+6, "")
    else
    	call setline(1,"############################################################")
    	call append(line(".")+1,"# Author:".$author_name)
    	call append(line(".")+2,"# Mail;:".$author_email)
    	call append(line(".")+3,"# Created time:".strftime("%c"))
    	call append(line(".")+4,"# =================================================")
    	call append(line(".")+5, "#!/usr/bin/python")
    	call append(line(".")+6, "")
    endif
    autocmd BufNewFile * normal G
    endfunc
    

      

    高亮显示

    echo -e     终端颜色+ 显示内容+  结束后的颜色

    echo -e “e[1;30m]” "hello world" $(tput sgr0)

    echo -e "e[1;24m hello world e[1;0m"   开始颜色+内容+闭合颜色

    shell控制脚本

    monitor_man.sh控制脚本                                                       

    #!/bin/bash
    resettem=$(tput sgr0)
    declare -A ssharray
    i=0
    numbers=""
    
    for script_file in `ls -I "monitor_man.sh" ./`
    do 
    	echo -e "e[1;35m" "The Script:" ${i} '==>'${resettem} ${script_file}
    	numbners=[$i]=${script_file}
    	numbers="${numbers} | ${i}"
    	i=$((i+1))
    done
    while true
    do
    	read -p "please input a number [ ${numbers}]:" execshell
    	if [[ ! ${execshell} =~ ^[0-9]+ ]];then
    	exit o
    	fi
    	/bin/sh ./${ssharry[$execshell]}
    
    done
    

    system_monitor.sh                                             

    ############monitor########################
    clear
    if [[ $# -eq 0 ]]
    then
    #define variable reset_terminal
    reset_terminal=$(tput sgr0)
    
    
    #check os type
    os=$(uname -o)
    echo -e 'E[32m'"operating system type :"$reset_terminal $os
    
    #check os release versioin and name
    os_name=$(cat /etc/issue|grep -e "Server")
    echo -e 'E[32m'"os release versioin and name:"$reset_terminal $os_name
    
    #check architecture
    architecture=$(uname -m)
    echo -e 'E[32m'"architecture:"$reset_terminal $architecture
    
    #check kernel release
    kernelrelease=$(uname -r)
    echo -e 'E[32m'"kernel release:"$reset_terminal $kernelrelease
    
    #check hostname $HOSTNAME
    
    #check internal ip
    internal=$(hostname -I)
    echo -e 'E[32m'"internal ip:"$reset_terminal $internal
    
    #check external ip
    externalip=$(curl -s http://ipecho.net/plain)
    echo -e 'E[32m'"external ip:"$reset_terminal $externalip
    
    #check dns
    nameservers=$( cat /etc/resolv.conf |grep -E "<nameserver[ ]+"|awk '{print $NF}')
    echo -e 'E[32m'"dns:"$reset_terminal $nameservers
    
    #check if connected to internet or not
    ping -c 2 www.baidu.com &>/dev/null && echo "internet:connected" || echo internet:disconnected
    
    #check logged in users
    who>/tmp/who
    echo -e 'E[32m' "Logged in users" && cat /tmp/who
    rm -f /tmp/who
    
    ##############analyse###########################
    system_mem_usages=$(awk '/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/1024}' /proc/meminfo)
    apps_mem_usages=$(awk '/MemTotal/{total=$2}/MemFree/{free=$2}/^Cached/{cached=$2}/Buffers/{buffers=$2}END{print (total-free-cached-buffers)/1024}' /proc/meminfo)
    echo -e 'E[32m'"system memuserages:" $reset_terminal $system_mem_usages
    echo -e 'E[32m'"apps memuserages:" $reset_terminal $apps_mem_usages
    
    loadaverage=$(top -n 1 -b|grep "load average:"|awk '{print $10 $11 $12}')
    echo -e 'E[32m'"load averages:" $reset_terminal $loadaverage
    
    diskaverage=$(df -hP|grep -vE 'Filesystem|tmpfs'|awk '{print $1 " " $5}')
    echo -e 'E[32m'"disk averages:" $reset_terminal $diskaverage
    
    fi
    

      

             

    check_http_log.sh                                             

     系统日志:

    var/log/messages 系统日志

    var/log/secure 认证安全

    var/log/dmesg 系统启动相关

    应用服务日志:

    access.log nginx访问日志

    mysql.log mysql日志

    xferlog ftp服务

    程序脚本日志:

    开发语言:c java php

    框架:django mvc serlet

    脚本语言:shell python

    check_server.sh                                                   

    Resettem=$(tput sgr0)
    Check_Nginx_Server()
    Nginxserver='http://127.0.0.1'
    Mysql_Slave_server='12.2.1..2'
    {
    	Status_code=$(curl -m 5 -s -w %{http_code} ${Nginxserver} -0 /dev/null)
    	if [ $Status_cod -eq 000 -o $Status_cod -ge 500 ];then
    		echo -e 'E[32m'"check http server error! Response status code " $Resettem $Status_code
    	else
    		Http_content=$(curl -s ${Nginxserver})
    		echo -e 'E[32m' "check http sercer success" $Resettem $Http_content
    	fi
    }
    
    Check_Mysql_Server()
    {
    	nc -z -w2 ${Mysql_Slave_server} 3306 &>/dev/null
    	if [ $? -eq 0 ];then
    	mysql -u{$Mysql_User} -p${Mysql_Pass} -h${Mysql_Slave_Server} -e "show slave statusG "|grep "Slave_IO_Running"|awk '{if($2!="Yse"){print "Slave thread not running!";exit 1}}'
    		if [ $? -eq 0 ];then
    			mysql -u{$Mysql_User} -p${Mysql_Pass} -h${Mysql_Slave_Server} -e "show slave statusG "|grep "Second_Behind_Master"
    		fi
    	else
    		echo "connecte mysql server not successed"
    	fi
    }
    

      

    网络命令:ping nslookup(检查dns) nm-tool tracertroute telnet nc (检查路由网络端口) curl (http响应值是否成功) dig

    监控进程:ps netstat pgrep

    客户端工具:mysql ab mongo php jstack

    第三方工具包:nginxstatus nagios-libexec   

  • 相关阅读:
    Django进阶2
    Django进阶
    Django基础
    jQuery基本操作
    Dom编程
    JavaScript简介
    Python—sqlalchemy
    Python—RabbitMQ
    Python—redis
    Python—操作redis
  • 原文地址:https://www.cnblogs.com/maoxianfei/p/5877419.html
Copyright © 2020-2023  润新知