Python simple monitoring of memory/hard disk space/and Oracle tablespace usage

Source: Internet
Author: User
Tags ssh


The use of the intranet environment, so the use of user name password authentication, we recommend the use of key authentication
check.py




import paramiko
#Get SSH connection and execute shellcommand to return correct result
def doshell(hostname,port,username,password,shellcommand):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname,port,username,password)
stdin, stdout, stderr = ssh.exec_command(shellcommand)
result=stdout.readlines()
Ssh.close ()
return  result
#Query memory
def check_mem(hostname,port,username,password):
shellcommand = ‘free -m‘
result=doshell(hostname,port,username,password,shellcommand)
line_number=0
for line in result:
rs = line.split()
if line_number == 0:
Print ('[host address]% s'% hostname)
Print ("[memory]")
elif line_number == 2:
Print ('Program use:% s% s'% (RS [2], 'm'))
Print ('system Embezzlement:% s% s'% (RS [3], 'm'))
print("[swap]")
Else:
Print ('total size:% s% s'% (RS [1], 'm'))
Print ('free memory:% s% s'% (RS [3], 'm'))
line_number += 1
#Check the hard disk
def check_disk(hostname,port,username,password,part):
shellcommand = ‘df -h ‘+part
result = doshell(hostname, port, username, password, shellcommand)
line_number = 0
for line in result:
rs = line.split()
if line_number == 0:
#Print ('[host address]% s'% hostname)
Print ("[hard disk]")
Else:
Print ('partition:% s'% (RS [0]))
Print ('total size:% s'% (RS [1]))
Print ('free space:% s'% (RS [3]))
line_number += 1
#Check table space
def check_tablespace(hostname,port,username,password,sid):
shellcommand = "export ORACLE_HOME=/whmms/oracle/db11g/;export PATH=$ORACLE_HOME/bin:$PATH;export ORACLE_SID=MSPROD; sqlplus  wuyang/[email protected]%s/%s </tmp/tablespace.sql"%(hostname,sid)
result=doshell(hostname,port,username,password,shellcommand)
Print ('[tablespace]')
for line in result:
finded=line.find(‘DATA_TBS‘)
if finded != -1:
rs = line.split()
Print ('tablespace Name:% SG '% (RS [0]))
Print ('total size:% SG '% (RS [1]))
Print ('used:% SG '% (RS [2]))
Print ('idle:% SG '% (RS [3]))
Print ('usage rate:% SG '% (RS [4]))


Create a new file call written method, each execution main.py script will check the results recorded in Log.txt How does not exist Log.txt will be error, because it is a simple detection, so there is no write automatically generated Log.txt code if you need to detect a large number of machines recommend using multithreading


Main.py
Import check
Import sys
Import time
#Host list
hosts=[‘172.16.1.20‘,‘172.16.1.21‘]
Port number
Port = 22
User name
username = ‘rzfb‘
Password
password = ‘password‘
#Partition to check
part = ‘/dev/mapper/VolData-lvdata‘
#oracle_sid
sid = "MSPROD"
#Save print output to a file
output=sys.stdout
outputfile=open("log.txt","a")
sys.stdout=outputfile
type = sys.getfilesystemencoding()
#Record execution time
print(time.strftime(‘%Y-%m-%d %H:%M:%S‘, time.localtime(time.time())))
#Check the operation of each host
for hostname in hosts:
if hostname == ‘172.16.1.21‘:
#The SID of the two databases is different, so I make a judgment here
Sid= "WHMTST"
check.check_mem(hostname,port,username,password)
check.check_disk(hostname,port,username,password,part)
check.check_tablespace(hostname,port,username,password,sid)
print("\n\n") 


Python simple monitoring of memory/hard disk space/and Oracle tablespace usage


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.