由于有cx_Oracle 和InfluxDBClient,您可以编写一个简单的 Python 脚本来打开所有必要的数据库连接,准备一堆 SQL 语句并重复执行它们并将它们输入到 influxdb 实例中。
基本上是这样的:
from influxdb import InfluxDBClient
import cx_Oracle
ic = InfluxDBClient('vigilante.example.org', 8086, 'collector', 'pw', 'db'
db_dict = {}
for sid in sids:
db_dict[sid] = cx_Oracle.connect(ORACLE_USER, ORACLE_PASS, sid)
cursor_dict = {}
for (name, c) in config.items():
cursor = db_dict[c['sid']].cursor()
cursor.prepare(c['config'])
cursor_dict[name, c['sid']] = (cursor, c['line'])
def collect(cursor_dict):
vs = []
for ((name, sid), (cursor, line)) in cursor_dict:
rows.execute(cursosr.fetchall())
if rows:
vs.append(line.format(sid, *rows[0]))
if vs:
ic.write_points(vs, protocol='line')
while True:
collect(cursor_dict)
time.sleep(sleep_for_some_dynamically_computed_time)
在该示例中,每个 SQL 语句都只返回一行,并有一个关联的 influxdb 行 (c['line']),如下所示:
measurement,db={} col_name1={},col_name2={},col_name3={}
您还可以在该行添加时间戳 - 如果省略,influxdb 使用当前时间。请注意,此脚本运行时间很长 - 并使用准备好的语句来避免浪费数据库资源。
或者,如果您已经有一个 collectd 正在运行:collectd 有一个 oracle plugin。因此,您可以将现有查询与该插件和 configure collectd to forward 数据点 to an influxdb 实例一起使用(通过 UDP,即在 influxdb 配置中启用 UDP 侦听端口)。