blob: b1adf62759ef881c694e26ae0c0c1cef584ddb0b [file] [log] [blame]
Zsolt Haraszti99509d32016-12-10 16:41:45 -08001#!/usr/bin/env python
2#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08003# Copyright 2017 the original author or authors.
Zsolt Haraszti99509d32016-12-10 16:41:45 -08004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18"""
19Voltha internal diagnostics
20"""
Zsolt Haraszti0778a242017-01-18 01:11:54 -080021
Zsolt Harasztiaa4626e2016-12-08 16:53:06 -080022import arrow
Zsolt Haraszti99509d32016-12-10 16:41:45 -080023import gc
Zsolt Harasztiaa4626e2016-12-08 16:53:06 -080024import structlog
25import resource
Zsolt Haraszti99509d32016-12-10 16:41:45 -080026
Zsolt Harasztib5d72f12017-01-15 20:44:02 -080027import sys
Zsolt Haraszti99509d32016-12-10 16:41:45 -080028from simplejson import dumps
29from twisted.internet.defer import Deferred
30from twisted.internet.task import LoopingCall
Zsolt Haraszti99509d32016-12-10 16:41:45 -080031from zope.interface import implementer
32
33from common.event_bus import EventBusClient
Chip Boling8f171622018-08-17 10:39:54 -050034from voltha.protos.events_pb2 import KpiEvent2, KpiEventType, MetricInformation, MetricMetaData
Zsolt Haraszti99509d32016-12-10 16:41:45 -080035from voltha.registry import IComponent, registry
36
37log = structlog.get_logger()
38
39
40@implementer(IComponent)
41class Diagnostics(object):
42
43 def __init__(self, config):
44 self.config = config
Zsolt Harasztiaa4626e2016-12-08 16:53:06 -080045 self.periodic_check_interval = config.get(
46 'periodic_check_interval', 15)
Zsolt Haraszti99509d32016-12-10 16:41:45 -080047 self.periodic_checks = None
48 self.event_bus = EventBusClient()
49 self.instance_id = registry('main').get_args().instance_id
50
51 def start(self):
52 log.debug('starting')
53 self.periodic_checks = LoopingCall(self.run_periodic_checks)
54 self.periodic_checks.start(self.periodic_check_interval)
55 log.info('started')
56 return self
57
58 def stop(self):
59 log.debug('stopping')
60 if self.periodic_checks is not None:
61 self.periodic_checks.stop()
62 log.info('stopped')
63
64 def run_periodic_checks(self):
65
Chip Boling8f171622018-08-17 10:39:54 -050066 ts = arrow.utcnow().float_timestamp
Zsolt Haraszti99509d32016-12-10 16:41:45 -080067
Zsolt Harasztiaa4626e2016-12-08 16:53:06 -080068 def deferreds():
69 return len(gc.get_referrers(Deferred))
70
71 def rss_mb():
Zsolt Harasztib5d72f12017-01-15 20:44:02 -080072 rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024
73 if sys.platform.startswith('darwin'):
74 rss /= 1024
75 return rss
Zsolt Harasztiaa4626e2016-12-08 16:53:06 -080076
Chip Boling8f171622018-08-17 10:39:54 -050077 kpi_event = KpiEvent2(
Zsolt Haraszti0778a242017-01-18 01:11:54 -080078 type=KpiEventType.slice,
Zsolt Haraszti99509d32016-12-10 16:41:45 -080079 ts=ts,
Chip Boling8f171622018-08-17 10:39:54 -050080 slice_data=[
81 MetricInformation(metadata=MetricMetaData(title='voltha.internal',
82 ts=ts,
83 context={'instance_id': self.instance_id}),
84 metrics={'deferreds': deferreds(),
85 'rss-mb': rss_mb()}
86 )
87 ])
Zsolt Haraszti0778a242017-01-18 01:11:54 -080088 self.event_bus.publish('kpis', kpi_event)
Zsolt Haraszti99509d32016-12-10 16:41:45 -080089 log.debug('periodic-check', ts=ts)
Chip Boling8f171622018-08-17 10:39:54 -050090