Skip to content

Instantly share code, notes, and snippets.

@chnn
Created June 27, 2019 21:45
Show Gist options
  • Select an option

  • Save chnn/c659774fddad87472d29da4802df1faf to your computer and use it in GitHub Desktop.

Select an option

Save chnn/c659774fddad87472d29da4802df1faf to your computer and use it in GitHub Desktop.
type AlertLevel =
  | "crit"
  | "warn"
  | "info"
  | "ok"
  | "unknown"

type AlertGenerator = {
  id: string
  ownerID: string
  orgID: string
  taskID: string
  name: string
  enabled: boolean
  checkEvery: Duration

  // User-defined Flux script that returns the data that the alert generator
  // will generate alerts from; expects a sequence of tables, each with a
  // `_time` and `_value` column
  fluxScript: string

  config: AlertGeneratorConfig 
}

type AlertGeneratorConfig =
 | ThresholdConfig
 | DeadmanConfig

type ThresholdConfig = {
  type: "threshold"
  thresholds: Threshold[]
}

type Threshold = 
  | GreaterThreshold
  | LessThreshold
  | RangeThreshold

type GreaterThreshold = {
  type: "greater",
  level: AlertLevel,
  value: number

  // If true, generate alert when all values from user query meet threshold
  // criteria; otherwise, generate alert when at least one value from the user
  // query meet the threshold criteria
  allValues: boolean,
}

type LessThreshold = {
  type: "less",
  level: AlertLevel,
  value: number
  allValues: boolean,
}

type RangeThreshold = {
  type: "range",
  level: AlertLevel,
  minValue: number
  maxValue: number
  allValues: boolean,
  within: boolean
}

type DeadmanConfig = {
  type: "deadman"
  level: AlertLevel
  timeSince: Duration

  // If true, will generate an alert if only zero values have been reported
  // since `timeSince` ago; otherwise, generate alert if no values have been
  // reported since `timeSince` ago
  reportZero: boolean
}

type NotificationGenerator = {
  id: string
  ownerID: string
  orgID: string
  alertGeneratorID: string // belongs to `AlertGenerator` 
  notificationEndpointIDs: string[] // has many `NotificationEndpoint`s
  name: string
  sleepUntil: Time
  checkEvery: Duration
  limitEvery: Duration
  limit: number
  batchNotify: boolean
  level: AlertLevel
}

type NotificationEndpoint = {
  id: string
  orgID: string
  ownerID: string
  name: string
  enabled: boolean
  config: NotificationEndpointConfig
}

type NotificationEndpointConfig =
  | {type: "slack", /* ... */}
  | {type: "smtp"}
  | {type: "pagerduty"}
  | {type: "webhook"}

Line protocol example for notifications:

notifications,generator_id=1,alert_generator_id=2,org_id=2,level=warn,endpoint_id=23 sent=true,error="optional if sent is false"

Line protocol example for alerts:

alerts,generator_id=1,org_id=2,level=warn,type=threshold message="foo bar asdf",value=23.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment