LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   help with script to check processes on multiple servers (http://www.linuxquestions.org/questions/programming-9/help-with-script-to-check-processes-on-multiple-servers-640817/)

ncsuapex 05-08-2008 09:34 AM

help with script to check processes on multiple servers
 
I found this script that tinkster posted in another thread that was similar to what I was looking for. Thanks tinkster!

for i in `cat hostlist`; do ssh root@$i -C ps -ef | grep mysqld | grep -v grep; done;


I modified it a bit to do what I need it to do but I have 2 questions.

1:) How can I print the server name with the results, so I know if the process is running or not running on a specific server?

2:) how can I add to this script a range of IPs instead of calling it from a hostlist file? ie. I want to check all servers in a range of 192.168.1.1 to 192.168.1.66


for the 2nd question I tried:
for i in (( $i=50 ; $i<=66 ; $i++ )); do ssh root@192.168.1.$i -C ps -ef | grep mysqld | grep -v grep; done;

got this error:

-bash: syntax error near unexpected token `('

thanks!

MensaWater 05-08-2008 10:05 AM

Code:

CNT=1
until [ $CNT -eq 67 ]
do IP=192.168.1.$CNT
  REMHOST=`ssh root@$IP -C hostname`
  echo "Host $REMHOST at IP $IP returned the following result:"
  ssh root@$IP -C ps -ef | grep mysqld | grep -v grep
  CNT=`expr $CNT + 1`
done

The above tells it to loop through from 1 to 66 (67 is where it ends).
It puts the hostname and IP address on the line above the ps output.

ncsuapex 05-08-2008 10:12 AM

BEAUTIFUL! Worked perfectly. Thanks!

unSpawn 05-08-2008 10:13 AM

Code:

for ((i=50;i<=66;i++)) ; do
 echo -en "host 192.168.1.${i}: "
 ssh root@192.168.1.$i 'pgrep -f mysqld >/dev/null 2>&1&& echo YES||echo NO'
done

The above tells it to loop through from 50 to 66.
Somewhat like the previous poster did, this puts (or should) the IP address on the line *with* the output.
And unlike the previous poster this doesn't use ye aulde convoluted 'ps|grep|grep -v grep' but 'pgrep' (GASP! OMG!111!!).

ncsuapex 05-08-2008 10:23 AM

^hmm... Only thing that didn't work with yours is it printed YES to all the servers, when I know a handful do not have mysqlsd running on them. When I logged into one of the servers that I know doesn't have mysqld running on it and just ran "pgrep -f mysqld >/dev/null 2>&1&& echo YES||echo NO" It printed "NO"




So I changed it to this: and it printed YES to the servers to does have mysqld running on it and no to the ones that didn't.

for ((i=50;i<=66;i++)) ; do
echo -en "host 192.168.1.${i}: "
ssh root@192.168.1.$i 'ps -ef | grep mysqld | grep -v grep >/dev/null 2>&1&& echo YES||echo NO'
done




thanks for the help!

MensaWater 05-08-2008 12:21 PM

Quote:

Originally Posted by unSpawn (Post 3147010)
Code:

for ((i=50;i<=66;i++)) ; do
 echo -en "host 192.168.1.${i}: "
 ssh root@192.168.1.$i 'pgrep -f mysqld >/dev/null 2>&1&& echo YES||echo NO'
done

The above tells it to loop through from 50 to 66.
Somewhat like the previous poster did, this puts (or should) the IP address on the line *with* the output.
And unlike the previous poster this doesn't use ye aulde convoluted 'ps|grep|grep -v grep' but 'pgrep' (GASP! OMG!111!!).

Previous poster didn't suggest the ps |grep ... - he incorporated it from what OP wrote. :p

ncsuapex 06-09-2008 02:49 PM

ok new twist. Now I want to be able to copy a file from one machine(server65) to the rest of the machines on the network. Running the script from server65.


I modified the script to have a scp command and I added the authorized_keys from my host machine server65 to server3-8. If I ssh from server65 to server3-8 I don't get prompted for password, so the ssh keys work. But if I try ssh from server3-8 to server65 it prompts for PW. I do not have the keys set to go that way.


My question is.. In order to copy a file from server65 to server3-8 what do I put in for the command? If I manually do a scp filename server03:/location from server65 it works.


CNT=3
until [ $CNT -eq 9 ]
do IP=10.1.1.$CNT
REMHOST=`ssh root@$IP -C hostname`
echo "Host $REMHOST at IP $IP returned the following result:"
ssh root@$IP -C scp root@lifenet65:/location/file $IP:/location/file
CNT=`expr $CNT + 1`
done




nevermind:

changed it to this and it worked. It ALWAYS happens. I beat my head against the wall trying to figure something out, then I give in and post a message and within 5 minutes I figure it out myself



CNT=3
until [ $CNT -eq 9 ]
do IP=10.1.1.$CNT
echo "Host at IP $IP returned the following result:"
scp /location/file $IP:/location/file
CNT=`expr $CNT + 1`
done

ncsuapex 06-10-2008 11:02 AM

ok got a new one for you guys since I figured out that last one on my own.

I got this command which works as needed BUT 2 things

1:) How do I print the hostname BEFORE the results? As it is now it prints the results then the hostname, which is fine, but it would look better and be easier to read if it printed the hostname on the line above the results

2:) how can I get it to NOT print the hostnames that do not match the query? As the command runs now it prints all the hostnames from the hostname file. I only want to print the hostnames with results.


command.

for i in `cat hostnames`; do ssh user@$i -C /command | grep something ; echo "$i" ; done;




ok made a script out of the command but it still returns the hostname after the results

for i in `cat hostnames`
do ssh user@$i -C /command | grep something
REMHOST=$i
echo "Host $REMHOST at IP $IP returned the following result:"
done





edit:


once again I think i figured it out myself.

for i in `cat hostnames`
do
echo "hostname $i returned the following results:"
ssh user@$i -C /command | grep something
done;



does that look like it would work? Ive tested it but not enough to know for sure.


All times are GMT -5. The time now is 08:06 AM.