-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapt-upgradable
More file actions
executable file
·140 lines (130 loc) · 4.46 KB
/
apt-upgradable
File metadata and controls
executable file
·140 lines (130 loc) · 4.46 KB
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
#!/bin/bash
#
# For more information either run `munindoc apt-upgradable` or read
# `apt-upgradable.pod`
#
#%# family=auto
#%# capabilities=autoconf
APTITUDE=${aptitude_path:-/usr/bin/aptitude}
UPG_COLOR=${upgrades_color:-00ff00}
ESS_COLOR=${essential_color:-ff0000}
IMP_COLOR=${important_color:-000000}
IMPORTANT_PKGS=${important:-""}
ARR_IMPORTANT=($IMPORTANT_PKGS)
CACHE="/var/lib/munin-node/plugin-state/$(whoami)/apt_upgradable"
if [ ! -d ${CACHE} ]; then
mkdir ${CACHE}
fi
function in_array() {
local x
ENTRY=$1
shift 1
ARRAY=( "$@" )
[ -z "${ARRAY}" ] && return 1
[ -z "${ENTRY}" ] && return 1
for x in ${ARRAY[@]}; do
[ "${x}" == "${ENTRY}" ] && return 0
done
return 1
}
###
# Returns the content of a file in the cache. If not available, return U
#
# Params
# $1 The filename (relative to the cache dir)
##
function cached() {
local fname
fname=$1
if [ -f ${CACHE}/${fname} ]; then
echo $(cat ${CACHE}/${fname})
else
echo U
fi
}
case $1 in
autoconf)
if [ -x ${APTITUDE} ] ; then
echo "yes"
else
echo "no ('${APTITUDE}' is not executable. You can override the path using the 'aptitude_path' environment variable)!"
fi
exit 0;;
config)
echo "graph_title Available APT upgrades"
echo "graph_vlabel packages"
echo "graph_category system"
echo "graph_printf %3.0lf"
echo "graph_args --base 1000 --lower-limit 0"
echo "upgrades.label Upgradable packages"
echo "upgrades.draw AREA"
echo "upgrades.warning 15"
echo "upgrades.critical 30"
echo "upgrades.colour ${UPG_COLOR}"
echo "essential.label Essential upgrades"
echo "essential.draw STACK"
echo "essential.warning 5"
echo "essential.critical 10"
echo "essential.colour ${ESS_COLOR}"
if [ "${#ARR_IMPORTANT[@]}" -ne 0 ]; then
echo "important.label Important upgrades"
echo "important.draw LINE"
echo "important.warning 0:0"
echo "important.colour ${IMP_COLOR}"
fi
exit 0;;
esac
ESSENTIAL=$(cached essential)
UPGRADES=$(cached upgrades)
IMPORTANT=$(cached important)
echo "essential.value $ESSENTIAL"
echo "upgrades.value $UPGRADES"
echo "important.value $IMPORTANT"
pgrep apt
if [[ "$?" == "0" ]]; then
# Another instance of apt is running. This should not break this script. We
# will simply return the cached values.
exit 0
fi
# Now run the value updated in background. A lock file is created to avoid
# running multiple instances. This ensures that the munin plugin returns fast,
# so munin can continue processing other plugins. This also means that the
# values are always delayed by one execution of this plugin, and that the first
# value will be unknown. But this is not a real-time plugin, so that's okay.
if [ ! -f ${CACHE}/lock ]; then
{
touch ${CACHE}/lock
NEW_ESS=$(${APTITUDE} search "?and(?and(?not(?automatic), ?upgradable), ?essential)" | wc -l)
NEW_UPG=$(${APTITUDE} search "?and(?and(?not(?automatic), ?upgradable), ?not(?essential))" | wc -l)
NEW_IMP="U"
if [ "${#ARR_IMPORTANT[@]}" -ne 0 ]; then
NUM_IMPORTANT=0
for pkg in $($APTITUDE -F "%p" search ~U); do
in_array ${pkg} "${ARR_IMPORTANT[@]}"
RET=$?
if [ "${RET}" -eq 0 ]; then
NUM_IMPORTANT=$(($NUM_IMPORTANT+1))
fi
done
NEW_IMP=${NUM_IMPORTANT}
fi
# Try to write all values in one atomic operation before removing the
# lock. This way we should either get all or no values and should avoid
# interleaving of multiple processes in parallel.
# NOTE: In the case of parallel processes, the last one to finish wins.
{
echo $NEW_ESS > ${CACHE}/essential
echo $NEW_UPG > ${CACHE}/upgrades
echo $NEW_IMP > ${CACHE}/important
} && rm ${CACHE}/lock
} &
else
# Either another process is running (highly unlikely, except on EXTREMELY
# high load) or something went wrong (more likely). In order to avoid
# graphs with incorrect data, we will reset them to "UNKNOWN". This should
# trigger the interest of a sysadmin to see what's going on.
echo Lock file ${CACHE}/lock exists. Not updating the APT values. 1>&2
echo U > ${CACHE}/essential
echo U > ${CACHE}/upgrades
echo U > ${CACHE}/important
fi