Execute a custom check command on linux servers via SSH

  • 27 June 2022
  • 4 replies
  • 4068 views

Userlevel 1
Badge +5

  

 

The plugin os::linux::local is used to monitor Linux servers locally. Most of the time this plugin is used to monitor CPU, disks, memory or other standard metrics.
But the plugin has a cmd-return mode that allows to execute custom checks commands directly on the remote host via the SSH protocol.

For instance, we can test if a NFS mount is properly mounted or the presence of a particular file.
We will be able to set the output message of the probe as well as an OK or CRITICAL status depending on the result of the command.

 

SSH keys
 

We need to establish a SSH connection between our monitoring server and the monitored host.

Centreon executes all its commands with the centreon-engine user. We need to generate a SSH key for this user and then share it on the monitored servers.
On the monitoring server, switch to the centreon-engine user:

su - centreon-engine

Create a SSH key pair in the user folder of centreon-engine (do not enter passwords):

centreon-engine@xxxxx:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/centreon-engine/.ssh/id_rsa):
Created directory '/var/lib/centreon-engine/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/centreon-engine/.ssh/id_rsa.
Your public key has been saved in /var/lib/centreon-engine/.ssh/id_rsa.pub.
The key fingerprint is:
1d:a3:3d:c6:13:22:af:6a:46:6f:c2:50:a2:53:3c:25 centreon-engine@xxxxx
The key's randomart image is:
+--[ RSA 2048]----+
| |
| E . |
| . o . . + |
| = . o * + |
| o + S B |
|o . . . . o |
| . + .. |
| =.o |
| o.o |
+-----------------+

It is recommended to use a dedicated user on the remote host to execute the checks. For instance, you can create a centreon user with a user folder /home/centreon/. The key will be stored in /home/centreon/.ssh/authorized_keys on the remote server.
You will need to give this user enough rights to execute the commands you will use.
The public key of centreon-engine is located in /var/lib/centreon-engine/.ssh/ id_rsa.pub

Centreon-engine@xxxxx:~$ ssh-copy-id -i .ssh/id_rsa.pub 
root@<IP_TARGET_SERVER>

error message is to connect via SSH from the centreon-engine user on the poller to the centreon user on the remote server.

su - centreon-engine
ssh centreon@<IP_TARGET_SERVER>

Now that you are connected to the remote server you can close the connection:

exit

 

Check command
 

You can create a generic check command on Centreon with this command line:

/usr/lib/centreon/plugins/centreon_plugins.pl --
plugin=os::linux::local::plugin --hostname=$HOSTADDRESS$ --ssh-
username=$_HOSTSSHUSER$ --mode=cmd-return --exec-
command=’$_SERVICECOMMAND$’ --command-options='$_SERVICEARGUMENTS$’
--manage-
returns='0,OK,$_SERVICEOKMESSAGE$#1,CRITICAL,$_SERVICECRITICALMESSAGE$#,
UNKNOWN,Command problem'

With this definition, you will need to fill this macro values:

  • On the host/host template:
    • SSHUSER
  • On the service/service template:
    • COMMAND
    • ARGUMENTS
    • OKMESSAGE
    • CRITICALMESSAGE

Example 1: Test the presence of the Nikon USB device on the monitored host :

/usr/lib/centreon/plugins/centreon_plugins.pl --
plugin=os::linux::local::plugin --hostname=<IP_TARGET_SERVER> --ssh-
username=centreon --mode=cmd-return --exec-command=lsusb --command-
options='| grep Nikon' --manage-returns='0,OK,APN IS
CONNECTED#1,CRITICAL,APN IS DISCONNECTED#,UNKNOWN,Command problem'

Presence of the USB device:

OK: APN IS CONNECTED | 'code'=0;;;;

No USB device:

CRITICAL: APN IS DISCONNECTED | 'code'=1;;;;


Example 2: Test if the /data NFS is mounted on our monitored host :

/usr/lib/centreon/plugins/centreon_plugins.pl --
plugin=os::linux::local::plugin --hostname=<IP_TARGET_SERVER> --ssh-
username=centreon --mode=cmd-return --exec-command=cat --command-
options='/proc/mounts | grep “<IP_OF_NFS_SERVER>:/data /data nfs4 rw,"'
--manage-returns='0,OK,/DATA IS UP#1,CRITICAL,/DATA IS DOWN#,UNKNOWN,Command problem'

The bolded area is surrounded by "". Without it we would have had an UNKNOWN status, so consider this if you have to put spaces in the value of the –-command-option.

 

NFS folder is well mounted: 

OK: /DATA IS UP | 'code'=0;;;;


NFS folder isn’t mounted:

CRITICAL: /DATA IS DOWN | 'code'=1;;;;


There are many possibilities with this mode. You also can execute your own custom script on the monitored server:

/usr/lib/centreon/plugins/centreon_plugins.pl --
plugin=os::linux::local::plugin --hostname=<IP_TARGET_SERVER> --ssh-
username=centreon --mode=cmd-return --exec-command=python --command-
options='/folder/check_apn.py' --manage-returns='0,OK,APN_REBOOT#1,CRITICAL,APN_NOT_CONNECTED#,UNKNOWN,Command problem'

 


4 replies

Badge +3

Hello, if I wanted to run a command like "ls" how could I put it to see if it responds to me

Userlevel 4
Badge +14

@cristianandres9 

About this mode, remember it’s a return of the command

