Nagios shell脚本无法执行

Nagios shell脚本无法执行

我正在尝试使用 nagios 监控 GitLab。我创建了以下命令定义和 shell 脚本,但在检查服务时我收到了以下电子邮件。我该如何解决这个问题?该文件是可执行的。

[...] nagios : 3 incorrect password attempts ; TTY=unknown ; PWD=/ ; USER=git ; COMMAND=/bin/bash -c /var/lib/nagios/custom_plugins/check_gitlab.sh

命令定义:

define command {
    command_name custom_check_gitlab
    command_line /var/lib/nagios/custom_plugins/check_gitlab.sh
}

Shell 脚本:

#! /bin/sh
# [...]
RAILS_ENV="production"

# Script variable names should be lower-case not to conflict with internal /bin/sh variables such as PATH, EDITOR or SHELL.
app_root="/home/git/gitlab"
app_user="git"
unicorn_conf="$app_root/config/unicorn.rb"
pid_path="$app_root/tmp/pids"
socket_path="$app_root/tmp/sockets"
web_server_pid_path="$pid_path/unicorn.pid"
sidekiq_pid_path="$pid_path/sidekiq.pid"

### Here ends user configuration ###

# Switch to the app_user if it is not he/she who is running the script.
if [ "$USER" != "$app_user" ]; then
  sudo -u "$app_user" -H -i $0 "$@"; exit;
fi

# Switch to the gitlab path, if it fails exit with an error.
if ! cd "$app_root" ; then
 echo "Failed to cd into $app_root, exiting!";  exit 1
fi

### Init Script functions
check_pids(){
  if ! mkdir -p "$pid_path"; then
    echo "Could not create the path $pid_path needed to store the pids."
    exit 1
  fi
  # If there exists a file which should hold the value of the Unicorn pid: read it.
  if [ -f "$web_server_pid_path" ]; then
    wpid=$(cat "$web_server_pid_path")
  else
    wpid=0
  fi
  if [ -f "$sidekiq_pid_path" ]; then
    spid=$(cat "$sidekiq_pid_path")
  else
    spid=0
  fi
}

# Checks whether the different parts of the service are already running or not.
check_status(){
  check_pids
  # If the web server is running kill -0 $wpid returns true, or rather 0.
  # Checks of *_status should only check for == 0 or != 0, never anything else.
  if [ $wpid -ne 0 ]; then
    kill -0 "$wpid" 2>/dev/null
    web_status="$?"
  else
    web_status="-1"
  fi
  if [ $spid -ne 0 ]; then
    kill -0 "$spid" 2>/dev/null
    sidekiq_status="$?"
  else
    sidekiq_status="-1"
  fi
}

check_pids
check_status

if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
    echo "GitLab is not running."
    exit 2
fi
if [ "$web_status" != "0" ]; then
    printf "The GitLab Unicorn webserver is \033[31mnot running\033[0m.\n"
    exit 1
fi
if [ "$sidekiq_status" != "0" ]; then
    printf "The GitLab Sidekiq job dispatcher is \033[31mnot running\033[0m.\n"
    exit 1
fi
if [ "$web_status" = "0" -a "$sidekiq_status" = "0" ]; then
    printf "GitLab and all it's components are \033[32mup and running\033[0m.\n"
    exit 0
fi

答案1

问题出在这块代码上:

if [ "$USER" != "$app_user" ]; then
  sudo -u "$app_user" -H -i $0 "$@"; exit;
fi

您不需要向我们展示您的sudoers文件,但您需要授予用户以用户身份执行相关命令的nagios权限,并且无需提供密码。sudogit

就像是

nagios  ALL=(git) NOPASSWD: /bin/bash -c /var/lib/nagios/custom_plugins/check_gitlab.sh

可能是一个很好的起点。 编辑:您应该将该条目放入您的sudoers文件中。

相关内容