Crawl alarm information on Oracle EM CC 12C based on Python and push it to WeChat enterprise account

This article mainly introduces the use of Python crawler scripts to crawl down the alarm information of all targets and push them to the WeChat enterprise account application to achieve single-point to multi-target inspection. In this way, the workload of the DBA can be reduced.

If you don’t know about Oracle Enterprise Manager Cloud Control 12c, you can search for information to find out by yourself.

1. Set Incident Manager: All open incidents as the main page opened by the manager, as shown below:

 

Second, use Python to write crawlers

    Use python+selenium+geckodriver to crawl an alarm message and push it to the enterprise account.

    Note: You need to install the firefox browser (the Firefox version that comes with Linux by default is lower, please upgrade to the latest version by yourself), and it is written in python2.7. Download the code here:

    https://download.csdn.net/download/lanxuxml/10499863

 

    The following is the implementation code:

    

# -*- coding:utf-8 -*-
import sys
import urllib2
import json
import cx_Oracle
import os
from selenium import webdriver
from requests import Session
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.chrome.options import Options as ChromeOptions
from time import sleep
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

class Token(object):
    def __init__(self, corp_id, corp_secret):
        self.corp_id = corp_id
        self.corp_secret = corp_secret
        self.baseurl = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}'.format(self.corp_id, self.corp_secret)
        self.send_values = {}

    # Get AccessToken
    def get_token(self):
        conn = cx_Oracle.connect('FI_SAL/FI_SAL@*.*.*.*:1521/orcl')#本人企业号token定时爬取存在数据库里
        cur = conn.cursor()
        sql = "select * from WX_ACCESS_TOKEN"
        cur.execute(sql)
        result = cur.fetchall()
        for row in result:
            self.access_token = row[0]
        cur.close()
        conn.close()
        return self.access_token

    # Send Message
    def send_data(self, userid, message):
        self.message = message
        self.send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.access_token
        self.send_values = {
            "touser": userid,#接收人
            "msgtype": "text",
            "agentid": "55",#企业号对应的应用ID
            "text": {
                "content": message
            },
            "safe": "0"
        }
        send_data = json.dumps(self.send_values, ensure_ascii=False)
        send_request = urllib2.Request(self.send_url, send_data)
        response = urllib2.urlopen(send_request)
        # Get Response Message
        msg = response.read()
        print userid + ':' + msg
        return msg

corpid = '*****************************'
corpsecret = '**********************************'
#接收人 DBA加部门领导
toUser = '******|******'

req = Session()
req.headers.clear()
options = FirefoxOptions()
options.add_argument("--headless")
wd = webdriver.Firefox(firefox_options=options)
#wd = webdriver.Firefox()
logInUrl = 'https://登录页面的IP:登录页面的端口/em/faces/logon/core-uifwk-console-login'
wd.get(logInUrl)

wd.find_element_by_xpath('//*[@id="j_username::content"]').send_keys('***填写登录账号**')
wd.find_element_by_xpath('//*[@id="j_password::content"]').send_keys('***填写登录密码**')
sleep(2)
wd.find_element_by_xpath('//*[@id="login"]').click()
sleep(2)
wd.find_element_by_xpath('//*[@id="emT:lrmd1:iCustVw:4:custViewLink"]').click()
sleep(2)
i = 1
wxPostList = []
# start with 0 column
while i < 100 :
    j = 1
    while j < 100 :
        xPathColumn1 = '//*[@id="emT:lrmd1:tbmd1:pc2:t2::db"]/table[' + str(i) + ']/tbody/tr[' + str(j) + ']/td[2]/div/table/tbody/tr/td[1]'
        xPathColumn2 = '//*[@id="emT:lrmd1:tbmd1:pc2:t2::db"]/table[' + str(i) + ']/tbody/tr[' + str(j) + ']/td[2]/div/table/tbody/tr/td[2]'
        xPathColumn3 = '//*[@id="emT:lrmd1:tbmd1:pc2:t2::db"]/table[' + str(i) + ']/tbody/tr[' + str(j) + ']/td[2]/div/table/tbody/tr/td[3]'
        xPathColumn6 = '//*[@id="emT:lrmd1:tbmd1:pc2:t2::db"]/table[' + str(i) + ']/tbody/tr[' + str(j) + ']/td[2]/div/table/tbody/tr/td[6]'
        try :
            tableElementColumn1 = wd.find_element_by_xpath(xPathColumn1)
            tableElementColumn2 = wd.find_element_by_xpath(xPathColumn2)
            tableElementColumn3 = wd.find_element_by_xpath(xPathColumn3)
            tableElementColumn6 = wd.find_element_by_xpath(xPathColumn6)
            tableElementColumn1ImgTag = tableElementColumn1.find_element_by_tag_name("img")
            tableElementColumn1Content = tableElementColumn1ImgTag.get_attribute("title")
            tableElementColumn2Content = tableElementColumn2.get_attribute('textContent')
            tableElementColumn3Content = tableElementColumn3.get_attribute('textContent')
            tableElementColumn6Content = tableElementColumn6.get_attribute('textContent')

            tempMsg = \
                "Target: " + tableElementColumn3Content +  "\n" \
                + "Severity: " + tableElementColumn1Content + "\n" \
                + "Last Updated: \n" + tableElementColumn6Content[0:24]  + "\n" \
                + "Summary: " + tableElementColumn2Content + "\n\n"
            wxPostList.append(tempMsg)
            j = j + 1
        except :
            break
    #sleep(1)
    i = i  + 1
wd.quit()

i = 0
j = 0
wxMsg = '信息技术局技术开发部\n数据库巡检结果:\n\n'
if len(wxPostList) > 0 :
    for i in range(len(wxPostList)) :
        wxMsg = wxMsg + wxPostList[i]
        if j == 9 or i ==  (len(wxPostList) - 1):
            wxMsg = wxMsg.encode('utf-8')
            get_test = Token(corpid, corpsecret)
            get_test.get_token()
            msg = get_test.send_data(toUser, wxMsg)
            wxMsg = ''
            j = 0
        j = j + 1
else:
    wxMsg = wxMsg +  '**机房所有数据库无任何告警信息!'
    get_test = Token(corpid, corpsecret)
    get_test.get_token()
    msg = get_test.send_data(toUser, wxMsg)

 

Three, the following is the push result

 

Guess you like

Origin blog.csdn.net/lanxuxml/article/details/80812310