用于导出审计数据的应用示例
此应用示例为 ThingWorx SchedulerThing,使用 ThingWorx Marketplace“Tools”部分的 SSH File Transfer Protocol (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 中创建使用 Scheduler 事物模板的新事物。请继续查看下一部分内容以设置此 Scheduler。
SFTP Audit Exporter Scheduler 的设置
按以下步骤配置新的 Scheduler:
1. “常规信息”页面中,配置以下属性:
名称 - SFTPAuditExporter
事物模板 - Scheduler
2. “属性”页面中,添加以下属性:
属性
基本类型
事物模板
HasDefaultValue
持久化
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 事物。导出审计条目时,此应用程序会在发送至已配置的 SFTP 服务器之前对其进行本地组织。传输完成后,系统将移除本地文件。
sftpThing - 指定上述“安装”部分第 3 步中创建的 SFTP 事物。
sftpDirectory - 指定要在其中存储审计条目文件的远程 SFTP 服务器上的父目录。假定远程服务器具有针对每个用户的默认目录 (默认值为 *.*),则指定应使用的默认目录。如果需要,可更改该值。
此外,在此 Scheduler 的“配置”页面上,可选择计划时间以调整执行导出的时间。默认情况下,每天午夜执行计划。
操作说明
在远程 SFTP 服务器上保存审计条目时,保存的文件将命名为 AuditArchive-<年>-<月>-<日>-<小时><分钟>.zip,其中时间表示存档文件的创建时间。每个 zip 文件均包含一个具有本地化审计记录的 JSON 格式文件。存档文件按月组织到子目录中,使用的命名约定为 <年>-<月>。例如,2019-08
要更改用于审计条目的区域设置,请编辑 Scheduler 脚本并设置 exportParams 定义中的 locale 字段。
* 
默认情况下,ThingWorx 平台上的脚本超时设置为 30 秒。如果脚本运行时间超过此时间限制,则平台会终止执行。ThingWorx 管理员可以在 platform-settings.json 配置文件的“基本设置”部分配置脚本超时。另请参见 plateform-settings.json 配置详细信息