Inventory Archive CleanUp Script & Usage

clean-up-inventory.sh
#!/bin/bash

# Funktion zur Anzeige der Hilfe
show_help() {
  echo "Verwendung: $0 [Tage]"
  echo "Löscht Dateien im Verzeichnis /opt/omd/sites/\$site/var/check_mk/inventory_archive/, die älter als die angegebene Anzahl von Tagen sind."
  echo "Standardwert für Tage ist 30."
  echo "Beispiel: $0 45"
  exit 1
}

# Überprüfe, ob das Hilfeargument angegeben wurde
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
  show_help
fi

# Standardwert für Tage ist 30
days=${1:-30}

# Hole die Namen der Sites
sites=$(omd sites | awk '{print $1}')

# Initialisiere Zähler und Speicherplatz
total_deleted_files=0
total_freed_space=0

# Iteriere über die Sites und lösche die Dateien
for site in $sites; do
  archive_dir="/opt/omd/sites/$site/var/check_mk/inventory_archive/"
  echo "Verzeichnis: $archive_dir"

  # Finde und lösche Dateien, die älter als die angegebene Anzahl von Tagen sind
  deleted_files=$(find $archive_dir -type f -mtime +$days -exec rm -v {} + | wc -l)
  freed_space=$(du -sb $archive_dir | awk '{print $1}')

  # Aktualisiere die Gesamtwerte
  total_deleted_files=$((total_deleted_files + deleted_files))
  total_freed_space=$((total_freed_space + freed_space))

  echo "Site: $site"
  echo "Gelöschte Dateien: $deleted_files"
  echo "Freigegebener Speicherplatz: $(du -sh $archive_dir | awk '{print $1}')"
  echo
done

# Ausgabe der Gesamtwerte
echo "Gesamt gelöschte Dateien: $total_deleted_files"
echo "Gesamt freigegebener Speicherplatz: $(du -sh /opt/omd/sites | awk '{print $1}')"

Verwendung des Skripts / Usage of script

Deutsche Anleitung

Deutsch

Beschreibung

Dieses Bash-Skript löscht Dateien im Verzeichnis /opt/omd/sites/$site/var/check_mk/inventory_archive/, die älter als die angegebene Anzahl von Tagen sind. Es ist für die Verwendung mit Check_MK/OMD-Systemen gedacht, um Platz im Archivverzeichnis freizugeben.

Verwendung


./script.sh [Tage]

  • Tage: (Optional) Anzahl der Tage, nach denen Dateien als veraltet gelten und gelöscht werden. Standardwert ist 30 Tage.

  • Hilfe: Verwende -h oder --help, um die Hilfe anzuzeigen.

Beispiel


./script.sh 45

Löscht alle Dateien, die älter als 45 Tage sind, in den Archivverzeichnissen aller OMD-Sites.

Ausgabe

Das Skript gibt für jede Site Folgendes aus:

  • Verzeichnis, das durchsucht wird.

  • Anzahl der gelöschten Dateien.

  • Freigegebener Speicherplatz (in lesbarem Format).

Am Ende wird eine Zusammenfassung der gesamt gelöschten Dateien und des freigegebenen Speicherplatzes angezeigt.

Voraussetzungen

  • Das Skript muss mit Root-Berechtigungen ausgeführt werden.

  • Das omd-Kommando muss verfügbar sein.

  • Die Befehle find, rm, du und awk müssen installiert sein.

Automatisierung mit Cron

Um das Skript regelmäßig auszuführen, können Sie einen Cronjob einrichten. Führen Sie crontab -e als root-Benutzer aus und fügen Sie eine Zeile wie die folgende hinzu:


# Lösche wöchentlich (jeden Sonntag um 03:30 Uhr) Inventar-Archivdateien, die älter als 60 Tage sind.

30 3 * * 0 /pfad/zum/script.sh 60 > /dev/null 2>&1

  • 30 3 * * 0: Führt den Befehl jeden Sonntag um 03:30 Uhr aus.

  • /pfad/zum/script.sh 60: Der absolute Pfad zu Ihrem Skript, gefolgt von der gewünschten Anzahl an Tagen.

  • > /dev/null 2>&1: Unterdrückt die Ausgabe des Skripts, um E-Mail-Benachrichtigungen durch Cron zu vermeiden.

Hinweise

  • Vorsicht: Das Skript löscht Dateien unwiderruflich.

  • Testen Sie das Skript vor der Automatisierung gründlich.


English description

English

Description

This Bash script deletes files in the directory /opt/omd/sites/$site/var/check_mk/inventory_archive/ that are older than the specified number of days. It is designed for use with Check_MK/OMD systems to free up space in the archive directory.

Usage


./script.sh [days]

  • days: (Optional) Number of days after which files are considered outdated and deleted. The default is 30 days.

  • Help: Use -h or --help to display the help message.

Example


