Skip to content

Commit 2bfcfa3

Browse files
AnvarUJenkins
authored andcommitted
mk-oracle: warn about SQLS_ITEM_SID in the migration
CMK-37379 Change-Id: Ie5f5bb6c457278c319f8fbd514dcdb228db7cae5
1 parent 6084ba1 commit 2bfcfa3

2 files changed

Lines changed: 161 additions & 0 deletions

File tree

packages/mk-oracle/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,40 @@ The alias then forms the entry alone and the dropped restriction is reported:
11001100
In both cases, check that the alias resolves to the database the section was meant to
11011101
query — the connection now depends on your `tnsnames.ora` alone.
11021102

1103+
##### `SQLS_ITEM_SID`
1104+
1105+
The legacy plugin builds the item of the `oracle_sql` output as
1106+
`[[[<SQLS_ITEM_SID>|<SQLS_ITEM_NAME>]]]`, which lets the displayed SID differ from the
1107+
internal name of the monitored instance. It is mainly used for remote instances, whose
1108+
internal name is the `REMOTE_INSTANCE_<ID>` variable:
1109+
1110+
```bash
1111+
foo_views_chk1 () {
1112+
SQLS_SIDS="REMOTE_INSTANCE_PRODPDB1"
1113+
SQLS_SQL=foo_view_check1.sql
1114+
SQLS_ITEM_NAME="foo_views_kim1"
1115+
SQLS_ITEM_SID="PRODPDB1"
1116+
}
1117+
```
1118+
1119+
**`mk-oracle` has no equivalent field**: the item is always built from the name of the
1120+
instance the section runs on (`sid:`, or the discovered SID). `SQLS_ITEM_SID` is
1121+
therefore not migrated, and the migration warns about every section where the item
1122+
would change:
1123+
1124+
```
1125+
# WARNING: foo_views_chk1: SQLS_ITEM_SID 'PRODPDB1' is not supported and is not migrated; the item of the oracle_sql section is built from the name of the instance the section runs on, so the name of the service changes and it is rediscovered
1126+
```
1127+
1128+
The item is part of the Checkmk service name, so an affected service disappears and is
1129+
rediscovered under the new name — together with the rules, downtimes and history bound
1130+
to the old one. Compare the old and the new item before rediscovering, and rename the
1131+
instance (`sid:`) if you need to keep the previous service name.
1132+
1133+
No warning is emitted when the value cannot change anything: the section runs on that
1134+
one SID anyway, or it uses a custom `SQLS_SECTION_NAME`, for which the legacy plugin
1135+
emits no item at all.
1136+
11031137
### What Is Not Migrated
11041138

11051139
The following variables are recognized but only preserved as comments in the output;
@@ -1109,6 +1143,7 @@ port them manually if you still need them:
11091143
| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
11101144
| `SQLS_DBUSER`, `SQLS_DBPASSWORD`, `SQLS_DBSYSCONNECT` | Per-custom-SQL credentials; use per-instance `authentication:` overrides instead |
11111145
| `SQLS_PARAMETERS` | SQL\*Plus parameter passing is not supported |
1146+
| `SQLS_ITEM_SID` | The item always carries the name of the instance the section runs on (see [above](#sqls_item_sid)) |
11121147
| `EXCLUDE_<SID>="<section> ..."` | Per-SID exclusion of individual sections; only `EXCLUDE_<SID>="ALL"` is converted |
11131148
| `ORACLE_HOME`, `REMOTE_ORACLE_HOME` | The OCI runtime is located as described in [Options](#options) (`use_host_client`) |
11141149
| `ID_BY` | Selects `SID=` vs `SERVICE_NAME=` in the legacy connect string; use the `sid:` / `service_name:` instance fields instead |

packages/mk-oracle/src/config/migration.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ struct LegacyCustomSql {
9090
tns_alias: Option<String>,
9191
header_name: Option<String>,
9292
header_sep: Option<char>,
93+
/// `SQLS_ITEM_SID`: the SID the legacy plugin puts into the output item.
94+
/// Not supported by the new plugin, only used to warn about it.
95+
item_sid: Option<String>,
9396
}
9497

9598
impl LegacyCustomSql {
@@ -256,6 +259,9 @@ fn parse_custom_sqls(legacy: &str, variables: &HashMap<String, String>) -> Vec<L
256259
header_sep: section_var("SQLS_SECTION_SEP")
257260
.or_else(|| variables.get("SQLS_SECTION_SEP"))
258261
.and_then(|v| parse_header_sep(name, v)),
262+
// no global fallback: the legacy plugin unsets SQLS_ITEM_SID
263+
// before each section
264+
item_sid: section_var("SQLS_ITEM_SID").cloned(),
259265
})
260266
})
261267
.collect()
@@ -490,6 +496,28 @@ fn warn_custom_sql_alias_sids(
490496
.collect()
491497
}
492498

