Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 3 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 4 | # |
| 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 | """ |
| 19 | Voltha internal diagnostics |
| 20 | """ |
Zsolt Haraszti | 0778a24 | 2017-01-18 01:11:54 -0800 | [diff] [blame] | 21 | |
Zsolt Haraszti | aa4626e | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 22 | import arrow |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 23 | import gc |
Zsolt Haraszti | aa4626e | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 24 | import structlog |
| 25 | import resource |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 26 | |
Zsolt Haraszti | b5d72f1 | 2017-01-15 20:44:02 -0800 | [diff] [blame] | 27 | import sys |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 28 | from simplejson import dumps |
| 29 | from twisted.internet.defer import Deferred |
| 30 | from twisted.internet.task import LoopingCall |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 31 | from zope.interface import implementer |
| 32 | |
| 33 | from common.event_bus import EventBusClient |
Chip Boling | 8f17162 | 2018-08-17 10:39:54 -0500 | [diff] [blame^] | 34 | from voltha.protos.events_pb2 import KpiEvent2, KpiEventType, MetricInformation, MetricMetaData |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 35 | from voltha.registry import IComponent, registry |
| 36 | |
| 37 | log = structlog.get_logger() |
| 38 | |
| 39 | |
| 40 | @implementer(IComponent) |
| 41 | class Diagnostics(object): |
| 42 | |
| 43 | def __init__(self, config): |
| 44 | self.config = config |
Zsolt Haraszti | aa4626e | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 45 | self.periodic_check_interval = config.get( |
| 46 | 'periodic_check_interval', 15) |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 47 | 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 Boling | 8f17162 | 2018-08-17 10:39:54 -0500 | [diff] [blame^] | 66 | ts = arrow.utcnow().float_timestamp |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 67 | |
Zsolt Haraszti | aa4626e | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 68 | def deferreds(): |
| 69 | return len(gc.get_referrers(Deferred)) |
| 70 | |
| 71 | def rss_mb(): |
Zsolt Haraszti | b5d72f1 | 2017-01-15 20:44:02 -0800 | [diff] [blame] | 72 | rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024 |
| 73 | if sys.platform.startswith('darwin'): |
| 74 | rss /= 1024 |
| 75 | return rss |
Zsolt Haraszti | aa4626e | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 76 | |
Chip Boling | 8f17162 | 2018-08-17 10:39:54 -0500 | [diff] [blame^] | 77 | kpi_event = KpiEvent2( |
Zsolt Haraszti | 0778a24 | 2017-01-18 01:11:54 -0800 | [diff] [blame] | 78 | type=KpiEventType.slice, |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 79 | ts=ts, |
Chip Boling | 8f17162 | 2018-08-17 10:39:54 -0500 | [diff] [blame^] | 80 | 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 Haraszti | 0778a24 | 2017-01-18 01:11:54 -0800 | [diff] [blame] | 88 | self.event_bus.publish('kpis', kpi_event) |
Zsolt Haraszti | 99509d3 | 2016-12-10 16:41:45 -0800 | [diff] [blame] | 89 | log.debug('periodic-check', ts=ts) |
Chip Boling | 8f17162 | 2018-08-17 10:39:54 -0500 | [diff] [blame^] | 90 | |