# ログ管理

### Azure Monitor へのログ記録を有効にする

バージョン 3.0 以降、SCEPman と Certificate Master は、Microsoft の Log Ingestion API を使用してログを Azure Monitor に書き込みます。これには、データを保持して分析を可能にする Log Analytics Workspace の概念と、App Service とログストレージの間で連携する Data Collection Rule が使用されます。これにより、SCEPman が LAW にアクセスするための RBAC ベースのアクセス許可を含む、より最新のアプローチが可能になります。

Log Analytics Workspace の作成と Data Collection Rule の構成は、次を実行することで自動的に行われます `Complete-SCEPmanInstallation` SCEPman PowerShell モジュールの

{% hint style="info" %}
この **既定の保持** Log Analytics Table に保存されるデータの期間は **30日**です。別の保持期間が必要な場合は、Table "SCEPman\_CL" の構成をそれに応じて調整してください。
{% endhint %}

#### Data Collector API の再有効化

何らかの理由で、以前の API を再度使用したい場合は、Log Ingestion 関連の app service 変数を削除し、Data Collector API で使用するものを再度追加することで可能です。

削除する **変数**:

* [AppConfig:LoggingConfig:DataCollectionEndpointUri](https://app.gitbook.com/o/-LhPlvZ6dc8XcqY7tdZw/s/-LoGejQeUQcw7lqnQ3WX/~/edit/~/changes/787/scepman-configuration/application-settings/dependencies-azure-services/logging#appconfig-loggingconfig-datacollectionendpointuri)
* [AppConfig:LoggingConfig:RuleId](https://app.gitbook.com/o/-LhPlvZ6dc8XcqY7tdZw/s/-LoGejQeUQcw7lqnQ3WX/~/edit/~/changes/787/scepman-configuration/application-settings/dependencies-azure-services/logging#appconfig-loggingconfig-ruleid)

追加する変数:

* [AppConfig:LoggingConfig:WorkspaceId](https://app.gitbook.com/o/-LhPlvZ6dc8XcqY7tdZw/s/-LoGejQeUQcw7lqnQ3WX/~/edit/~/changes/787/scepman-configuration/application-settings/dependencies-azure-services/logging#appconfig-loggingconfig-workspaceid)
* [AppConfig:LoggingConfig:SharedKey](https://app.gitbook.com/o/-LhPlvZ6dc8XcqY7tdZw/s/-LoGejQeUQcw7lqnQ3WX/~/edit/~/changes/787/scepman-configuration/application-settings/dependencies-azure-services/logging#appconfig-loggingconfig-sharedkey)

SCEPman は再起動後に設定を自動的に取得し、再び Data Collector API を利用します。

## KQL クエリの例

### SCEPman インスタンスの問題を確認する

```kusto
SCEPman_CL
| where Level == "Warn" or Level == "Error" or Level == "Fatal"
```

### 選択した期間におけるエンドポイント別の発行済み証明書数

{% hint style="success" %}
このクエリは、ログ記録に Log Ingestion API を使用している場合、SCEPman 3.0 以降で確実に動作します。このクエリを使用できなくする SCEPman への変更は、Breaking Changes と見なされます。
{% endhint %}

{% tabs %}
{% tab title="Log Ingestion API ( 既定 )" %}

```kql
SCEPman_CL
| where Level == "Info" and Message startswith_cs "Issued a certificate with serial number"
| project Message, RequestBase = trim_end('/', replace_string(replace_string(replace_regex(RequestUrl, "(/pkiclient\\.exe)?(\\?operation=PKIOperation(&message=.+)?)?", ""),"certsrv/mscep/mscep.dll","intune"),"step/enrollment","activedirectory"))
| summarize IssuanceCount = count() by Endpoint = extract("/([a-zA-Z]+)$", 1, RequestBase)
```

{% endtab %}

{% tab title="Data Collector API ( 旧 )" %}

```kusto
SCEPman_CL
| where Level == "Info" and Message startswith_cs "Issued a certificate with serial number"
| project Message, RequestBase = trim_end('/', replace_string(replace_string(replace_regex(RequestUrl_s, "(/pkiclient\\.exe)?(\\?operation=PKIOperation(&message=.+)?)?", ""),"certsrv/mscep/mscep.dll","intune"),"step/enrollment","activedirectory"))
| summarize IssuanceCount = count() by Endpoint = extract("/([a-zA-Z]+)$", 1, RequestBase)
```

{% endtab %}
{% endtabs %}

SCEPman 2.8 以降では、発行された各証明書ごとに、ログメッセージが "Issued a certificate with serial number " で始まる Info レベルのログエントリが常にちょうど 1 つ存在し、その後にシリアル番号が続きます。ただし、解決不能な [二軍問題](https://en.wikipedia.org/wiki/Two_Generals%27_Problem)により、作成された証明書が要求元に届かなかったり、他の種類のエラーによって実際の登録が妨げられたりすることがあります。同様に、重大なエラーの場合、ログエントリは存在するのに対応するデータベースエントリが存在しない、またはその逆が起こることがあります。

### OCSP チェック付きの個別証明書

{% tabs %}
{% tab title="Log Ingestion API ( 既定 )" %}

```kusto
let map_certtype = datatable(serial_start:string, readable:string)
[
  "40", "Intune Device",
  "41", "Intune Device",
  "42", "Intune Incompliant Device",
  "50", "Static",
  "51", "Static",
  "60", "Intune User",
  "61", "Intune User",
  "64", "Jamf User",
  "65", "Jamf User",
  "6C", "Jamf User on Device",
  "6D", "Jamf User on Device",
  "70", "Domain Controller",
  "7C", "Jamf User on Computer",
  "7D", "Jamf User on Computer",
  "54", "Jamf Computer",
  "55", "Jamf Computer",
  "44", "Jamf Device",
  "45", "Jamf Device"
];
SCEPman_CL
| where LogCategory == "Scepman.Server.Controllers.OcspController" and Level == "Info"
| where Message startswith_cs "OCSP Response"
| project serial = extract("Serial Number ([A-F0-9]+)", 1, Message)
| distinct serial
| extend serial_start = substring(serial,0,2)
| join kind=leftouter map_certtype on serial_start
| summarize count() by (readable)
```

{% endtab %}

{% tab title="Data Collector API ( 旧 )" %}

```kql
let map_certtype = datatable(serial_start:string, readable:string)
[
  "40", "Intune Device",
  "41", "Intune Device",
  "42", "Intune Incompliant Device",
  "50", "Static",
  "51", "Static",
  "60", "Intune User",
  "61", "Intune User",
  "64", "Jamf User",
  "65", "Jamf User",
  "6C", "Jamf User on Device",
  "6D", "Jamf User on Device",
  "70", "Domain Controller",
  "7C", "Jamf User on Computer",
  "7D", "Jamf User on Computer",
  "54", "Jamf Computer",
  "55", "Jamf Computer",
  "44", "Jamf Device",
  "45", "Jamf Device"
];
SCEPman_CL
| where LogCategory_s == "Scepman.Server.Controllers.OcspController" and Level == "Info"
| where Message startswith_cs "OCSP Response"
| project serial = extract("Serial Number ([A-F0-9]+)", 1, Message)
| distinct serial
| extend serial_start = substring(serial,0,2)
| join kind=leftouter map_certtype on serial_start
| summarize count() by (readable)
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.scepman.com/ja/azure-gou-cheng/log-configuration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
