Processing Policy: GatewayScript Actions to invoke REST APIs
This section will configure the Processing Policy that will contain the code to invoke the statistics REST APIs.
7. First configure the Processing Policy. In the MPGW, click on the add (+) icon from the Multi-Protocol Gateway Policy
8. Name the Policy once the Configure Multi-Protocol Gateway Style Policy pops up (e.g. GetStat_Policy), and name the first rule (e.g. GetStat_Policy_rule). Drop down the Rule Direction and select Client to Server.
9. Double click on the Match Action Action
, click on the add (+) icon the create a new Matching Rule, and add a new URL match rule for */getstat. Once added, Apply the changes for each screen until you get back to the Configure Multi-Protocol Gateway Style Policy panel.
10. setDeviceName.xsl: The setDeviceName stylesheet extracts the appliance name that is set in the System Settings section (Appliance name), and will set it to a context variable to be used down stream.
Locate the Transform Action (
), drag, and drop the Transform Action after the Match Action.
11. In the Transform Action, use the following:
● Drop down and select INPUT for the Input.
● Select Transform with XSLT style sheet for the Use Document Processing Instructions.
● In the Transform File section drop down the first drop down section to select local:///GetStat (if the GetStat folder if not available, open the File Management section in another browser window to add the GetStat subfolder under local:///).
● Upload the setDeviceName.xsl to the local:///GetStat folder. The code could be found in the appendix section titled setDeviceName.xsl at the bottom of this doc.
● Drop down and select NULL for the Output.
Click Done when completed.
12. getMem.js: The getMem script is a gatewayscript which will invoke the current appliances REST interface to pull the memory usage statistics. The code could be found in the appendix under getMem.js.
Locate the GatewayScript Action, drag, and drop it after the Transform Action (the right side of the Transform Action):
13. In the GatewayScript Action (double-click to open), use the following:
● Drop down and select INPUT for Input.
● In the GatewayScript file section, drop down the first drop down to select local:///Getstat folder, and upload the getMem.js script.
● Drop down and select OUTPUT for Output.
Click Done when complete.
14. Continue the same steps to add the other scripts to run the other REST APIs.
NOTE: Be mindful of the REST API uri, some will require different domains, such as the getServicesMemStatus.js in the appendix.
15. After adding all the GatewayScript Actions, drag and drop the Advanced Action icon over to the right of the last GatewayScript Action to set a loopback variable to the Processing Rule.
16. Double click on the Advanced Action, select the Set Variable action type and click Next.
17. Enter service/mpgw/skip-backside for the Variable Name and 1 for the Variable Assignment. Once complete, click on Done and Apply Changes in the Processing Policy.
18. After all the changes, you should be back in the main MPGW, Apply the changes, and Save Config.
Appendix
setDeviceName.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tns="http://www.example.org/tns" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes="dp">
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="/">
<!-- Getting the appliance device name which is set by DataPower on var://service/system/ident -->
<xsl:variable name="vDeviceName">
<xsl:value-of select="dp:variable('var://service/system/ident')//*[local-name()='device-name']" />
</xsl:variable>
<!-- Set the device name to var://context/getstat/devicename to be used in a GatewayScript -->
<dp:set-variable name="'var://context/getstat/devicename'" value="$vDeviceName" />
</xsl:template>
</xsl:stylesheet>
getMem.js
/*
Script Name: getMem.js
Purpose: Get Statistics that cannot be produced by the log target on DataPower. The getMem.js is to obtain the system memory usage.
**Dependencies:
1. Ensure the logging category is set on the appliances Log Category section of the appliance, otherwise the log category will not exist for this event to be logged.
2. Ensure the setDeviceName.xsl is used in the service otherwise the context variable will not be found, and the script will fail.
3. Ensure the XML Manager > User Agent contains the SSL Client Profile and Basic-Auth which is used because the REST call requires https connection, and DataPower username/pass authentication. The call will fail without it.
*/
//Properties:
var vHost = "https://MGMT:5554/mgmt/status/default/MemoryStatus";
var vLogCategory = {'category':'GetStatCategory'};
var sm = require('service-metadata');
var hm = require('header-metadata');
var urlopen = require('urlopen');
var ct = hm.current.get('Content-Type');
var ctx = session.name('getstat') || session.createContext('getstat');
var vAppliance = ctx.getVar('devicename').replace('<?xml version="1.0" encoding="UTF-8"?>','') || 0;
var vDomainName = sm.getVar("var://service/domain-name");
var vXMLContentType = "application/json";
// define the urlopen options
var options = {
target : vHost,
method : 'GET',
contentType : vXMLContentType,
timeout : 2
};
// open connection to target and send data over
urlopen.open(options, function (error, response) {
if (error) {
// an error occurred during request sending or response header parsing
console.log('urlopen error: ' + error);
session.output.write("urlopen connect error: " + error);
} else {
// read response data
// get the response status code
var responseStatusCode = response.statusCode;
if (responseStatusCode == 200) {
response.readAsJSON(function(err, readAsJSONResponse) {
if (err) {
session.reject("readAsJSON error: " + JSON.stringify(err));
} else {
session.output.write(" Success ");
console.options(vLogCategory).log("Appliance: " + vAppliance + ", MemoryUsage: " + JSON.stringify(readAsJSONResponse.MemoryStatus.Usage));
}
});
};
}
});
getCPU.js
/*
Script Name: getCPU.js
Purpose: Get Statistics that cannot be produced by the log target on DataPower.
**Dependencies:
1. Ensure the logging category is set on the appliances Log Category section of the appliance, otherwise the log category will not exist for this event to be logged.
2. Ensure the setDeviceName.xsl is used in the service otherwise the context variable will not be found, and the script will fail.
3. Ensure the XML Manager > User Agent contains the SSL Client Profile and Basic-Auth which is used because the REST call requires https connection, and DataPower username/pass authentication. The call will fail without it.
*/
//Properties:
var vHost = "https://MGMT:5554/mgmt/status/default/CPUUsage";
var vLogCategory = {
'category': 'GetStatCategory'
};
var sm = require('service-metadata');
var hm = require('header-metadata');
var urlopen = require('urlopen');
var ct = hm.current.get('Content-Type');
var ctx = session.name('getstat') || session.createContext('getstat');
var vAppliance = ctx.getVar('devicename').replace('<?xml version="1.0" encoding="UTF-8"?>','') || 0;
var vDomainName = sm.getVar("var://service/domain-name");
var vXMLContentType = "application/json";
// define the urlopen options
var options = {
target : vHost,
method : 'GET',
contentType : vXMLContentType,
timeout : 2
};
// open connection to target and send data over
urlopen.open(options, function (error, response) {
if (error) {
// an error occurred during request sending or response header parsing
console.log('urlopen error: ' + error);
session.output.write("urlopen connect error: " + error);
} else {
// read response data
// get the response status code
var responseStatusCode = response.statusCode;
if (responseStatusCode == 200) {
response.readAsJSON(function(err, readAsJSONResponse) {
if (err) {
session.reject("readAsJSON error: " + JSON.stringify(err));
} else {
session.output.write(" Success ");
console.options(vLogCategory).log("Appliance: " + vAppliance + ", CPUUsage: " + JSON.stringify(readAsJSONResponse.CPUUsage.tenSeconds));
}
});
};
}
});
getServiceMemStatus.js
/*
Script Name: getServicesMemStatus.js
Purpose: Get Statistics that cannot be produced by the log target on DataPower.
**Dependencies:
1. Ensure the logging category is set on the appliances Log Category section of the appliance, otherwise the log category will not exist for this event to be logged.
2. Ensure the setDeviceName.xsl is used in the service otherwise the context variable will not be found, and the script will fail.
3. Ensure the XML Manager > User Agent contains the SSL Client Profile and Basic-Auth which is used because the REST call requires https connection, and DataPower username/pass authentication. The call will fail without it.
4. Ensure the REST API url is update to reflect the proper domain. For example, in this script there is a domain in the URL which will need updating when migrating to staging and production.
*/
//Properties:
var vHost = "https://MGMT:5554/mgmt/status/<domain_here>/ServicesMemoryStatus2";
var vLogCategory = {
'category': 'GetStatCategory'
};
var sm = require('service-metadata');
var hm = require('header-metadata');
var urlopen = require('urlopen');
var ct = hm.current.get('Content-Type');
var ctx = session.name('getstat') || session.createContext('getstat');
var vAppliance = ctx.getVar('devicename').replace('<?xml version="1.0" encoding="UTF-8"?>', '') || 0;
var vDomainName = sm.getVar("var://service/domain-name");
var vXMLContentType = "application/json";
// define the urlopen options
var options = {
target: vHost,
method: 'GET',
contentType: vXMLContentType,
timeout: 2
};
// open connection to target and send data over
urlopen.open(options, function (error, response) {
if (error) {
// an error occurred during request sending or response header parsing
console.log('urlopen error: ' + error);
session.output.write("urlopen connect error: " + error);
} else {
// read response data
// get the response status code
var responseStatusCode = response.statusCode;
if (responseStatusCode == 200) {
response.readAsJSON(function (err, readAsJSONResponse) {
if (err) {
session.reject("readAsJSON error: " + JSON.stringify(err));
} else {
session.output.write(" Success ");
var vServiceMemoryStatus = readAsJSONResponse.ServicesMemoryStatus2;
for (var i = 0; i < vServiceMemoryStatus.length; i++) {
console.options(vLogCategory).log('Appliance: ' + vAppliance + ', Services Memory Status: ' + '[{"serviceName": "' + vServiceMemoryStatus[i].serviceName + '", "currentMemory": "' + vServiceMemoryStatus[i].current + '"}]');
};
}
});
};
}
});
getDomainMemStatus.js
/*
Script Name: getDomainMemStatus.js
Purpose: Get Statistics that cannot be produced by the log target on DataPower.
**Dependencies:
1. Ensure the logging category is set on the appliances Log Category section of the appliance, otherwise the log category will not exist for this event to be logged.
2. Ensure the setDeviceName.xsl is used in the service otherwise the context variable will not be found, and the script will fail.
3. Ensure the XML Manager > User Agent contains the SSL Client Profile and Basic-Auth which is used because the REST call requires https connection, and DataPower username/pass authentication. The call will fail without it.
*/
//Properties:
var vHost = "https://MGMT:5554/mgmt/status/default/DomainsMemoryStatus2";
var vLogCategory = {'category':'GetStatCategory'};
var sm = require('service-metadata');
var hm = require('header-metadata');
var urlopen = require('urlopen');
var ct = hm.current.get('Content-Type');
var ctx = session.name('getstat') || session.createContext('getstat');
var vAppliance = ctx.getVar('devicename').replace('<?xml version="1.0" encoding="UTF-8"?>','') || 0;
var vDomainName = sm.getVar("var://service/domain-name");
var vXMLContentType = "application/json";
// define the urlopen options
var options = {
target : vHost,
method : 'GET',
contentType : vXMLContentType,
timeout : 2
};
// open connection to target and send data over
urlopen.open(options, function (error, response) {
if (error) {
// an error occurred during request sending or response header parsing
console.log('urlopen error: ' + error);
session.output.write("urlopen connect error: " + error);
} else {
// read response data
// get the response status code
var responseStatusCode = response.statusCode;
if (responseStatusCode == 200) {
response.readAsJSON(function(err, readAsJSONResponse) {
if (err) {
session.reject("readAsJSON error: " + JSON.stringify(err));
} else {
session.output.write(" Success ");
var vDomainMemoryStatus = readAsJSONResponse.DomainsMemoryStatus2;
for (var i = 0; i < vDomainMemoryStatus.length; i++) {
console.options(vLogCategory).log("Appliance: " + vAppliance
+ ", Domain Memory Status: "
+ "[{Domain: "
+ vDomainMemoryStatus[i].Domain
+ ", ServicesCurrent: "
+ vDomainMemoryStatus[i].ServicesCurrent
+ "}]");
}
}
});
};
}
});
callGetStat.xsl
<!--
Script Name: callGetStat.xsl
Purpose: Get statistics which are not available from logging to output in syslogs. Stylesheet can be placed in XML Manager
-->
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions"
xmlns:dpconfig="http://www.datapower.com/param/config"
xmlns:webapi="http://www.ibm.com/apimanagement"
xmlns:func="http://exslt.org/functions"
xmlns:wxsl="http://www.w3.org/1999/XSL/TransformAlias"
xmlns:exsl="http://exslt.org/common" xmlns:client="http://datapower.com/sslClientProfile"
extension-element-prefixes="dp dpconfig exsl">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<dp:url-open
target="http://127.0.0.1:8888/getstat"
response="ignore"
http-method="get"
>
</dp:url-open>
</xsl:template>
</xsl:stylesheet>