Skip to content
Snippets Groups Projects
Select Git revision
  • c9022a9d175f1e607bb133d64c25bfd78dfe48e4
  • master default protected
  • simple-task/7248-eol-check-add-node-22
  • 6877_check_iml_deployment
4 results

check_reboot_required

Blame
  • check_reboot_required 4.05 KiB
    #!/bin/bash
    # ======================================================================
    #
    # NAGIOS CLIENT CHECK :: is a restart required?
    #
    # ----------------------------------------------------------------------
    # works for sure on
    # - centos7+
    # - debian6+
    # - ubuntu10+
    # ----------------------------------------------------------------------
    # 2016-08-12  added ouput of packages
    # 2020-03-05  v1.2  <axel.hahn@iml.unibe.ch> switch to ph.* helper functions
    # 2022-10-14  v1.3  <axel.hahn@unibe.ch>     fix debian output if /var/run/reboot-required.pkgs does not exist
    #                                            Shellfixes; add help
    # 2023-03-29  v1.4  <martin.gasser@unibe.ch> add almalinux as distro
    # 2024-03-07  v1.4  <martin.gasser@unibe.ch> add rocky as distro
    # 2025-02-11  v1.5  <axel.hahn@unibe.ch>     add default banner for IML checks; add support for manjaro
    # ======================================================================
    
    
    . $(dirname $0)/inc_pluginfunctions
    
    self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
    self_APPVERSION=1.5
    
    distro=$( ph.getOS )
    
    # ----------------------------------------------------------------------
    # functions
    # ----------------------------------------------------------------------
    
    function showHelp(){
    cat <<EOF
    $( ph.showImlHelpHeader )
    
    Check if a reboot is required.
    It works for Centos and Debian/ Ubuntu and Manjaro
    
    If the reboot is required:
    
    * On Centos it shows the kernel number that will be installed,
    * On Debian/ Ubuntu it shows the packages that require the reboot,
    * On Manjaro it shows if a newer kernel or which libs require the reboot.
      The check for libs uses lsof.
    
    If your distro is based on Arch, Centos or Debian then it easily can be added
    in the script if it does not work. Report the distro name to the author.
    
    SYNTAX:
    $(basename $0)
    
    OPTIONS:
    
        -h or --help   show this help.
    
    PARAMETERS:
    
        None.
    
    EXAMPLE:
    $(basename $0)
    
    EOF
    }
    # ----------------------------------------------------------------------
    
    # --- check param -h
    case "$1" in
        "--help"|"-h")
            showHelp
            exit 0
            ;;
        *)
    esac
    
    case $distro in
    
      "centos"|"almalinux"|"rocky")
        currentkernel=$(uname -r)
        out=$(rpm -q --last kernel | head -1 | grep -F $currentkernel)
        if [ -z "$out" ]; then
          ph.setStatus "warning"
          ph.status "[$distro] need to reboot for kernel $(rpm -q --last kernel | head -1) (current: $currentkernel)"
        else
          ph.status "[$distro] no reboot required (kernel is up to date: $currentkernel)"
        fi
        ;;
    
      "debian"|"ubuntu")
      
        if [ ! -f /var/run/reboot-required ]; then
            ph.status "[$distro] no reboot required"
        else
            byPackages="(I do not know the package that requires it)"
            test -f /var/run/reboot-required.pkgs && byPackages="by $( tr '\n' ',' < /var/run/reboot-required.pkgs )"
            ph.setStatus "warning"
            ph.status "[$distro] $( cat /var/run/reboot-required ) ${byPackages}"
        fi
        ;;
    
      # 
      "manjaro")
        kernel_current="$( uname -r | sed 's/-[a-z]*$//i' )"
        kernel_installed="$( pacman -Q linux | cut -f 2 -d " " )"
    
        sReason=""
        if [ "$kernel_current" != "$kernel_installed" ]; then
          sReason+="Kernel $kernel_installed was installed ... "
        fi
    
        libs=""
        if which lsof > /dev/null; then
          libs=$(lsof -n +c 0 2> /dev/null | grep 'DEL.*lib' | awk '1 { print $1 ": " $NF }' | sort -u)
          if [[ -n $libs ]]; then
            sReason+="Libs reqire a reboot ... "
          fi
        fi
    
        if [ -n "$sReason" ]; then
          ph.setStatus "warning"
          ph.status "[$distro] $sReason"
          cat <<< $libs
        else
          ph.status "[$distro] No reboot required."
          if ! which lsof > /dev/null; then
            echo "Remark: Install lsof to search for libs that require a reboot."
          fi
        fi
        ;;
    
      *)
        ph.setStatus "unknown"
        ph.status "distro [$distro] was detected but is not supported (yet)."
        echo "If your distro is based on Arch, Centos or Debian then it easily can be added in the script. Report the distro name to the author."
        ;; 
    esac
    
    ph.exit
    
    # ----------------------------------------------------------------------