499+
/// Warn about the custom SQL sections that set `SQLS_ITEM_SID`.
500+
fn warn_custom_sql_item_sid(custom_sqls: &[LegacyCustomSql]) -> Vec<String> {
501+
custom_sqls
502+
.iter()
503+
.filter(|custom| custom.header_name.is_none())
504+
.filter_map(|custom| {
505+
let item_sid = custom.item_sid.as_ref()?;
506+
// the item is unchanged when the section already runs on exactly
507+
// that instance
508+
if custom.tns_alias.is_none() && custom.reference_sid().as_ref() == Some(item_sid) {
509+
return None;
510+
}
511+
Some(format!(
512+
"{}: SQLS_ITEM_SID '{item_sid}' is not supported and is not migrated; the item of \
513+
the oracle_sql section is built from the name of the instance the section runs \
514+
on, so the name of the service changes and it is rediscovered",
515+
custom.name
516+
))
517+
})
518+
.collect()
519+
}
520+
493521
/// Collect raw `SQLS_SIDS=` assignments from the legacy config text, keyed by
494522
/// the enclosing function name (None = top level).
495523
fn collect_raw_sqls_sids(legacy: &str) -> HashMap<Option<String>, String> {
@@ -580,6 +608,7 @@ pub fn convert(
580608
&custom_sqls,
581609
&known_aliases(&dbuser, &dbuser_extras),
582610
));
611+
warnings.extend(warn_custom_sql_item_sid(&custom_sqls));
583612
warnings.extend(custom_sql_warnings(&custom_sqls));
584613
for warning in warnings {
585614
let warning = format!("# WARNING: {warning}\n");
@@ -1349,6 +1378,7 @@ mod tests {
13491378
tns_alias: None,
13501379
header_name: None,
13511380
header_sep: None,
1381+
item_sid: None,
13521382
}]
13531383
);
13541384
}
@@ -1372,6 +1402,7 @@ mod tests {
13721402
tns_alias: None,
13731403
header_name: None,
13741404
header_sep: None,
1405+
item_sid: None,
13751406
}]
13761407
);
13771408
}
@@ -1732,6 +1763,36 @@ sec2 () {
17321763
);
17331764
}
17341765

