[comment]: # translation:outdated

[comment]: # ({d1387fc4-f9178426})
# 获取SLI

[comment]: # ({/d1387fc4-f9178426})

[comment]: # ({3fde2eb3-531f7cc6})

### 描述

`object sla.getsli(object parameters)`

这个方法可以计算服务水平指标(SLI)数据用于服务水平协议(SLA)。

::: noteclassic
这个方法可以用于任何用户类型。可以在用户角色设置中取消调用该方法的权限。查看 [用户角色](/manual/web_interface/frontend_sections/users/user_roles)获取更多信息。
:::

[comment]: # ({/3fde2eb3-531f7cc6})

[comment]: # ({880cf558-bcbf1cf2})
### 参数

`(object)` 包含 SLA ID、报告周期以及可选的服务 ID 的参数——用于计算 SLI。

|参数|[类型](/manual/api/reference_commentary#data-types)|说明|
|--|--|------|
|slaid|ID|要返回可用性信息的 SLA 的 ID。<br><br>[参数行为](/manual/api/reference_commentary#parameter-behavior)：<br>- *必需*|
|period\_from|timestamp|要报告 SLI 的起始时间戳（含）。<br><br>可能的值：Unix 时间戳。|
|period\_to|timestamp|要报告 SLI 的结束时间戳（含）。<br><br>可能的值：Unix 时间戳。|
|periods|integer|要报告的周期数。<br><br>可能的值：1-100|
|serviceids|ID/array|要返回 SLI 的服务 ID。|

[comment]: # ({/880cf558-bcbf1cf2})

[comment]: # ({d1b5c39b-31bc72e4})
#### 周期的划分

下表展示了根据参数组合返回的周期切片排列方式。

::: noteclassic
返回的周期不会早于第一个可用周期（基于 SLA 的生效日期），也不会超过当前周期。
:::

|参数|<|<|返回的周期|
|-|-|-|-------|
|**period\_from**|**period\_to**|**periods**|   |
|-|-|-|包括当前周期在内的最近 20 个周期。|
|-|-|N|最近 N 个周期。|
|-|指定|-|`period_to` 之前最近的 20 个周期。|
|-|指定|N|`period_to` 之前最近的 N 个周期。|
|指定|-|-|从 `period_from` 开始的前 20 个周期。|
|指定|-|N|从 `period_from` 开始的前 N 个周期。|
|指定|指定|-|指定范围内最多 100 个周期。|
|指定|指定|N|指定范围内的 N 个周期。|

[comment]: # ({/d1b5c39b-31bc72e4})

[comment]: # ({26e4fdd7-b46f1257})
### 返回值

`(object)` 返回计算结果。

|属性|[Type](/manual/api/reference_commentary#data-types)|描述|
|--|--|------|
|periods|array|已报告周期的列表。<br><br>每个已报告周期表示为一个对象，包含：<br>- `period_from` - `(timestamp)` 已报告周期的起始时间戳（包含）。<br>- `period_to` - `(timestamp)` 已报告周期的结束时间戳（不包含）。<br><br>各周期按 `period_from` 排序，最早的周期排在最前面。|
|serviceids|array|已报告周期中的服务 ID 列表。<br><br>该列表的排序顺序未定义。即使已将 `serviceids` 参数传递给 `sla.getsli` 方法也是如此。|
|sli|array|每个已报告周期和服务的 SLI 数据（以**二维数组**形式表示）。<br><br>`periods` 属性的索引用作 `sli` 属性的**第一**维。<br><br>`serviceids` 属性的索引用作 `sli` 属性的**第二**维。|

[comment]: # ({/26e4fdd7-b46f1257})

[comment]: # ({1b312b3d-ba0b5441})
#### SLI 数据

每个报告周期和服务返回的 SLI 数据包括：

|属性|[类型](/manual/api/reference_commentary#data-types)|说明|
|--|--|------|
|uptime|integer|在计划运行时间内，服务处于 _OK_ 状态的时长，减去被排除的停机时间。|
|downtime|integer|在计划运行时间内，服务处于 _not OK_ 状态的时长，减去被排除的停机时间。|
|sli|float|SLI（总运行时间的百分比），基于 uptime 和 downtime 计算。|
|error\_budget|integer|错误预算（以秒为单位），基于 SLI 和 SLO 计算。|
|excluded\_downtimes|array|此报告周期内被排除的停机时间数组。<br><br>每个对象将包含以下参数：<br>- `name` - `(string)` 被排除停机时间的名称。<br>- `period_from` - `(timestamp)` 被排除停机时间的起始时间戳（包含）。<br>- `period_to` - `(timestamp)` 被排除停机时间的结束时间戳（不包含）。<br><br>被排除的停机时间按 `period_from` 排序，最早的时间段排在最前面。|

[comment]: # ({/1b312b3d-ba0b5441})

[comment]: # ({b41637d2-b41637d2})
### 示例

[comment]: # ({/b41637d2-b41637d2})

[comment]: # ({b67c0708-610cd288})
#### 计算每日 SLA 的 SLI

检索与 ID 为“1”的 SLA 关联的、ID 为“1”和“4”的服务的 SLI 数据。
检索截至“1761861599”（2025 年 10 月 30 日 23:59:59 GMT+0200）的单个周期数据。
由于该 SLA 的[报告周期](/manual/it_services/sla)为每日，因此 SLI 数据将从“1761775200”（2025 年 10 月 30 日 00:00:00 GMT+0200）检索到“1761861600”（2025 年 10 月 31 日 00:00:00 GMT+0200）。

[请求](/manual/api#performing-requests)：

```json
{
    "jsonrpc": "2.0",
    "method": "sla.getsli",
    "params": {
        "slaid": "1",
        "serviceids": [
            1,
            4
        ],
        "periods": 1,
        "period_to": 1761861599
    },
    "id": 1
}
```

响应：

```json
{
    "jsonrpc": "2.0",
    "result": {
        "periods": [
            {
                "period_from": 1761775200,
                "period_to": 1761861600
            }
        ],
        "serviceids": [
            1,
            4
        ],
        "sli": [
            [
                {
                    "uptime": 43843,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": [
                        {
                            "name": "Maintenance OCT",
                            "period_from": 1761825600,
                            "period_to": 1761829200
                        }
                    ]
                },
                {
                    "uptime": 32225,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": []
                }
            ]
        ]
    },
    "id": 1
}
```

[comment]: # ({/b67c0708-610cd288})

[comment]: # ({d21e6bab-c6c818e1})
#### 计算月度 SLA 的 SLI

检索与 ID 为“5”的 SLA 关联的、ID 为“50”、“60”和“70”的服务的 SLI 数据。
从“1635724800”（2021 年 11 月 01 日 00:00:00 UTC）开始，检索三个周期的数据。
由于该 SLA 的[报告周期](/manual/it_services/sla)为按月，因此会检索以下三个月的 SLI 数据：

-   从“1635724800”（2021 年 11 月 01 日 00:00:00 UTC）到“1638316800”（2021 年 12 月 01 日 00:00:00 UTC）
-   从“1638316800”（2021 年 12 月 01 日 00:00:00 UTC）到“1640995200”（2022 年 01 月 01 日 00:00:00 UTC）
-   从“1640995200”（2022 年 01 月 01 日 00:00:00 UTC）到“1643673600”（2022 年 02 月 01 日 00:00:00 UTC）

[请求](/manual/api#performing-requests)：

```json
{
    "jsonrpc": "2.0",
    "method": "sla.getsli",
    "params": {
        "slaid": "5",
        "serviceids": [
            50,
            60,
            70
        ],
        "periods": 3,
        "period_from": 1635724800
    },
    "id": 1
}
```

响应：

```json
{
    "jsonrpc": "2.0",
    "result": {
        "periods": [
            {
                "period_from": 1635724800,
                "period_to": 1638316800
            },
            {
                "period_from": 1638316800,
                "period_to": 1640995200
            },
            {
                "period_from": 1640995200,
                "period_to": 1643673600
            }
        ],
        "serviceids": [
            50,
            60,
            70
        ],
        "sli": [
            [
                {
                    "uptime": 1186212,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": [
                        {
                            "name": "Maintenance Nov 25 - Dec 01",
                            "period_from": 1637836212,
                            "period_to": 1638316800
                        }
                    ]
                },
                {
                    "uptime": 1186212,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": [
                        {
                            "name": "Maintenance Nov 25 - Dec 01",
                            "period_from": 1637836212,
                            "period_to": 1638316800
                        }
                    ]
                },
                {
                    "uptime": 1186212,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": [
                        {
                            "name": "Maintenance Nov 25 - Dec 01",
                            "period_from": 1637836212,
                            "period_to": 1638316800
                        }
                    ]
                }
            ],
            [
                {
                    "uptime": 1147548,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": [
                        {
                            "name": "Maintenance Dec 02 - Dec 10",
                            "period_from": 1638439200,
                            "period_to": 1639109652
                        }
                    ]
                },
                {
                    "uptime": 1147548,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": [
                        {
                            "name": "Maintenance Dec 02 - Dec 10",
                            "period_from": 1638439200,
                            "period_to": 1639109652
                        }
                    ]
                },
                {
                    "uptime": 1147548,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": [
                        {
                            "name": "Maintenance Dec 02 - Dec 10",
                            "period_from": 1638439200,
                            "period_to": 1639109652
                        }
                    ]
                }
            ],
            [
                {
                    "uptime": 1674000,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": []
                },
                {
                    "uptime": 1674000,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": []
                },
                {
                    "uptime": 1674000,
                    "downtime": 0,
                    "sli": 100,
                    "error_budget": 0,
                    "excluded_downtimes": []
                }
            ]
        ]
    },
    "id": 1
}
```

[comment]: # ({/d21e6bab-c6c818e1})

[comment]: # ({13a91d62-a5414084})
### 来源

CSla::getSli() 在 *ui/include/classes/api/services/CSla.php* 。

[comment]: # ({/13a91d62-a5414084})
