Multiple Commands in Single SSH Session

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
mattstan
Posts: 3
Joined: Wed Feb 20, 2019 11:13 pm

Multiple Commands in Single SSH Session

Post by mattstan »

I have what is likely a simple (I hope) question.

I have 10 devices that support ssh. I would like to ssh into the devices and collect various metrics. I have read about creating services and commands. Because I am an in bandwidth constrained environment I want to run all of my commands within a single ssh session. In other words, say every 30 minutes I connect to the devices and run the commands to collect the data. I'm not sure how to do this. From my reading it looks like each command will be its own ssh session. Can someone please help clarify? Thank you.

An additional wrinkle is that I cannot use the check_ssh command as these devices are read-only so I can't put an ssh public key on them for the monitoring server to permit automatic logic.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Multiple Commands in Single SSH Session

Post by ssax »

The check_by_ssh plugin supports multiple -C options per session so that would be the best bet but since you're unable to use public key auth, you will likely need to install something like sshpass and then call your plugin like:
- Note: I'm using CentOS so I had to "yum install sshpass", your distro may vary

Code: Select all

/usr/bin/sshpass -p 'YOURPASS' /usr/local/nagios/libexec/check_by_ssh -H X.X.X.X -C command1 -C command2 -C command3 -l root

Code: Select all

[root@xid ~]# /usr/local/nagios/libexec/check_by_ssh -h
check_by_ssh v2.2.1 (nagios-plugins 2.2.1)
Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>
Copyright (c) 2000-2014 Nagios Plugin Development Team
        <devel@nagios-plugins.org>

This plugin uses SSH to execute commands on a remote host

Usage:
 check_by_ssh -H <host> -C <command> [-fqv] [-1|-2] [-4|-6]
       [-S [lines]] [-E [lines]] [-t timeout] [-i identity]
       [-l user] [-n name] [-s servicelist] [-O outputfile]
       [-p port] [-o ssh-option] [-F configfile]

Options:
 -h, --help
    Print detailed help screen
 -V, --version
    Print version information
 --extra-opts=[section][@file]
    Read options from an ini file. See
    https://www.nagios-plugins.org/doc/extra-opts.html
    for usage and examples.
 -H, --hostname=ADDRESS
    Host name, IP Address, or unix socket (must be an absolute path)
 -p, --port=INTEGER
    Port number (default: none)
 -4, --use-ipv4
    Use IPv4 connection
 -6, --use-ipv6
    Use IPv6 connection
 -1, --proto1
    tell ssh to use Protocol 1 [optional]
 -2, --proto2
    tell ssh to use Protocol 2 [optional]
 -S, --skip-stdout[=n]
    Ignore all or (if specified) first n lines on STDOUT [optional]
 -E, --skip-stderr[=n]
    Ignore all or (if specified) first n lines on STDERR [optional]
 -f
    tells ssh to fork rather than create a tty [optional]. This will always return OK if ssh is executed
 -C, --command='COMMAND STRING'
    command to execute on the remote machine
 -l, --logname=USERNAME
    SSH user name on remote host [optional]
 -i, --identity=KEYFILE
    identity of an authorized key [optional]
 -O, --output=FILE
    external command file for nagios [optional]
 -s, --services=LIST
    list of nagios service names, separated by ':' [optional]
 -n, --name=NAME
    short name of host in nagios configuration [optional]
 -o, --ssh-option=OPTION
    Call ssh with '-o OPTION' (may be used multiple times) [optional]
 -F, --configfile
    Tell ssh to use this configfile [optional]
 -q, --quiet
    Tell ssh to suppress warning and diagnostic messages [optional]
 -w, --warning=DOUBLE
    Response time to result in warning status (seconds)
 -c, --critical=DOUBLE
    Response time to result in critical status (seconds)
 -t, --timeout=INTEGER:<timeout state>
    Seconds before connection times out (default: 10)
    Optional ":<timeout state>" can be a state integer (0,1,2,3) or a state STRING
 -v, --verbose
    Show details for command-line debugging (Nagios may truncate output)

 The most common mode of use is to refer to a local identity file with
 the '-i' option. In this mode, the identity pair should have a null
 passphrase and the public key should be listed in the authorized_keys
 file of the remote host. Usually the key will be restricted to running
 only one command on the remote server. If the remote SSH server tracks
 invocation arguments, the one remote program may be an agent that can
 execute additional commands as proxy

 To use passive mode, provide multiple '-C' options, and provide
 all of -O, -s, and -n options (servicelist order must match '-C'options)

Examples:
 $ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo
 $ cat /tmp/foo
 [1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days
 [1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days
 [1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days

Send email to help@nagios-plugins.org if you have questions regarding use
of this software. To submit patches or suggest improvements, send email to
devel@nagios-plugins.org
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Multiple Commands in Single SSH Session

Post by cdienger »

I'm not aware of any plugins that will do this so something would likely need to be written, but plink from the putty package can take in username and password and execute commands via ssh. Assuming a Redhat/CentOS machine:

yum -y install putty
plink username@remotemachine -pw password "uptime;free -m"


The above would get the uptime and memory stats from the remote machine. A plugin could be written to use this command and parse the data to make it meaningful to Nagios. Some useful documents to help create custom pluggins.


https://library.nagios.com/library/prod ... s-in-perl/
https://exchange.nagios.org/directory/T ... pt/details
https://assets.nagios.com/downloads/nag ... inapi.html
http://nagios-plugins.org/doc/guidelines.html
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
mattstan
Posts: 3
Joined: Wed Feb 20, 2019 11:13 pm

Re: Multiple Commands in Single SSH Session

Post by mattstan »

Thank you for the response @cdienger. That's a really good suggestion. In the interim I found the check_by_ssh script and I've modified it to suit my needs. It looks like there were some mem leaks in it so I have fixed those. Looking forward to my QuickStart training later this week.
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Multiple Commands in Single SSH Session

Post by cdienger »

Sounds good! Are we okay to lock this up?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
mattstan
Posts: 3
Joined: Wed Feb 20, 2019 11:13 pm

Re: Multiple Commands in Single SSH Session

Post by mattstan »

Yes, thank you very much for your help. RESOLVED
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Multiple Commands in Single SSH Session

Post by cdienger »

Glad to hear!
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Locked