mercredi 21 mars 2012

Surveillance des sauvegardes linux avec nagios/centreon

Bien, nous avons vu dans un post précédent, un script permettant de sauvegarder des configs variées sur un serveur linux.

Mais comment être sûr que la sauvegarde est bien effectuée sans aller regarder tous les jours ? En utilisant l'excellent couple nagios/centreon bien sûr. Je passe les détails d'une installation et configuration complète de cette solution, pour me concentrer uniquement sur le problème actuel :

Les sauvegardes sont placées sur un serveur windows (équipé d'un serveur rsync pour améliorer les perfs de la sauvegarde), et si tout se déroule correctement, dans le répertoire contenant les sauvegardes, on place un petit fichier "dateSauvegarde", qui contiendra uniquement le mot "ok" si tout s'est bien déroulé, et un rapport d'erreur en cas de problème.

La surveillance de la sauvegarde va ainsi se cantonner à
1) vérifier si la date du fichier "dateSauvegarde" est celle de la veille ou du jour
2) vérifier si le fichier contient "OK"

Pour cela, sur le serveur windows, on configure nsclient pour qu'il réponde aux requêtes nrpe, et on lui configure une commande (dans la section nrpe de nsc.ini) :

[External Scripts]
check_backup_tintin=c:\windows\system32\cscript.exe //NoLogo //T:60 C:\NSClient++-0.3.6-Win32-20090330-2223\check_file_backup.wsf /d:"D:\Backup-Siege\tintin" /s:"ok" /f:"dateSauvegarde" /ok:true /n:1

Sur centreon, on configure la sonde associée au serveur stockant les sauvegardes en utilisant check_nrpe, et en appelant la bonne commande : !check_backup_tintin

Et basiquement c'est tout. Par ce simple mécanisme, on sera averti si la sauvegarde n'a pas fonctionné (date du fichier < j-1), ou si elle a rencontré des erreurs (pas de "ok" dans le fichier).

Je persiste et signe : l'informatique est un boulot de fainéant!

En prime, le fameux vbscript qui vérifie tout ça :

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' jumboman@yahoo.com
'
' Feel free to improve it. But please let me have a copy if you do.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
<job>
    <runtime>
        <description>
            Checks if the latest file in a specified folder matching a template
            has the specified string. Return codes can be specified for both
            positive and negative match scenarios.
        </description>


    <named
        name="h"
        helpstring="Help"
        type="simple"
        required="false"
    />

    <named
        name="d"
        helpstring="directory to look into"
        type="string"
        required="true"
    />

    <named
        name="f"
        helpstring="regex to match file within the folder"
        type="string"
        required="true"
    />

    <named
        name="s"
        helpstring="The string to look for"
        type="string"
        required="true"
    />

    <named
        name="ok"
        helpstring="return success if match found"
        type="string"
        required="false"
    />

    <named
        name="n"
        helpstring="number of days exceeding which triggers error"
        type="string"
        required="false"
    />

    <named
        name="e"
        helpstring="Encoding - Unicode or Ascii (default)"
        type="string"
        required="false"
    />

    </runtime>
<script language="VBScript">

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Const intOK = 0
Const intWarning = 1
Const intCritical = 2
Const forReading = 1
Const Unicode = -1
Const Ascii = 0

limit = 0

latest = 0
latestFile = ""
success = true
enc = Ascii

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Handle input
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Wscript.Arguments.Named.Exists("h") Then
    Wscript.Echo "Usage: check_file_backup.wsf /d:<dir> /f:<regex> /s:<string> [/ok:<1|0>] [/n:<days>] [/e:<U|A]"
        Wscript.Echo "/d: - directory to look into "
        Wscript.Echo "/f: - regex string to match file names "
        Wscript.Echo "/s: - string to look for in the file "
        Wscript.Echo "/ok: -should the match be treated as success"
        Wscript.Echo "/n: - allowed age in number of days [optional]"
        Wscript.Echo "/e: - encoding U or A(default) [optional]"
        Wscript.Quit(intOK)
End If

If Wscript.Arguments.Named.Exists("d") Then

    dirName = Wscript.Arguments.Named("d")
Else
    Wscript.Echo "ERROR: Missing Directory Name Input"
    Wscript.Quit(intCritical)
End If

If Wscript.Arguments.Named.Exists("f") Then

    fileNameEx = Wscript.Arguments.Named("f")
Else
    Wscript.Echo "ERROR: Missing File Name Expression Input"
    Wscript.Quit(intCritical)
End If

If Wscript.Arguments.Named.Exists("s") Then

    searchStr = Wscript.Arguments.Named("s")
Else
    Wscript.Echo "ERROR: Missing Match string"
    Wscript.Quit(intCritical)
End If

If Wscript.Arguments.Named.Exists("ok") Then

    success = CBool(Wscript.Arguments.Named("ok"))
End If

If Wscript.Arguments.Named.Exists("n") Then

    limit = Cint(Wscript.Arguments.Named("n"))
End If

If Wscript.Arguments.Named.Exists("e") Then

    If Wscript.Arguments.Named("e") = "U" Then
        enc = Unicode
    End If
End If

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' logic
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = fileNameEx


Set fso = CreateObject("Scripting.FileSystemObject")

Set folder = fso.GetFolder(dirName)
Set files = folder.Files


'loop through files found and find the latest file


For each file In files

    Set match = objRegEx.Execute(file.Name)
    If match.Count > 0 Then
        'Wscript.Echo file.Name & " " & file.DateCreated
        x = CDate(file.DateLastModified)
        If x > latest Then
            Set latestFile = file
            latest = x
        End If

    End if
Next

If latest > 0 Then

    If limit > 0 Then
        age = DateDiff("d",latest,Date)
        If age > limit Then
            Wscript.Echo "ERROR: " & latestFile.Name & " " & latest & " more than " & limit & " Days old"
            Wscript.Quit(intCritical)
        End If
    End If

    found = false
    foundText = " Not Found in "
    foundLine = ""

    Set file1 = fso.OpenTextFile(latestFile, forReading, False, enc)

    Do Until file1.AtEndOfStream
        line = file1.Readline
        'Wscript.Echo line
        pos = Instr(1, line, searchStr, 1)
        If Int(pos) > 0 Then
            found = true
            foundText = " Found in "
            foundLine = line
            Exit Do
        End If
    Loop
    file1.Close()

    'do a XNOR

    If (not success and not found) or (success and found) Then
        Wscript.Echo "OK: '" & searchStr & "'" & foundText & latestFile.Name & " " & latest
        Wscript.Quit(intOK)
    Else
        Wscript.Echo "ERROR: '" & searchStr & "'" & foundText & latestFile.Name & " " & latest
        Wscript.Quit(intCritical)
    End If
Else
        Wscript.Echo "ERROR: No File Match Found"
        Wscript.Quit(intCritical)
End If

    </script>
</job>



Aucun commentaire:

Enregistrer un commentaire