UNPKG

cloud-report

Version:

Collects and analyzes cloud resources

79 lines (78 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Moment = require("moment"); const types_1 = require("../../../types"); const utils_1 = require("../../../utils"); const base_1 = require("../../base"); class VolumeSnapshotsRegularityAnalyzer extends base_1.BaseAnalyzer { analyze(params, fullReport) { const allVolumes = params.volumes; const allSnapshots = params.snapshots; if (!allVolumes || !allSnapshots) { return undefined; } const currentMoment = Moment(); const volume_snapshots_regularity = { type: types_1.CheckAnalysisType.Reliability }; volume_snapshots_regularity.what = "Are Snapshots being taken for EBS volumes?"; volume_snapshots_regularity.why = `If we take regular snapshots of EBS volumes then it prevents data loss incase of volume failure or accidental deletes`; volume_snapshots_regularity.recommendation = "Recommended to take regular snapshots for all in-use volumes"; volume_snapshots_regularity.benchmark = ['all']; const allRegionsAnalysis = {}; for (const region in allVolumes) { const regionVolumes = allVolumes[region]; allRegionsAnalysis[region] = []; for (const volume of regionVolumes) { if (volume.State !== "in-use") { continue; } const volumeAnalysis = {}; volumeAnalysis.resource = { volume, snapshots: allSnapshots[region][volume.VolumeId] }; volumeAnalysis.resourceSummary = { name: "Volume", value: `${utils_1.ResourceUtil.getNameByTags(volume)} | ${volume.VolumeId}`, }; const latestSnapshot = this.getLatestSnapshot(allSnapshots[region][volume.VolumeId]); if (latestSnapshot) { const lastSnapshotDate = Moment(latestSnapshot.StartTime); const lastSnapshotInDays = Math.floor(Moment.duration(Moment(). diff(Moment(lastSnapshotDate))).asDays()); if (lastSnapshotInDays <= 7) { volumeAnalysis.severity = types_1.SeverityStatus.Good; volumeAnalysis.message = `Last snapshot was taken ${lastSnapshotInDays} days ago`; } else { volumeAnalysis.severity = types_1.SeverityStatus.Warning; volumeAnalysis.message = `Last snapshot was taken ${lastSnapshotInDays} days ago`; volumeAnalysis.action = "Take daily snapshot for in-use volumes"; } } else { volumeAnalysis.severity = types_1.SeverityStatus.Failure; volumeAnalysis.message = "No snapshots from last 30 days"; volumeAnalysis.action = "Take daily snapshot for in-use volumes"; } allRegionsAnalysis[region].push(volumeAnalysis); } } volume_snapshots_regularity.regions = allRegionsAnalysis; return { volume_snapshots_regularity }; } getLatestSnapshot(snapshots) { if (!snapshots) { return undefined; } return snapshots.sort((snapshot1, snapshot2) => { if (snapshot1.StartTime < snapshot2.StartTime) { return 1; } else if (snapshot1.StartTime > snapshot2.StartTime) { return -1; } else { return 0; } })[0]; } } exports.VolumeSnapshotsRegularityAnalyzer = VolumeSnapshotsRegularityAnalyzer;