-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneedrestart.chart.py
84 lines (68 loc) · 2.39 KB
/
needrestart.chart.py
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
# -*- coding: utf-8 -*-
# Description: needrestart python.d module for netdata
# Author: nodiscc ([email protected])
# SPDX-License-Identifier: GPL-3.0-or-later
import os
import re
from bases.FrameworkServices.SimpleService import SimpleService
priority = 90000
update_every = 120
ORDER = [
'status'
]
CHARTS = {
'status': {
'options': [None, 'Restart required', 'needs restart', 'status', 'needrestart.status', 'stacked'],
'lines': [
['error', None, 'absolute'],
['kernel', None, 'absolute'],
['services', None, 'absolute'],
]
}
}
RE_KERNEL = re.compile(r'NEEDRESTART-KSTA: [0|2|3]')
RE_SERVICES = re.compile(r'NEEDRESTART-SVC.*')
class Service(SimpleService):
def __init__(self, configuration=None, name=None):
SimpleService.__init__(self, configuration=configuration, name=name)
self.order = ORDER
self.definitions = CHARTS
self.data = dict()
self.path = '/var/log/needrestart.log'
self.modtime = ''
self.data['error'] = 0
self.data['kernel'] = 0
self.data['services'] = 0
def check(self):
return True
def get_data(self):
if not is_readable(self.path) or is_empty(self.path):
self.debug("{0} is unreadable or empty".format(self.path))
self.data['error'] = 1
self.data['kernel'] = 0
self.data['services'] = 0
return self.data
else:
self.data['error'] = 0
try:
if not self.is_changed():
self.debug("{0} modification time is unchanged, returning previous values".format(self.path))
return self.data
file = open(self.path, 'r')
except:
self.error("Error while opening {0}".format(self.path))
self.data['error'] = 1
self.data['kernel'] = 0
self.data['services'] = 0
return self.data
self.modtime = os.path.getmtime(self.path)
lines = file.read()
self.data['kernel'] = len(re.findall(RE_KERNEL, lines))
self.data['services'] = len(re.findall(RE_SERVICES, lines))
return self.data
def is_changed(self):
return self.modtime != os.path.getmtime(self.path)
def is_readable(path):
return os.path.isfile(path) and os.access(path, os.R_OK)
def is_empty(path):
return os.path.getsize(path) == 0