-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapt_ubuntu
135 lines (111 loc) · 4.76 KB
/
apt_ubuntu
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
#!/usr/bin/env python
# -*- encoding: iso-8859-1 -*-
""" apt_ububtu
Plugin to monitor packages that should be installed on Ubuntu systems.
Author: Stefan Daniel Schwarz <[email protected]>
Author: Glen Searle <[email protected]>
v1.0 2008-11-07 - First draft
v1.1 2008-11-08 - critical = #: First # critical, rest warning
v1.2 2008-11-09 - Code cleanup for MuninExchange submission
v2.0 2014-11-10 - Full rewrite by Glen Searle as v1 doesn't work with current versions of apt.
Usage: place in /etc/munin/plugins/ (or link it there using ln -s)
Parameters understood
config (required)
autoconf (optional - used by munin-config)
Magic markers - optional - used by installation scripts and
munin-config"""
#%# capabilities=autoconf
#%# family=contrib
###########################################################
category = 'system' # 'upgrades'
title = 'Upgradable packages' # 'Upgradeable packages'
vlabel = 'Total packages'
other = 'other'
total = 'total'
#archives = ['security', 'updates', 'proposed', 'backports']
archives = ['security', 'updates']
colour = ['ff0000', '22ff22', '0022ff', '00aaaa', 'ff00ff']
origins = ['Ubuntu']
critical = 1
###########################################################
import os
import sys
import warnings
import time
import stat
import subprocess
""" Expected output with no command line options
security.value 78 │·
updates.value 33 │·
proposed.value 0 │·
backports.value 0 │·
other.value 1 │·
total.value 112
"""
def autoconf():
# I've been asked if I'm useful.
if os.path.exists("/var/lib/update-notifier/update-motd-updates-available"):
print 'yes'
else:
print 'no'
sys.exit(0)
def config():
print 'graph_category %s' % (category)
print 'graph_title %s' % (title)
#print 'graph_total %s' % (total)
print 'graph_vlabel %s' % (vlabel)
for i, archive in enumerate(archives + [other]):
if len(colour) > i:
print '%s.colour %s' % (archive, colour[i])
if i < critical:
print '%s.critical 0:0' % (archive)
if i == 0:
print '%s.draw AREA' % (archive)
else:
print '%s.draw STACK' % (archive)
print '%s.label %s' % (archive, archive)
if i + 1 > critical:
print '%s.warning 0:0' % (archive)
print 'total.colour 000000'
print 'total.draw LINE1'
print 'total.label %s' % (total)
sys.exit(0)
def command_line_argument():
if sys.argv[1] == 'autoconf':
autoconf()
elif sys.argv[1] == 'config':
config()
elif sys.argv[1]:
print('unknown argument "' + sys.argv[1] + '"')
sys.exit(0)
def new_count():
DEVNULL = open(os.devnull, 'w')
subprocess.call(['nohup', '/usr/lib/update-notifier/update-motd-updates-available'], stdout=FNULL, stderr=subprocess.STDOUT)
def file_age_in_seconds(pathname):
return time.time() - os.stat(pathname)[stat.ST_MTIME]
if __name__ == '__main__':
if len(sys.argv) > 1: command_line_argument()
# If there are no command line options there we continue here.
if not os.path.exists("/var/lib/update-notifier/updates-available"):
# We look for the MOTD file.
print "Missing file:updates-available This should have been generated by the Ubuntu motd scripts."
sys.exit(1)
else:
if file_age_in_seconds("/var/lib/update-notifier/updates-available") > 100000:
# A day is 86400 seconds long.
new_count()
available = open('/var/lib/update-notifier/updates-available', 'r')
#Strip the two values we want from the motd text.
for line in available:
if "package" in line:
packages = line.split(' ')[0]
if "security" in line:
security = line.split(' ')[0]
try:
total = int(packages) + int(security)
except ValueError:
print "There's an error with getting numbers from the updates-available file."
sys.exit(1)
print "security.value %d" % int(security)
print "updates.value %d" % int(packages)
print "total.value %d" % total