本文主要介绍通过Python爬虫脚本,将所有目标的告警信息爬下来,并推送到微信企业号应用实现单点对多目标的巡检。籍此以减少DBA的工作量。
如有不了解Oracle Enterprise Manager Cloud Control 12c的,可搜索信息自行了解。
一、设置Incident Manager: All open incidents为管理器打开的主页面,如下图所示:
二、利用Python编写爬虫
利用python+selenium+geckodriver爬取一中告警信息推送到企业号。
注意:需要安装firefox浏览器(linux默认自带的firefox版本较低的请自行升级到最新版),同时是用python2.7写的。代码在此下载:
以下是实现代码:
# -*- 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)
三、以下是推送结果