-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Zabbix MySQL replication bridge local DB cache and refresh. (#175)
* Initial pass at Zabbix MySQL replication bridge for item metrics. [#MON-3138] * Zabbix MySQL replication: additional notes and begin item key lookup. [#MON-3138] * Zabbix replication: lookup itemid:key hostname map. [#MON-3138] * Cleanup using priv drop and get more tunables into config. [#MON-3138] * Support only resume (live) replication events (for now). [#MON-3144] * [#MON-3137] TODO list for zabbix_bridge. * Explicitly load collectors list, rather than yielding. [#MON-3137] * Use a local SQLite DB cache (instead of in-memory); reload periodically (900s). [#MON-3140] * Fixup Zabbix bridge SQLite cache. [#MON-3140] * Zabbix bridge cache executable. * Add internal metrics for log position, drift, and key miss. [#MON-3138] * Support in memory sqlite db refreshed periodically from filesystem DB. [#MON-3140] * Remove TODO list. * Index on id column for perf. [#MON-3140] * Rm sqlite3 error handling as it's part of the standard py libs.
- Loading branch information
1 parent
70fa5e2
commit ffb338f
Showing
4 changed files
with
118 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Copyright (C) 2014 The tcollector Authors. | ||
# | ||
# This program is free software: you can redistribute it and/or modify it | ||
# under the terms of the GNU Lesser General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or (at your | ||
# option) any later version. This program is distributed in the hope that it | ||
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | ||
# General Public License for more details. You should have received a copy | ||
# of the GNU Lesser General Public License along with this program. If not, | ||
# see <http://www.gnu.org/licenses/>. | ||
# | ||
# Dump all replication item/metric insert events from a zabbix mysql server | ||
# to a local sqlite cache (that can also be shared). | ||
# | ||
|
||
import os | ||
import re | ||
import sqlite3 | ||
import sys | ||
import time | ||
try: | ||
import pymysql | ||
except ImportError: | ||
pymysql = None # This is handled gracefully in main() | ||
|
||
from collectors.etc import zabbix_bridge_conf | ||
from collectors.lib import utils | ||
|
||
|
||
def main(): | ||
utils.drop_privileges() | ||
if pymysql is None: | ||
utils.err("error: Python module `pymysql' is missing") | ||
return 1 | ||
settings = zabbix_bridge_conf.get_settings() | ||
|
||
db_filename = settings['sqlitedb'] | ||
db_is_new = not os.path.exists(db_filename) | ||
dbcache = sqlite3.connect(db_filename) | ||
|
||
if db_is_new: | ||
utils.err("Zabbix bridge SQLite DB file does not exist; creating: %s" % (db_filename)) | ||
cachecur = dbcache.cursor() | ||
cachecur.execute('''CREATE TABLE zabbix_cache | ||
(id integer, key text, host text, proxy text)''') | ||
dbcache.commit() | ||
else: | ||
utils.err("Zabbix bridge SQLite DB exists @ %s" % (db_filename)) | ||
|
||
|
||
dbzbx = pymysql.connect(**settings['mysql']) | ||
zbxcur = dbzbx.cursor() | ||
zbxcur.execute("SELECT i.itemid, i.key_, h.host, h2.host AS proxy FROM items i JOIN hosts h ON i.hostid=h.hostid LEFT JOIN hosts h2 ON h2.hostid=h.proxy_hostid") | ||
# Translation of item key_ | ||
# Note: http://opentsdb.net/docs/build/html/user_guide/writing.html#metrics-and-tags | ||
disallow = re.compile(settings['disallow']) | ||
cachecur = dbcache.cursor() | ||
print('tcollector.zabbix_bridge.deleterows %d %s' % | ||
(int(time.time()), cachecur.execute('DELETE FROM zabbix_cache').rowcount)) | ||
rowcount = 0 | ||
for row in zbxcur: | ||
cachecur.execute('''INSERT INTO zabbix_cache(id, key, host, proxy) VALUES (?,?,?,?)''', | ||
(row[0], re.sub(disallow, '_', row[1]), re.sub(disallow, '_', row[2]), row[3])) | ||
rowcount += 1 | ||
|
||
print('tcollector.zabbix_bridge.rows %d %s' % (int(time.time()), rowcount)) | ||
zbxcur.close() | ||
dbcache.commit() | ||
|
||
dbzbx.close() | ||
dbcache.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.stdin.close() | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters