如何从Oracle获取数据以将其发送到statsd或直接发送到InfluxDB?

时间:2015-02-20 17:09:30

标签: oracle monitoring statsd influxdb

是否有解决方案从Oracle数据库获取一些数据以进一步将其发送到statsd或直接发送到InfluxDB?我有很多sql查询,我需要定期运行以获取一些计数器。我需要ORABBIX(zabbix)的替代方案,它与数据库有持久的连接,但是对于stastd / InfluxDB。我想在查询表中的计数器时减少与数据库的连接。谢谢。

1 个答案:

答案 0 :(得分:0)

由于存在cx_OracleInfluxDBClient,您可以编写一个简单的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语句只返回一行并且具有关联的涌入线(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侦听端口)。