LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying to make a script in Nagios (https://www.linuxquestions.org/questions/linux-newbie-8/trying-to-make-a-script-in-nagios-871634/)

brownie_cookie 03-29-2011 01:53 AM

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)

EricTRA 03-29-2011 02:44 AM

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

brownie_cookie 03-29-2011 02:51 AM

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 :(

brownie_cookie 03-29-2011 03:31 AM

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?

EricTRA 03-29-2011 03:52 AM

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

brownie_cookie 03-29-2011 04:03 AM

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


brownie_cookie 03-29-2011 04:33 AM

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


brownie_cookie 03-29-2011 04:40 AM

up to the next step :hattip:

EricTRA 03-29-2011 04:42 AM

Quote:

Originally Posted by brownie_cookie (Post 4307284)
up to the next step :hattip:

Hi Brownie,

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

Kind regards,

Eric

brownie_cookie 03-29-2011 04:47 AM

haha :D thx :cool:

brownie_cookie 03-29-2011 05:00 AM

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?

brownie_cookie 03-29-2011 05:24 AM

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


brownie_cookie 03-29-2011 07:29 AM

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?)


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