/usr/share/munin/plugins/apt is in munin-node 1.4.6-3ubuntu3.4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | #!/usr/bin/perl -w
# -*- perl -*-
#
# Plugin to monitor packages that should be installed on systems using
# apt-get (mostly Debian, but also RedHat)
#
# Usage: place in /etc/munin/node.d/ (or link it there using ln -s)
#
# Parameters understood:
#
# config (required)
# autoconf (optional - used by munin-config)
# update <maxinterval> <probability>
# Updates the APT database randomly, guaranteeing there
# won't be more than <maxinterval> seconds between each
# update. Otherwise, there is a a 1 in <probability>
# chance that an update will occur.
#
=head1 NAME
apt - Monitor the packages that should be installed on systems using apt-get.
=head1 APPLICABLE SYSTEMS
Mostly Debian and Ubuntu but also RedHat and other distributions which now use apt.
=head1 CONFIGURATION
This plugin needs a cronjob that runs 'apt-get update' every hour or so
/etc/cron.d/munin-plugin-apt
53 * * * * root apt-get update > /dev/null 2>&1
23 08 * * * root apt-get update > /dev/null
Remember to randomize when these cronjobs are run on your servers
=head1 USAGE
Link this plugin in /etc/munin/plugins/ and restart the munin-node.
=head1 NOTES
This plugin does not actually draw graphs; it is rather designed to
generated warnings. To get a graph showing the number of available
packages, please see the "apt_all" plugin.
=head1 MAGIC MARKERS
#%# family=manual
#%# capabilities=autoconf
=head1 BUGS
None known.
=head1 VERSION
$Id: apt.in 3371 2010-02-22 11:36:13Z ssm $
=head1 AUTHOR
Unknown
=head1 LICENSE
GPLv2
=cut
# Now for the real work...
use strict;
use Munin::Common::Defaults;
$ENV{'LANG'}="C";
$ENV{'LC_ALL'}="C";
my $statefile = "$Munin::Common::Defaults::MUNIN_PLUGSTATE/plugin-apt.state";
sub update_state() {
if(-l $statefile) {
die("$statefile is a symbolic link, refusing to touch it.");
}
open(STATE, ">$statefile")
or die("Couldn't open state file $statefile for writing.");
print STATE "Last update: " . localtime() . "\n";
close(STATE);
}
sub update_helpandexit() {
print("apt update <maxinterval> <probability> -- update apt databases randomly\n\n",
" maxinterval:\n",
" Enforce the updating of the apt database if it has\n",
" been more than (maxinterval many seconds since the last update.\n\n",
" probability:\n",
" There's a 1 in (probability) chance that the database\n",
" will be updated.\n");
exit(1);
}
if ($ARGV[0] and $ARGV[0] eq "autoconf")
{
`apt-get -v >/dev/null 2>/dev/null`;
if ($? eq "0")
{
print "yes\n";
exit 0;
}
else
{
print "no (apt-get not found)\n";
exit 0;
}
}
if ($ARGV[0] and $ARGV[0] eq "config")
{
print "graph_title Pending packages\n";
print "graph no\n";
print "pending.label pending\n";
print "pending.warning 0:0\n";
print "hold.label hold\n";
exit 0;
}
if ($ARGV[0] and $ARGV[0] eq "update")
{
my $maxinterval = $ARGV[1] ? $ARGV[1] : update_helpandexit;
my $probability = $ARGV[2] ? $ARGV[2] : update_helpandexit;
# if it's been $probability seconds since the last update, do
# it now.
if(-e $statefile &&
(stat($statefile))[10] + $maxinterval < time()) {
update_state();
exec("/usr/bin/apt-get update")
or die("Unable to exec() apt-get");
}
# if the state-file doesn't exist, create it.
if(!-e $statefile) {
update_state();
}
# update the database if the 1 in $probability check hits.
if(!int(rand($probability))) {
update_state();
exec("/usr/bin/apt-get update")
or die("Unable to exec() apt-get");
}
exit(0);
}
open (APT, "apt-get -u dist-upgrade --print-uris --yes |") or exit 22;
my @pending = ();
my $hold = 0;
my @remove = ();
my @install = ();
while (<APT>)
{
if (/^The following packages will be REMOVED:/)
{
my $where = 0;
while (<APT>)
{
last if (/^\S/);
foreach my $package (split /\s+/)
{
next unless ($package =~ /\S/);
push (@remove, "-$package");
}
}
}
if (/^The following NEW packages will be installed:/)
{
my $where = 0;
while (<APT>)
{
last if (/^\S/);
foreach my $package (split /\s+/)
{
next unless ($package =~ /\S/);
push (@install, "+$package");
}
}
}
if (/^The following packages will be upgraded/)
{
my $where = 0;
while (<APT>)
{
last if (/^\S/);
foreach my $package (split /\s+/)
{
next unless ($package =~ /\S/);
push (@pending, $package);
}
}
}
if (/^\d+\supgraded,\s\d+\snewly installed, \d+ to remove and (\d+) not upgraded/) {
$hold = $1;
}
}
push (@pending, @install) if @install;
push (@pending, @remove ) if @remove;
close APT;
print "pending.value ", scalar (@pending), "\n";
if (@pending)
{
print "pending.extinfo ", join (' ', @pending), "\n";
}
print "hold.value $hold\n";
exit 0;
# vim:syntax=perl
|