First time post, please forgive any missing information.
I have a script that is supposed to work with icinga. I need icinga to log into my Linux box and run a command like "script ". The script will then run a command to that hostname like sudo /etc/init.d/apache2 status then report back "running or unused" and an exit status of 0 or 2.
I’m wondering how I could add another command and have it one or the other run depending on what hostname it’s given. Half of them need apache2 to be running and the other half need to have a process called dss to be running. I’d rather not have two separate scripts. Here is the working script and sorry it’s sloppy but I haven’t done any clean up and I’m not real good at bash yet.
so the user would run the script ./chkdss2 or
#!/bin/bash
ec=0
ec1=2
var3=run
var4=unused
for host in "$@"
do
var1=`ssh $host sudo /etc/init.d/dss status|awk '{print $6}'`
var2="$( echo $var1 | cut -c 3-5 )"
if [[ "$var2" == "$var3" ]]; then
echo "$host is running"
echo $ec
else
echo "$host is not running"
echo $ec1
fi
done
3
Answers
I was able to take a little bit from the answers I received and put together something that works well. Thank you all for your answers.
There are a couple ways to test if a particular hostname is for apache or dss. You only need to have a list of hostnames for each case, and check if the received hostnames are included in said lists.
Method 1: using arrays
To modify the lists of hosts, simply add or remove values in the declaration of arrays
apachehosts
anddsshosts
.Method 2: using
case
Here, you edit the patterns in each case.
Method 3: using
if
Here you modify the
if
conditions. I prefer the other methods, since this one is more complicated to edit, it is not as clear, especially if your list of hosts is long.Method 4: condition on the hostnames
If you are lucky, there is some pattern to your hostnames. Ex. all apache servers start with letters
ap
, all your dss servers includedss
in the name, …You can then simply use 2 if statements to decide which is which.
Note: hostname
apdss1
would come out as an Apache server here. Previous methods would respond "unknown host". You patterns must be strict enough to avoid mismatches.I had a similar task to get few report items using single
ssh
request.I had to retrieve in singel
ssh
command:I got my script to work in 3 stage.
1. Get multiple lines of information from remote host
Results:
2. Process multiple lines of information from remote host
Read lines of information from remote host, into an array
sshResultsArr
.Result:
3. Process each remote host in a loop