/usr/lib/centreon/plugins/centreon_plugins.pl -- plugin=os::linux::local::plugin --hostname=<IP_TARGET_SERVER> --ssh- username=centreon --mode=cmd-return --exec-command=/usr/bin/ls --command-options='-ltr | grep <my_file_or_my_folder>' --manage-returns='0,OK,My File or Folder is present#1,My File or Folder is missing#,UNKNOWN,Command problem'

 

Badge +1

@cristianandres9 @gespada @Intence-Tech 

i have centreon 23.04.9 i looked for “centreon_plugins.pl” but didn’t found it, i only have these:
root@cent:/usr/lib/centreon/plugins# ls -ltr
-rwxrwxr-x 1 root root 401863 Sep 12 04:37 centreon_printers_generic_snmp.pl
-rwxrwxr-x 1 root root 327662 Sep 12 04:37 centreon_protocol_dns.pl
-rwxrwxr-x 1 root root 357923 Sep 12 04:37 centreon_centreon_map.pl
-rwxrwxr-x 1 root root 460399 Sep 12 04:37 centreon_protocol_http.pl
-rwxrwxr-x 1 root root 336023 Sep 12 04:37 centreon_protocol_ldap.pl
-rwxrwxr-x 1 root root 703203 Sep 12 04:37 centreon_linux_snmp.pl
-rwxrwxr-x 1 root root 550139 Sep 12 04:37 centreon_centreon_database.pl
-rwxrwxr-x 1 root root 525578 Sep 12 04:37 centreon_mysql.pl
-rwxrwxr-x 1 root root 601759 Sep 12 04:38 centreon_windows_snmp.pl
-rwxrwxr-x 1 root root 840519 Sep 12 04:38 centreon_cisco_standard_snmp.pl
-rwxrwxr-x 1 root root 394557 Sep 12 04:38 centreon_ups_standard_rfc1628_snmp.pl
-rwxrwxr-x 1 root root 374822 Sep 12 04:38 centreon_jvm_actuator.pl
-rwxrwxr-x 1 root root 350198 Sep 12 04:38 centreon_protocol_ftp.pl
-rwxrwxr-x 1 root root    959 Oct 12 10:59 tplink_router.pl
-rwxrwxr-x 1 root root 434398 Oct 17 11:02 centreon_centreon_central.pl
-rwxrwxr-x 1 root root 421585 Oct 17 11:02 centreon_centreon_poller.pl
-rwxrwxr-x 1 root root    769 Nov  2 06:00 check_ping.pl
root@cent:/usr/lib/centreon/plugins#

some of them i created them myself,


how to get this centreon_plugins.pl

 

and if it possible to share with us this .pl script, thanks in advance

Userlevel 4
Badge +14

@cristianandres9 @gespada @Intence-Tech 

i have centreon 23.04.9 i looked for “centreon_plugins.pl” but didn’t found it, i only have these:

root@cent:/usr/lib/centreon/plugins# ls -ltr
-rwxrwxr-x 1 root root 401863 Sep 12 04:37 centreon_printers_generic_snmp.pl
-rwxrwxr-x 1 root root 327662 Sep 12 04:37 centreon_protocol_dns.pl
-rwxrwxr-x 1 root root 357923 Sep 12 04:37 centreon_centreon_map.pl
-rwxrwxr-x 1 root root 460399 Sep 12 04:37 centreon_protocol_http.pl
-rwxrwxr-x 1 root root 336023 Sep 12 04:37 centreon_protocol_ldap.pl
-rwxrwxr-x 1 root root 703203 Sep 12 04:37 centreon_linux_snmp.pl
-rwxrwxr-x 1 root root 550139 Sep 12 04:37 centreon_centreon_database.pl
-rwxrwxr-x 1 root root 525578 Sep 12 04:37 centreon_mysql.pl
-rwxrwxr-x 1 root root 601759 Sep 12 04:38 centreon_windows_snmp.pl
-rwxrwxr-x 1 root root 840519 Sep 12 04:38 centreon_cisco_standard_snmp.pl
-rwxrwxr-x 1 root root 394557 Sep 12 04:38 centreon_ups_standard_rfc1628_snmp.pl
-rwxrwxr-x 1 root root 374822 Sep 12 04:38 centreon_jvm_actuator.pl
-rwxrwxr-x 1 root root 350198 Sep 12 04:38 centreon_protocol_ftp.pl
-rwxrwxr-x 1 root root    959 Oct 12 10:59 tplink_router.pl
-rwxrwxr-x 1 root root 434398 Oct 17 11:02 centreon_centreon_central.pl
-rwxrwxr-x 1 root root 421585 Oct 17 11:02 centreon_centreon_poller.pl
-rwxrwxr-x 1 root root    769 Nov  2 06:00 check_ping.pl
root@cent:/usr/lib/centreon/plugins#

some of them i created them myself,


how to get this centreon_plugins.pl

and if it possible to share with us this .pl script, thanks in advance

Hi,

Simply because that the original Centreon Plugins → https://github.com/centreon/centreon-plugins/blob/develop/src/centreon_plugins.pl the list you provide is a result of a packaging (.deb package or .rpm package) with a flatpak perl process → https://github.com/centreon/centreon-plugins/blob/develop/packaging/centreon-plugin-Operatingsystems-Linux-Local/pkg.json

So to have the centreon plugins centreon_linux_local.pl you do search and install the centreon-plugin-Operatingsystems-Linux-Local

Reply