fixing issues after testing apache_cache.chart.py
paulfantom committed
Jul 1, 2016 at 17:33 UTC
55665fd09709ef4c8e8233789ea9f035467c2543
2 files changed
+25
-13
python.d/apache_cache.chart.py
+10
-4
@@ -6,6 +6,7 @@ from base import LogService
6
7
priority = 60000
8
retries = 5
9
+update_every = 3
10
11
ORDER = ['cache']
12
CHARTS = {
@@ -22,7 +23,7 @@ class Service(LogService):
23
def __init__(self, configuration=None, name=None):
24
LogService.__init__(self, configuration=configuration, name=name)
25
if len(self.log_path) == 0:
25
- self.log_path = "/var/log/httpd/cache.log"
26
+ self.log_path = "/var/log/apache2/cache.log"
27
self.order = ORDER
28
self.definitions = CHARTS
29
@@ -33,19 +34,24 @@ class Service(LogService):
34
"""
35
try:
36
raw = self._get_data()
37
+ if raw is None:
38
+ return None
39
except (ValueError, AttributeError):
40
return None
41
42
hit = 0
43
miss = 0
44
+
45
for line in raw:
46
if "cache hit" in line:
47
hit += 1
48
elif "cache miss" in line:
49
miss += 1
50
47
- if hit + miss == 0:
51
+ total = hit + miss
52
+ if total == 0:
53
return None
54
50
- return {'hit': int(hit/float(hit+miss) * 100),
51
- 'miss': int(miss/float(hit+miss) * 100)}
55
+ hit_percent = int(hit/float(total) * 100)
56
+ return {'hit': hit_percent,
57
+ 'miss': 100 - hit_percent}
python.d/python_modules/base.py
+15
-9
@@ -425,9 +425,13 @@ class LogService(SimpleService):
425
# 'chart_name_in_netdata' : [ charts['chart_name_in_netdata']['lines']['name'] ]
426
# }
427
self.log_path = ""
428
- self._last_line = 0
428
+ self._last_line = -1
429
# self._log_reader = None
430
SimpleService.__init__(self, configuration=configuration, name=name)
431
+ # FIXME Remove preventing of frequent log parsing
432
+ if self.timetable['freq'] < 3:
433
+ self.timetable['freq'] = 3
434
+ self.retries = 100000 # basically always retry
435
436
def _get_data(self):
437
# FIXME find faster solution of reading data. Maybe implement reading in subprocess?
@@ -436,7 +440,7 @@ class LogService(SimpleService):
440
# if self._log_reader.poll() is not None:
441
# self._log_reader = Popen(['tail', '-F', self.log_path], stdout=PIPE, stderr=STDOUT)
442
lines = []
439
- last = 0
443
+ last = -1
444
total = 0
445
try:
446
with open(self.log_path) as fp:
@@ -447,10 +451,10 @@ class LogService(SimpleService):
451
total += 1
452
except Exception as e:
453
msg.error(self.__module__, str(e))
450
- if last != 0:
454
+ if last != -1:
455
self._last_line = last
452
- elif self._last_line > total:
453
- self._last_line = 0
456
+ if self._last_line > total:
457
+ self._last_line = -1
458
459
if len(lines) != 0:
460
return lines
@@ -466,12 +470,14 @@ class LogService(SimpleService):
470
except (KeyError, TypeError):
471
self.error("No path to log specified. Using: '" + self.log_path + "'")
472
469
- # FIXME Remove preventing of frequent log parsing
470
- if self.update_every < 3:
471
- self.update_every = 3
472
-
473
if os.access(self.log_path, os.R_OK):
474
return True
475
else:
476
self.error("Cannot access file: '" + self.log_path + "'")
477
return False
478
+
479
+ def create(self):
480
+ status = SimpleService.create(self)
481
+ self._last_line = -1
482
+ return status
483
+