Icinga2 - Check erstellen: Unterschied zwischen den Versionen

Aus QBWiki
Zur Navigation springenZur Suche springen
 
Zeile 7: Zeile 7:
 
   border-left: 1px dotted #999;
 
   border-left: 1px dotted #999;
 
   border-top: none;
 
   border-top: none;
   line-height: 1.22em;
+
   line-height: 1.32em;
 +
  font-size: 97.5%;
 
}
 
}
  

Aktuelle Version vom 26. März 2019, 09:12 Uhr


Beispiel: Anzahl der verfügbaren Pakete zur Aktualisierung

 1 #!/bin/sh
 2 
 3 set -o nounset
 4 set -o errexit
 5 
 6 while getopts ":c:w:" opt; do
 7   case $opt in
 8     c)
 9       crit_val="$OPTARG" ;;
10     w)
11       warn_val="$OPTARG" ;;
12     \?)
13       echo "Invalid Option -$OPTARG" >&2
14   esac
15 done
16 
17 update_count="$(yum check-update|wc -l)"
18 
19 if [[ "${warn_val:-}" -ge "${crit_val:-}" ]]; then
20   echo "UNKNOWN: Warning threshold can't be bigger than the Critical threshold"
21   exit 3
22 fi
23 
24 if [[ "${update_count:-ERROR}" -ge "${crit_val}" ]]; then
25   echo "CRITICAL: ${update_count}"
26   exit 2
27 
28 elif [[ "${update_count}" -ge "${warn_val}" ]]; then
29   echo "WARNING: ${update_count}"
30   exit 1
31 
32 elif [[ "${update_count}" -lt "${warn_val}" ]]; then
33   echo "OK: ${update_count}"
34   exit 0
35 else
36   echo "UNKNOWN: ${update_count}"
37   exit 3
38 fi


Beispiel: Tage seit dem letzten Update mit Paketmanager YUM

Um z.B. zu überprüfen, wann auf einem Server mit Yum als Paketmanager das letzte mal geupdatet wurde, haben wir zwei Möglichkeiten:

Als root-user direkt über einen Befehl
Nachteil: Authentifizierung als Root Benutzer
Vorteil: Kein Skript auf dem Zielrechner notwendig
Als icinga-user über ein selbstgeschriebenes Plugin
Vorteil: kein Zugriff über den Root account notwendig
Nachteil: allerdings einen Eintrag in der sudoers Datei


Beispielskript: check_yum -c [crit days] -w [warn days]

 1 #!/bin/bash
 2 while getopts ":c:w:" opt ; do
 3   case $opt in
 4     c)
 5       crit_days="$OPTARG"  ;;
 6     w)
 7       warn_days="$OPTARG"  ;;
 8     \?)
 9       echo "Invalid Option -$OPTARG" >&2
10   esac
11 done
12 
13 # Centos , YYYY-MM-DD
14 last_update="$(yum history list all | egrep ' U |Update' | head -n1 |awk '{print $6}')"
15 last_update_unix_time="$(date -d "${last_update}" +%s)"
16 
17 cur_date="$(date +%s)"
18 
19 timediff_s="$((${cur_date} - ${last_update_unix_time}))"
20 timediff_m="$((${timediff_s} / 60))"
21 timediff_h="$((${timediff_m} / 60))"
22 timediff_d="$((${timediff_h} / 24))"
23 timediff_w="$((${timediff_d} / 7))"
24 
25 diff_msg="Last update was ${timediff_d} days ago"
26 
27 
28 if [[ ${timediff_d} -ge ${crit_days} ]] ; then
29   echo "Critical: ${diff_msg}"
30   exit 2
31 fi
32 
33 if [[ ${timediff_d} -ge ${warn_days} ]] ; then
34   echo "Warning: ${diff_msg}"
35   exit 1
36 fi
37 
38 if [[ ${timediff_d} -lt ${warn_days} ]] ; then
39   echo "OK: ${diff_msg}"
40   exit 0
41 fi
42 
43 echo "Unknown: ${diff_msg}"
44 exit 3

Im Idealfall liegt das Skript auf dem Remoteserver unter /usr/lib64/nagios/plugins. Unser check-plugin, dass wir auf unserem Icinga2 Server erstellen müssen, muss quasi folgenden Befehl ausführen

/usr/lib64/nagios/plugins/check_by_ssh -H 213.147.9.200 -l icinga -i ~/.ssh/id_ed25519 -C 'sudo /usr/lib64/nagios/plugins/check_yum -c15 -w10'

Dies können wir wie folgt simulieren:

1 su - icinga -s /bin/bash -c "/usr/lib64/nagios/plugins/check_by_ssh -H 213.147.9.200 -l icinga -i ~/.ssh/id_ed25519 -C 'sudo /usr/lib64/nagios/plugins/check_yum -c15 -w10'"