Introduction:
Debugging large TPL patterns can be very difficult as each time you want to test them, these need to be uploaded, complied and then run. That causes problems when it comes to parse large chunks of text or to perform calculations. Sometimes a workaround for that could be to parse an output on a remote host using shell commands and then just receive formatted output. This is not always straightforward as when executing SQL queries their output would need to be sent for the remote host for further transformation. Additionally long "one-liners" could be difficult to maintain or change later on. More suitable way is to send data directly to the appliance, where a custom script processes it and then returns calculated value. This also benefits with the fact that we can use local scripts written in any supported by Red Hat language.
Case study:
A case study where I found it useful and I had to face TPL limiations was something as simple as convertng units. TPL does not support floating point numbers. (
https://communities.bmc.com/thread/102496?start=0&tstart=0) That makes conversion almost impossible. In my case study the output I received came from an SQL query and contained schema sizes which the queried database, displayed as formatted adequately to their sizes, like: 143KB, 25MB, 0,5GB etc... The customer demand was to receive them as MB. Changing the query itself was not possible due to technical limitations and permissions of discovery user had on the database level. A simple solution to that was just a tiny python script deployed on the appliance that by parsing an input given in its parameter returned converted value as the output.
Example:
Let's try here a much simpler "hello world" scenario, that you can replace by any needed in your case. As the parameter let's use a customer name, and formatting it would end with a message "Hello $User". We just attach the output directly to a host node for simplicity. First we need to configure the appliance:
Appliance Configuration
- You need to be able to scan the appliance by itself. Please ensure that the Tideway credentials are configured on the appliance. Additionally the appliance has to be already discovered by itself.
- As there is no system variable that you can use for identifying the appliance, on which you are executing the TPL code (here is my idea of having it: https://communities.bmc.com/ideas/17047), you need to workaround it a bit and configure one of the appliance's options and put there its name.
- Let's use the appliance name field from identification tab on https://(appliance)/ui/SetupApplianceInfo
- The name should allow you to find the appliance's host node by executing a query from TPL. The easiest way is to put there its hostname or fqdn (depends what matches to Host.name for this appliance node).
Local Script
For this example we use a simple shell script, still anything that can be executed on an appliance should work (so java, python etc...).
echo Hello $1
Save it as /usr/tideway/custom/helloWorld.sh
and make it executable
chmod u+x /usr/tideway/custom/helloWorld.sh
You can test if it works:
/usr/tideway/custom/helloWorld.sh Jarek
Hello Jarek
TPL pattern:
tpl 1.13 module executeLocal;
pattern executeLocal 1.0
"""
Test how to execute a local script while scanning a remote host.
"""
metadata
products := "BMCDiscovery";
publishers := "https://itsystemengineer.blogspot.com/";
end metadata;
overview
tags test,bmc,jarek;
end overview;
triggers
on remoteHostObject := Host created,confirmed;
end triggers;
body
thisApplianceName := system.getOption("APPLIANCE_NAME");
thisApplianceObjectList := search(Host where name=%thisApplianceName%);
if thisApplianceObjectList and thisApplianceObjectList[0] then
thisApplianceObject := thisApplianceObjectList[0];
myName := "Jarek";
cmd := " sh /usr/tideway/custom/helloWorld.sh %myName%";
helloMessage := discovery.runCommand(thisApplianceObject,cmd);
if helloMessage and helloMessage.result then
remoteHostObject.hello_message := helloMessage.result;
end if;
end if;
end body;
end pattern;
To read more about system.getOption please refer to:
https://docs.bmc.com/docs/display/disco112/system.getOption
Scanning
To test it you need to start a scan of the both: local appliance and the remote host. Otherwise the command won't be executed. I have raised
a question on BMC communities could that be resolved.
To check if the value is attached to the host node simply execute: search Host show name, hello_message.
Comments
Post a Comment