'use strict' ; import { getDaemonLogger } from '../universal/LoggerBroker' ; import { ILogger , LoggerRotator } from 'pandora-service-logger' ; import { CpuUsageGaugeSet , NetTrafficGaugeSet , SystemMemoryGaugeSet , SystemLoadGaugeSet , DiskStatGaugeSet , MetricsActuatorServer , MetricLevel , MetricsCollectPeriodConfig , MetricName , NetworkTrafficGaugeSet } from 'pandora-metrics' ; import { GlobalConfigProcessor } from '../universal/GlobalConfigProcessor' ; const debug = require ( 'debug' ) ( 'pandora:cluster:monitor' ) ; /**
 * Class Monitor
 */ export class BaseMonitor { protected daemonLogger = getDaemonLogger ( ) ; protected globalConfigProcesser = GlobalConfigProcessor . getInstance ( ) ; protected globalConfig = this . globalConfigProcesser . getAllProperties ( ) ; protected server ; protected metricsCollectPeriodConfig = MetricsCollectPeriodConfig . getInstance ( ) ; return { Promise < void > } async start ( ) { debug ( 'start a monitor logger rotator service' ) ; const loggerRotator = new LoggerRotator ( { logger : < ILogger > this . daemonLogger } ) ; await loggerRotator . startService ( ) ; this . metricsCollectPeriodConfig . configGlobalPeriod ( this . globalConfig [ 'reporterInterval' ] ) ; debug ( 'start a metrics server' ) ; this . server = new MetricsActuatorServer ( { config : this . globalConfig [ 'actuator' ] , logger : this . daemonLogger , metricsManager : new this . globalConfig [ 'metricsManager' ] ( ) } ) ; this . startMetrics ( ) ; this . startMetricsReporter ( ) ; this . daemonLogger . info ( 'monitor started' ) ; } protected startMetrics ( ) { let metricsManager = this . server . getMetricsManager ( ) ; metricsManager . register ( 'system' , MetricName . build ( 'system' ) , new CpuUsageGaugeSet ( this . metricsCollectPeriodConfig . getCachedTimeForLevel ( MetricLevel . NORMAL ) ) ) ; metricsManager . register ( 'system' , MetricName . build ( 'system' ) , new NetTrafficGaugeSet ( this . metricsCollectPeriodConfig . getCachedTimeForLevel ( MetricLevel . NORMAL ) ) ) ; metricsManager . register ( 'system' , MetricName . build ( 'system' ) , new NetworkTrafficGaugeSet ( this . metricsCollectPeriodConfig . getCachedTimeForLevel ( MetricLevel . NORMAL ) ) ) ; metricsManager . register ( 'system' , MetricName . build ( 'system' ) , new SystemMemoryGaugeSet ( this . metricsCollectPeriodConfig . getCachedTimeForLevel ( MetricLevel . NORMAL ) ) ) ; metricsManager . register ( 'system' , MetricName . build ( 'system' ) , new SystemLoadGaugeSet ( this . metricsCollectPeriodConfig . getCachedTimeForLevel ( MetricLevel . NORMAL ) ) ) ; metricsManager . register ( 'system' , MetricName . build ( 'system' ) , new DiskStatGaugeSet ( this . metricsCollectPeriodConfig . getCachedTimeForLevel ( MetricLevel . NORMAL ) ) ) ; } protected startMetricsReporter ( ) { debug ( 'start a metrics reporter' ) ; for ( let reporterName in this . globalConfig [ 'reporter' ] ) { const reporterObj = this . globalConfig [ 'reporter' ] [ reporterName ] ; if ( reporterObj [ 'enabled' ] ) { let reporterIns = new reporterObj [ 'target' ] ( this . server , reporterObj . initConfig || { } ) ; reporterIns . start ( this . globalConfig [ 'reporterInterval' ] ) ; this . daemonLogger . info ( `${ reporterName } reporter started` ) ; } } } return { Promise < void > } async stop ( ) { await this . server . destroy ( ) ; } }