温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用paramiko监控Oracle alert日志

发布时间:2021-11-09 14:55:54 来源:亿速云 阅读:162 作者:小新 栏目:关系型数据库

小编给大家分享一下如何使用paramiko监控Oracle alert日志,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

环境设置

Linux系统为 Centos 6.8

Python环境为 Python 3.6

连接Unix类服务器模块: paramiko

使用的命令

我们使用grep命令来判断alert日志中是否有ORA Error及Checkpoint报警

监控alert日志程序

如下程序使用paramiko连接linux/Unix服务器后查看alert日志查看报警关键字,当发现错误时输入结果

我们可以for循环批量监控并发送结果到指定的邮箱,这部分就让大家自己写了,可参照我以前的文章

这样即可做到自动化监控了,可大幅减少DBA日常的工作

自动化监控多个Oracle表空间

使用Python发送邮件

程序名称为:checkoraclelog.py

#!/usr/bin/python
#coding=utf8
import paramiko
def oraclelog(ssh,path):
        alert_log=[]
        command='grep  -E \'ORA-|Checkpoint|Error\' '+path
        stdin,stdout,stderr=ssh.exec_command(command)
        err=stderr.readlines()
        if len(err) != 0:
            print (err)
            return False        else:
            stdout_content=stdout.readlines()
        if len(stdout_content)!=0:
            result='\n'.join(stdout_content)
            result= 'Oralce log on '+hostname+ ' have errors\n'+'The log path is '+path+'\n'+result
            alert_log.append(result)
            return alert_log        else:
            return 'noerror'if __name__ == '__main__':
    hostname='10.60.14.60'
    username='root'
    password='password'
    try:
        #使用SSHClient方法定义ssh变量
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        #连接目标服务器
        ssh.connect(hostname=hostname,port=22,username=username,password=password)
        path='/oracle/NP1/saptrace/background/alert_NP1.log'
        alert_log=oraclelog(ssh,path)
        ssh.close()
        if alert_log:            if alert_log !='noerror':                for i in alert_log:
                    print (i)            else:
                print ('There is no ORA- error on '+hostname)
    except Exception as e:
        print (hostname+' '+str(e))

验证结果

如何使用paramiko监控Oracle alert日志

如何使用paramiko监控Oracle alert日志

正常情况下如果有ORA等错误会输出结果

以上是“如何使用paramiko监控Oracle alert日志”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI