Ghr*_*rew 4 package-management apt bash scripts nagios3
我的脚本遇到了一些问题。
简而言之,我正在实习,我的老板给了我一个任务,让我为 Nagios 做一个 Probe。
这个探测器的目的是检查我们所有的主机,如果一个包从 repo 中丢失,如果是,那么我们在 Nagios 上有一个带有丢失包列表的警告。
如果我们想保留一个不在任何存储库中但我们不使用它的包,则此探测器可以包含一个白名单。
这是我的脚本:
#!/bin/bash
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
which apt-show-versions >/dev/null
STATE=$?
declare -a WHITELIST=( host1:elasticsearch:all host2:elasticsearch:all)
PACKAGES=()
NOT_AVAILABLE=()
HOST=$(hostname)
#while [ -f /var/lib/apt/lists/lock ]
#do
# sleep 2
#done
if [ "$STATE" = 0 ] #Verifie la condition apt-show-version = installer
then
packets=$(apt-show-versions | grep 'No available version in archive' | cut -d" " -f1)
for packet in $packets;do
PACKAGES+=("${HOST}:$packet")
done
for package in "${PACKAGES[@]}"; do
if [ "${WHITELIST[*]}" != "${package}" ]; then
NOT_AVAILABLE+=("$package")
fi
done
if [ -z "$NOT_AVAILABLE" ]; then
#Ok dans Nagios
echo "There is no package without version in archive"
exit $STATE_OK
else
#Warning dans Nagios
echo "Some package have no available version in archive"
echo ${NOT_AVAILABLE[*]//"${HOST}":}
exit $STATE_WARNING
fi
else
#Critical dans nagios
echo "Package apt-show-versions is missing"
exit $STATE_CRITICAL
fi
Run Code Online (Sandbox Code Playgroud)
还有来自 Nagios 的消息:
Remote command execution failed: Failed to open file /var/lib/apt/lists//security.debian.org_dists_stretch_updates_InRelease for reading: Permission denied
Run Code Online (Sandbox Code Playgroud)
我试图在我的脚本中使用锁定文件,但它不起作用,我就像一个无限循环。
我已经看过这个问题 并试图制作一个包装器,但他没有工作,或者我错过了一些东西,因为我是学生,这是我在 shell 脚本世界中的第一步。
我也看过这个,不过有点旧了。
编辑:我发现如何做到这一点非常感谢所有试图帮助我的人,特别是 Sergiy Kolodyazhnyy
所以我为此使用了 APT 和 DPkg Hooks,我不得不修改我的脚本来使用这个 hooks。
我用这两行在 /etc/apt/apt.conf.d/ 中创建了一个名为 00apt-show-version 的文件:
APT::Update::Post-Invoke {"apt-show-versions 1>/tmp/nagios_apt_show_versions";};
DPkg::Post-Invoke {"apt-show-versions 1> /tmp/nagios_apt_show_versions";}
Run Code Online (Sandbox Code Playgroud)
每次 apt-get update 被调用或 apt-get install/remove 时,apt-show-versions 将完成工作并且 apt-show-versions 的所有标准输出都写在 nagios_apt_show_versions 中。
之后,我不得不像这样修改我的脚本:
#!/bin/bash
#Script qui verifie si les paquets installer sur une machine ne dispose
#pas de versions dans les depots.
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
which apt-show-versions >/dev/null
STATE=$?
declare -a WHITELISTHOST=(host1:elasticsearch:all host2:elasticsearch:all host2:linux-image-4.9.0-0.bpo.5-amd64:amd64 host3:python-django-flatpages-tinymce:all)
WHITELIST=()
NOT_AVAILABLE=()
HOST=$(hostname)
if [ "$STATE" = 0 ]; then #Verifie la condition apt-show-version = installer
#Verifie que le fichier n existe pas et le cree
if [ ! -f "/tmp/nagios_apt_show_versions" ]; then
touch /tmp/nagios_apt_show_versions
fi
packets=$(grep 'No available version in archive' /tmp/nagios_apt_show_versions | cut -d: -f1)
for white in "${WHITELISTHOST[@]}"; do
if [ "${HOST}" = "$(echo $white | cut -d: -f1)" ]; then
WHITELIST+=("$(echo $white | cut -d: -f2)")
fi
done
for packet in $packets; do
if [ "${WHITELIST[@]}" != "${packet}" ]; then
NOT_AVAILABLE+=("$packet")
fi
done
if [ -z "$NOT_AVAILABLE" ]; then
#Ok dans Nagios
echo "There is no package without version in archive"
exit $STATE_OK
else
#Warning dans Nagios
echo "Some package have no available version in archive"
echo ${NOT_AVAILABLE[*]}
exit $STATE_WARNING
fi
else
#Critical dans nagios
echo "Package apt-show-versions is missing"
exit $STATE_CRITICAL
fi
Run Code Online (Sandbox Code Playgroud)
顺便说一句,Withelist 现在正在工作(很好,但 dat 不是这个线程的目标,我只是在问钩子)
考虑使用lsof来检查文件是否正在使用,如相关 Serverfault 帖子中所推荐。你可以这样做:
while [ "x$(lsof /var/lib/apt/lists/lock)" != "x" ] ; do
# if lsof returns output, that means some apt task is running
# wait 60 seconds and check again
sleep 60
done
Run Code Online (Sandbox Code Playgroud)
另一个命令是fuser(恕我直言,比 更好lsof)。根据文档:
如果没有访问任何指定文件或发生致命错误,fuser 将返回非零返回代码。如果至少找到了一个访问,则 fuser 返回零。
这意味着您可以依赖循环中的退出状态,这使得语法更好:
while fuser /var/lib/apt/lists/lock > /dev/null ; do
sleep 60
done
Run Code Online (Sandbox Code Playgroud)
理想情况下,您可能应该fnctl()通过 C 或 Python使用函数类型来查看文件是否被锁定。
也可以看看:
| 归档时间: |
|
| 查看次数: |
1374 次 |
| 最近记录: |