監査データエクスポートのサンプルアプリケーション
このサンプルアプリケーションは、 ThingWorx Marketplace の「Tools」セクションにある SSH File Transfer Protocol (SFTP) 拡張機能を使用して、エクスポートされた監査レコードをリモート SFTP サーバーにプッシュする ThingWorx SchedulerThing です。自分の環境に最適な時刻と頻度で実行するよう SchedulerThing を設定できます。SchedulerThing によってプロパティ lastExportDate が管理され、このプロパティはエクスポートが正常に実行されるたびに更新されます。
ThingWorx 監査サブシステムは、以下の図と説明で示されているように、監査エントリのエクスポートを管理します。
プラットフォームによって生成された監査データは ThingWorx データベース内で永続化されます。このデータは監査カテゴリと日付範囲を指定して照会およびフィルタできます。オンラインストレージ内のデータ量は、監査サブシステムの DaysOnline パラメータで設定されている保持期間による制約があります。
AuditArchiveScheduler Thing に指定されている頻度に基づいて、オンライン監査データが定期的にアーカイブされます。PurgeAuditScheduler Thing に指定されている頻度に基づいて、監査データが定期的にパージされます。両方の操作のデフォルトの頻度は 24 時間ごとです。
このサンプルアプリケーションを使用して監査レコードをエクスポートした場合、ExportAuditData サービスには最新のオンライン監査データをアーカイブする呼び出しが含まれます。この呼び出しにより、エクスポートされた監査データには、生成されたがまだアーカイブされていない監査エントリによるギャップがなくなります。
拡張機能のインストールとコンフィギュレーション
SFTP 拡張機能をインストールして設定するには、次の手順に従います。
1. "SSH File Transfer Protocol" 拡張機能およびこの拡張機能のユーザーガイドを ThingWorx Marketplace からダウンロードします
2. ThingWorx SFTP Extension User Guide の指示に従ってこの拡張機能をインストールします。
3. ThingWorx Composer から、新しい SFTP Thing を作成し、この拡張機能とともにインポートされた SFTPRepositoryTemplate を選択します。SFTP Thing の「コンフィギュレーション」ページで、目的のリモートサーバーの場所と資格証明を設定します。詳細については、この拡張機能のユーザーガイドを参照してください。
4. Composer から、スケジューラ Thing Template を使用する新しい Thing を作成します。次のセクションに進んでこの Scheduler をセットアップします。
SFTP 監査エクスポータ Scheduler のセットアップ
新しい Scheduler を以下のように設定します。
1. 「一般情報」ページで、以下のプロパティを設定します。
名前 - SFTPAuditExporter
Thing Template - Scheduler
2. 「プロパティ」ページで、以下のプロパティを追加します。
プロパティ
ベースタイプ
Thing Template
デフォルト値あり
永続
lastExportDate
DATETIME
該当なし
オン、デフォルト値は 1/1/1970 00:00:00
オン
targetRepository
THINGNAME
FileRepository
該当なし
オン
sftpThing
THINGNAME
SftpRepositoryTemplate
該当なし
オン
sftpDirectory
STRING
該当なし
オン、デフォルト値は *.*
オン
3. 「購読」ページで、以下の購読情報とコードを追加します。
「ソース」 - このフィールドは空白のままにします。
「イベント」 - ScheduledEvent を指定します。
「有効」 - このチェックボックスをオンにします。
「スクリプト」 - 以下に示すコードブロックからコードを追加します。
「スクリプト」フィールドにコピーするスクリプトを以下に示します。

// Sample script for a scheduleThing that will periodically call the ExportAuditData service on the
// AuditSubsystem to capture audit records prior to those records aging off the system.
// These then are transferred to an external SFTP server using the SFTP Thingworx Extension.

logger.info("Audit export starting");

// Property validation checks
if (me.sftpThing == undefined) {
throw "sftpThing property not set. Cannot continue.";
}
if (me.targetRepository == undefined) {
throw "targetRepository property not set. Cannot continue.";
}
if (me.sftpDirectory == undefined || me.sftpDirectory == null || me.sftpDirectory == "") {
// set a default value if not setup previously.
me.sftpDirectory = ".";
}

var exportEndTime = new Date();
var uploadDirFormatted = dateFormat(exportEndTime, "yyyy-MM"); // store in directories by month

