zabgrab.services.migrate

Moving a file’s items to new IDs or servers, and retiring items (migrate).

When a Zabbix server is replaced without its history, the same items come back on the new server under new IDs. A file identifies items by server and ID, so without help its data from before and after would look like different items. A migration rewrites the file’s items to their new server and IDs, recording where each came from, so the file reads as one series per item again. Items that can’t be found any more are retired: kept, but no longer updated.

The plan is worked out first (plan_migration()), shown and confirmed, then applied to the groups (apply_migration()), which the caller writes back.

Module Attributes

MATCHED

looked up by host and key, or given by the user.

Functions

apply_migration(groups, plan, *, at, ...)

Apply a migration to a file's groups.

find_items(groups, itemids, option)

Find the items a file holds with the given IDs.

look_up(server, connect, *, entries, itemids)

Look up what a migration needs on its target server.

match_items(entries, client)

Look items up on a server by their host's technical name and their key.

parse_mapping(text)

Parse --map OLD=NEW.

plan_migration(groups, *, target, matched, given)

Work out a migration: which items move, which retire, which come back.

read_mapping_file(text, name)

Read a file of item ID mappings, one per line.

server_names(groups)

Get the config names a file's servers were fetched with.

specs_by_id(specs, itemids)

List the item IDs that item specs select directly, among some IDs.

Classes

MigrationPlan(target[, sources, moves, ...])

What a migration will change.

Move(old, old_server_name, old_hostid, ...)

An item moving to a new ID (and perhaps a new server).

Retirement(item, label, reason)

An item to retire.

zabgrab.services.migrate.MATCHED = 'matched'

looked up by host and key, or given by the user.

Type:

How a mapping was found

class zabgrab.services.migrate.Move(old, old_server_name, old_hostid, label, server, item, how)[source]

Bases: object

An item moving to a new ID (and perhaps a new server).

Parameters:
old

The item’s server URL and ID in the file.

Type:

Identity

old_server_name

Its server’s name in the config when it was fetched.

Type:

str or None

old_hostid

Its host’s ID on the old server.

Type:

int or None

label

host: name, for messages.

Type:

str

server

The server it moves to.

Type:

Server

item

The item on that server.

Type:

Item

how

MATCHED or GIVEN.

Type:

str

property new: tuple[str, int]

The item’s new server URL and ID.

class zabgrab.services.migrate.Retirement(item, label, reason)[source]

Bases: object

An item to retire.

Parameters:
item

The item’s server URL and ID.

Type:

Identity

label

host: name, for messages.

Type:

str

reason

Why, e.g. "not found on 'labnew'".

Type:

str

class zabgrab.services.migrate.MigrationPlan(target, sources=<factory>, moves=<factory>, retirements=<factory>, unretired=<factory>, unchanged=<factory>, names=<factory>)[source]

Bases: object

What a migration will change.

Parameters:
target

The server the items move to (--server), if any.

Type:

Server or None

sources

The servers the moving items come from.

Type:

list of Server

moves

The items moving to new IDs or servers.

Type:

list of Move

retirements

The items to retire.

Type:

list of Retirement

unretired

The items to bring back, with their labels.

Type:

list of tuple of Identity and str

unchanged

Items found on the target server under the same ID (nothing to change).

Type:

list of tuple of Identity and str

names

The file’s servers’ names in the config, by URL (see server_names()).

Type:

dict of str to str or None

property changes: bool

Whether the plan changes anything.

zabgrab.services.migrate.parse_mapping(text)[source]

Parse --map OLD=NEW.

Parameters:

text (str) – The option’s value.

Returns:

tuple of int – The old and new item IDs.

Raises:

ValueError – If it isn’t two item IDs separated by =.

Return type:

tuple[int, int]

Examples

>>> parse_mapping("41207=90311")
(41207, 90311)
zabgrab.services.migrate.read_mapping_file(text, name)[source]

Read a file of item ID mappings, one per line.

Each line holds the old and new item IDs, separated by a comma, a tab or spaces. Blank lines and lines starting with # are ignored, as is a first line of column headings.

Parameters:
  • text (str) – The file’s contents.

  • name (str) – The file’s name, for errors.

Returns:

list of tuple of int – The old and new IDs, in order.

Raises:

ValueError – If a line isn’t two item IDs.

