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
looked up by host and key, or given by the user. |
Functions
|
Apply a migration to a file's groups. |
|
Find the items a file holds with the given IDs. |
|
Look up what a migration needs on its target server. |
|
Look items up on a server by their host's technical name and their key. |
|
Parse |
|
Work out a migration: which items move, which retire, which come back. |
|
Read a file of item ID mappings, one per line. |
|
Get the config names a file's servers were fetched with. |
|
List the item IDs that item specs select directly, among some IDs. |
Classes
|
What a migration will change. |
|
An item moving to a new ID (and perhaps a new server). |
|
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:
objectAn item moving to a new ID (and perhaps a new server).
- Parameters:
- class zabgrab.services.migrate.Retirement(item, label, reason)[source]¶
Bases:
objectAn item to retire.
- class zabgrab.services.migrate.MigrationPlan(target, sources=<factory>, moves=<factory>, retirements=<factory>, unretired=<factory>, unchanged=<factory>, names=<factory>)[source]¶
Bases:
objectWhat a migration will change.
- Parameters:
- retirements¶
The items to retire.
- Type:
list of Retirement
- unchanged¶
Items found on the target server under the same ID (nothing to change).
- names¶
The file’s servers’ names in the config, by URL (see
server_names()).- Type:
dict of str to str or None
- 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:
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:
- 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:
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:
- 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:
- 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.
- 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:
- 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
updatewould 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:
- 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’sresumeif it no longer has them). Every group’smigrationslog 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.