工作流可以通过调用 audit() 函数创建审计记录。audit 函数唯一的参数是要放置到审计日志中的字符串。
只有当 setid 设置为 false 时,使用 audit() 函数才会显示执行了工作流的实际用户。但是,如果工作流归 root 拥有并且 setid 设置为 true,则审计日志会将 root 显示为用户,即使工作流是由其他用户返回的也是如此。
无论 setid 设置成什么,要确定正在执行工作流的用户,请使用 whoami() 函数。
示例 24 工作流测试 whoami 函数var workflow = { name: "Test whoami", description: "Print current username", execute: function () { return ("Hello " + whoami()); } };
对于执行时间较长的复杂工作流,向执行工作流的用户提供明确的进度信息可能比较有用。要允许通过这种方式报告工作流的执行情况,execute 成员应该返回步骤数组。每个数组元素必须包含以下成员:
|
与整个工作流上的 execute 函数一样,每个步骤的 execute 成员都将包含工作流参数的对象作为参数。
示例 25 工作流执行报告例如,以下工作流通过三个步骤创建新的项目、共享资源和审计记录:
var steps = [ { step: 'Checking for associated project', execute: function (params) { try { run('shares select ' + params.unit); } catch (err) { if (err.code != EAKSH_ENTITY_BADSELECT) throw (err); /* * We haven't yet created a project that corresponds to * this business unit; create it now. */ run('shares project ' + params.unit); set('mountpoint', '/export/' + params.unit); run('commit'); run('shares select ' + params.unit); } } }, { step: 'Creating share', execute: function (params) { run('filesystem ' + params.name); run('commit'); } }, { step: 'Creating audit record', execute: function (params) { audit('created "' + params.name + '" in "' + params.unit); } } ]; var workflow = { name: 'Create share', description: 'Creates a new share in a business unit', parameters: { name: { label: 'Name of new share', type: 'String' }, unit: { label: 'Business unit', type: 'ChooseOne', options: [ 'development', 'finance', 'qa', 'sales' ], optionlabels: [ 'Development', 'Finance', 'Quality Assurance', 'Sales/Administrative' ], } }, validate: function (params) { try { run('shares select ' + params.unit); run('select ' + params.name); } catch (err) { if (err.code == EAKSH_ENTITY_BADSELECT) return; } return ({ name: 'share already exists' }); }, execute: function (params) { return (steps); } };
通过使用邮件功能,工作流可以通过电子邮件交付工作流的某些输出。邮件功能必须包含以下参数:具有 to 和 subject 参数的对象,以及一个 messageBody 字符串。
示例 26 通过邮件程序执行工作流var workflow = { name: 'email controller state', description: 'email controller state', execute: function () { // verify state of the controller var faulted = run('maintenance hardware "chassis-000" get faulted'); var messageBody = faulted; emailAddress = 'first.last@xyz.com'; subjectLine = 'Controller State'; mail({To: emailAddress, Subject: subjectLine}, messageBody); } };