var exportParams = {
startDate: me.lastExportDate, // Start after the last successsful export
endDate: exportEndTime, // Use the end time recorded above
targetRepositoryName: me.targetRepository, // destination repository from the scheduler's properties

// Set the targetPath to custom folder for exporting. The files will be deleted from this folder on
// completion of the export.
targetPath: "scheduledExport",

// Add the current date to the targetFileName
// The targetFileName must be unique. The ExportAuditData service will not overwrite an existing file.
targetFileName: "AuditArchive-" + dateFormat(exportEndTime, "yyyy-MM-dd-HHmm"),

// The locale for the exported audit messages. If undefined, the system default is used
locale: undefined
};

try {

Subsystems["AuditSubsystem"].ExportAuditData(exportParams);

// The export to the target repository now needs to be transferred to an external location.
// This example uses the SFTP Extension to transfer to a remote SFTP server.
// The location and credentials for the SFTP server are configured on the SFTP Thing.

// Check to make sure the destination directory exists as the SFTP server will not auto create it.
var checkDirParams = {
path: me.sftpDirectory
};
var findDirObj = {
name: uploadDirFormatted
};
if (Things[me.sftpThing].ListDirectories(checkDirParams).Find(findDirObj) == undefined) {
var createDirParams = {
path: me.sftpDirectory + "/" + uploadDirFormatted
};
Things[me.sftpThing].CreateFolder(createDirParams);
}

var uploadParams = {
FileRepository: me.targetRepository,
RepoFilePath: exportParams.targetPath + "/" + exportParams.targetFileName + ".zip",
RemoteFilePath: me.sftpDirectory + "/" + uploadDirFormatted + "/" + exportParams.targetFileName + ".zip"
};
Things[me.sftpThing].UploadFile(uploadParams);

// Upon successfully storing the export file, update the start time
// breadcrumb to the time the export was started.
me.lastExportDate = exportEndTime;

} catch (err) {
logger.error("Failed to export the auditing records: " + err);
} finally {
// Delete the temporary export file from the local repository
var deleteFileParams = {
path: exportParams.targetPath + "/" + exportParams.targetFileName + ".zip"
};
Things[me.targetRepository].DeleteFile(deleteFileParams);
}
logger.info("Audit export completed");
コンフィギュレーション
SFTPAuditExporter には、使用する前に設定する必要がある以下のコンフィギュレーションプロパティがあります。
targetRepository - エクスポートされた監査エントリの一時的な場所として使用する ThingWorx FileRepository Thing を指定します。監査エントリをエクスポートする際に、このアプリケーションは設定済みの SFTP サーバーに送信する前にこれらの監査エントリをローカルにステージします。転送が完了すると、ローカルファイルは除去されます。
sftpThing - 上の「インストール」セクションの手順 3 で作成した SFTP Thing を指定します。
sftpDirectory - 監査エントリファイルの保存先となる、リモート SFTP サーバー上の親ディレクトリを指定します。リモートサーバーにユーザーごとのデフォルトディレクトリがあるとした場合、デフォルト値 *.* ではそのデフォルトディレクトリが使用されます。この値は必要に応じて変更できます。
さらに、この Scheduler の「コンフィギュレーション」ページで、スケジュール時刻を選択して、エクスポートを実行するタイミングを調整できます。デフォルトでは、毎日午前 0 時に実行されます。
操作に関する注意事項
監査エントリがリモート SFTP サーバーに保存されると、保存されたファイルには AuditArchive-<年>-<月>-<日>-<時間><分>.zip という名前が付きます。この時間はアーカイブファイルが作成された時刻を示します。各 ZIP ファイルには、単一の JSON フォーマットのファイルとローカライズ済み監査レコードが含まれています。アーカイブファイルは、<年>-<月> (例: 2019-08) という命名規則を使用した月別のサブディレクトリに分かれています。
監査エントリに使用されるロケールを変更するには、Scheduler スクリプトを編集し、exportParams 定義で locale フィールドを設定します。
* 
デフォルトでは、ThingWorx プラットフォームでのスクリプトのタイムアウトの設定は 30 秒です。スクリプトがこれよりも長く実行されると、プラットフォームが実行を強制終了します。ThingWorx 管理者は、platform-settings.json コンフィギュレーションファイルの基本設定セクションでスクリプトのタイムアウトを設定できます。「 platform-settings.json コンフィギュレーションの詳細」も参照してください。