我有几个 Oracle 数据库,我的内部应用程序正在其中运行。这些应用程序同时使用 dba_jobs
和 dba_scheduler_jobs
.
我要写监控函数:check_my_jobs
Nagios 会定期调用它来检查我的工作是否一切正常。 (他们在跑吗?它坏了吗?next_run_date 延迟了吗?等等)
解决方案:由于我必须监视不同数据库上的作业,因此有两种实现解决方案的方法:
优点:集中功能,易于维护。
缺点:我必须使用数据库链接进行检查。
优点:我不必使用数据库链接
缺点:每个数据库上都有重复的监控代码
哪种解决方案更好?
最佳答案
我会选择选项 #1 - 使用数据库链接的集中式功能。
数据库链接名声不好。主要原因之一是太多人使用公共(public)数据库链接,任何连接到数据库的人都可以使用该链接。这显然是一场安全噩梦,但这不是默认设置,而且很容易避免这个陷阱。
数据库链接的其他一些问题:
DBMS_UTILITY.EXEC_DDL_STATEMENT@LINK_NAME('create ...');
确保只在那里使用 DDL。其他类型的命令将静默失败。 declare
v_result varchar2(4000);
begin
--Loop through a configuration table of links.
for links in
(
select database_name, db_link
from dbs_to_monitor
left join user_db_links
on dbs_to_monitor.database_name = user_db_links.db_link
order by database_name
) loop
--Run the query if the link exists.
if links.db_link is not null then
begin
--Note the user of REPLACE and the alternative quoting mechanism, q'[...]';
--This looks a bit silly with this small example, but in a real-life query
--it avoids concatenation hell and makes the query much easier to read.
execute immediate replace(q'[
select dummy from dual@#DB_LINK#
]',
'#DB_LINK#', links.db_link)
into v_result;
dbms_output.put_line('Result: '||v_result);
--Catch errors if the links are broken or some other error happens.
exception when others then
dbms_output.put_line('Error with '||links.db_link||': '||sqlerrm);
end;
--Error if the link was not created.
--You will have to run:
--create database link LINK_NAME connect to USERNAME identified by "PASSWORD" using 'TNS_STRING';
else
dbms_output.put_line('ERROR - '||links.db_link||' does not exist!');
end if;
end loop;
end;
/
尽管如此,数据库链接还是很棒的,因为您可以在一个数据库上使用 PL/SQL 完成所有操作。您可以使用单一语言创建无代理监控解决方案,而不必担心安装和修复代理。
例如,我构建了开源程序 Method5使用数据库链接做所有事情。安装该程序后,您可以从数百个数据库中收集结果,就像运行
select * from table(m5('select * from dba_jobs'));
一样简单。 .该程序可能对您的场景来说是多余的,但它表明数据库链接是完整监控解决方案所需的全部。
关于oracle - 设计 Oracle 数据库上的作业监控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48663740/