• 使用を開始する
  • 関連ドキュメント

アラートのカスタマイズとフィルタリングのための正規表現

Opsgenie provides opportunity of using Java-like regular expressions to strengthen filtering and extracting information to define alert fields that both can be used in integrations, alert policies and callbacks. You can extract the desired information in a more powerful way by using regular expressions on many cases.

正規表現は基本的に 2 つの目的で使用できます。一致演算子を使用したフィルターのルールの定義と、extract文字列処理メソッドを使用した、アラートフィールドに設定するデータの抽出です。

一致ルールでフィルターするための正規表現

The conditions with Matches operator are fulfilled if whole string value matches the pattern of given regular expression. If type of the input value is List, then the condition will be matched if at least one of list items matches the given regular expression. This operator provides ability to define complex filtering in a single condition rule and reduces your dependency to already-defined condition operators. You can refer Action Filters for further information about condition operators.

一致条件演算子の利用に適した一般的なユースケースの例を以下に示します。

  • AND/OR による複数ルールの結合
    件名に「日々のレポート」が、メッセージに「重要」、「エラー」、または「ダウン」のいずれかが含まれている場合にメール統合からアラートを作成するには、一致ルール内で次の正規表現を使用できます。

 

.*(Critical|Error|Down).*

2 つのルールを組み合わせてアラートを作成する方法を示す画像。

類似ルールの重複防止
アラートの説明が server1、server2、...、server100 のいずれかのサーバー名で始まる場合にアラートを再通知するには、20 個の次で始まるルールではなく、1 つの一致ルール内で次の正規表現を代用します。

^(server(100|[1-9]\d?)).*

ルールの重複を防ぐ方法を示す画像。

複雑なルールの定義
アラート メッセージに有効なメール アドレスが含まれている場合に Webhook コールバックを実行するには、次の正規表現を使用できます。

.*(\s+.*)?([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})(\s+.*)?

 

複雑なルールの作成方法を示す画像。

Extract 文字列処理メソッドを使用してアラート フィールドを設定するための正規表現

You can use extract string processing method to extract desired matching group according to given regular expression to set into an alert field. You can refer here for further information about string processing methods.

Working with extract method may require understanding of Capturing Groups to get desired results. It has two definitions:

  • field_name.extract(reg_exp)

    : 指定された正規表現の最初の括弧で囲まれたセクション (グループ) に一致する文字列の一部を指定します。文字列が指定された正規表現に一致しない場合は、空の文字列を返します。指定された正規表現が文字列と一致するが、括弧で囲まれたセクションを含まない場合は、文字列全体を返します。

  • field_name.extract(reg_exp, index)

    : 指定された正規表現の index 番目の括弧で囲まれたセクション (グループ) に一致する文字列を指定します。文字列が指定された正規表現に一致しない場合は、空の文字列を返します。指定された正規表現が文字列と一致するが、index の数以上の括弧で囲まれたセクションを含まない場合は、文字列全体を返します。

次に、両方のメソッド定義を使用する例をいくつか示します。

messageHost: First Second

  • message.extract(/Host: (\S+)/)

     = First

  • message.extract(/Host: (\S+) (\S+)/)

     = First

  • message.extract(/Host:(\S+)/)

     = 空の値

  • message.extract(/Host: (\S+)/, 0)

     = Host: First Second

  • message.extract(/Host: (\S+)/, 1)

     = First

  • message.extract(/Host: (\S+)/, 2)

     = Host: First

  • message.extract(/Host: (\S+) (\S+)/, 2)

     = Second

  • message.extract(/(\S)+/)

     = Host:

  • message.extract(/\S+/)

     = Host:

descriptionsome value server3

  • description.extract(/(server(100|[1-9]\d?))/)

    = server3

  • description.extract(/(server(100|[1-9]\d?))/, 1)

     = server3

  • description.extract(/(server(100|[1-9]\d?))/, 2)

     = 3

  • description.extract(/(server(100|[1-9]\d?))/, 3)

     = server3

  • description.extract(/server(100|[1-9]\d?)/, 0)

     = server3

  • description.extract(/server(100|[1-9]\d?)/, 1)

     = 3

  • description.extract(/server(100|[1-9]\d?)/, 2)

     = server3

  • description.extract(/(server(100|[1-9]\d?))/, 2)

     = 3

extract メソッドを使用して目的のアラートフィールドを設定できるようにするには、アラートフィールドに書き込まれたテキストが、次のいずれかのパターンである必要があります。

{{ field_name.extract(/reg_exp/) }}

{{ field_name.extract(/reg_exp/, group_number) }}

アラートフィールド内の式の入力場所を示す画像。

さらにヘルプが必要ですか?

アトラシアン コミュニティをご利用ください。