Use Nagios to Check Your Zypper
August 20th, 2008 by Mitch Frazier in
If you use Nagios to monitor your system and run openSUSE on a remote server the bash script presented here will check for online updates and is designed to be run by Nagios so that the result will appear on the Nagios service-detail page.
The script is pretty unsophisticated as it just parses the output from the zypper command. A more sophsiticated solution might interact directly with libzypp, the library that provides zypper with its functionality. Of course, that's not possible using bash. Based on a quick scan of the libzypp documentation it appears the only current option for doing that is C++.
A Nagios plugin script outputs information in two ways:
- It writes a short status message (one line of text) to the standard output which Nagios displays on the service-detail page.
- It sets its exit status to indicate to Nagios the status
of the service:
- 0 if the service status is "OK"
- 1 if the service status is "WARNING"
- 2 if the service status is "CRITICAL"
If you run zypper from the command line you will see output similar to the following:
$ sudo zypper list-updates * Reading repository 'openSUSE-10.3-Updates' cache * Reading repository 'Main Repository (OSS)' cache * Reading repository 'Main Repository (NON-OSS)' cache * Reading repository 'Packman Repository' cache * Reading installed packages [100%] Repository: | Name | Version | Category | Status ----------------------+------------------+---------+----------+------- openSUSE-10.3-Updates | dhcpcd | 5390-0 | optional | Needed openSUSE-10.3-Updates | openmotif22-libs | 4540-0 | optional | Needed openSUSE-10.3-Updates | ruby | 5483-0 | security | NeededThe script looks for the "----" line and then breaks the subsequent lines into fields, using the "|" as the separator character. Using this data it then builds the output text and sets the status. The status is set to 0 if there are no updates available, 1 if there are updates available, and 2 if any of the updates are security related.
Running zypper can take a while so you can run the script in test mode to get some immediate satisfaction. In test mode it contains some test zypper output which it parses:
$ env TEST=1 sh check_zypper 3 Updates needed: package-1(required/Needed), package-3(required/Needed), package-4(required/Needed) (1 Optional update) $ echo $? 1The output text is what Nagios would display on the service-detail page. Nagios will set the service status to "WARNING" since the script status on exit is 1 (meaning updates are availabe).
The script follows:
#!/bin/bash
max_packages=3
TEST=${TEST:-0}
VERBOSE=${VERBOSE:-0}
nl='
'
# Read list of packages that need updating according to zypper.
OIFS=$IFS
IFS=$nl
if [[ $TEST -eq 0 ]]; then
zypper_lines=($(/usr/bin/sudo /usr/bin/zypper list-updates 2>&1))
stat=$?
if [[ $stat -ne 0 ]]; then
echo "Zypper exited with code: $stat"
exit 1
fi
else
zypper_lines=(
"test line 1"
"test line 2"
"------------"
"Main Repository | package-1 | 50-0 | required | Needed"
"Main Repository | package-2 | 48-0 | optional | Needed"
"Test Repository | package-3 | 40-0 | required | Needed"
"Test Repository | package-4 | 40-0 | required | Needed"
)
fi
IFS=$OIFS
# Count the number of optional and non-optional packages.
npackages=0
noptional_packages=0
nsecurity_packages=0
in_packages=0
for ix in ${!zypper_lines[*]}
do
line=${zypper_lines[$ix]}
if [[ $VERBOSE -ne 0 ]]; then echo $line; fi
if [[ $in_packages -eq 0 ]]; then
if [[ $line =~ ^---- ]]; then in_packages=1; fi
else
IFS='|'
set -- $line
IFS=$OIFS
if [[ $# -eq 5 ]]; then
trepo=$(echo $1)
tpackage=$(echo $2)
tversion=$(echo $3)
tcategory=$(echo $4)
tstatus=$(echo $5)
if [[ "$tcategory" == 'optional' ]]; then
let noptional_packages++
else
repo[$npackages]=$trepo
package[$npackages]=$tpackage
version[$npackages]=$tversion
category[$npackages]=$tcategory
status[$npackages]=$tstatus
let npackages++
if [[ "$tcategory" == 'security' ]]; then let nsecurity_packages++; fi
fi
fi
fi
done
# Output summary.
if [[ $npackages -ne 1 ]]; then s1='s'; else s1='' ; fi
if [[ $noptional_packages -ne 1 ]]; then s2='s'; else s2='' ; fi
n=0
for ix in ${!package[*]}
do
t[$n]="${package[$ix]}(${category[$ix]}/${status[$ix]})"
let n++
if [[ ix -eq $max_packages ]]; then break; fi
done
if [[ ${#package[*]} -gt $max_packages ]]; then t[$n]="..."; fi
if [[ ${#t[*]} -gt 0 ]]; then
if [[ $npackages -gt 0 ]]; then
echo -n "$npackages Update$s1 needed: "
first=1
for ix in ${!t[*]}
do
if [[ $first -eq 1 ]]; then
echo -n ' '
first=0
else
echo -n ', '
fi
echo -n "${t[$ix]}"
done
if [[ $noptional_packages -gt 0 ]]; then
echo -n " ($noptional_packages Optional update$s2)"
fi
else
echo -n "$noptional_packages Optional update$s2"
fi
echo
stat=1
if [[ $nsecurity_packages -gt 0 ]]; then stat=2; fi
else
echo "OK, no package updates available"
stat=0
fi
exit $stat
There are a few things you need to do to integrate the script with Nagios. First setup sudo to allow the Nagios user to run zypper. Use visudo to add this to the sudoers file:
nagios ALL=(ALL) NOPASSWD:/usr/bin/zypperSecond, because running zypper takes a while you need to increase the service-run-time in the Nagios configuration file /etc/nagios/nagios.cfg:
service_check_timeout=600And lastly, of course, you need to setup Nagios to run the script:
define command{
command_name check-zypper
command_line /path/to/check_zypper
}
...
define service{
use generic-service
host_name localhost
service_description Updates
is_volatile 0
check_period 24x7
max_check_attempts 1
normal_check_interval 600
retry_check_interval 60
contact_groups admins
notifications_enabled 0
notification_options c
notification_interval 960
notification_period 24x7
check_command check-zypper
}
Among other things, this tells Nagios to run check_zypper
every 10 hours and if it fails, to retry in an hour.
p.s. Yeah, the title is pretty "cheap humor", but then again I'm not the one who decided to call my package manager zypper.
__________________________Mitch Frazier is an Associate Editor at Linux Journal.
Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Sorry, offer available in the US only. International orders, click here.
Subscribe now!
The Latest
Featured Videos
Linux Journal Live - eBook Readers and DRM
November 14th, 2008 by Shawn Powers in
The November 13, 2008 edition of Linux Journal Live! Shawn Powers and special guest, Linux Journal Author Daniel Bartholomew, talk e-book readers and Daniel's Kindle, DRM, and other goodness.
Run Your Windows Partition Without Rebooting
November 13th, 2008 by Elliot Isaacson in
Dual booting is a necessary evil and very inconvenient. What if you could run your windows partition in a virtual machine, so you wouldn't have to worry about rebooting anymore? With VMWare Workstation, you can.
Recently Popular
From the Magazine
December 2008, #176
The Oxford English Dictionary says the word "gadget" is a placeholder name for a technical item whose precise name one can't remember. Like that book-reader thingy from Amazon...what's it called? Spindle, Gindle...Kindle, that's it. Check it out in this month's gadget issue.
Other gadgets covered include the Nokia tablets, the BlackBerry, the Neo FreeRunner, the Dash Express, the Roku Netflix Player, the Kangaroo TV, The TomTom GO 930 and the MooBella Ice Cream System. On the larger hardware front, read the reviews of the Acer Aspire One and the YDL PowerStation. On the software front, check out the articles and columns on memcached, Samba security, Mutt, desktop gadgets, bash and Puppet. To wrap it all up, read Doc's thoughts on Google and the browser platform.
Delicious
Digg
Reddit
Newsvine
Technorati







Post new comment