天天看點

腳本

實作 nginx 日志切割,腳本執行時切割備份 nginx 日志,生成目前時間 <code>年-月-日:時:分:秒</code> 格式的目錄,并将切割的日志放到該目錄下,腳本不能影響 nginx 正常運作,不能丢失任何日志内容。

#!/bin/bash

logs_path=/data/bkce/logs/oldlogs

cur_logs_path=/data/bkce/logs/nginx

yesterday=$(date +%y-%m-%d-%h:%m:%s)

cp $cur_logs_path/access.log $logs_path/${yesterday}_access.log

crontab –e

59 23 bash /xxx.sh //每天23:59分執行切割

####################################################

修改 paas 的子工程 apigw、appengine、esb的worker數量為4個,login的 worker 數量為 6 個

調整調 paas 的 worker 數量數 /data/bkce/etc/uwsgi-open_paas.ini 裡的 workers 配置

/data/src/open_paas/support-files/templates/#etc#uwsgi-open_paas-apigw.ini

增加或修改參數workers = 4

./bkcec render paas

./bkcec sync paas

cat /data/bkce/etc/uwsgi-open_paas-apigw.ini

配置nginx監聽内網ip,并啟動nginx

ip=$(ifconfig eth0|awk 'nr==2{print $2}')

sed -i -r "/^[^#].listen.[.]/d; s/^[^#].(listen).*/ \1 $ip\;/g" /etc/nginx/nginx.conf

nginx -s stop

cat &gt;&gt; /data/bkce/etc/nginx/conhttp.conf &lt;&lt;eof

