Skip to content

Commit 1104afe

Browse files
authored
Merge pull request #1058 from aguzev/sql_assessment_api_docs
Update SQL Assessment APi documentation
2 parents f87a898 + 22b2374 commit 1104afe

46 files changed

Lines changed: 2216 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Data Transformation
2+
3+
Sometimes data needs to go through series of transformations.
4+
5+
When dta comes in an unconvenient format, a transformation can be applied in the probe implementation. For example, the following T-SQL query returns `host_release` as a string like '10.0.19044.2006'.
6+
7+
```SQL
8+
SELECT host_platform, host_release
9+
FROM sys.dm_os_host_info
10+
```
11+
12+
While it is possible to parse the string in T-SQL, a transformation make the code more readable and easier to create and maintain:
13+
14+
```json
15+
"implementation": {
16+
"query": "SELECT host_platform, host_release FROM sys.dm_os_host_info",
17+
"transform": {
18+
"type": "parse",
19+
"map": {
20+
"host_release": "/^(?<major>\\d+)\\.(?<minor>\\d+)"
21+
}
22+
}
23+
}
24+
```
25+
26+
Transformations can be applied in [probe references](./ProbeReference.md) to increase probe reuse. When one check needs an average value for some metric while another best practice involves maximum for the same metric. Both checks can have references to the same probe but each applies its own specific transformation. In this case not only the probe code is reused, but the data is reused as well, because the probe will be called only once.
27+
28+
See [Data transformation reference](../Reference/DataTransformation/README.md) for more details.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Local Variables
2+
3+
A check can define local variables available for conditions and message templates. Local variables are any expressions involving literals, probe data, and transformation results.
4+
5+
```json
6+
{
7+
"probes": ["SysDmOsSysInfo"],
8+
"locals": {
9+
"workers": {"sub": [ "@max_workers_count", 1 ] }
10+
},
11+
"message": "Workers = @{workers}.",
12+
"condition": {
13+
"lt": [ 0, "@workers" ],
14+
"lt": [ "@workers", 4 ]
15+
}
16+
}
17+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Probe
2+
3+
A *Probe* is a JSON property. The property name is the probe ID used in checks. The property value is an array of [probe implementations](#probe-implementation). The SQL Assessment API engine selects the first implementation with matching target pattern. This means that the order of the implementations in the array is important. A probe can be comprised by a mix of CLR and SQL implementations.
4+
5+
Another ruleset may add probe implementations on top of the list.
6+
7+
A probe should be designed as a function with no side effects. The order of calling probes is not determined. The engine may reorder probe calls to optimize the target SQL Server load. When no check needs data from a probe, that probe will not be called.
8+
9+
Probes from the default rule set read metadata only, e.g. update logs or server properties. They do not read user data from tables or write anything to databases or instances. Probes do not set any flags or properties.
10+
11+
## Probe implementation
12+
13+
Probe implementation is represented by a JSON object. Its properties define procedures for getting data and selecting appropriate implementation.
14+
15+
![Probe structure](img\ProbeStructure.svg)
16+
17+
## Probe properties
18+
19+
### implementation
20+
21+
The `implementation` property contains probe parameters and [data transformations](DataTransformation.md) affecting the probe output. For example, for a T-SQL probe the main parameter is the query for selecting data.
22+
23+
Probe parameters are specific for [probe type](#type).
24+
25+
### target
26+
27+
Target object [pattern](TargetPattern.md).
28+
29+
### type
30+
31+
Probe `type` determines the mechanism used to get data. it may be a T-SQL or a WMI. Available probe types are listed in the following table.
32+
33+
|Type|Description|
34+
|---|---|
35+
|[AzGraph](../Reference/Probes/AzGraphProbe.md)|Kusto query to Azure resource graph|
36+
|[AzMetadata](../Reference/Probes/AzMetadataProbe.md)|JSONPath for the object returned by Azure Instance Metadata Service|
37+
|[CMD](../Reference/Probes/CMDShellProbes.md)|Command shell script run on the target machine|
38+
|[External](../Reference//Probes/ExternalProbe.md)|Arbitrary .NET code|
39+
|[PowerShell](../Reference/Probes/PowerShellProbes.md)|PowerShell script|
40+
|[Registry](../Reference/Probes/RegistryProbes.md)|Data from registry|
41+
|[SQL](../Reference/Probes/TSQLProbes.md)|T-SQl query|
42+
|[WMI](../Reference/Probes/WMIProbes.md)|WMI query|
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Probe reference
2+
3+
Data for checks comes from probes. Probe reference is a JSON object that describes a probe needed by the check. When a probe is referenced with no additional options or parameters, a probe reference may be abbreviated to a string containing probe ID.
4+
5+
![Probe reference structure](img\ProbeRefStructure.svg)
6+
7+
## Properties
8+
9+
### alias
10+
11+
An alternative name used for this probe.
12+
13+
Alias is an alternative name for the probe which can be used in expressions in this check. It is useful in two scenarios: [calling a probe multiple times](#calling-probe-multiple-times) and [passing data from one probe to another one](#using-data-from-another-probe).
14+
15+
### id
16+
17+
Referenced probe ID.
18+
19+
### params
20+
21+
Parameters passed to the probe called for this check. They are represented as properties of a JSON object. Property name is the parameter name while property value can be any [expression](Expression.md). Expression can include constants, global and local variables, and data returned by other probes (see [alias](#alias)).
22+
23+
### transform
24+
25+
Data transformation applied to the data before calculation the condition value, see [Data transformation](DataTransformation.md).
26+
27+
## Calling probe multiple times
28+
29+
For some checks you may need to call a probe multiple times with different parameters. In this case add multiple probe references with the same `id`. To distinguish results returned for different parameters prepend the output variable name with probe alias and double colon '::'.
30+
31+
The following check compares f4ree space on disks C: and D:.
32+
33+
```json
34+
"probes": [
35+
{
36+
"type": "DiskInfo",
37+
"alias": "FirstDisk",
38+
"params": {
39+
"DiskName": "C:"
40+
}
41+
},
42+
{
43+
"type": "DiskInfo",
44+
"alias": "SecondDisk",
45+
"params": {
46+
"DiskName": "D:"
47+
}
48+
}
49+
],
50+
"condition": {
51+
"greater": [
52+
"@FirstDisk::FreeSpace",
53+
"@SecondDisk::FreeSpace"
54+
]
55+
}
56+
```
57+
58+
## Using data from another probe
59+
60+
Data returned by one probe can be passed to another one as a parameter. The expression for the parameter should contain the output variable name prefixed with the previous probe id or alias.
61+
62+
In the following example *DatabaseMasterFiles* probe finds volume IDs for all disks used by the target database. The *AzStorage* probe is called for each volume ID and adds Azure related storage properties along with volume ID.
63+
64+
```json
65+
"probes": [
66+
{
67+
"id": "DatabaseMasterFiles",
68+
"alias": "db_files",
69+
"params": {
70+
"dbId": null,
71+
"type": null
72+
},
73+
"transform": [
74+
{
75+
"type": "aggregate",
76+
"group": [
77+
"volume_mount_point",
78+
"volume_id"
79+
]
80+
},
81+
{
82+
"type": "aggregate",
83+
"group": "volume_id",
84+
"map": {
85+
"volume_mount_point": "join"
86+
}
87+
}
88+
]
89+
},
90+
{
91+
"id": "AzStorage",
92+
"params": {
93+
"path": "@db_files::volume_id"
94+
}
95+
}
96+
]
97+
```
98+
99+
100+
101+
102+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Customization
2+
3+
The SQL Assessment API is a comprehensive solution that allows you not only to use the functionality that comes out of the box, but also customize the assessment workflows, depending on your needs.
4+
5+
## In This Section
6+
7+
- [Understanding rules and probes](RulesandProbes.md)
8+
- [Rule](Rule.md)
9+
- [Probe reference](ProbeReference.md)
10+
- [Probe](Probe.md)
11+
- [Data Transformation](DataTransformation.md)
12+
- [Local Variables](LocalVariables.md)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# SQL Assessment API Rule
2+
3+
A rule is represented by a JSON object. A rule applies its properties to a new or existing check.
4+
5+
![Rule structure](img\RuleStructure.svg)
6+
7+
## Rule properties
8+
9+
### itemType
10+
11+
__Allowed values:__ *definition*, *override*
12+
13+
This property specifies whether this rule defines a new check or modifies an existing one. A check can be modified by more than one rule.
14+
15+
### tags
16+
17+
An array of short strings used to categorize checks. For example, checks marked with the "Memory" tag are coming from memory-related best practices. Single-word tags work best.
18+
19+
### id
20+
21+
An ID of the check to be defined or modified by this rule. A check can be modified by more than one rule.
22+
23+
An array of IDs or tags of checks to be modified by this rule. A rule can modify multiple checks at once. Checks are selected either by ID or by tag. If multiple tags are specified, every check having _any_ listed tag will be affected by this rule.
24+
25+
### target
26+
27+
Target pattern is a JSON object used to select applicable rules. A rule will be applied if the assessed object matches the `target` pattern. For more information, see [Target pattern]().
28+
29+
### targetFilter
30+
31+
Target pattern with the same syntax as the target property, but used exclusively in overrides. For more information on the usage and object format, see [Overrides]() and [Target pattern](), respectively.
32+
33+
### displayName
34+
35+
Short display names are shown in checklists. You may think of a display name as of a file name if a rule was stored in a separate file. Usually, the display name tells what the check is looking for.
36+
37+
### message
38+
39+
A string which is used as a template for generating messages to the user. Such a message appears when the check detects configurations that are not in compliance with the best practice recommendation. For more information, see [Message template]().
40+
41+
### description
42+
43+
Long description that explains why the check looks for this particular issue. It briefly discusses the impact of the detected issue.
44+
45+
### helpLink
46+
47+
Hyperlink to an article that explains the best practice recommendations and remediation suggestions.
48+
49+
### level
50+
51+
__Allowed values:__ `Information`, `Low`, `Medium`, 'High'
52+
53+
The severity level of the issue detected by this check.
54+
55+
### probes
56+
57+
The `probes` array contains references to probes to be used to get data required by this check. For more information, see [Probe References]().
58+
59+
### condition
60+
61+
Condition is an expression in form of JSON object tree. It uses check parameters and data from probes This expression should return _true_ when the best practice found implemented. If condition evaluates to _false_, the message is displayed to the user.
62+
63+
### parameters
64+
65+
Arbitrary properties passed to conditions, message templates, probe parameters, and transforms along with the data from probes.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Understanding rules and probes
2+
3+
The SQL Assessment API uses sets of best practice recommendations to check if a SQL Server environment or configuration could be improved. Best practices are not universal. Guidelines depend on SQL Server version, edition, and configuration, hosting platform, and even usage pattern. For example, some best practices are applicable only to cloud environments, whereas others work for SQL Server instances installed on a physical machine. Databases may also be of different types. For example, a set of best practices for the `msdb` database might be different from that used for user databases.
4+
5+
SQL Assessment API makes recommendations more specific by implementing a two-step process:
6+
7+
1. Build a checklist for the given target
8+
9+
Such a checklist contains a complete set of checks, depending on the specified target.
10+
11+
2. Go through the checklist and report every best practice violation
12+
13+
At this step, the SQL Assessment API goes through the checklist to validate whether the given target satisfies the given best practice.
14+
15+
The checklist construction process is based on rules. Each rule either defines a new check or modifies one or more of the existing ones. Rules are stored in rulesets. Each ruleset has its own name and version. Users can add multiple rulesets to the SQL Assessment API engine one by one. While building the checklist, the engine will apply rules in the order they were added.
16+
17+
A single check can be affected by multiple rules. The first rule must define the check and assign a unique string ID. Checks may have tags that allow managing them in groups. The following rules override checks referred by IDs or tags, which gives the override the ability to modify multiple checks at once.
18+
19+
For example, the following rule makes a check with the `MaxMemory` ID appear in the checklist for all SQL Server 2012 instances and higher. Note that the rule sets the `limit` parameter to 2147483647.
20+
21+
```json
22+
{
23+
"id": "MaxMemory",
24+
"itemType": "definition",
25+
"target":
26+
{
27+
"type": "Server",
28+
"version": "[11.0,)"
29+
},
30+
"limit": 2147483647
31+
}
32+
```
33+
34+
The rule below modifies the limit parameter for the `MaxMemory` check defined above.
35+
36+
```json
37+
{
38+
"id": "MaxMemory",
39+
"itemType": "override",
40+
"limit": 2000000000
41+
}
42+
```
43+
44+
Note that the rule override can modify checks for selected targets if needed. The following rules modify the limit of the `MaxMemory` check for specific SQL Server editions.
45+
46+
```json
47+
{
48+
"id": "MaxMemory",
49+
"itemType": "override",
50+
"targetFilter":
51+
{
52+
"engineEdition": "Standard"
53+
},
54+
"limit": 131072
55+
},
56+
57+
{
58+
"id": "MaxMemory",
59+
"itemType": "override",
60+
"targetFilter":
61+
{
62+
"engineEdition": "Express"
63+
},
64+
"limit": 1410
65+
}
66+
```
67+
68+
Having a check in the checklist does not mean this check will be run by the assessment engine. Each check has the `enabled` property which is set to *true* by default. If the property becomes false after all rules are applied, the engine will skip the check.
69+
70+
The following rule disables the `MaxMemory` check and all performance-related checks for all instances running on Linux.
71+
72+
```json
73+
{
74+
"id": [ "MaxMemory", "Performance" ],
75+
"itemType": "override",
76+
"targetFilter": {
77+
"platform": "Linux"
78+
},
79+
"enabled": false
80+
}
81+
```
82+
83+
Each check needs data to analyze. Checks do not retrieve any data from the target server or database but refer to *probes* instead. Most of the probes use T-SQL queries, but the SQL Assessment API also supports probes for WMI, Windows registry, Azure Instance Metadata Service, as well as custom probes implemented as .NET classes.
84+
85+
Each probe returns zero or more data rows containing named items. If a check gets data from multiple probes, the resulting data set is constructed as *all* combinations of rows. This behavior is similar to `CROSS JOIN` in T-SQL.
86+
87+
A check has a condition expression which formally describes best practice as an arithmetic expression referring to data from a data row. Condition is calculated and checked if it holds for each row separately. For example, when a database uses 3 disks for storing its files, then a free space best practice may apply to each disk independently. If a check uses data from two probes, and one probe produces 2 rows while another one produces 3, the check condition will be evaluated 2×3=6 times. If condition gives *false* 2 times out of 6, the check will produce 2 messages.
88+
89+
At the same time, probe implementation may depend on the SQL Server version. For example, dynamic management views may vary between SQL Server releases. This is why a probe may have multiple implementations. Each implementation has a target specification, the same way the rules do. The assessment engine uses the most appropriate implementation for each target.
90+
91+
## Related topics
92+
93+
- [Probes](../Reference/Probes/README.md)
94+
- [Tutorials](../Tutorials/README.md)
95+
- [How-To](../How-To/README.md)

0 commit comments

Comments
 (0)