简体   繁体   English

如何在 Elasticsearch 建立观察者生成 OTRS 票据?

[英]How to build a watcher in Elasticsearch for generating OTRS ticket?

I want to configure a elasticsearch webhook watcher, which will look for the keyword "error" in my indices and genarate an OTRS ticket, if found.我想配置一个 elasticsearch webhook 观察器,它将在我的索引中查找关键字“error”并生成一个 OTRS 票证(如果找到)。

Right now I have following configuration:现在我有以下配置:

{
  "trigger": {
    "schedule": {"interval": "1m"}
  },
  "input": {
    "search": {
      "request": {
        "body": {
          "size": 0,
          "query": {"match_all": "Error"}
        },
        "indices": ["*"]
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gte": 1
      }
    }
  },
  "actions" : {
  "create_otrs" : {
    "transform": {
      "script": """{"Ticket":{"Queue":"EngineeringTeam","Priority":"P3","CustomerUser":"root@localhost","Title":"RESTCreateTest","State":"new","Type":"Incident"},"Article":{"ContentType":"text/plain;charset=utf8","Subject":"RestCreateTest","Body":"Thisisonlyatest"}}"""
    },
    "webhook" : {
      "method" : "POST",
      "host" : "http://myotrs.com/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket?UserLogin=<user>&Password=<pass>",
      "port": 9200,
      "body": "{{#toJson}}ctx.payload{{/toJson}}",
      "auth" : {
        "basic" : {
          "username" : "elastic", 
          "password" : "<elasticsearch pass>"
        }
      }
    }
  }
}
}

This gives me Error saving watch: compile error and watcher will not simulate.这给了我Error saving watch: compile error and watcher will not simulate。 There is no syntax error in the json by the way.顺便说一句,json 没有语法错误。 What is wrong in the configuration?配置有什么问题? A curl operation successfully generates the OTRS ticket but I am getting a hard time configuring it with elasticsearch. curl 操作成功生成了 OTRS 票证,但我很难用 elasticsearch 配置它。

Tldr; Tldr;

Your transform script is wrong.您的转换脚本是错误的。 As per the documentation:根据文档:

The executed script may either return a valid model that is the equivalent of a Java™ Map or a JSON object (you will need to consult the documentation of the specific scripting language to find out what this construct is).执行的脚本可能会返回有效的 model,它等效于 Java™ Map 或 JSON object(您需要查阅特定脚本语言的文档以了解此构造是什么)。

Solution解决方案

You can do something as simple as, converting your json into a string你可以做一些简单的事情,将你的 json 转换成一个字符串

{
  "Ticket": {
    "Queue": "EngineeringTeam",
    "Priority": "P3",
    "CustomerUser": "root@localhost",
    "Title": "RESTCreateTest",
    "State": "new",
    "Type": "Incident"
  },
  "Article": {
    "ContentType": "text/plain;charset=utf8",
    "Subject": "RestCreateTest",
    "Body": "Thisisonlyatest"
  }
}

Becomes:变成:

"{\"Ticket\":{\"Queue\":\"EngineeringTeam\",\"Priority\":\"P3\",\"CustomerUser\":\"root@localhost\",\"Title\":\"RESTCreateTest\",\"State\":\"new\",\"Type\":\"Incident\"},\"Article\":{\"ContentType\":\"text/plain;charset=utf8\",\"Subject\":\"RestCreateTest\",\"Body\":\"Thisisonlyatest\"}}"

And use the Json.load function to convert the string into a proper object.并使用Json.load function 将字符串转换为正确的 object。

Your watch will look like:您的手表将如下所示:

{
  "watch" : {
    "trigger": {
      "schedule": {"interval": "1m"}
    },
    "input": {
      "search": {
        "request": {
          "body": {
            "size": 0,
            "query": {"match_all": "Error"}
          },
          "indices": ["*"]
        }
      }
    },
    "condition": {
      "compare": {
        "ctx.payload.hits.total": {
          "gte": 1
        }
      }
    },
    "actions" : {
      "create_otrs" : {
        "transform": {
          "script": """return Json.load("{\"Ticket\":{\"Queue\":\"EngineeringTeam\",\"Priority\":\"P3\",\"CustomerUser\":\"root@localhost\",\"Title\":\"RESTCreateTest\",\"State\":\"new\",\"Type\":\"Incident\"},\"Article\":{\"ContentType\":\"text/plain;charset=utf8\",\"Subject\":\"RestCreateTest\",\"Body\":\"Thisisonlyatest\"}}");"""
        },
        "webhook" : {
          "method" : "POST",
          "host" : "http://myotrs.com/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket?UserLogin=<user>&Password=<pass>",
          "port": 9200,
          "body": "{{#toJson}}ctx.payload{{/toJson}}",
          "auth" : {
            "basic" : {
              "username" : "elastic", 
              "password" : "<elasticsearch pass>"
            }
          }
        }
      }
    }
  }
}

Then another error you have in your watch is the query那么您手表中的另一个错误是查询

{
  "search": {
    "request": {
      "body": {
        "size": 0,
        "query": {"match_all": "Error"}
      },
      "indices": ["*"]
    }
  }
}

match_all should take an object such as {} so "Error" is not going to work. match_all应采用 object,例如{} ,因此"Error"将不起作用。

So in the end the watcher looks like:所以最后观察者看起来像:

{
  "watch" : {
    "trigger": {
      "schedule": {"interval": "1m"}
    },
    "input": {
      "search": {
        "request": {
          "body": {
            "size": 0,
            "query": {"match_all": {}}
          },
          "indices": ["*"]
        }
      }
    },
    "condition": {
      "compare": {
        "ctx.payload.hits.total": {
          "gte": 1
        }
      }
    },
    "actions" : {
      "create_otrs" : {
        "transform": {
          "script": """return Json.load("{\"Ticket\":{\"Queue\":\"EngineeringTeam\",\"Priority\":\"P3\",\"CustomerUser\":\"root@localhost\",\"Title\":\"RESTCreateTest\",\"State\":\"new\",\"Type\":\"Incident\"},\"Article\":{\"ContentType\":\"text/plain;charset=utf8\",\"Subject\":\"RestCreateTest\",\"Body\":\"Thisisonlyatest\"}}");"""
        },
        "webhook" : {
          "method" : "POST",
          "host" : "http://myotrs.com/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket?UserLogin=<user>&Password=<pass>",
          "port": 9200,
          "body": "{{#toJson}}ctx.payload{{/toJson}}",
          "auth" : {
            "basic" : {
              "username" : "elastic", 
              "password" : "<elasticsearch pass>"
            }
          }
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM