Skip to main content

18_SMB

Surveiller le port SMB (445) — méthode la plus simple

Nagios possède déjà le plugin check_tcp.

Commande à ajouter dans commands.cfg

define command{
    command_name    check_smb_port
    command_line    $USER1$/check_tcp -H $HOSTADDRESS$ -p 445
}

Service à ajouter dans ton fichier host (ex: monclient.cfg)

define service{
    use                     generic-service
    host_name               monclient
    service_description     Port SMB 445
    check_command           check_smb_port
}

Ça vérifie que le port SMB est ouvert
Fonctionne même si Samba n’est pas installé (NAS, appliances, etc.)

Surveiller le service Samba (smbd) via NRPE

Sur le client Linux, ajoute dans /etc/nagios/nrpe.cfg :

command[check_smbd]=/usr/lib/nagios/plugins/check_procs -C smbd -c 1:

Puis redémarre NRPE :

sudo systemctl restart nagios-nrpe-server

Dans Nagios (serveur)

define service{
    use                     generic-service
    host_name               monclient
    service_description     Service SMB (smbd)
    check_command           check_nrpe!check_smbd
}

On ajoute la ligne suivante dans notre commands.cfg

define command{
    command_name    check_nrpe
    command_line    /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

Vérifie que le processus smbd tourne
Plus précis que le port
Nécessite NRPE

Vérifier un partage SMB

Parfait, on va te créer un vrai plugin Nagios custom pour tester un partage Samba précis, exactement comme check_smb le faisait avant.

👉 Tu vas taper UNE commande dans le terminal
👉 Et tu auras OK / CRITICAL comme un vrai plugin Nagios.

Je te donne toute la procédure complète, propre, prête à l’emploi.

Créer le script check_samba_share

On installe smbclient:

sudo apt install smbclient

Sur ton serveur Nagios, crée un fichier :

sudo nano /usr/local/nagios/libexec/check_samba_share

Colle exactement ceci :

#!/bin/bash

HOST="$1"
SHARE="$2"
USER="$3"
PASS="$4"

if [ -z "$HOST" ] || [ -z "$SHARE" ]; then
    echo "Usage: $0 <host> <share> [user] [pass]"
    exit 3
fi

if [ -z "$USER" ]; then
    smbclient //$HOST/$SHARE -N -c ls >/dev/null 2>&1
else
    smbclient //$HOST/$SHARE -U $USER%$PASS -c ls >/dev/null 2>&1
fi

if [ $? -eq 0 ]; then
    echo "OK - Le partage //$HOST/$SHARE est accessible"
    exit 0
else
    echo "CRITICAL - Impossible d'accéder au partage //$HOST/$SHARE"
    exit 2
fi

🛠️ 2) Rendre le script exécutable

sudo chmod +x /usr/local/nagios/libexec/check_samba_share

🧪 3) Tester dans le terminal (ce que tu veux)

🔹 Partage public (sans login)

/usr/local/nagios/libexec/check_samba_share 192.168.200.16 public

Résultat :

OK - Le partage //192.168.200.16/public est accessible

ou :

CRITICAL - Impossible d'accéder au partage //192.168.200.16/public

🔹 Partage protégé (avec login)

/usr/local/nagios/libexec/check_samba_share 192.168.200.16 public monuser monpass

À mettre dans commands.cfg**

Ajoute ceci dans :

/usr/local/nagios/etc/objects/commands.cfg
define command{
    command_name    check_samba_share
    command_line    /usr/local/nagios/libexec/check_samba_share $HOSTADDRESS$ $ARG1$ $ARG2$ $ARG3$
}

$ARG1 = nom du partage
$ARG2 = utilisateur (optionnel)
$ARG3 = mot de passe (optionnel)

À mettre dans monclient.cfg**

Ouvre :

/usr/local/nagios/etc/objects/monclient.cfg

Et ajoute un service pour un partage public (sans login) :

define service{
    use                     generic-service
    host_name               filerbrowser
    service_description     Samba_share_images
    check_command           check_samba_share!images
}

Ici, images est le nom du partage Samba.

Version avec authentification**

Si ton partage demande un login :

define service{
    use                     generic-service
    host_name               filerbrowser
    service_description     Samba_share_images_auth
    check_command           check_samba_share!images!user!password
}

⚠️ Attention :
Les mots de passe dans les fichiers Nagios sont en clair.
Si tu veux, je peux te montrer comment les mettre dans un fichier sécurisé $USERx$.

Recharger Nagios**

Toujours sur ton serveur :

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Tu dois avoir :

Total Errors: 0

Puis :

sudo systemctl restart nagios