用來匯出稽核資料的範例應用程式
此範例應用程式為 ThingWorx SchedulerThing,其使用 ThingWorx Marketplace「工具」部份的「SSH 檔案傳輸通訊協定」(SFTP) 延伸功能將匯出的稽核記錄推送至遠端 SFTP 伺服器。您可配置 SchedulerThing,使其以最適合您環境的時間與頻率執行。SchedulerThing 會保留名為 lastExportDate 的內容,此內容在每次匯出成功執行後都會更新。
ThingWorx 稽核子系統可管理稽核項目的匯出 (如下圖所示),說明如下:
平台產生的稽核資料會持續保留在 ThingWorx 資料庫中。您可依稽核類別與日期範圍查詢並篩選此資料。線上儲存區中的資料量受限於針對稽核子系統 DaysOnline 參數配置的保留時間。
系統會根據針對 AuditArchiveScheduler 物件指定的頻率定期封存線上稽核資料。系統會根據針對 PurgeAuditScheduler 物件指定的頻率定期清除稽核資料。這兩項操作的預設頻率都是每 24 小時執行一次。
使用此範例應用程式匯出稽核記錄時,ExportAuditData 服務包括對於封存目前線上稽核資料的呼叫。此呼叫可確保匯出的稽核資料對於已產生但尚未封存的稽核項目不存在任何間隙。
安裝及配置延伸功能
欲安裝及配置 SFTP 延伸功能,請遵循下列步驟:
1. ThingWorx Marketplace 下載 SSH File Transfer Protocol 延伸功能及該延伸功能的使用者指南
2. 依照《ThingWorx SFTP 延伸功能使用者指南》中的指示安裝延伸功能。
3. 從 ThingWorx Composer 中,建立一個新 SFTP 物件,並選取使用延伸功能匯入的 SFTPRepositoryTemplate。在 SFTP 物件的「組態」頁,配置所需遠端伺服器位置與認證。如需詳細資訊,請參閱延伸功能的使用者指南。
4. 從 Composer 中,建立一個使用排程器物範本的新物件。繼續下一部份,設定此排程器。
設定 SFTP Audit Exporter Scheduler
依照下列方式配置新排程器:
1. 「一般資訊」頁,配置下列內容:
名稱 - SFTPAuditExporter
物範本 - Scheduler
2. 「內容」頁,新增下列內容:
內容
基礎類型
物範本
具有預設值
持續
lastExportDate
DATETIME
N/A
已選取,預設值為 1/1/1970 00:00:00
已選取
targetRepository
THINGNAME
FileRepository
N/A
已選取
sftpThing
THINGNAME
SftpRepositoryTemplate
N/A
已選取
sftpDirectory
STRING
N/A
已選取,預設值為 *.*
已選取
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 物件。匯出稽核項目時,此應用程式會先將其暫存在本機,然後再傳送至配置的 SFTP 伺服器。完成傳輸後,會移除本機檔案。
sftpThing - 指定您在上述「安裝」部份步驟 3 中建立的 SFTP 物件。
sftpDirectory - 指定用來儲存稽核項目檔案之遠端 SFTP 伺服器上的父目錄。假設遠端伺服器對於每位使用者都有一個預設目錄,即預設值 *.*,其指定應使用預設目錄。如有需要,可變更此值。
此外,在此排程器的「組態」頁,也可選擇排程時間來調整何時執行匯出。依預設,排程會在每天午夜執行。
操作註記
將稽核項目儲存於遠端 SFTP 伺服器時,儲存的檔案將會命名為 AuditArchive-<年>-<月>-<日>-<小時><分鐘>.zip,其中的時間表示建立封存檔案的時間。每個 zip 檔案都包含一個內含本地化稽核記錄的 JSON 格式檔案。系統會使用 <年>-<月> 的命名慣例,將封存檔案按月組織到子目錄中。例如,2019-08
欲變更用於稽核項目的地區設定,請編輯排程器指令集,並設定 exportParams 定義中的 locale 欄位。
* 
依預設,ThingWorx 平台上的指令集逾時設定為 30 秒。如果指令集的執行時間比該時間長,則平台會終止執行。ThingWorx 管理員可以在 platform-settings.json 組態檔案的基本設定部份中配置指令集逾時。請參閱 platform-settings.json 組態詳細資訊