python結合shell腳本實現簡單的日常集中巡檢


一、環境配置

   1.說明

           下面的安裝過程適合開發、調試Python腳本,如果是直接使用的話沒有這么復雜。為了防止由於版本問題導致安裝問題,請到http://pan.baidu.com/s/1nt1NKSh  下載所需軟件(本人上傳,鏈接不會失效)。

   2.安裝過程(如果不是下載的安裝包,除了python之外,其他都可以采用pip安裝,更方便)

     1)安裝python

             因為Python 本身不大,並且屬於解釋型語言,所以建議采用默認安裝,即安裝到C盤。 需要注意的是到下圖步驟后點開標識的下拉箭頭選擇第一個,否則還需要手動配置環境變量。

    

 

    2)安裝pycrypto,過程如圖所示

     

      3)安裝ecdsa,步驟同上

      4)安裝paramiko,步驟同上

      5)修改文件        

              Python安裝目錄下:/Crypto/Random/OSRNG/nt.py 文件中找到

              import winrandom

              改成

              from Crypto.Random.OSRNG import winrandom

二、所需文件及其內容

    1.IP地址及口令文件

       創建一個名字為ip.txt的文本文件(只要跟python腳本中文件名字一致即可),內容格式:

       IP,username,password  

       參數說明:

       IP:遠程服務器的IP地址

       username:oracle用戶,即能登錄sqlplus的用戶

       password:oracle用戶密碼

       

   2.數據庫巡檢文件,代碼如下

#!/bin/bash
. $HOME/.profile 
sqlplus -S  "/ as sysdba" <<EOF
host echo "**************** check Instance status************"
select status,database_status from v\$instance;
host echo "****************check database status************"
select log_mode,open_mode from v\$database;
EOF
echo "*********************check alter log file ***********"
dump=$(sqlplus -s "/ as sysdba" <<EOF
set head off
select value from v\$parameter where name='background_dump_dest';
exit
EOF)
startline=$(cat $dump/alert*.log |grep -n "$(date +'%a %b %e')"|head -1| awk -F: '{print $1}')
echo "Contains the ORA- line in the file:"
   if [ "$startline" = "" ]
      then
          echo "There is no information today."
      else
          awk -v line=$startline 'BEGIN{ORS="\n"}NR>line{print $0}' $dump/alert*.log |grep ORA- |wc -l
      fi
echo

echo "****************check listening status****************"
##startline=$(lsnrctl status|grep -n "Listening Endpoints Summary"| head -1 |awk -F: '{print $1}')
##lsnrctl status |awk -v line=$startline 'BEGIN{ORS="\n"}NR>=line{print $0}'
line=$(lsnrctl status |grep -n "The command completed successfully"| head -1 |awk -F: '{print $1}')
##echo $line
    if [ "$line" = "" ]
      then
          echo "can not get Listening info."
      else
          echo "Listening normal"
      fi
echo
echo "****************check disk space (above 90%)****************"
df |sed -e 's/%//'|awk '$5>90{print $0}' 

    3.數據庫服務器狀態文件

echo "********** memory ***********************"
free
echo " "
echo "********** disk space *******************"
df -Th
echo " "
echo "********** vm state ********************"
vmstat 2 3
echo " "
echo "********** load state ********************"
w
echo " "

  4.python腳本文件

import paramiko
import datetime
import os
##讀取的腳本功能 1:巡檢內容  2:負載狀態
func=1
##讀取當前路徑
base_dir=os.getcwd()
##命令開始執行時間
starttime=datetime.datetime.now()
print(" -------------------------------------------------------------")
print("|                                                             |")
print("  startime:        ",starttime)
print("|                                                             |")
print(" -------------------------------------------------------------")
##注意路徑前面的r,否則有些文件會當作轉義字符處理
##讀取命令腳本
if func==1:
    cmd_filepath=base_dir+r"\xunjian.txt"
else :
    cmd_filepath=base_dir+r"\fuzai.txt"
cmd_file=open(cmd_filepath,"r")
cmd=cmd_file.read()
##讀取IP地址列表
ip_filepath=base_dir+r"\ip.txt"
ip_file=open(ip_filepath,"r")
while 1:    
    ipinfo=ip_file.readline()
    if not ipinfo :
        break
    else :
    ##讀取IP,用戶名,密碼
         infos=ipinfo.split(',')   
         host=infos[0]
         username=infos[1]
         pwd=infos[2].strip()
         pwd=pwd.strip('\n')
    ##遠程連接服務器
         client = paramiko.SSHClient()  
         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
         client.connect(host, 22, username, pwd)
         stdin, stdout, stderr = client.exec_command(cmd)
         print(" -------------------------------------------------------------")
         print("|                                                             |")
         print("                      ",host)
         print("|                                                             |")
         print(" -------------------------------------------------------------")
         for line in stdout:  
             print(line.strip('\n'))  
         client.close()
         print("")
print('check complete................................')
##命令執行完成時間
endtime=datetime.datetime.now()
print(" -------------------------------------------------------------")
print("|                                                             |")
print("    endtime:      ",endtime)
print("|                                                             |")
print(" -------------------------------------------------------------")
print(" -------------------------------------------------------------")
print("|                                                             |")
print("  startime:       ",starttime)
print("  endtime:        ",endtime)
print("  cost:           ",endtime-starttime)
print("|                                                             |")
print(" -------------------------------------------------------------")

三、效果展示

1.數據庫狀態

2.服務器狀態

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM