天天看點

python編寫linux巡檢腳本_python結合shell腳本實作簡單的日常集中巡檢

一、環境配置

1.說明

下面的安裝過程适合開發、調試Python腳本,如果是直接使用的話沒有這麼複雜。為了防止由于版本問題導緻安裝問題,請到

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

1)安裝python

因為Python 本身不大,并且屬于解釋型語言,是以建議采用預設安裝,即安裝到C槽。需要注意的是到下圖步驟後點開辨別的下拉箭頭選擇第一個,否則還需要手動配置環境變量。

python編寫linux巡檢腳本_python結合shell腳本實作簡單的日常集中巡檢

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

python編寫linux巡檢腳本_python結合shell腳本實作簡單的日常集中巡檢

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使用者密碼

python編寫linux巡檢腳本_python結合shell腳本實作簡單的日常集中巡檢

2.資料庫巡檢檔案,代碼如下

#!/bin/bash

. $HOME/.profile

sqlplus-S "/ as sysdba" <

host echo"**************** check Instance status************"select status,database_statusfromv\$instance;

host echo"****************check database status************"select log_mode,open_modefromv\$database;

EOF

echo"*********************check alter log file ***********"dump=$(sqlplus -s "/ as sysdba" <

set head off

select valuefrom 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."

elseawk-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."

elseecho"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 ********************"vmstat2 3echo" "echo"********** load state ********************"w

echo" "

4.python腳本檔案

importparamikoimportdatetimeimportos##讀取的腳本功能 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 notipinfo :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 instdout: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.資料庫狀态

python編寫linux巡檢腳本_python結合shell腳本實作簡單的日常集中巡檢

2.伺服器狀态

python編寫linux巡檢腳本_python結合shell腳本實作簡單的日常集中巡檢