bash 如何从 Nagios 设置对 Docker 容器的监控
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31342815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I setup monitoring of Docker Container from Nagios
提问by user2882674
I am trying to set up a monitoring of docker container from the nagios.My nagios is on one VM and my docker is on another VM . So to monitor docker I am trying to use the shell script below:
我正在尝试从 nagios 设置对 docker 容器的监控。我的 nagios 在一个 VM 上,而我的 docker 在另一个 VM 上。因此,为了监视 docker,我尝试使用下面的 shell 脚本:
#!/bin/bash
# Author: Erik Kristensen
# Email: [email protected]
# License: MIT
# Nagios Usage: check_nrpe!check_docker_container!_container_id_
# Usage: ./check_docker_container.sh _container_id_
#
# The script checks if a container is running.
# OK - running
# WARNING - container is ghosted
# CRITICAL - container is stopped
# UNKNOWN - does not exist
CONTAINER=
RUNNING=$(docker inspect --format="{{ .State.Running }}" $CONTAINER 2> /dev/null)
if [ $? -eq 1 ]; then
echo "UNKNOWN - $CONTAINER does not exist."
exit 3
fi
if [ "$RUNNING" == "false" ]; then
echo "CRITICAL - $CONTAINER is not running."
exit 2
fi
GHOST=$(docker inspect --format="{{ .State.Ghost }}" $CONTAINER)
if [ "$GHOST" == "true" ]; then
echo "WARNING - $CONTAINER has been ghosted."
exit 1
fi
STARTED=$(docker inspect --format="{{ .State.StartedAt }}" $CONTAINER)
NETWORK=$(docker inspect --format="{{ .NetworkSettings.IPAddress }}" $CONTAINER)
echo "OK - $CONTAINER is running. IP: $NETWORK, StartedAt: $STARTED"
and placed the file on location /usr/lib64/nagios/plugins/ but in when I am run this script it tends to throw error.
并将文件放在位置 /usr/lib64/nagios/plugins/ 但在我运行此脚本时,它往往会引发错误。
Error:
错误:
check_docker: line 40: syntax error: unexpected end of file
check_docker:第 40 行:语法错误:文件意外结束
Being a java guy I really dont know much of this
作为一个 java 人,我真的不太了解这个
So could anybody please tell how can I achieve this task of monitoring the docker as I have already wasted my time on this
所以有人可以告诉我如何完成监视 docker 的任务,因为我已经浪费了时间
Thanks for help in advance.
提前感谢您的帮助。
回答by Palu Macil
The problem is an unexpected eol char from the copy / paste.
问题是复制/粘贴中意外的 eol 字符。
This is actually a Bash issue, or more specific... it's a special character at the end which you probably can't see because you probably copy/pasted from a website.
这实际上是一个 Bash 问题,或者更具体地说......它是最后一个你可能看不到的特殊字符,因为你可能是从网站复制/粘贴的。
Reason for the issue:
问题原因:
Lots of sites mention eol issues, but look herefor something short and to the point.
许多网站都提到了 eol 问题,但请在此处查看简短而中肯的内容。
DOS uses carriage return and line feed
("\r\n")
as a line ending, which Unix uses just line feed("\n")
. You need to be careful about transferring files between Windows machines and Unix machines to make sure the line endings are translated properly.
DOS 使用回车和换行
("\r\n")
作为换行符,而 Unix 只使用换行符("\n")
。在 Windows 机器和 Unix 机器之间传输文件时需要小心,以确保行尾正确转换。
Solution:
解决方案:
To fix the line endings for unix style eol, run dos2unix file.sh
but you might need to install the utility first. In Ubuntu it would be sudo apt-get install dos2unix
. Depending upon the environment, it could be the same for you.
要修复 unix 样式 eol 的行尾,请运行,dos2unix file.sh
但您可能需要先安装该实用程序。在 Ubuntu 中,它将是sudo apt-get install dos2unix
. 根据环境的不同,它对你来说可能是一样的。
Alternatives:
备择方案:
If you'd like to find out exactly what is at the end, you can look at Notepad++ on Windowsas it understands eol chars of various formats. You need to enable all chars in the menu.
如果您想确切地找出最后是什么,您可以查看Windows上的 Notepad++,因为它可以理解各种格式的 eol 字符。您需要启用菜单中的所有字符。
On Linux, Emacs Whitespace modewill show the same thing:
在Linux 上,Emacs 空白模式将显示相同的内容:
Toggle locally with M-x whitespace-mode RET
or see the link for (many) details and examples.
在本地切换M-x whitespace-mode RET
或查看(许多)详细信息和示例的链接。