import os from 'os' ; import getosAsync , { LinuxOs } from 'getos' ; import { promisify } from 'util' ; import { Logger } from '@kbn/logging' ; import { OpsOsMetrics , MetricsCollector } from './types' ; import { OsCgroupMetricsCollector } from './cgroup' ; const getos = promisify ( getosAsync ) ; export interface OpsMetricsCollectorOptions { logger : Logger ; cpuPath ? : string ; cpuAcctPath ? : string ; } export class OsMetricsCollector implements MetricsCollector < OpsOsMetrics > { private readonly cgroupCollector : OsCgroupMetricsCollector ; constructor ( options ) { this . cgroupCollector = new OsCgroupMetricsCollector ( { ... options , logger : options . logger . get ( 'cgroup' ) , } ) ; } public async collect ( ) { const platform = os . platform ( ) ; const load = os . loadavg ( ) ; const metrics = { platform , platformRelease : `${ platform }-${ os . release ( ) }` , load : { '1m' : load [ 0 ] , '5m' : load [ 1 ] , '15m' : load [ 2 ] , } , memory : { total_in_bytes : os . totalmem ( ) , free_in_bytes : os . freemem ( ) , used_in_bytes : os . totalmem ( ) - os . freemem ( ) , } , uptime_in_millis : os . uptime ( ) * 1000 , ... ( await this . getDistroStats ( platform ) ) , ... ( await this . cgroupCollector . collect ( ) ) , } ; return metrics ; } public reset ( ) { } private async getDistroStats ( platform ) { if ( platform === 'linux' ) { try { const distro = ( await getos ( ) ) as LinuxOs ; return { distro : distro . dist , distroRelease : `${ distro . dist }-${ distro . release }` , } ; } catch ( e ) { } } return { } ; } }