Using return values of external scripts/commands

All questions related to installations, configurations and maintenance of Advanced Host Monitor (including additional tools such as RMA for Windows, RMA Manager, Web Servie, RCC).
Post Reply
DavidK
Posts: 3
Joined: Tue Mar 04, 2008 5:59 am

Using return values of external scripts/commands

Post by DavidK »

Hi,

I need to execute a external program (either in a hms-script or in an action profile, doesn't matter) if a test gone bad and have the *return value* of that program for embedding as a variable in an alert.

Background:
I receive snmp traps from a cisco switch if a port went into a state called "errDisabled". This trap does not contain the actual port # but something like a pointer to a snmp table i then have to query for finding
the actual interface.

Example:

The trap has OID 1.3.6.1.4.1.9.9.548.1.3.1.1.2.10111.0 .
I take that OID, pass it to a external script to snmp query the sending device for OID .1.3.6.1.2.1.2.2.1.2.10111 (the "10111" is the pointer to the interface) and get back something like "GigabitEthernet0/11" as return value from the external command.

Is there any way to get this return value as a variable to use in a Hostmonitor action?
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Actually, your question is not completely clear for me, but I will try to answer it.

Variant 1: You just should pass certain parameter into external program and return the result back to HostMonitor to use it as "Reply".
In this case, you may use "Shell Script" test method. http://www.ks-soft.net/hostmon.eng/mfra ... m#chkShell
"Shell script" test method allows you to pass status and result value back to HostMonitor using "echo ScriptRes:Status:Reply" syntax.
Please, read carefully "Requirements to the script" section. http://www.ks-soft.net/hostmon.eng/mfra ... m#shellres
You may find many useful examples in "Script Manager's" window: http://www.ks-soft.net/hostmon.eng/mfra ... m#shellmng

Variant 2: HostMonitor should perform certain test (Test A) and you want to pass the "Reply" value of a "Test A" as a parameter into external program (Test B). That's more complicated case. As "Test B", you may use "Shell script" test method in a way, described above. You should use "Master/Dependant" relations to make "Test B" to be dependent on "Test A". http://www.ks-soft.net/hostmon.eng/mfra ... htm#Master
As you know, "Shell script" test method allows you to use "Global macro variables" as a parameters. So, you should store "Reply"value of the "Test A" into some "Global macro variable". To do that, you should assign to "Test A" an action profile with "Execute HMS Script" action. "HMS Script" action, should perform the simple script:

Code: Select all

SetUserVariable udv_test_a_value %Reply%
SaveUserVariables
This script will create/update %udv_test_a_value% variable each time it is performed. After that, you should specify %udv_test_a_value% into "Params" box in "Test Properties" window for the "Shell Script" test method and enable "Translate macros" option.

Solution is not simple, but should work.

BTW. Have you tried to use "SNMP Trap" test method, offered by HostMonitor? There are several macro variables you may use in actions, assigned to "SNMP Trap" test method:
http://www.ks-soft.net/hostmon.eng/mfra ... .htm#macro
===================================
The following variables applicable for "SNMP Trap" test only, they represent parameters of the last filtered message (filtered message is the one that have passed all conditions of the test item filter). When HostMonitor launches action in response to received trap message, it substitutes macro variable with its value pertinent to the trap message being received.
%TrapHost% Represents IP address of the host that have sent the message
%TrapHostInfo% If you have specified host name or description in HOSTINFO.LST file, HostMonitor will get information from the file and replace IP address with appropriate description. See HOSTINFO.LST file for details (it's a text file)
%TrapType% Represents type of the trap. It provides information about generic type and enterprise specific number. Generic type could be one of the following: Cold Start, Warm Start, Link Down, Link Up, Auth Failure, EGP Loss and Specific. Enterprise specific number is only applicable when generic trap type is Enterprise Specific, otherwise enterprise specific number is 0
%TimeTicks% Represents time ticks. Time ticks is the time interval (measured in hundredth of seconds) since the initialization (boot, start-up) of the entity that have sent the trap.
%Enterprise% Enterprise field contains an OBJECT IDENTIFIER which names the device that sends the trap
%EnterpriseName% Using the database of compiled MIB files HostMonitor may translate Enterprise OID from its numeric form to a MIB name. If you need to extend the database, include information about MIBs supported by some specific SNMP enabled device, use MIB Browser
%EnterpriseNameShort% Similar to %EnterpriseName% but shows the name without names of parent nodes.
E.g.

* %Enterprise% = 1.3.6.1.4.1.3955.5
* %EnterpriseName% = iso.org.dod.internet.private.enterprises.linksys.etherHub
* %EnterpriseNameShort% = etherHub

Each trap message may contain one or several variables that provide information about the event. Following macro variables allow you to access that information:
%MibOid% Represents OID (object identifier) of the variable
%MibName% Using the database of compiled MIB files HostMonitor may translate %MibOID% from its numeric form to a MIB name. If you need to extend the database by including information about MIBs supported by some specific SNMP enabled device, use MIB Browser
%MibNameShort% Similar to %MibName% but shows the name of the variable (plus possible index) without names of parent nodes.
E.g.

* %MibOID% = .1.3.6.1.2.1.2.2.1.8.1
* %MibName% = iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifOperStatus.1
* %MibNameShort% = ifOperStatus.1

%MibType% Type of the variable
%MibValue% Variable value
%MibRelative%

Relative value of the variable. This macro is applicable when you check incoming trap messages for some specific variable and compare current value of the variable with its previous value (see "Message contains OID" option in the Trap Filter dialog).
%MibRelative% may represent:

* simple difference between current and previous value (if you use "increases by", "decreases by" or "changes by" compare option)
* relative difference as a percentage of previous value (if you use "increases by (%)", "decreases by (%)" or "changes by (%)" compare option)
* average increase/decrease of the counter per second since previous message (if you use "increases /sec", "decreases /sec" or "changes /sec" compare option)

%TrapAllValues% This macro returns information about ALL variables received within Trap message (%TrapAllValues% returns "oid=value" line(s); if there is more than 1 variable in SNMP Trap message, lines will be separated by #13#10 characters).
Note: %MibOid%, %MibName%, %MibType%, %MibValue% variables provide the information about specific OID variable (if you have set “Message contains OID” option of the test filter) or the information about 1st variable within SNMP Trap packet.
===================================

Regards,
Max
DavidK
Posts: 3
Joined: Tue Mar 04, 2008 5:59 am

Post by DavidK »

Thank you for your long answer but I'm sorry that doesn't help me much.

As i wrote, I receive snmp traps via the test method "SNMP Trap" with hostmonitor. That works quite well.

I then have to process the value received via the trap through a external program and would like to have the return value of that external program for usage in hostmonitor alerts.

Thanks,
David
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

DavidK wrote:Thank you for your long answer but I'm sorry that doesn't help me much.
Why not? "Variant 2" is exactly what you need. In your case "Test A" will be "SNMP Trap" test method, and "Test B" should be "Shell Script" test method, that should perform external command.
DavidK wrote:I then have to process the value received via the trap through a external program and would like to have the return value of that external program for usage in hostmonitor alerts.
1. You should assign to the "SNMP Trap" test method an action profile with "Execute HMS script" action. "Execute HMS Script" action should perform the following script:

Code: Select all

SetUserVariable udv_test_a_value %Reply% 
SaveUserVariables
This script sets the OID, retrieved by "SNMP trap" test method into %udv_test_a_value% macro variable.

2. You have to setup "Shell Script" test method, that shoud execute an external program (script to snmp query). After that, you should specify %udv_test_a_value% into "Params" box in "Test Properties" window for the "Shell Script" test method and enable "Translate macros" option. the OID, retrieved by "SNMP Trap" test is stored in %udv_test_a_value% vriable, so it means you pass this OID to an external program.

3. To make it work properly, you should enable "Master/dependent" relations between "SNMP Test" and "Schell script" tet method in order to start "Shell script" test when the "SNMP Trap" test becomes "Bad" only. To the "Shell script" test, you may assign an action profile with "Send Email" action and use "%Reply%" variable within the mail body. "%Reply%" variable contains the return value of the external program, that was executed by "Shell Script" test method.

Also, to make it work properly, you have to setup "Shell script" test to be performed in a time interval, that is less then timeout, specified in "set "Bad" status if no "Good" trap received within NN sec" option of "SNMP Trap" test method.

Regards,
Max
DavidK
Posts: 3
Joined: Tue Mar 04, 2008 5:59 am

Post by DavidK »

Ah, ok, now I understand ... I'll give it a try.

Thank you,
David
Post Reply