server {

listen 80;

server_name localhost;

allow 127.0.0.1; # 表示隻允許本機通路

deny all; # 表示禁止任何來源通路

location /nginx_status {

stub_status on;

}

eof

######

統計一分鐘/api/c/compapi/cc/get_app_list接口post請求出現的次數,并把指令和結果輸出到post_60.txt;

#####################################################

腳本支援參數兩個正整數,輸出 n-m 的累加值(n-m 代表倆個參數),若是參數不符合,輸出錯誤。

read -p "please input a number:" a

read -p 'please input the end num:' b

if expr $a + 1 &amp;&gt;/dev/null &amp;&amp; expr $b + 1 &amp;&gt;/dev/null

then

for i in <code>seq $a $b</code>

do

sum=$[$sum+$i]

done

echo $sum

else

echo “錯誤”

fi

寫一個腳本,實作判斷 10.0.6.0/23 網絡裡,目前線上使用者的 ip 有哪些?

nmap -sn 10.0.6.0/23

模拟磁盤檔案指令:

dd if=/dev/zero of=/tmp/exam/10gb.log bs=100mb count=100

touch -d "10 days ago" /tmp/exam/10gb.log

設定腳本上報,所有采集linux <code>time_wait、established、close_wait</code>的tcp狀态數量

echo loadavg1 $(netstat -anptl|grep established|wc -l)

echo loadavg2 $(netstat -anptl|grep time_wait|wc -l)

echo loadavg3 $(netstat -anptl|grep close_wait|wc -l)

建立腳本,采集機器的 cpu 使用率。

#!/bin/sh

#腳本功能描述:依據/proc/stat檔案擷取并計算cpu使用率

#cpu時間計算公式:cpu_time=user+system+nice+idle+iowait+irq+softirq

#cpu使用率計算公式:cpu_usage=[(user_2 +sys_2+nice_2) - (user_1 + sys_1+nice_1)]/(total_2 - total_1)*100

#預設時間間隔

time_interval=5

time=$(date "+%s")

last_cpu_info=$(cat /proc/stat | grep -w cpu | awk '{print $2,$3,$4,$5,$6,$7,$8}')

last_sys_idle=$(echo $last_cpu_info | awk '{print $4}')

last_user_busy=$(echo $last_cpu_info | awk '{print $1}')

last_sys_busy=$(echo $last_cpu_info | awk '{print $3}')

last_total_cpu_t=$(echo $last_cpu_info | awk '{print $1+$2+$3+$4+$5+$6+$7}')

last_cpu_usage=$(echo $last_cpu_info | awk '{print $1+$2+$3}')

sleep ${time_interval}

next_cpu_info=$(cat /proc/stat | grep -w cpu | awk '{print $2,$3,$4,$5,$6,$7,$8}')

next_sys_idle=$(echo $next_cpu_info | awk '{print $4}')

next_user_busy=$(echo $next_cpu_info | awk '{print $1}')

next_sys_busy=$(echo $next_cpu_info | awk '{print $3}')

next_total_cpu_t=$(echo $next_cpu_info | awk '{print $1+$2+$3+$4+$5+$6+$7}')

next_cpu_usage=$(echo $next_cpu_info | awk '{print $1+$2+$3}')

#系統空閑時間

system_idle=<code>echo ${next_sys_idle} ${last_sys_idle} | awk '{print $1-$2}'</code>

#系統使用時間

system_busy=<code>echo ${next_sys_busy} ${last_sys_busy} | awk '{print $1-$2}'</code>

#使用者使用時間

user_busy=<code>echo ${next_user_busy} ${last_user_busy} | awk '{print $1-$2}'</code>

#使用者+系統+nice時間

total_busy=<code>echo ${next_cpu_usage} ${last_cpu_usage} | awk '{print $1-$2}'</code>

#cpu總時間

total_time=<code>echo ${next_total_cpu_t} ${last_total_cpu_t} | awk '{print $1-$2}'</code>

#cpu總時間百分比

cpu_usage=<code>echo ${total_busy} ${total_time} | awk '{printf "%.2f", $1/$2*100}'</code>

#使用者時間百分比

#cpu_user_usage=<code>echo ${user_busy} ${total_time}|awk '{printf "%.2f", $1/$2*100}'</code>

#系統時間百分比

#cpu_sys_usage=<code>echo ${system_busy} ${total_time} |awk '{printf "%.2f", $1/$2*100}'</code>

echo "cpu_usage" ${cpu_usage}

打開 tcp 連接配接快速回收功能 提示:

echo "net.ipv4.tcp_tw_reuse = 1" &gt;&gt; /etc/sysctl.conf

修改主機名,如果機器 ip 為 10.0.6.33 ,則修改主機名為 agent-6-33

host_ip=<code>hostname -i|awk '{print $2}'</code>

if [ $host_ip=='10.0.6.33' ]

hostnamectl set-hostname agent-6-33

建立 logicsvr 程序監控,如果程序不存在自動拉起程序,并且寫日志到 <code>/opt/logicsvr_monitor.log</code>

procnum=<code>ps ax|grep logicsvr|grep -v grep|wc -l</code>

if [[ $procnum -eq 0 ]]; then

/opt/logicsvr/logicsvr -c /opt/logicsvr/logicsvr.conf

pid=<code>ps ax|grep logicsvr|grep -v grep|awk '{print $1}'</code>

echo "logicsvr程序不存在,程序号:$pid,重新開機時間:<code>date</code>" &gt;&gt; /opt/logicsvr_monitor.log

echo "logicsvr程序已存在,程序号:$pid,時間:<code>date</code>" &gt;&gt; /opt/logicsvr_monitor.log

nginx 元件監控- 監控插件下載下傳

nginx配置檔案配置:

listen 80 default_server;

listen [::]:80 default_server;

server_name localhost;

root /usr/share/nginx/html;

############################################################

文檔:nginx 日志常用操作.md

連結:http://note.youdao.com/noteshare?id=f15c4cb82db1051f9353f24f4ebded6b&amp;sub=f34600fee4c645d397c6df2698e0cbd7

過濾日志的$body_bytes_sent 字段最大值輸入到/opt/body_bytes_sent.txt

cat paas_inner_access.log |awk 'begin {max = 0} {if ($10+0 &gt; max+0) max=$10} end {print "max=", max}'

統計一分鐘/api/c/compapi/cc/get_app_list接口post請求出現的次數,并把指令和結果輸出到post_60.txt

awk '/post \/api.*get_app_list/{print $0}' paas_inner_access.log |egrep "2020\:11\:2[3-4]" &gt;post_60.txt

過濾每秒對應的post請求的次數。格式為 "時間 - 通路路徑 - post 請求次數 ”  導入檔案/opt/post_sum.txt,寫下解題思路或指令; (5分) --(截圖)

awk '{a[$7]++}end{for(i in a)print $4,$5,i,a[i]|"sort -k1 -nr"}' paas_inner_access.log  &gt;/opt/post_sum.txt

根據通路ip統計uv

awk '{print $1}' paas_inner_access.log |sort|uniq -c|wc -l

根據通路url統計pv

awk '{print $7}' paas_inner_access.log|wc -l

統計通路最多的前10個頁面

awk '{print $7}' paas_inner_access.log|sort|uniq -c|sort -nr -k1 |head

統計通路最頻繁的ip

awk '{print $1}' paas_inner_access.log |sort -n|uniq -c|awk 'nr==1'

根據時間段統計檢視日志

cat paas_inner_access.log |sed -n '/07\/dec\/2020:15/,/07\/dec\/2020:16/p'|more

統計當日的的pv和uv

grep "04/dec/2020"paas_web_access.log |awk '{print $1}'|sort|uniq -c|wc -l|awk '{print "uv:",$1}'

grep "04/dec/2020"paas_web_access.log |awk '{print $7}'|wc -l|awk '{print "pv:",$1}'

擷取最耗時的請求時間、url、耗時

請求時間:vim /data/bkce/etc/nginx/paas.conf,access_log /data/bkce/logs/nginx/paas_inner_access.log main; access_log /data/bkce/logs/nginx/paas_fqdn_access.log main;加main參數,重新開機nginx。

cat paas_inner_access.log |awk '{print $4,$7,$(nf-2)}'|sort -nr -k 3|head -n 1

擷取每分鐘的請求數量

awk -f: '{count[$2":"$3]++} end {for (minute in count) print minute, count[minute]}' paas_inner_access.log

檢視某個時間段之間的ip通路個數

cat paas_inner_access.log| sed -n '/04\/dec\/2020:10/,/04\/dec\/2020:11/p'|awk '{print $1}'|sort|uniq -c|wc -l

統計每個url通路内容的總大小

awk '{sum[$7]+=$10}end{for(url in sum){print url,sum[url]}}' paas_inner_access.log |sort -nr -k2

#############################################################################

 1) 設定mysql日志過期時間為3天,重新開機mysql該設定不會失效 (5分) --(截圖)

#臨時生效

show variables like 'expire_logs_days';

set global expire_logs_days = 3;

-- my.cnf    配置

expire_logs_days = 3;

2) bkdata/monitor 預設無日志輸出,請修改配置檔案輸出日志檔案/data/bkce/log/bkdata/monitor-exam.log (5分) --(截圖)

        3) /tmp/bkc.log日志使用logrotate進行日志分割,要求: (5分) --(截圖)

                - 保留3個日志檔案

                - 分割按日

                - 分割檔案名不帶日期

                - 啟用壓縮

                - 日志開始第一行顯示日期時間 

$ cat /etc/logrotate.d/bkc

/tmp/bkc.log {

    missingok

    notifempty

    daily

    rotate 3

    compress

    nodateext

    create 

    postrotate

        /usr/bin/date &gt;/tmp/bkc.log

    endscript

        4) logrotate -f 配置檔案可能會有錯誤,請解決錯誤 (5分) --(截圖)

錯誤資訊:

error: skipping "/tmp/bkc.log" because parent directory has insecure permissions (it's world writable or writable by group which is not "root") set "su" directive in config file to tell logrotate which user/group should be used for rotation.

解決方法:

create 

su root root   # su user group

l