./script.sh 45

Deletes all files older than 45 days in the archive directories of all OMD sites.

Output

The script outputs the following for each site:

  • The directory being processed.

  • The number of deleted files.

  • The amount of freed storage space (in human-readable format).

At the end, a summary of the total deleted files and freed storage space is displayed.

Requirements

  • The script must be run with root permissions.

  • The omd command must be available.

  • The commands find, rm, du, and awk must be installed.

Automation with Cron

To run the script regularly, you can set up a cron job. Run crontab -e as the root user and add a line similar to the following:


# Weekly cleanup (every Sunday at 3:30 AM) of inventory archive files older than 60 days.

30 3 * * 0 /path/to/your/script.sh 60 > /dev/null 2>&1

  • 30 3 * * 0: Executes the command every Sunday at 3:30 AM.

  • /path/to/your/script.sh 60: The absolute path to your script, followed by the desired number of days.

  • > /dev/null 2>&1: Suppresses the script’s output to prevent cron from sending email notifications.

Notes

  • Caution: The script deletes files permanently.

  • Test the script thoroughly before automating it.

1 Like

Thanks Bernd!
Keep in mind: the script only removes archive files and does not handle delta caches.
There are dependencies between archive and delta cache files, so it is not that straight forward to write a proper clean-up. So, use at own risk :slight_smile:

Thanks Martin for the note!
Do you have any ideas for improvements that I should or could integrate? It would be nice if it were finally integrated directly into CMK.

Integration of CleanUp job for Inventory Archive - Checkmk

other Idea

Inventory - switch from flat files to a modern database. - Checkmk

Greetz Bernd

I have too little knowledge about that part of the code, but just got that hint from a dev. So can’t give any advice.

It’s on our to-do list though (clean-up job).

1 Like

This issue has been known for a long time and was also submitted to the Checkmk Ideas Portal back in 2023:
https://ideas.checkmk.com/suggestions/489943/improve-automatic-disk-cleanup-eg-inventory_archive
While it hasn’t been part of the official roadmap so far, it’s good to hear from Martin that it’s now at least on their internal to-do list though.

The history files are only used to generate the delta cache files when someone opens the inventory history view. Each time this view is opend in the GUI, it triggers the creation of these delta cache files. If the inventory history view times out due to the large number of history files, you can work around this by reopening the view multiple times. Each time, the system generates some of the missing delta cache files — until eventually all required deltas are available and the view loads properly.

With this in mind, you could, for example, first trigger the generation of the delta cache files via an automation user. Once the view no longer returns an error and all deltas have been generated, you can safely delete the old history files without losing any history.

2 Likes

What would be your preferred criterion for history file cleanup?

  • Keep inventory files that are not older than A days (e.g., 100 days).
  • Keep a maximum of N history entries per host (e.g., 50), regardless of age.
3 Likes

This valuable workaround offers prompt and significant relief and helps mitigate the symptoms until the primary causes of the high disk space usage in the inventory process are comprehensively addressed in due course.

We would greatly appreciate it if this effective workaround could also be made available in version 2.4. Doing so would enable all your affected customers to benefit at the earliest opportunity, rather than having to develop individual solutions and wait until they are prepared to upgrade to version 2.5 in 2026/27.

1 Like

This is no workaround.

It might get backported to 2.4, if it does not risk stability. Let’s see :slight_smile:

2 Likes

now I have made a generell CleanUp script:

#!/bin/bash
# Checkmk Site Cleanup Script for Version 2.4
# Run this as the site user (omd su <site>)

echo "--- Starte Checkmk Cleanup für Instanz: $OMD_SITE ---"

# 1. Entferne alte Crash-Dumps (oft sehr groß)
echo "Bereinige Crash-Dumps..."
find ~/var/check_mk/crashes -type f -mtime +30 -delete

# 2. Lösche alte Kern-Dumps (falls vorhanden)
echo "Bereinige Core-Dumps..."
find ~/var/check_mk/core -type f -mtime +14 -delete

# 3. Bereinige den Agenten-Cache (Special Agents etc.)
echo "Lösche temporären Agent-Cache..."
rm -rf ~/tmp/check_mk/cache/*

# 4. Alte Logfiles der Instanz rotieren/löschen
# Wir löschen Logs, die älter als 30 Tage sind und bereits komprimiert wurden
echo "Bereinige archivierte Logfiles..."
find ~/var/log -name "*.gz" -mtime +30 -delete

# 5. Temporäre UI-Dateien (Web-Exports etc.)
echo "Bereinige temporäre Web-Exporte..."
find ~/var/check_mk/web -name "*.export" -mtime +7 -delete

# 6. Lösche alte Update-Reste (falls vorhanden)
echo "Bereinige omd_update Reste..."
rm -rf ~/var/omd/update_temp/*

echo "--- Cleanup abgeschlossen ---"
df -h /