Return type:

list[tuple[int, int]]

Examples

>>> text = "old,new\n# moved in May\n41207,90311\n41208 90312\n"
>>> read_mapping_file(text, "mapping.csv")
[(41207, 90311), (41208, 90312)]
zabgrab.services.migrate.find_items(groups, itemids, option)[source]

Find the items a file holds with the given IDs.

Parameters:
  • groups (sequence of Group) – The file’s groups.

  • itemids (iterable of int) – Item IDs given on the command line.

  • option (str) – The option they were given with, for errors.

Returns:

dict of int to Identity – Each ID’s item.

Raises:

ZabgrabError – If the file has no item with an ID, or items with it on several servers.

Return type:

dict[int, tuple[str, int]]

async zabgrab.services.migrate.match_items(entries, client)[source]

Look items up on a server by their host’s technical name and their key.

Zabbix keeps item keys unique within a host, so a host and key find at most one item.

Parameters:
  • entries (sequence of mapping) – The items’ metadata entries (with host and key).

  • client (ZabbixClient) – A connection to the server to look on.

Returns:

dict of Identity to Item – The items found, by the identity of the entry they match.

Return type:

dict[tuple[str, int], Item]

async zabgrab.services.migrate.look_up(server, connect, *, entries, itemids)[source]

Look up what a migration needs on its target server.

Parameters:
  • server (Server) – The target server.

  • connect (callable) – Returns a logged-in client for a server.

  • entries (sequence of mapping) – Items to match by host and key (see match_items()); none for no matching.

  • itemids (iterable of int) – New item IDs given by the user, which must exist on the server.

Returns:

tuple of dict – The matched items by the identity of their entries, and the given items by ID.

Raises:

ZabgrabError – If a given item ID isn’t on the server.

Return type:

tuple[dict[tuple[str, int], Item], dict[int, Item]]

zabgrab.services.migrate.plan_migration(groups, *, target, matched, given, retire=(), unretire=())[source]

Work out a migration: which items move, which retire, which come back.

With a target server, every active item (one update would fetch, or one being brought back) moves to the item given for it (--map), else to the one matched by host and key, and is retired if there’s neither. Items given a new ID move too, active or not.

Parameters:
  • groups (sequence of Group) – The file’s groups, in order.

  • target (Server or None) – The server the items move to (--server), if any.

  • matched (mapping of Identity to Item) – Items found on the target server by host and key.

  • given (mapping of Identity to tuple of Server and Item) – Items given new IDs, with the server and item they move to.

  • retire (iterable of Identity) – Items to retire, or to bring back.

  • unretire (iterable of Identity) – Items to retire, or to bring back.

Returns:

MigrationPlan – The plan.

Raises:

ZabgrabError – If an item is both moved and retired, retired twice or brought back without being retired, two items would get the same ID, or a group would hold an item twice.

Return type:

MigrationPlan

zabgrab.services.migrate.apply_migration(groups, plan, *, at, command, version)[source]

Apply a migration to a file’s groups.

Moved items get their new server and IDs in every group, keeping their data, with their earlier server and IDs added to their history; retired items are marked, and unretired ones unmarked (and listed in the last group’s resume if it no longer has them). Every group’s migrations log gains an entry describing the migration.

Parameters:
  • groups (sequence of Group) – The file’s groups, in order (not changed).

  • plan (MigrationPlan) – The migration.

  • at (datetime) – When the migration is made.

  • command (sequence of str) – The command line.

  • version (str) – zabgrab’s version.

Returns:

list of Group – Changed copies of the groups, to write back to the file.

Return type:

list[G]

zabgrab.services.migrate.server_names(groups)[source]

Get the config names a file’s servers were fetched with.

Parameters:

groups (sequence of Group) – The file’s groups.

Returns:

dict of str to str or None – Each server’s name (None if it had none), by URL.

Return type:

dict[str, str | None]

zabgrab.services.migrate.specs_by_id(specs, itemids)[source]

List the item IDs that item specs select directly, among some IDs.

Used to note config templates that still select moved items by their old IDs.

Parameters:
  • specs (iterable of ItemSpec) – A template’s item specs.

  • itemids (iterable of int) – The moved items’ old IDs.

Returns:

list of int – The IDs both select, in order.

Return type:

list[int]