diff --git a/x-pack/plugins/monitoring/public/alerts/badge.tsx b/x-pack/plugins/monitoring/public/alerts/badge.tsx index 4518d2c56cabb9..02963e9457ab5b 100644 --- a/x-pack/plugins/monitoring/public/alerts/badge.tsx +++ b/x-pack/plugins/monitoring/public/alerts/badge.tsx @@ -23,18 +23,25 @@ import { AlertPanel } from './panel'; import { Legacy } from '../legacy_shims'; import { isInSetupMode } from '../lib/setup_mode'; -function getDateFromState(states: CommonAlertState[]) { - const timestamp = states[0].state.ui.triggeredMS; +function getDateFromState(state: CommonAlertState) { + const timestamp = state.state.ui.triggeredMS; const tz = Legacy.shims.uiSettings.get('dateFormat:tz'); return formatDateTimeLocal(timestamp, false, tz === 'Browser' ? null : tz); } export const numberOfAlertsLabel = (count: number) => `${count} alert${count > 1 ? 's' : ''}`; +interface AlertInPanel { + alert: CommonAlertStatus; + alertState: CommonAlertState; +} + interface Props { alerts: { [alertTypeId: string]: CommonAlertStatus }; + stateFilter: (state: AlertState) => boolean; } export const AlertsBadge: React.FC = (props: Props) => { + const { stateFilter = () => true } = props; const [showPopover, setShowPopover] = React.useState(null); const inSetupMode = isInSetupMode(); const alerts = Object.values(props.alerts).filter(Boolean); @@ -93,15 +100,20 @@ export const AlertsBadge: React.FC = (props: Props) => { ); } else { const byType = { - [AlertSeverity.Danger]: [] as CommonAlertStatus[], - [AlertSeverity.Warning]: [] as CommonAlertStatus[], - [AlertSeverity.Success]: [] as CommonAlertStatus[], + [AlertSeverity.Danger]: [] as AlertInPanel[], + [AlertSeverity.Warning]: [] as AlertInPanel[], + [AlertSeverity.Success]: [] as AlertInPanel[], }; for (const alert of alerts) { for (const alertState of alert.states) { - const state = alertState.state as AlertState; - byType[state.ui.severity].push(alert); + if (alertState.firing && stateFilter(alertState.state)) { + const state = alertState.state as AlertState; + byType[state.ui.severity].push({ + alertState, + alert, + }); + } } } @@ -127,14 +139,14 @@ export const AlertsBadge: React.FC = (props: Props) => { { id: 0, title: `Alerts`, - items: list.map(({ alert, states }, index) => { + items: list.map(({ alert, alertState }, index) => { return { name: ( -

{getDateFromState(states)}

+

{getDateFromState(alertState)}

- {alert.label} + {alert.alert.label}
), panel: index + 1, @@ -144,9 +156,9 @@ export const AlertsBadge: React.FC = (props: Props) => { ...list.map((alertStatus, index) => { return { id: index + 1, - title: getDateFromState(alertStatus.states), + title: getDateFromState(alertStatus.alertState), width: 400, - content: , + content: , }; }), ]; diff --git a/x-pack/plugins/monitoring/public/alerts/callout.tsx b/x-pack/plugins/monitoring/public/alerts/callout.tsx index d000f470da3342..cad98dd1e6aec4 100644 --- a/x-pack/plugins/monitoring/public/alerts/callout.tsx +++ b/x-pack/plugins/monitoring/public/alerts/callout.tsx @@ -10,7 +10,7 @@ import { EuiCallOut, EuiSpacer } from '@elastic/eui'; import { CommonAlertStatus } from '../../common/types'; import { AlertSeverity } from '../../common/enums'; import { replaceTokens } from './lib/replace_tokens'; -import { AlertMessage } from '../../server/alerts/types'; +import { AlertMessage, AlertState } from '../../server/alerts/types'; const TYPES = [ { @@ -31,16 +31,17 @@ const TYPES = [ interface Props { alerts: { [alertTypeId: string]: CommonAlertStatus }; + stateFilter: (state: AlertState) => boolean; } export const AlertsCallout: React.FC = (props: Props) => { - const { alerts } = props; + const { alerts, stateFilter = () => true } = props; const callouts = TYPES.map((type) => { const list = []; for (const alertTypeId of Object.keys(alerts)) { const alertInstance = alerts[alertTypeId]; - for (const { state } of alertInstance.states) { - if (state.ui.severity === type.severity) { + for (const { firing, state } of alertInstance.states) { + if (firing && stateFilter(state) && state.ui.severity === type.severity) { list.push(state); } } diff --git a/x-pack/plugins/monitoring/public/alerts/panel.tsx b/x-pack/plugins/monitoring/public/alerts/panel.tsx index 3c5a4ef55a96bb..91a426cc8798e2 100644 --- a/x-pack/plugins/monitoring/public/alerts/panel.tsx +++ b/x-pack/plugins/monitoring/public/alerts/panel.tsx @@ -18,7 +18,7 @@ import { EuiListGroupItem, } from '@elastic/eui'; -import { CommonAlertStatus } from '../../common/types'; +import { CommonAlertStatus, CommonAlertState } from '../../common/types'; import { AlertMessage } from '../../server/alerts/types'; import { Legacy } from '../legacy_shims'; import { replaceTokens } from './lib/replace_tokens'; @@ -30,10 +30,12 @@ import { BASE_ALERT_API_PATH } from '../../../alerts/common'; interface Props { alert: CommonAlertStatus; + alertState?: CommonAlertState; } export const AlertPanel: React.FC = (props: Props) => { const { - alert: { states, alert }, + alert: { alert }, + alertState, } = props; const [showFlyout, setShowFlyout] = React.useState(false); const [isEnabled, setIsEnabled] = React.useState(alert.rawAlert.enabled); @@ -190,20 +192,14 @@ export const AlertPanel: React.FC = (props: Props) => { ); - if (inSetupMode) { + if (inSetupMode || !alertState) { return
{configurationUi}
; } - const firingStates = states.filter((state) => state.firing); - if (!firingStates.length) { - return
{configurationUi}
; - } - - const firingState = firingStates[0]; const nextStepsUi = - firingState.state.ui.message.nextSteps && firingState.state.ui.message.nextSteps.length ? ( + alertState.state.ui.message.nextSteps && alertState.state.ui.message.nextSteps.length ? ( - {firingState.state.ui.message.nextSteps.map((step: AlertMessage, index: number) => ( + {alertState.state.ui.message.nextSteps.map((step: AlertMessage, index: number) => ( ))} @@ -213,7 +209,7 @@ export const AlertPanel: React.FC = (props: Props) => {
-
{replaceTokens(firingState.state.ui.message)}
+
{replaceTokens(alertState.state.ui.message)}
{nextStepsUi ? : null} {nextStepsUi} diff --git a/x-pack/plugins/monitoring/public/alerts/status.tsx b/x-pack/plugins/monitoring/public/alerts/status.tsx index 9c262884d7257c..0407ddfecf5e90 100644 --- a/x-pack/plugins/monitoring/public/alerts/status.tsx +++ b/x-pack/plugins/monitoring/public/alerts/status.tsx @@ -11,14 +11,17 @@ import { CommonAlertStatus } from '../../common/types'; import { AlertSeverity } from '../../common/enums'; import { AlertState } from '../../server/alerts/types'; import { AlertsBadge } from './badge'; +import { isInSetupMode } from '../lib/setup_mode'; interface Props { alerts: { [alertTypeId: string]: CommonAlertStatus }; showBadge: boolean; showOnlyCount: boolean; + stateFilter: (state: AlertState) => boolean; } export const AlertsStatus: React.FC = (props: Props) => { - const { alerts, showBadge = false, showOnlyCount = false } = props; + const { alerts, showBadge = false, showOnlyCount = false, stateFilter = () => true } = props; + const inSetupMode = isInSetupMode(); if (!alerts) { return null; @@ -26,21 +29,26 @@ export const AlertsStatus: React.FC = (props: Props) => { let atLeastOneDanger = false; const count = Object.values(alerts).reduce((cnt, alertStatus) => { - if (alertStatus.states.length) { + const firingStates = alertStatus.states.filter((state) => state.firing); + const firingAndFilterStates = firingStates.filter((state) => stateFilter(state.state)); + cnt += firingAndFilterStates.length; + if (firingStates.length) { if (!atLeastOneDanger) { for (const state of alertStatus.states) { - if ((state.state as AlertState).ui.severity === AlertSeverity.Danger) { + if ( + stateFilter(state.state) && + (state.state as AlertState).ui.severity === AlertSeverity.Danger + ) { atLeastOneDanger = true; break; } } } - cnt++; } return cnt; }, 0); - if (count === 0) { + if (count === 0 && (!inSetupMode || showOnlyCount)) { return ( = (props: Props) => { ); } - if (showBadge) { - return ; + if (showBadge || inSetupMode) { + return ; } const severity = atLeastOneDanger ? AlertSeverity.Danger : AlertSeverity.Warning; diff --git a/x-pack/plugins/monitoring/public/components/elasticsearch/node/node.js b/x-pack/plugins/monitoring/public/components/elasticsearch/node/node.js index 0ef98782ad407e..2693ced5f919bd 100644 --- a/x-pack/plugins/monitoring/public/components/elasticsearch/node/node.js +++ b/x-pack/plugins/monitoring/public/components/elasticsearch/node/node.js @@ -60,10 +60,14 @@ export const Node = ({ - + state.nodeId === nodeId} + /> - + state.nodeId === nodeId} /> {metricsToShow.map((metric, index) => ( diff --git a/x-pack/plugins/monitoring/public/components/elasticsearch/node_detail_status/index.js b/x-pack/plugins/monitoring/public/components/elasticsearch/node_detail_status/index.js index 85b4d0daddade1..77d0b294f66d06 100644 --- a/x-pack/plugins/monitoring/public/components/elasticsearch/node_detail_status/index.js +++ b/x-pack/plugins/monitoring/public/components/elasticsearch/node_detail_status/index.js @@ -11,7 +11,7 @@ import { formatMetric } from '../../../lib/format_number'; import { i18n } from '@kbn/i18n'; import { AlertsStatus } from '../../../alerts/status'; -export function NodeDetailStatus({ stats, alerts = {} }) { +export function NodeDetailStatus({ stats, alerts = {}, alertsStateFilter = () => true }) { const { transport_address: transportAddress, usedHeap, @@ -33,7 +33,7 @@ export function NodeDetailStatus({ stats, alerts = {} }) { label: i18n.translate('xpack.monitoring.elasticsearch.nodeDetailStatus.alerts', { defaultMessage: 'Alerts', }), - value: , + value: , }, { label: i18n.translate('xpack.monitoring.elasticsearch.nodeDetailStatus.transportAddress', { diff --git a/x-pack/plugins/monitoring/public/components/elasticsearch/nodes/nodes.js b/x-pack/plugins/monitoring/public/components/elasticsearch/nodes/nodes.js index df4fc0b7e77196..49d3245b9de35f 100644 --- a/x-pack/plugins/monitoring/public/components/elasticsearch/nodes/nodes.js +++ b/x-pack/plugins/monitoring/public/components/elasticsearch/nodes/nodes.js @@ -129,8 +129,14 @@ const getColumns = (showCgroupMetricsElasticsearch, setupMode, clusterUuid, aler field: 'alerts', width: '175px', sortable: true, - render: () => { - return ; + render: (_field, node) => { + return ( + state.nodeId === node.resolver} + /> + ); }, }); diff --git a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts index 1a66560ae124a7..2596252c92d11c 100644 --- a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts +++ b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts @@ -372,5 +372,197 @@ describe('CpuUsageAlert', () => { state: 'firing', }); }); + + it('should show proper counts for resolved and firing nodes', async () => { + (fetchCpuUsageNodeStats as jest.Mock).mockImplementation(() => { + return [ + { + ...stat, + cpuUsage: 1, + }, + { + ...stat, + nodeId: 'anotherNode', + nodeName: 'anotherNode', + cpuUsage: 99, + }, + ]; + }); + (getState as jest.Mock).mockImplementation(() => { + return { + alertStates: [ + { + cluster: { + clusterUuid, + clusterName, + }, + ccs: null, + cpuUsage: 91, + nodeId, + nodeName, + ui: { + isFiring: true, + message: null, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + { + cluster: { + clusterUuid, + clusterName, + }, + ccs: null, + cpuUsage: 100, + nodeId: 'anotherNode', + nodeName: 'anotherNode', + ui: { + isFiring: true, + message: null, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }; + }); + const alert = new CpuUsageAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + const count = 1; + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid, clusterName }, + ccs: null, + cpuUsage: 1, + nodeId, + nodeName, + ui: { + isFiring: false, + message: { + text: + 'The cpu usage on node myNodeName is now under the threshold, currently reporting at 1.00% as of #resolved', + tokens: [ + { + startToken: '#resolved', + type: 'time', + isAbsolute: true, + isRelative: false, + timestamp: 1, + }, + ], + }, + severity: 'danger', + resolvedMS: 1, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + { + ccs: null, + cluster: { clusterUuid, clusterName }, + cpuUsage: 99, + nodeId: 'anotherNode', + nodeName: 'anotherNode', + ui: { + isFiring: true, + message: { + text: + 'Node #start_linkanotherNode#end_link is reporting cpu usage of 99.00% at #absolute', + nextSteps: [ + { + text: '#start_linkCheck hot threads#end_link', + tokens: [ + { + startToken: '#start_link', + endToken: '#end_link', + type: 'docLink', + partialUrl: + '{elasticWebsiteUrl}/guide/en/elasticsearch/reference/{docLinkVersion}/cluster-nodes-hot-threads.html', + }, + ], + }, + { + text: '#start_linkCheck long running tasks#end_link', + tokens: [ + { + startToken: '#start_link', + endToken: '#end_link', + type: 'docLink', + partialUrl: + '{elasticWebsiteUrl}/guide/en/elasticsearch/reference/{docLinkVersion}/tasks.html', + }, + ], + }, + ], + tokens: [ + { + startToken: '#absolute', + type: 'time', + isAbsolute: true, + isRelative: false, + timestamp: 1, + }, + { + startToken: '#start_link', + endToken: '#end_link', + type: 'link', + url: 'elasticsearch/nodes/anotherNode', + }, + ], + }, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledTimes(1); + // expect(scheduleActions.mock.calls[0]).toEqual([ + // 'default', + // { + // internalFullMessage: `CPU usage alert is resolved for ${count} node(s) in cluster: ${clusterName}.`, + // internalShortMessage: `CPU usage alert is resolved for ${count} node(s) in cluster: ${clusterName}.`, + // clusterName, + // count, + // nodes: `${nodeName}:1.00`, + // state: 'resolved', + // }, + // ]); + expect(scheduleActions.mock.calls[0]).toEqual([ + 'default', + { + action: + '[View nodes](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:abc123))', + actionPlain: 'Verify CPU levels across affected nodes.', + internalFullMessage: + 'CPU usage alert is firing for 1 node(s) in cluster: testCluster. [View nodes](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:abc123))', + internalShortMessage: + 'CPU usage alert is firing for 1 node(s) in cluster: testCluster. Verify CPU levels across affected nodes.', + nodes: 'anotherNode:99.00', + clusterName, + count, + state: 'firing', + }, + ]); + }); }); }); diff --git a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts index b543a4c976377f..4742f55487045d 100644 --- a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts +++ b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts @@ -291,13 +291,6 @@ export class CpuUsageAlert extends BaseAlert { return; } - const nodes = instanceState.alertStates - .map((_state) => { - const state = _state as AlertCpuUsageState; - return `${state.nodeName}:${state.cpuUsage.toFixed(2)}`; - }) - .join(','); - const ccs = instanceState.alertStates.reduce((accum: string, state): string => { if (state.ccs) { return state.ccs; @@ -305,35 +298,16 @@ export class CpuUsageAlert extends BaseAlert { return accum; }, ''); - const count = instanceState.alertStates.length; - if (!instanceState.alertStates[0].ui.isFiring) { - instance.scheduleActions('default', { - internalShortMessage: i18n.translate( - 'xpack.monitoring.alerts.cpuUsage.resolved.internalShortMessage', - { - defaultMessage: `CPU usage alert is resolved for {count} node(s) in cluster: {clusterName}.`, - values: { - count, - clusterName: cluster.clusterName, - }, - } - ), - internalFullMessage: i18n.translate( - 'xpack.monitoring.alerts.cpuUsage.resolved.internalFullMessage', - { - defaultMessage: `CPU usage alert is resolved for {count} node(s) in cluster: {clusterName}.`, - values: { - count, - clusterName: cluster.clusterName, - }, - } - ), - state: RESOLVED, - nodes, - count, - clusterName: cluster.clusterName, - }); - } else { + const firingCount = instanceState.alertStates.filter((alertState) => alertState.ui.isFiring) + .length; + const firingNodes = instanceState.alertStates + .filter((_state) => (_state as AlertCpuUsageState).ui.isFiring) + .map((_state) => { + const state = _state as AlertCpuUsageState; + return `${state.nodeName}:${state.cpuUsage.toFixed(2)}`; + }) + .join(','); + if (firingCount > 0) { const shortActionText = i18n.translate('xpack.monitoring.alerts.cpuUsage.shortAction', { defaultMessage: 'Verify CPU levels across affected nodes.', }); @@ -354,7 +328,7 @@ export class CpuUsageAlert extends BaseAlert { { defaultMessage: `CPU usage alert is firing for {count} node(s) in cluster: {clusterName}. {shortActionText}`, values: { - count, + count: firingCount, clusterName: cluster.clusterName, shortActionText, }, @@ -365,19 +339,58 @@ export class CpuUsageAlert extends BaseAlert { { defaultMessage: `CPU usage alert is firing for {count} node(s) in cluster: {clusterName}. {action}`, values: { - count, + count: firingCount, clusterName: cluster.clusterName, action, }, } ), state: FIRING, - nodes, - count, + nodes: firingNodes, + count: firingCount, clusterName: cluster.clusterName, action, actionPlain: shortActionText, }); + } else { + const resolvedCount = instanceState.alertStates.filter( + (alertState) => !alertState.ui.isFiring + ).length; + const resolvedNodes = instanceState.alertStates + .filter((_state) => !(_state as AlertCpuUsageState).ui.isFiring) + .map((_state) => { + const state = _state as AlertCpuUsageState; + return `${state.nodeName}:${state.cpuUsage.toFixed(2)}`; + }) + .join(','); + if (resolvedCount > 0) { + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.cpuUsage.resolved.internalShortMessage', + { + defaultMessage: `CPU usage alert is resolved for {count} node(s) in cluster: {clusterName}.`, + values: { + count: resolvedCount, + clusterName: cluster.clusterName, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.cpuUsage.resolved.internalFullMessage', + { + defaultMessage: `CPU usage alert is resolved for {count} node(s) in cluster: {clusterName}.`, + values: { + count: resolvedCount, + clusterName: cluster.clusterName, + }, + } + ), + state: RESOLVED, + nodes: resolvedNodes, + count: resolvedCount, + clusterName: cluster.clusterName, + }); + } } }