ansible 推送腳本巡檢閩政通

image.png

image.png

推送的須要的機子多最好使用數據庫,機子少就寫死變量了,在被巡檢主機上新建一個普通用戶,擴展能夠用yml role配上數據庫就很方便了,懶的花時間折騰linux

這裏須要被巡檢主機免安裝就沒用數據庫記錄,yml要用copy模塊後用sh執行腳本,用script模塊會找不到執行路徑ios

還寫了一套用於監控告警的腳本,還在繼續寫。。。數據庫

image.png

#!/bin/bash
#QQ450433231
#2020-2-24 
#巡檢併發鏈接數 發送企業微信告警 按期巡檢通知

#定義時間
cur_time(){
	date "+%Y-%m-%d %H:%M:%S"
}
wx(){
#生成告警腳本 發送到企業微信
sh_name=$0
cat > $sh_name.json <<-EOF
curl '填寫微信機器人Webhook地址:' \
   -H 'Content-Type: application/json' \
   -d '
   {
        "msgtype": "text",
        "text": {
            "content": "$1",
            "mentioned_mobile_list":["$2"]
        }
   }'
EOF
#發送告警腳本,發送成功後清空腳本
sh $sh_name.json && echo "Send successfully" > $sh_name.json
}
#cpu使用
cpu_usage(){
	top -n 1 -d 1|awk '/%Cpu/{printf("%.2f%\n",(100-$8))}'
}


#獲取本機IP 
ip=`ifconfig eth0|awk '/inet /{print$2}'`
#監控端口 併發數告警閥值 獲取當前併發數
port=8303
#併發數告警閥值 
max_number=2000
#獲取當前併發數
number=`netstat -ant|grep -w "$ip:$port"|grep "ESTABLISHED"|wc -l`
mem=`free|awk '/Mem:/{printf("%.2f%\n",($2-$4)/$2*100)}'`
#磁盤讀:
read=`iostat|awk '/.da/{print$3 "kb/s"}'`
#磁盤寫:
write=`iostat|awk '/.da/{print$4 "kb/s"}'`
#IO利用率:
io=`iostat -x|awk '/.da/{print$14"%"}'`


ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/12.1.0/client_1; export ORACLE_HOME
PATH=.:${JAVA_HOME}/bin:${PATH}:$HOME/bin:$ORACLE_HOME/bin
export PATH

#數據庫用tnsping 由於有4臺rac的VIP,2個實例,這邊沒用數據庫只能用字典了,仍是懶
declare -A instances
instances=([192.168.1.1]="a" [192.168.1.2]="a" [192.168.56.195]="b" [192.168.56.196]="b")
for db_ip in $(echo ${!instances[*]})
do
        status=`tnsping $db_ip:1521/${instances[$db_ip]}|grep "OK"|wc -l`
        if [ "$status" -eq 0 ];then
                database="$db_ip:1521/${instances[$db_ip]} Error"
                break
        elif [ "$status" -eq 1 ];then
                database="Successfully"
        fi
done


wx "$(cur_time) 巡檢主機 \n$(hostname) $ip\n端口: $port 當前併發鏈接數: $number 預警閥值: $max_number \nCPU利用率: $(cpu_usage) 內存利用率: $mem \n磁盤讀: $read 磁盤寫: $write IO利用率: $io \n數據庫鏈接:$database"
---
- hosts: linux
  remote_user: centos
  gather_facts: false
  tasks: 
  -  name: copy script remote host
     copy: src=/root/connect_check dest=/home/centos/connect_check
  -  name: execution script
     command: sh /home/centos/connect_check
相關文章
相關標籤/搜索