Anonymous_User Absent Member.
Absent Member.
676 views

Calling a Bash Script from FormulaScript


I am trying to make a formulascript that will, among other things, get a
status.dat file from nagios servers using scp to copy it to the NOC
server (both are linux servers). I wrote a quick bash script that
accepts the nagios server name as a parameter, and it looks like this:

scpFromNagios.sh
#!/bin/bash
#Prints the user's name
echo "Hello $USER"\
#Sets the parameter passed by the user as SERVERNAME
SERVERNAME=$1
#Prints the parameter passed by the user
echo $SERVERNAME
#Runs the scp command to copy the file from the Nagios server to
the NOC server
scp -P 22222 nuser@$SERVERNAME:/var/log/nagios/status.dat
/opt/ManagedObjects/$SERVERNAME.subfile.dontTouch

I have tested this script and it works, but the challenge I am now
facing is calling this script from a for loop in the formulascript.
I would like to call the bash script in the formulascript, at the
beginning of this for loop (where the ??????? are located):

for( var i = 0; i < servers.length; i++ ){
???????
var synced = setup.SyncStatusFile( configData, servers,
"/opt/ManagedObjects/" );
if( synced == false ){
throw "Could not sync status file.";
// cannot sync a nagios file. continue on, but log the
occurrence
//var now = new java.util.Date();
//var str = now.getDate() + "-"
+now.getDay()+"-"+now.getFullYear()+" " +now.getHours() +":" +
now.getMinutes();
log.logPrint( str + " Could not sync status file for: "
+ servers );
}
var nagios = new NagiosSync(configData, servers);
nagios.sync();
}

This seems like it should be easy, and I have googled it and tried a
couple of this, but I am not sure how to call the bash script in the
formula script.
I tried something like:
java.lang.Runtime.getRuntime().exec("cmd /c start cmd.exe /K
\"./scpFromNagios.sh"");


--
jeffcroskrey
------------------------------------------------------------------------
jeffcroskrey's Profile: https://forums.netiq.com/member.php?userid=2905
View this thread: https://forums.netiq.com/showthread.php?t=46259

Labels (1)
0 Likes
10 Replies
Anonymous_User Absent Member.
Absent Member.

Re: Calling a Bash Script from FormulaScript


The exec() method is the way to go, I'm not sure of the OS you are
running on, but "cmd.exe" is windows based. I did a quick custom
operation, serverscript and this worked for me...

java.lang.Runtime.getRuntime().exec( "cmd.exe /c dir > c:\\process.txt"
);


--
tisenberg
------------------------------------------------------------------------
tisenberg's Profile: https://forums.netiq.com/member.php?userid=1851
View this thread: https://forums.netiq.com/showthread.php?t=46259

0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: Calling a Bash Script from FormulaScript


oops, I meant:
java.lang.Runtime.getRuntime().exec("cmd /c start cmd.exe /K
\"./scpFromNagios.sh servername"");


--
jeffcroskrey
------------------------------------------------------------------------
jeffcroskrey's Profile: https://forums.netiq.com/member.php?userid=2905
View this thread: https://forums.netiq.com/showthread.php?t=46259

0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: Calling a Bash Script from FormulaScript


I am using linux, that is why it wouldn't work, thanks! Will your
example work on Linux, if not, is there a way to run a command in
formulascript from the command line in a linux OS?
Thanks for your help!


--
jeffcroskrey
------------------------------------------------------------------------
jeffcroskrey's Profile: https://forums.netiq.com/member.php?userid=2905
View this thread: https://forums.netiq.com/showthread.php?t=46259

0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: Calling a Bash Script from FormulaScript


so instead of "cmd" it would probably be something like "/bin/ksh".
You should probably just create a unix script file that does what you
want it to do and call that script directly with parms.


--
tisenberg
------------------------------------------------------------------------
tisenberg's Profile: https://forums.netiq.com/member.php?userid=1851
View this thread: https://forums.netiq.com/showthread.php?t=46259

0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: Calling a Bash Script from FormulaScript


Sorry for not being clear, I wrote a bash script, it is called
scpFromNagios.sh, and it just copies the files from the Nagios servers
to the MOM servers. I just need to call that script at the beginning of
another script that is written in formulascript. Are there examples of
calling that script in formula script?
Thanks!


--
jeffcroskrey
------------------------------------------------------------------------
jeffcroskrey's Profile: https://forums.netiq.com/member.php?userid=2905
View this thread: https://forums.netiq.com/showthread.php?t=46259

0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: Calling a Bash Script from FormulaScript


I assume you are talking about the basics of one noc script using
another noc script, here is an example...


/* begining of scriptOne.fs */

function logThis( str )
{
formula.log.info( str )
}

/* end of scriptone.fs */



/* begining of scriptTwo.fs */

load( "scriptOne.fs" );
logThis( "It worked" );

/* end of scriptTwo.fs */


--
tisenberg
------------------------------------------------------------------------
tisenberg's Profile: https://forums.netiq.com/member.php?userid=1851
View this thread: https://forums.netiq.com/showthread.php?t=46259

0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: Calling a Bash Script from FormulaScript


Per your other comments and a Nagios log file. If you are bringing the
nagios log file into Event Manager (AKA: Eve, AKA: Event Integrator),
you can just put an eve agent on the nagios box to have it watch the log
file. There should be documentation on that. If you have your own
custom script reading the log file, you could probably run that on the
nagios box as well.


--
tisenberg
------------------------------------------------------------------------
tisenberg's Profile: https://forums.netiq.com/member.php?userid=1851
View this thread: https://forums.netiq.com/showthread.php?t=46259

0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: Calling a Bash Script from FormulaScript


This is basically what I need to do, but I am calling a linux shell
script from the formula script. We have a job that was created in the
client app, that runs a formula script that is called NagiosSync.fs. In
NagiosSync.fs, I want to call a linux shell script that I created and
pass a variable to the script. So I have a loop, and I want the script
to call the shell script every time it goes through the loop. Here is
the loop
for( var i = 0; i < servers.length; i++ ){
<Code that calls the linux shell script and passes the server name, like
this server>
}
From what I found search on google was something like this:
java.lang.Runtime.getRuntime().exec("scpFromNagios.sh " + servers);
I am just not sure of the syntax, or what I need to do to make the
NagiosSync.fs call the shell script called scpFromNagios.sh, both files
are in the same directory, do I need to specify a directory before the
file name, and what else do I need to change for the correct syntax.
Thank you!


--
jeffcroskrey
------------------------------------------------------------------------
jeffcroskrey's Profile: https://forums.netiq.com/member.php?userid=2905
View this thread: https://forums.netiq.com/showthread.php?t=46259

0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: Calling a Bash Script from FormulaScript


You should probably put the full path to the script (IE:
/opt/NagiosScript.sh )

also, I think the exec has a return code or a stream you can look at.
I can only assume you are trying it and it is not working.

Check out this link and code. It may help with debugging your problem
(IE: access denied, path not found, etc)...

http://tinyurl.com/2p8nk9

Keep in mind that there is a stdout and stderr


--
tisenberg
------------------------------------------------------------------------
tisenberg's Profile: https://forums.netiq.com/member.php?userid=1851
View this thread: https://forums.netiq.com/showthread.php?t=46259

0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: Calling a Bash Script from FormulaScript


Thanks for your help, I got it working, the syntax was correct, I just
had to change something in the client app, with how the job was running.
Thanks again!


--
jeffcroskrey
------------------------------------------------------------------------
jeffcroskrey's Profile: https://forums.netiq.com/member.php?userid=2905
View this thread: https://forums.netiq.com/showthread.php?t=46259

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.