
因为使用了内部网络环境,所以使用了用户名密码身份验证,建议使用密钥身份验证

check.py

import paramiko
#获取ssh连接并执行shellcomand返回正确的结果
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
#查询内存情况
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('[主机地址]%s' % hostname)
print("[内存]")
elif line_number == 2:
print('程序使用:%s%s ' % (rs[2], 'M'))
print('系统挪用:%s%s ' % (rs[3], 'M'))
print("[swap]")
else:
print('总大小:%s%s ' % (rs[1], 'M'))
print('空闲内存:%s%s ' % (rs[3], 'M'))
line_number += 1
#检查硬盘情况
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('[主机地址]%s' % hostname)
print("[硬盘]")
else :
print('分区:%s ' % (rs[0]))
print('总大小:%s ' % (rs[1]))
print('空闲空间:%s ' % (rs[3]))
line_number += 1
#检查表空间
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/wy123456@%s/%s 
创建一个新文件并调用书面方法。每次执行main.py脚本时,检查结果都会记录在log.txt中。 log.txt如何报告错误,因为它是一个简单的测试,因此不会自动生成log.txt。如果您需要检测大量计算机,建议使用多线程

main.py
import check
import sys
import time
#主机列表
hosts=['172.16.1.20','172.16.1.21']
#端口号
port = 22
#用户名
username = 'rzfb'
#密码
password = 'password'
#需要检查的分区
part = '/dev/mapper/VolData-lvdata'
#oracle_sid
sid = "MSPROD"
#将print输出保存到文件中
output=sys.stdout
outputfile=open("log.txt","a")
sys.stdout=outputfile
type = sys.getfilesystemencoding()
#记录执行时间
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
#逐个主机检查运行情况
for hostname in hosts:
if hostname == '172.16.1.21':
#两个的sid不同所以在这里做了个判断
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")转载于:
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/dianqi/article-318572-1.html
过期食品不要买好么~~~