LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2008, 04:52 AM   #1
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Rep: Reputation: 15
Check process of other server using ssh


hi guys,
I have several servers and i want to make use of 1 of them to do some simple monitoring. It should be able check the process of other servers. What i did is

for SERVER in ${SERVERS}; do
echo "testing 2"
LINECOUNT= ssh ${SERVER} "ps -ef | grep -v grep |grep -v ${SERVER} | grep ${SERVICE} | wc -l"
echo "linecount is ${LINECOUNT}"
if [ ${LINECOUNT}=0 ]; then
echo " ${SERVICE} is not running."
else
echo " ${SERVICE} is running."
fi


I can run the ps command locally and able to get the result.
But the thing is when i use ssh to execute the remote command, it does return me the output like 1 or 0. What can i do? Thanks in advance

Regards
 
Old 05-28-2008, 05:51 AM   #2
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 33
you could to the grep locally or the pipe to wc -l
i.e.
LINECOUNT=`ssh ${SERVER} "ps -ef | grep -v grep |grep -v ${SERVER} | grep ${SERVICE}" | wc -l`
 
Old 05-28-2008, 11:47 AM   #3
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
hi nat, thanks for the reply. it works!!
 
Old 05-29-2008, 12:16 AM   #4
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
hi nat, i have some other issues over here. I have this script over here but when i run it has this error
./remotechecking.sh: line 15: syntax error near unexpected token `fi'
./remotechecking.sh: line 15: ` fi'


#!/bin/bash -x
LOGFILE=/root/remotechecking.log
exec > ${LOGFILE}
exec 2>&1
SERVERS="testserver testserver2 testserver3"
for SERVER in ${SERVERS}; do
echo "in for loop ${SERVER}"
if [ SERVER="testserver" ]; then
#CHECK_APACHE
echo "in the 2nd loop "
CHECK_WONDERFUL()
fi
done

CHECK_APACHE() {
LINECOUNT=`ssh ${SERVER} "ps -ef | grep -v grep |grep -v ${SERVER} | grep apache" | wc -l`
if [ ${LINECOUNT}=1 ]; then
echo " apache on ${SERVER} is running."
else
echo " apache on ${SERVER} is not running."
fi
}

CHECK_WONDERFUL()
{
LINECOUNT=`ssh ${SERVER} "ps -ef | grep -v grep |grep -v ${SERVER} | grep wonderful" | wc -l`
if [ ${LINECOUNT}=1 ]; then
echo " wonderful on ${SERVER} is running."
else
echo " wonderful on ${SERVER} is not running."
 
Old 05-29-2008, 12:57 AM   #5
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
sorry guys, i managed to figure this out. i need to put in a function infront of CHECK_WONDERFUL() and it will be then a method.

I have another question though. Although it can do the check but the log show which i create will show the banner when it ssh into other server, any ways to eliminate the banner?
 
Old 05-29-2008, 02:14 AM   #6
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
hi guys..another question....when i run this script,

./remotechecking.sh: line 25: syntax error near unexpected token `done'
./remotechecking.sh: line 25: `done'

anywhere wrong with the else if condition?


9 for SERVER in ${SERVERS}; do
10 echo "in for loop ${SERVER}"
11 if [ ${SERVER}="sinp2ps11" ]; then
12 CHECK_P2PS
13 else if [ ${SERVER}="sinp2ps12" ]; then
14 CHECK_P2PS
15 else if [ ${SERVER}="sinp2ps13" ]; then
16 CHECK_P2PS
17 else if [ ${SERVER}="sinp2ps14" ]; then
18 CHECK_P2PS
19 else if [ ${SERVER}="sindacs" ]; then
20 CHECK_DACS
21 else if [ ${SERVER}="sindts" ]; then
22 CHECK_DTS
23 endif
24
25 done
 
Old 05-29-2008, 02:36 AM   #7
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 33
2 things:

1. not every member here enjoyes his nick being nicked (subtle hint: i am one of them!)

2. why do you not use case ? (google for bash case if you dont know it yet ^^)
 
Old 05-29-2008, 06:49 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If that's shell cmds, its usually

if/then/elif/else/fi

with as many elif's as you need
 
Old 05-29-2008, 09:01 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
BTW, isn't the old 'ps -ef | grep -v grep |grep -v something | grep somethingElse' reflex a 'pgrep -f somethingElse' replacement candidate?
 
Old 05-29-2008, 09:08 PM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Might the facility that you are looking for already exist?

Check out: [url=http://www.nagios.org]http://www.nagios.org/url]

If we abstract this requirement to its fullest possible extent, what we are left with is the need to explore the status of a particular set of hardware and/or the status of particular running processes on that hardware. This might well prove to be an existing-solution that you might be able to productively leverage or use...

Dictum Ne Agas -- "Do Not Do A Thing Already Done."
 
Old 05-30-2008, 01:29 AM   #11
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
sorry nathanael
 
Old 05-30-2008, 03:05 PM   #12
cghcgh
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
hi..i'm not really sure whether nagios covers solaris OS as well so at the meantime, i will be the script to capture what i want and at the same time hosts the finding on a apache server. How do i make use of the information captured to display on a website? shall i run the check on a cron job basis....and the website will extract the information from the log file? or shall i run the check and generate the website after that?

I have another question though. Although it can do the check but the log show which i create will show the banner when it ssh into other server, any ways to eliminate the banner?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check Process hung or not??? jbilla Linux - Server 13 08-09-2010 05:37 AM
check process ust Linux - Software 5 03-05-2008 02:46 AM
How to restart & check status of ssh server bostonantifan Ubuntu 3 10-30-2007 11:06 AM
Check is process is running? Zeno McDohl Programming 2 08-13-2007 07:48 PM
Check the process sunhui Linux - Software 5 03-01-2006 07:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration