Skip to content

Commit

Permalink
Finished hotfix 1.0.13.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsaliieva committed Sep 12, 2023
2 parents 771bf49 + 025c4fa commit fa809ce
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 14 deletions.
6 changes: 4 additions & 2 deletions ZenPacks/zenoss/PostgreSQL/Table.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ class Table(DeviceComponent, ManagedEntity, CollectedOrModeledMixin):
event_key = "ComponentId"

def device(self):
return self.database().device()

db = self.database()
if db:
return db.device()
return None
27 changes: 20 additions & 7 deletions ZenPacks/zenoss/PostgreSQL/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
###########################################################################

import logging

log = logging.getLogger('zen.PostgreSQL')

import os
Expand All @@ -22,15 +23,24 @@
from Products.ZenRelations.RelSchema import ToManyCont, ToOne
from Products.ZenUtils.Utils import monkeypatch, zenPath


class ZenPack(ZenPackBase):
packZProperties = [
('zPostgreSQLPort', 5432, 'int'),
('zPostgreSQLUsername', 'postgres', 'string'),
('zPostgreSQLPassword', '', 'password'),
('zPostgreSQLUseSSL', False, 'boolean'),
('zPostgreSQLDefaultDB', 'postgres', 'string'),
('zPostgreSQLTableRegex', [], 'lines'),
]

packZProperties_data = {
'zPostgreSQLTableRegex': {
'description': "List of regular expressions (matched against table names) to control which tables are NOT modeled from All databases.",
'label': "Regex Table Filter",
'type': "lines" },
}

def install(self, app):
super(ZenPack, self).install(app)
self.patchPostgreSQLDriver()
Expand All @@ -40,10 +50,10 @@ def remove(self, app, leaveObjects=False):
if not leaveObjects:
# Remove our custom relations addition.
Device._relations = tuple(
[ x for x in Device._relations if x[0] != 'pgDatabases' ])
[x for x in Device._relations if x[0] != 'pgDatabases'])

self.updateExistingRelations(app.zport.dmd)

# Revert all pg8000 library patches
self.patchPostgreSQLDriver(revert=True)

Expand All @@ -57,28 +67,30 @@ def updateExistingRelations(self, dmd):
def patchPostgreSQLDriver(self, revert=False):
log.info('Patching pg8000 core library')
patch_dir = self.path('lib')

# Getting a list of all patches which will be applied
patches_list = [file for file in os.listdir(patch_dir) if file.endswith('.patch')]
cmd = "patch -p0 -d %s -i %s" if not revert else "patch -p0 -R -d %s -i %s"

cmd = "patch -p0 -d %s -i %s" if not revert else "patch -p0 -R -d %s -i %s"
for patch in patches_list:
os.system(cmd % (
os.path.join(patch_dir, 'pg8000'),
os.path.join(patch_dir, patch)
))


# Allow PostgreSQL databases to be related to any device.
Device._relations += (
('pgDatabases', ToManyCont(ToOne,
'ZenPacks.zenoss.PostgreSQL.Database.Database', 'server')),
'ZenPacks.zenoss.PostgreSQL.Database.Database', 'server')),
)

# We need to filter components by id instead of name.
EventManagerBase.ComponentIdWhere = (
"\"(device = '%s' and component = '%s')\""
" % (me.device().getDmdKey(), me.id)")


@monkeypatch('Products.ZenModel.Device.Device')
def setPostgreSQL(self, active=True):
if not active:
Expand All @@ -96,11 +108,12 @@ def setPostgreSQL(self, active=True):
if device.zCommandCommandTimeout < 180:
device.setZenProperty('zCommandCommandTimeout', 180)


@monkeypatch('Products.ZenModel.Device.Device')
def getPostgreSQL(self):
device = self.primaryAq()
if 'PostgreSQLServer' in device.zDeviceTemplates \
and device.zCommandCommandTimeout >= 180:
and device.zCommandCommandTimeout >= 180:
return True

return False
Expand Down
18 changes: 15 additions & 3 deletions ZenPacks/zenoss/PostgreSQL/modeler/plugins/zenoss/PostgreSQL.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
###########################################################################

import logging
import re

log = logging.getLogger('zen.PostgreSQL')

from Products.DataCollector.plugins.CollectorPlugin import PythonPlugin
from Products.DataCollector.plugins.DataMaps import ObjectMap, RelationshipMap
from Products.ZenUtils.Utils import prepId

from ZenPacks.zenoss.PostgreSQL.util import PgHelper
from ZenPacks.zenoss.PostgreSQL.util import PgHelper, exclude_patterns_list, is_suppressed


