I've encouter some problems with my scripts.
So to make it short and clear, I'm in an internship and my boss gave me an assignement to make a Probe for Nagios.
The purpose of this Probe is to check on all our hosts if a package is missing from the repo if yes then we have a Warning on Nagios with the list of missing packages.
This Probe can include a Whitelist if we want to keep a package which is not in whatever repo but we're not using it.
Here is my script:
#!/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
And there is the message that is coming frome Nagios :
Remote command execution failed: Failed to open file /var/lib/apt/lists//security.debian.org_dists_stretch_updates_InRelease for reading: Permission denied
I tried to play with lock file in my script but it doesn't work, I got like an infinite loop.
I already gave a look at this question and tried to make a wrapper but he didn't worked or I missed something since I'm student and this is my first step in the shell scripting world.
I looked at this, too but it's kinda old.
EDIT : I found how to do it Thx a lot to all the people trying to help me and specially Sergiy Kolodyazhnyy
So I used APT and DPkg Hooks for that and I had to modify my script to use this hooks.
I create a file named 00apt-show-version in /etc/apt/apt.conf.d/ with this two lines :
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";}
Each time apt-get update is called or apt-get install/remove, apt-show-versions will do is job and all the stdout of apt-show-versions is written in nagios_apt_show_versions.
After that I had to modify my script like this :
#!/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
BTW the Withelist is now working (well kind of but dat's not goal of this thread, I was just asking about hooks)
apt-cache
command. Package actions can be queued using aptdaemon, but that can be complex to use. – user535733 Jan 21 '19 at 18:29