LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-29-2011, 01:53 AM   #1
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
Trying to make a script in Nagios


Howdy everyone

as the subject already told you, i'm trying to make a script in/for Nagios.
This is the first time i'm doing this, so bare with me

Oke, the goal is that i can check some files/folders on a REMOTE server (which is protected with a login and pass).
Now, I already did a ssh-keyswap, so i can connect to the remote server without needing to fill in a login and pass.

I've got some examples (Nagios plugins) from a friend, but I've got some questions (consider i'm new to all of this), my friend is a busy man so he doesn't got loads of time:

in his code he got something like:
Code:
PATH=""

find="/usr/bin/find"
xargs="/usr/bin/xargs"
tail="/usr/bin/tail"
awk="/usr/bin/awk"
cut="/usr/bin/cut"
wc="/usr/bin/wc"
grep="/bin/grep"

PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="Revision 1.0"
AUTHOR="(c) 2010 name"
is this piece of code needed? if yes, why?
what does that progname/progpath do?
what does this do (see the BOLD piece)
Code:
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
I've looked on the internet for some information, but i kinda hoped that someone can explain it very simple

if someone got any tips for writing a nagios plugin, feel free to share it with me and the rest who's reading this

kind regards

Brownie

EDIT
Maybe good to know, i want to use seperated script.
I mean that i want to make a control script (which checks the files/folders) and a main script (which copies the control scripts to the remote server and then executes them)

Last edited by brownie_cookie; 03-29-2011 at 02:04 AM.
 
Old 03-29-2011, 02:44 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

The part of the script you indicate is not 'required'. It's put in place to easily switch paths depending on OS. Throughout the script variables are used instead of the commands. If you need to change the path to a command because you want to execute the script say on AIX instead of Debian, you just change the paths in the variables and your script works on AIX.

In Bash $0 is a special variable holding the scripts own name (Advanced Bash Scripting Guide).
Quote:
$0 is the name of the script itself, $1 is the first argument, $2 the second, $3 the third, and so forth. [2] After $9, the arguments must be enclosed in brackets, for example, ${10}, ${11}, ${12}.
The part that includes the sed command just outputs the path where your script is located.

You can easily copy those lines in a script and execute it to see what it does. That makes it easier to understand.
Code:
#!/bin/bash

echo $0

PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`

echo $PROGPATH
Kind regards,

Eric
 
Old 03-29-2011, 02:51 AM   #3
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
Thanks for the info !

this is what i got so far
Code:
#!/bin/bash

PATH=""

find="/usr/bin/find"
xargs="/usr/bin/xargs"
tail="/usr/bin/tail"
awk="/usr/bin/awk"
cut="/usr/bin/cut"
wc="/usr/bin/wc"
grep="/usr/bin/grep"

PROGNAME=`/bin/basename $0`
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="Revision 1.0"
AUTHOR="(c) 2011 name"

# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

print_revision() {
	echo "$REVISION $AUTHOR"
}

print_usage() {
	echo "Usage: $PROGNAME -d <path> -w <warning> -c <critical>"
	echo "Usage: $PROGNAME --help"
	echo "USAGE: $PROGNAME --version"
}

print_help() {
	print_revision $PROGNAME $REVISION
	echo ""
	echo "Checking for folders and files on a remote server"
	echo ""
	print_usage
	echo ""
}

# Make sure that the user used the correct number of commands
if [ $# -lt 1 ]; then
	print_usage
	exit $STATE_UNKNOWN
fi

# copying a file to the remote server
ssh user@host 'echo test'
scp /folder/scp_cd.sh user@host:/some/folder
ssh vusbe@wlsdev.intern.vgt.vito.be 'pwd'
this is my result:
Code:
Usage: check_test.sh -d <path> -w <warning> -c <critical>
Usage: check_test.sh --help
USAGE: check_test.sh --version
and this is my result when i put the IF lus in command (#)
Code:
./check_test.sh: line 57: ssh: No such file or directory
./check_test.sh: line 58: scp: No such file or directory
./check_test.sh: line 59: ssh: No such file or directory
probably it has to do something with my command like SCP and SSH, but i don't have a clue how have to use it for a Nagios script
 
Old 03-29-2011, 03:31 AM   #4
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
Okay, this problem is solved...

I put the
Code:
PATH=""
in a comment and now it works.
So what did i do wrong with
Code:
PATH=""
??

can someone help?
 
Old 03-29-2011, 03:52 AM   #5
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

Open a console and type
Code:
echo $PATH
That shows you your paths in which your shell searches for applications. If in a script you type PATH="" you empty that environment and thus your script cannot find any applications unless you put in the full path with it. So either you put in PATH"" the directories where you want your shell to look for applications, or you don't define it (by commenting it out in your script you're using the environment defined for you which is shown by the echo command above). Hope that clears up the doubt a bit.

Kind regards,

Eric
 
Old 03-29-2011, 04:03 AM   #6
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
Code:
echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
now it's working thx buddy

okay, now i'm a little bit further. I'm going to show you some code, except some variables because i don't use them (yet)
main script
Code:
# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

print_revision() {
	echo "$REVISION $AUTHOR"
}

print_usage() {
	echo "Usage: $PROGNAME -d <path> -w <warning> -c <critical>"
	echo "Usage: $PROGNAME --help"
	echo "USAGE: $PROGNAME --version"
}

print_help() {
	print_revision $PROGNAME $REVISION
	echo ""
	echo "Checking for folders and files on a remote server"
	echo ""
	print_usage
	echo ""
}

# Make sure that the user used the correct number of commands
#if [ $# -lt 1 ]; then
#	print_usage
#	exit $STATE_UNKNOWN
#fi

# copying a file to the remote server
ssh user@host 'echo test'
scp /folder/scp_cd.sh user@host:/some/folder
ssh user@host 'pwd'
ssh user@host './scp_cd.sh'
ssh user@host 'rm -f /some/folder/scp_cd.sh'
subscript
Code:
#!/usr/bin/ksh

echo dit is het subscriptje

test=""
grepped=`grep -i "warning" /home/vusbe/logfile.txt`

if [ "$grepped" = "warning" ] || [ "$grepped" = "WARNING" ]; then
	$test == "1";
fi;

echo $test
So what i'm trying to do with the subscript (the mainscript works so far) is that when he finds a warning, he needs to put the number 1 in a variable (test) and then he needs to show it (echo $test, for testing if he really contains the number)
but all i get is the first echo
Code:
echo dit is het subscriptje
is the semicolon ( at the end of $test == "1" at a wrong place?
or is it something else?

Also, when i figured out what's the problem, how do i get this variable ($test) send back to the local server (so i can handle this 1 as a result, so i can send a message to someone)

thanks in advance

Brownie

EDIT
This is the output btw
Code:
test
scp_cd.sh                                     100%  202     0.2KB/s   00:00
/some/folder
dit is het subscriptje

Last edited by brownie_cookie; 03-29-2011 at 04:04 AM.
 
Old 03-29-2011, 04:33 AM   #7
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
ok, now i've got this (so ignore my prev post)
it's only the subscript because the main hasn't changed

Code:
#!/usr/bin/ksh

echo dit is het subscriptje

TESTVAR="0"
echo $TESTVAR
GREPPED=`grep -i "warning" /some/folder/logfile.txt`

if [ "$GREPPED" = "warning" ] || [ "$GREPPED" = "WARNING" ]; then
	echo testing
	$TESTVAR=="1"
fi

echo $TESTVAR
output
Code:
test
scp_cd.sh                                     100%  236     0.2KB/s   00:00
/some/folder
dit is het subscriptje
0
testing
0
./scp_cd.sh[11]: 0==1:  not found
so it works (for some parts...) i know now that he comes in the IF part, but why doesn't he change the value of my variable $testvar ??

UPDATE
found it
Code:
#!/usr/bin/ksh

echo dit is het subscriptje

TESTVAR="0"
echo $TESTVAR
GREPPED=`grep -i "warning" /home/vusbe/logfile.txt`
echo dit heb ik gevonden: $GREPPED

if [ "$GREPPED" = "warning" ] || [ "$GREPPED" = "WARNING" ]; then
	echo testing
	TESTVAR="1"
fi

echo $TESTVAR
echo einde

Last edited by brownie_cookie; 03-29-2011 at 04:39 AM.
 
Old 03-29-2011, 04:40 AM   #8
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
up to the next step
 
Old 03-29-2011, 04:42 AM   #9
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Quote:
Originally Posted by brownie_cookie View Post
up to the next step
Hi Brownie,

Smooth!! You're on a roll buddy Keep it up.

Kind regards,

Eric
 
Old 03-29-2011, 04:47 AM   #10
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
haha thx
 
Old 03-29-2011, 05:00 AM   #11
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
i'm back with another question
but i also want to say is that when i ask a question, i don't sit and wait for someone to give me the answer, but i also try to find the answer myself (best way to learn anything). it seems that i ask a lot of question, but that's because i'm a rookie compared to most of you guys

anyway, everything works, but now i want to SCP a file from the REMOTE server to the LOCAL server, and that's where it goes wrong...
subscript:
Code:
#!/usr/bin/ksh

echo dit is het subscriptje

TESTVAR="0"
echo $TESTVAR
GREPPED=`grep -i "warning" /some/folder/logfile.txt`
echo dit heb ik gevonden: $GREPPED

if [ "$GREPPED" = "warning" ] || [ "$GREPPED" = "WARNING" ]; then
	echo testing
	TESTVAR="1"
	echo $TESTVAR
	echo $TESTVAR >> /some/folder/leesbestand.txt
fi

cat /some/folder/leesbestand.txt

scp user@host:leesbestand.txt /root/folder

echo einde
and when i run it, i get this error (i give only the error message, because everything else works, so it's irrelevant)
output
Code:
Host key verification failed.
then i tried to connect to the remote host with the ssh-command (not through a script) and it still works, i don't have to put in a login and pass, it automatically logs me in..

where does it go wrong?

Last edited by brownie_cookie; 03-29-2011 at 05:01 AM.
 
Old 03-29-2011, 05:24 AM   #12
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
does it have to do anything with this in /etc/ssh/sshd_conf ?
Code:
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile     .ssh/authorized_keys
these are the files that are in the .ssh folder
Code:
[root@host .ssh]# ll
total 32
-rw-r--r-- 1 root root  414 Mar 25 09:09 authorized_keys2
-rw------- 1 root root 1675 Mar 28 13:59 id_remote
-rw-r--r-- 1 root root  414 Mar 28 13:59 id_remote.pub
-rw------- 1 root root 1675 Mar 25 09:07 id_rsa
-rw-r--r-- 1 root root  414 Mar 25 09:07 id_rsa.pub
-rw-r--r-- 1 root root  636 Mar 28 14:04 key_remote
-rw-r--r-- 1 root root 4009 Mar 28 13:39 known_hosts
-rw-r--r-- 1 root root  414 Mar 25 09:10 user@host
 
Old 03-29-2011, 07:29 AM   #13
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
change of plans !

instead of using a file to store my output in, can't i better work with STANDARD OUT (or something like that?)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Help with Nagios Script pokker67 Linux - Software 2 09-21-2009 08:22 PM
nagios + pnp4nagios how make graph from total processes mkiler Linux - Server 0 02-27-2009 07:44 AM
Perl Script works from command line, but not when evoked by Nagios gfem Linux - General 10 06-05-2008 03:59 PM
Trying to get current cpu percentages in a script for nagios. (bash) helptonewbie Linux - Newbie 3 05-01-2008 06:09 PM
Nagios check_sun_hardware script alek66 Solaris / OpenSolaris 2 02-14-2008 05:22 AM


All times are GMT -5. The time now is 07:53 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration