Skip to content

Commit

Permalink
PRO TIP: Migrate inventory plugins
Browse files Browse the repository at this point in the history
In order to migrate an inventory plugin to the new API the following
steps must be taken:

 * Move the file to the new location (or add the content to an existing
   file, as in this case).

 * Adjust the arguments of the inventory function (refer to the sphinx
   documentation or the doc-string of the `register.inventory_plugin`
   for details).

 * Reorganize the function to be a generator of TableRows. Note that
   the SID is the only `key_column`. If all of the key_columns of two
   TableRows contain the same values, the backend will assume they are
   in fact refering to the same 'thing', and merge them into the same
   row.
   This logic is used when computing the inventory history or merging
   information gathered from configured 'retention intervals'.

Change-Id: Ic24e6b454430377b1626c3510842922e725f60c7
  • Loading branch information
mo-ki committed Aug 6, 2021
1 parent 5e4ce18 commit e926cc5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 145 deletions.
51 changes: 49 additions & 2 deletions cmk/base/plugins/agent_based/oracle_instance.py
Expand Up @@ -6,8 +6,8 @@

from typing import Dict, Optional, Sequence, Mapping, Union

from .agent_based_api.v1.type_defs import StringTable
from .agent_based_api.v1 import register
from .agent_based_api.v1.type_defs import InventoryResult, StringTable
from .agent_based_api.v1 import register, TableRow

Instance = Mapping[str, Union[None, str, bool]]

Expand Down Expand Up @@ -116,3 +116,50 @@ def parse_oracle_instance(string_table: StringTable) -> Section:
name="oracle_instance",
parse_function=parse_oracle_instance,
)


def _parse_raw_db_creation_time(raw_str) -> Optional[str]:
""" "%d%m%Y%H%M%S" => "%Y-%m-%d %H:%M"
>>> _parse_raw_db_creation_time("080220151025")
'2015-02-08 10:25'
"""

if not (isinstance(raw_str, str) and raw_str.isdigit() and len(raw_str) == 12):
return None

return f"{raw_str[4:8]}-{raw_str[2:4]}-{raw_str[:2]} {raw_str[8:10]}:{raw_str[10:]}"


def inventory_oracle_instance(section: Section) -> InventoryResult:
path = ["software", "applications", "oracle", "instance"]

for item_data in sorted(section.values(), key=lambda v: str(v["sid"])):
if item_data['invalid_data']:
continue

try:
status_columns = {"db_uptime": int(item_data['up_seconds'])} # type: ignore[arg-type]
except (TypeError, ValueError):
status_columns = {}

yield TableRow(
path=path,
key_columns={"sid": item_data['sid']},
inventory_columns={
"pname": item_data['pname'],
"version": item_data['version'],
"openmode": item_data['openmode'],
"logmode": item_data['log_mode'],
"logins": item_data['logins'],
"db_creation_time": _parse_raw_db_creation_time(item_data['db_creation_time']),
},
status_columns=status_columns,
)


register.inventory_plugin(
name="oracle_instance",
inventory_function=inventory_oracle_instance,
)
56 changes: 0 additions & 56 deletions inventory/oracle_instance

This file was deleted.

99 changes: 12 additions & 87 deletions tests/unit/cmk/base/plugins/agent_based/test_inv_oracle_instance.py
Expand Up @@ -21,26 +21,16 @@
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": None,
},
inventory_columns={
"pname": None,
"version": "VERSION",
"openmode": "OPENMODE",
"logmode": None,
"logins": "LOGINS",
"db_creation_time": None,
},
),
TableRow(
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": None,
},
status_columns={
"db_uptime": None,
},
),
]),
([
'SID',
Expand All @@ -59,26 +49,16 @@
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": None,
},
inventory_columns={
"pname": None,
"version": "VERSION",
"openmode": "OPENMODE",
"logmode": "LOGMODE",
"logins": "LOGINS",
"db_creation_time": None,
},
),
TableRow(
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": None,
},
status_columns={
"db_uptime": None,
},
),
]),
([
'SID',
Expand All @@ -97,22 +77,15 @@
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": None,
},
inventory_columns={
"pname": None,
"version": "VERSION",
"openmode": "OPENMODE",
"logmode": "LOGMODE",
"logins": "LOGINS",
"db_creation_time": None,
},
),
TableRow(
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": None,
},
status_columns={
"db_uptime": 123,
},
Expand All @@ -138,26 +111,16 @@
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": None,
},
inventory_columns={
"pname": None,
"version": "VERSION",
"openmode": "OPENMODE",
"logmode": 'LOGMODE',
"logins": "LOGINS",
"db_creation_time": "2015-02-08 10:25",
},
),
TableRow(
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": None,
},
status_columns={
"db_uptime": None,
},
),
],
),
(
Expand All @@ -180,22 +143,15 @@
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": None,
},
inventory_columns={
"pname": None,
"version": "VERSION",
"openmode": "OPENMODE",
"logmode": 'LOGMODE',
"logins": "LOGINS",
"db_creation_time": "2015-02-08 10:25",
},
),
TableRow(
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": None,
},
status_columns={
"db_uptime": 123,
},
Expand Down Expand Up @@ -232,26 +188,16 @@
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": "PNAME",
},
inventory_columns={
"pname": "PNAME",
"version": "VERSION",
"openmode": "OPENMODE",
"logmode": 'LOGMODE',
"logins": "LOGINS",
"db_creation_time": None,
},
),
TableRow(
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": "PNAME",
},
status_columns={
"db_uptime": None,
},
),
],
),
(
Expand Down Expand Up @@ -284,22 +230,15 @@
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": "PNAME",
},
inventory_columns={
"pname": "PNAME",
"version": "VERSION",
"openmode": "OPENMODE",
"logmode": 'LOGMODE',
"logins": "LOGINS",
"db_creation_time": "2015-02-08 10:25",
},
),
TableRow(
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": "PNAME",
},
status_columns={
"db_uptime": 123,
},
Expand Down Expand Up @@ -375,46 +314,32 @@ def test_inv_oracle_instance_multiline(fix_register):
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": "",
},
inventory_columns={
"pname": "",
"version": "VERSION",
"openmode": "OPENMODE",
"logmode": 'LOGMODE',
"logins": "LOGINS",
"db_creation_time": "2015-02-08 10:25",
},
status_columns={
"db_uptime": 123,
},
),
TableRow(
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": "PNAME",
},
inventory_columns={
"pname": "PNAME",
"version": "VERSION",
"openmode": "POPENMODE",
"logmode": 'LOGMODE',
"logins": "ALLOWED",
"db_creation_time": "2015-02-08 10:26",
},
),
TableRow(
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": "",
},
status_columns={
"db_uptime": 123,
},
),
TableRow(
path=['software', 'applications', 'oracle', 'instance'],
key_columns={
"sid": "SID",
"pname": "PNAME",
},
status_columns={
"db_uptime": 456,
},
Expand Down

0 comments on commit e926cc5

Please sign in to comment.