From 7596e82760b08a77f4a4c41df6157cc60bc3727c Mon Sep 17 00:00:00 2001 From: itam-automation <203017558+itam-automation@users.noreply.github.com> Date: Wed, 11 Nov 2026 12:48:00 +0100 Subject: [PATCH 1/2] Add asset/CI status drift check based on alm_asset_ci_state_mapping --- .../Asset CI Status Drift Check/README.md | 49 ++++++++ .../asset_ci_status_drift.js | 118 ++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 Specialized Areas/CMDB/Asset CI Status Drift Check/README.md create mode 100644 Specialized Areas/CMDB/Asset CI Status Drift Check/asset_ci_status_drift.js diff --git a/Specialized Areas/CMDB/Asset CI Status Drift Check/README.md b/Specialized Areas/CMDB/Asset CI Status Drift Check/README.md new file mode 100644 index 0000000000..53572c8d14 --- /dev/null +++ b/Specialized Areas/CMDB/Asset CI Status Drift Check/README.md @@ -0,0 +1,49 @@ +# Asset CI Status Drift Check + +A read-only background script that finds hardware assets whose CI has a different status than the asset-to-CI mapping says it should have. + +## Why + +ServiceNow keeps `alm_hardware.install_status` and the `install_status` of the linked CI in sync. Which asset status (and substatus) leads to which CI status is configured in the table `alm_asset_ci_state_mapping`. The sync runs in business rules, so every update that skips them breaks it without a trace, for example `setWorkflow(false)` in fix scripts or imports that write with business rules turned off. The result is a CMDB that says "Installed" for a laptop the asset record shows as "In stock", and reports that disagree depending on which table they use. + +## How it works + +1. Reads the active rows of `alm_asset_ci_state_mapping` with direction "Asset to configuration item" or "Both" into a lookup. Rows without a substatus apply to all substatuses. +2. Groups all assets that have a CI by asset status, substatus and `ci.install_status` in a single `GlideAggregate` query. That is one query no matter how many assets there are. +3. Compares each group with the expected CI status and prints every mismatch with a count and up to five asset tags. + +It changes nothing. + +## Usage + +Run `asset_ci_status_drift.js` in Scripts - Background, or put the function into a scheduled script to watch the drift over time. + +- `ASSET_TABLE`: `alm_hardware` by default. +- `EXTRA_QUERY`: an encoded query to limit the check, for example `company.name=ACME`. +- `SAMPLES`: how many asset tags to list per mismatch. + +## Example output + +From a personal developer instance (release Brazil) with the out-of-box demo data: + +``` +Asset/CI status drift on alm_hardware: 1091 assets with CI checked, 120 with a CI status that does not match the mapping, 0 with a status that has no mapping. +41 x asset In use (1): CI is (empty), expected Installed (1) | eg. P1000153, P1000109, P1000221, P1000222, P1000174 +6 x asset In use (1): CI is In Stock (6), expected Installed (1) | eg. P22166, P22338, P22896, P22877, P1000047 +70 x asset In stock (6) / Available (available): CI is (empty), expected In Stock (6) | eg. P1000183, P1000185, P1000125, P1000188, P1000118 +1 x asset In stock (6) / Available (available): CI is Installed (1), expected In Stock (6) | eg. P1000028 +1 x asset In stock (6) / Defective (defective): CI is (empty), expected Pending Repair (5) | eg. P1000117 +1 x asset In stock (6) / Pending transfer (pending_transfer): CI is (empty), expected In Stock (6) | eg. P1000130 +``` + +`CI is (empty)` means the linked CI has no `install_status`, or the reference points to a CI that no longer exists. On the test instance the CIs existed and had no status. + +## Fixing the drift + +The script only reports. Decide per group which side is right, then update the wrong side to the value from the mapping with business rules enabled, so the platform logic runs as it normally would. Avoid `setWorkflow(false)` for this, because that is how the drift usually starts. + +## Notes + +- Newer releases also sync `hardware_status` and `hardware_substatus` on `cmdb_ci_hardware` through `alm_hardware_state_mapping`. This script checks `install_status` only. +- `GlideAggregate` groups by the dot-walked field `ci.install_status`, which keeps the check fast on large tables. +- ES5 only, no ES2021 features. diff --git a/Specialized Areas/CMDB/Asset CI Status Drift Check/asset_ci_status_drift.js b/Specialized Areas/CMDB/Asset CI Status Drift Check/asset_ci_status_drift.js new file mode 100644 index 0000000000..034f7863cc --- /dev/null +++ b/Specialized Areas/CMDB/Asset CI Status Drift Check/asset_ci_status_drift.js @@ -0,0 +1,118 @@ +/** + * Background script / scheduled script: find hardware assets whose CI status does not match. + * + * ServiceNow keeps alm_hardware.install_status and cmdb_ci.install_status in sync through the + * mapping in alm_asset_ci_state_mapping. Updates that skip business rules (setWorkflow(false), + * some imports and integrations) break that sync without anyone noticing. + * + * The script reads the active mapping (direction "asset_to_ci" or "both"), groups all assets with a + * CI by asset status, substatus and CI status in one GlideAggregate query, and reports every + * combination where the CI status is not the one the mapping expects. Read only, it changes nothing. + */ +(function findAssetCiStatusDrift() { + + var ASSET_TABLE = 'alm_hardware'; + var EXTRA_QUERY = ''; // eg. 'company.name=ACME' to check one company only + var SAMPLES = 5; + + // asset status + substatus -> expected CI install_status + var expected = {}; + var map = new GlideRecord('alm_asset_ci_state_mapping'); + map.addActiveQuery(); + map.addQuery('synch_direction', 'IN', 'asset_to_ci,both'); + map.query(); + while (map.next()) { + expected[map.getValue('asset_state') + '|' + (map.getValue('asset_substate') || '')] = map.getValue('configuration_item_status') || ''; + } + + function expectedCiStatus(state, substate) { + var key = state + '|' + (substate || ''); + if (expected.hasOwnProperty(key)) { + return expected[key]; + } + // mapping rows without substate apply to all substates + return expected.hasOwnProperty(state + '|') ? expected[state + '|'] : null; + } + + // display value of a choice, with the value, eg. "In use (1)" + function label(table, values, field) { + if (!values[field]) { + return '(empty)'; + } + var gr = new GlideRecord(table); + gr.initialize(); + for (var name in values) { + gr.setValue(name, values[name]); + } + return gr.getDisplayValue(field) + ' (' + values[field] + ')'; + } + + var ga = new GlideAggregate(ASSET_TABLE); + ga.addNotNullQuery('ci'); + if (EXTRA_QUERY) { + ga.addEncodedQuery(EXTRA_QUERY); + } + ga.groupBy('install_status'); + ga.groupBy('substatus'); + ga.groupBy('ci.install_status'); + ga.addAggregate('COUNT'); + ga.query(); + + var checked = 0; + var drifted = 0; + var unmapped = 0; + var lines = []; + + while (ga.next()) { + var state = ga.getValue('install_status'); + var substate = ga.getValue('substatus'); + var ciStatus = ga.getValue('ci.install_status') || ''; + var count = parseInt(ga.getAggregate('COUNT'), 10); + var want = expectedCiStatus(state, substate); + checked += count; + + if (want === null) { + unmapped += count; + continue; + } + if (want === '' || want === ciStatus) { + continue; + } + + drifted += count; + + var sample = new GlideRecord(ASSET_TABLE); + sample.addNotNullQuery('ci'); + if (EXTRA_QUERY) { + sample.addEncodedQuery(EXTRA_QUERY); + } + sample.addQuery('install_status', state); + if (substate) { + sample.addQuery('substatus', substate); + } else { + sample.addNullQuery('substatus'); + } + if (ciStatus) { + sample.addQuery('ci.install_status', ciStatus); + } else { + sample.addNullQuery('ci.install_status'); + } + sample.setLimit(SAMPLES); + sample.query(); + var tags = []; + while (sample.next()) { + tags.push(sample.getValue('asset_tag') || sample.getUniqueValue()); + } + + var assetValues = { install_status: state, substatus: substate }; + lines.push(count + ' x asset ' + label(ASSET_TABLE, assetValues, 'install_status') + + (substate ? ' / ' + label(ASSET_TABLE, assetValues, 'substatus') : '') + + ': CI is ' + label('cmdb_ci', { install_status: ciStatus }, 'install_status') + + ', expected ' + label('cmdb_ci', { install_status: want }, 'install_status') + + ' | eg. ' + tags.join(', ')); + } + + gs.info('Asset/CI status drift on ' + ASSET_TABLE + ': ' + checked + ' assets with CI checked, ' + + drifted + ' with a CI status that does not match the mapping, ' + unmapped + ' with a status that has no mapping.\n' + + lines.join('\n')); +})(); From f4a28f3d4a9fe218169e9f3d4604c7fe56535411 Mon Sep 17 00:00:00 2001 From: itam-automation <203017558+itam-automation@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:32:34 +0100 Subject: [PATCH 2/2] README: more causes of drift, test data, Hardware Status Drift does not only come from setWorkflow(false). The example output included test assets of my own, and Hardware Status is a second pair of status fields on hardware CIs, not a feature of newer releases. --- .../CMDB/Asset CI Status Drift Check/README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Specialized Areas/CMDB/Asset CI Status Drift Check/README.md b/Specialized Areas/CMDB/Asset CI Status Drift Check/README.md index 53572c8d14..3ef7e14bbe 100644 --- a/Specialized Areas/CMDB/Asset CI Status Drift Check/README.md +++ b/Specialized Areas/CMDB/Asset CI Status Drift Check/README.md @@ -4,7 +4,13 @@ A read-only background script that finds hardware assets whose CI has a differen ## Why -ServiceNow keeps `alm_hardware.install_status` and the `install_status` of the linked CI in sync. Which asset status (and substatus) leads to which CI status is configured in the table `alm_asset_ci_state_mapping`. The sync runs in business rules, so every update that skips them breaks it without a trace, for example `setWorkflow(false)` in fix scripts or imports that write with business rules turned off. The result is a CMDB that says "Installed" for a laptop the asset record shows as "In stock", and reports that disagree depending on which table they use. +ServiceNow syncs the status between a hardware asset and its CI. Which asset status (and substatus) leads to which CI status is configured in the table `alm_asset_ci_state_mapping`, and each row has a direction: asset to CI, CI to asset, or both. The two records still drift apart, for example: + +- An integration or Discovery writes the CI status, and no mapping row syncs that status back to the asset. +- A fix script or an import writes with business rules turned off (`setWorkflow(false)`), so the sync does not run at all. +- The platform creates assets for existing CIs, and the new asset starts as In use instead of taking the status of the CI. + +The result is a CMDB that says "Installed" for a laptop the asset record shows as "In stock", and reports that disagree depending on which table they use. ## How it works @@ -24,7 +30,7 @@ Run `asset_ci_status_drift.js` in Scripts - Background, or put the function into ## Example output -From a personal developer instance (release Brazil) with the out-of-box demo data: +From a personal developer instance (release Brazil) with the out-of-box demo data plus 197 hardware assets from my own test data: ``` Asset/CI status drift on alm_hardware: 1091 assets with CI checked, 120 with a CI status that does not match the mapping, 0 with a status that has no mapping. @@ -40,10 +46,10 @@ Asset/CI status drift on alm_hardware: 1091 assets with CI checked, 120 with a C ## Fixing the drift -The script only reports. Decide per group which side is right, then update the wrong side to the value from the mapping with business rules enabled, so the platform logic runs as it normally would. Avoid `setWorkflow(false)` for this, because that is how the drift usually starts. +The script only reports. Decide per group which side is right, then update the wrong side to the value from the mapping with business rules enabled, so the platform logic runs as it normally would. Avoid `setWorkflow(false)` for this, because it skips the sync. ## Notes -- Newer releases also sync `hardware_status` and `hardware_substatus` on `cmdb_ci_hardware` through `alm_hardware_state_mapping`. This script checks `install_status` only. +- Hardware CIs have a second pair of status fields, `hardware_status` and `hardware_substatus` on `cmdb_ci_hardware`, which are synced with the asset through `alm_hardware_state_mapping`. This script checks `install_status` only, so a drift in Hardware Status is not reported. - `GlideAggregate` groups by the dot-walked field `ci.install_status`, which keeps the check fast on large tables. - ES5 only, no ES2021 features.