class PostgreSQL(PythonPlugin):
deviceProperties = PythonPlugin.deviceProperties + (
Expand All @@ -27,6 +30,7 @@ class PostgreSQL(PythonPlugin):
'zPostgreSQLPassword',
'zPostgreSQLUseSSL',
'zPostgreSQLDefaultDB',
'zPostgreSQLTableRegex',
)

def collect(self, device, unused):
Expand All @@ -39,6 +43,7 @@ def collect(self, device, unused):
device.zPostgreSQLDefaultDB)

results = {}
exclude_patterns = exclude_patterns_list(getattr(device, 'zPostgreSQLTableRegex', []))

log.info("Getting database list")
try:
Expand All @@ -55,7 +60,14 @@ def collect(self, device, unused):

log.info("Getting tables list for {0}".format(dbName))
try:
results['databases'][dbName]['tables'] = pg.getTablesInDatabase(dbName)
tables = pg.getTablesInDatabase(dbName)
if exclude_patterns:
for key in tables.keys():
if is_suppressed(key, exclude_patterns):
del tables[key]

results['databases'][dbName]['tables'] = tables

except Exception, ex:
log.warn("Error getting tables list for {0}: {1}".format(
dbName, ex))
Expand All @@ -68,7 +80,7 @@ def process(self, devices, results, unused):
if results is None:
return None

maps = [ self.objectMap(dict(setPostgreSQL=True)) ]
maps = [self.objectMap(dict(setPostgreSQL=True))]

databases = []
for dbName, dbDetail in results['databases'].items():
Expand Down
29 changes: 28 additions & 1 deletion ZenPacks/zenoss/PostgreSQL/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import math
import sys
import time

import re
import logging
LOG = logging.getLogger('zen.PostgreSQL.utils')

def addLocalLibPath():
"""
Expand Down Expand Up @@ -545,3 +547,28 @@ def getTableStatsForDatabase(self, db):
cursor.close()

return tableStats

def exclude_patterns_list(excludes):
exclude_patterns = []

for exclude in excludes:
exclude = exclude.strip()
if exclude == "" or exclude.startswith("#"):
continue

try:
exclude_patterns.append(re.compile(exclude))
except Exception:
LOG.warn("Invalid zPostgreSQLTableRegex value: '%s', this modeling filter will not be applied.", exclude)
continue

return exclude_patterns


def is_suppressed(item, exclude_patterns):

for exclude_pattern in exclude_patterns:
if exclude_pattern.search(item):
return True

return False
6 changes: 6 additions & 0 deletions docs/body.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ individual devices.
- *zPostgreSQLUsername* - Must be a superuser. Default: postgres
- *zPostgreSQLPassword* - Password for user. No default.
- *zPostgreSQLDefaultDB* - Default database. Default: postgres
- *zPostgreSQLTableRegex* - Filter tables of all databases with Regex. Default: ""

In addition to setting these properties you must add the ''zenoss.PostgreSQL''
modeler plugin to a device class or individual device. This modeler plugin will
Expand Down Expand Up @@ -164,6 +165,11 @@ zSnmpMonitorIgnore property to True and remodel.
Changes
---------------

1.0.13

* Make PostgreSQL table modeling optional (ZPS-8554)
* Tested with Zenoss Cloud, Zenoss 6.7.0 and Service Impact 5.6.1

1.0.12

* Resolved isuue with error in pg8000 library on Ubuntu OS (ZPS-7424)
Expand Down
4 changes: 4 additions & 0 deletions docs/releases.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Releases
--------

Version 1.0.13-<a rel="nofollow" class="external" href="https://delivery.zenoss.com/">Download</a>
Released on 2023/9/12
Compatible with Zenoss 6.x, Zenoss Cloud and Service Impact 5.6.1

Version 1.0.12-<a rel="nofollow" class="external" href="https://delivery.zenoss.com/">Download</a>
Released on 2021/1/14
Compatible with Zenoss 6.4.1 - 6.5.0, Zenoss Cloud and Service Impact 5.5.3
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# or saved. Do not modify them directly here.
# NB: PACKAGES is deprecated
NAME = "ZenPacks.zenoss.PostgreSQL"
VERSION = "1.1.0"
VERSION = "1.0.13"
AUTHOR = "Zenoss"
LICENSE = "GPLv2"
NAMESPACE_PACKAGES = ['ZenPacks', 'ZenPacks.zenoss']
Expand Down

0 comments on commit fa809ce

Please sign in to comment.