1766+
#[cfg(not(windows))]
1767+
#[test]
1768+
fn test_convert_warns_on_custom_sql_item_sid() {
1769+
let legacy = "sec1 () {\n SQLS_SIDS=\"REMOTE_INSTANCE_PRODPDB1\"\n \
1770+
SQLS_SQL=\"a.sql\"\n SQLS_ITEM_SID=\"PRODPDB1\"\n}\n";
1771+
let vars = HashMap::from([
1772+
("DBUSER".into(), "checkmk:secret::::".into()),
1773+
(
1774+
"REMOTE_INSTANCE_PRODPDB1".into(),
1775+
"user:pass::remotehost:1521::PRODCDB:11.2".into(),
1776+
),
1777+
("SQLS_SECTIONS".into(), "sec1".into()),
1778+
(
1779+
"SQLS.sec1.SQLS_SIDS".into(),
1780+
"REMOTE_INSTANCE_PRODPDB1".into(),
1781+
),
1782+
("SQLS.sec1.SQLS_SQL".into(), "a.sql".into()),
1783+
("SQLS.sec1.SQLS_ITEM_SID".into(), "PRODPDB1".into()),
1784+
]);
1785+
let result = convert(legacy, "/test/cfg", &vars, TS).unwrap();
1786+
assert!(
1787+
result.contains("# WARNING: sec1: SQLS_ITEM_SID 'PRODPDB1' is not supported"),
1788+
"got: {result}"
1789+
);
1790+
assert!(
1791+
!result.contains("item_sid"),
1792+
"there is no field to migrate it to, got: {result}"
1793+
);
1794+
}
1795+
17351796
#[test]
17361797
fn test_parse_custom_sqls_header_name() {
17371798
let vars = HashMap::from([
@@ -1760,6 +1821,70 @@ sec2 () {
17601821
assert_eq!(result[1].name, "sec2", "default is the section name");
17611822
}
17621823

1824+
#[test]
1825+
fn test_parse_custom_sqls_item_sid() {
1826+
let vars = HashMap::from([
1827+
("SQLS_SECTIONS".into(), "sec1 sec2".into()),
1828+
("SQLS_SQL".into(), "a.sql".into()),
1829+
("SQLS_ITEM_SID".into(), "GLOBAL".into()),
1830+
("SQLS.sec1.SQLS_ITEM_SID".into(), "PRODPDB1".into()),
1831+
]);
1832+
let result = parse_custom_sqls("", &vars);
1833+
assert_eq!(result[0].item_sid.as_deref(), Some("PRODPDB1"));
1834+
assert!(
1835+
result[1].item_sid.is_none(),
1836+
"SQLS_ITEM_SID has no global fallback"
1837+
);
1838+
}
1839+
1840+
#[test]
1841+
fn test_warn_custom_sql_item_sid() {
1842+
let mut custom = make_custom_sql("sec1", None, "a.sql", &["REMOTE_PROD"]);
1843+
custom.item_sid = Some("PRODPDB1".into());
1844+
assert_eq!(
1845+
warn_custom_sql_item_sid(&[custom]),
1846+
vec![
1847+
"sec1: SQLS_ITEM_SID 'PRODPDB1' is not supported and is not migrated; the item of \
1848+
the oracle_sql section is built from the name of the instance the section runs \
1849+
on, so the name of the service changes and it is rediscovered"
1850+
]
1851+
);
1852+
}
1853+
1854+
#[test]
1855+
fn test_warn_custom_sql_item_sid_same_as_only_sid() {
1856+
let mut custom = make_custom_sql("sec1", None, "a.sql", &["PRODPDB1"]);
1857+
custom.item_sid = Some("PRODPDB1".into());
1858+
assert!(
1859+
warn_custom_sql_item_sid(&[custom]).is_empty(),
1860+
"item is unchanged when the section runs on that instance only"
1861+
);
1862+
}
1863+
1864+
#[test]
1865+
fn test_warn_custom_sql_item_sid_of_aliased_section() {
1866+
let mut custom = make_custom_sql("sec1", None, "a.sql", &["PRODPDB1"]);
1867+
custom.item_sid = Some("PRODPDB1".into());
1868+
custom.tns_alias = Some("PROD_ALIAS".into());
1869+
assert_eq!(
1870+
warn_custom_sql_item_sid(&[custom]).len(),
1871+
1,
1872+
"an aliased section may connect to any instance"
1873+
);
1874+
}
1875+
1876+
#[test]
1877+
fn test_warn_custom_sql_item_sid_ignores_unaffected_sections() {
1878+
let plain = make_custom_sql("plain", None, "a.sql", &["PRODPDB1"]);
1879+
let mut custom_section = make_custom_sql("custom_section", None, "a.sql", &["PRODPDB1"]);
1880+
custom_section.item_sid = Some("OTHER".into());
1881+
custom_section.header_name = Some("my_section".into());
1882+
assert!(
1883+
warn_custom_sql_item_sid(&[plain, custom_section]).is_empty(),
1884+
"no SQLS_ITEM_SID, or a section the legacy plugin emits no item for"
1885+
);
1886+
}
1887+
17631888
#[test]
17641889
fn test_parse_custom_sqls_section_sep() {
17651890
let vars = HashMap::from([
@@ -1864,6 +1989,7 @@ sec3 () {
18641989
tns_alias: None,
18651990
header_name: None,
18661991
header_sep: None,
1992+
item_sid: None,
18671993
}
18681994
}
18691995

0 commit comments

Comments
 (0)