2
votes

I have a python script that writes JSON objects (line by line) to /var/log/myLog.json with following format:

{"timestamp":"2016-07-21T01:20:04.392799-0400","in_iface":"docker0","event_type":"alert","src_ip":"172.17.0.2","dest_ip":"172.17.0.3","proto":"ICMP","icmp_type":0,"icmp_code":0,"alert":{"action":"allowed","**gid**":2,"signature_id":2,"rev":0,"signature":"ICMP msg","category":"","severity":3},"payload":"hFuQVwAA","payload_printable":"kk"}

I'd like to use Logstash in order to:

  1. Read the json objects, line by line, from /var/log/myLog.json

  2. Parse the gid and forward to another machine as a udp msg (given a specific IP address+port) -- For example: if gid==2 then forward this json object to 172.123.10.3:10001

Additionally, I'd like to be able to update that Logstash configuration file filter dynamically (Aka, to be able to add another rule like: "if gid==x then forware this json object to another IP).

How can I do so?

How should the Logstash conf file(s) should look like? And how the command to insert/delete dynamic filters look like?

Thanks, guys.

1

1 Answers

0
votes

You can running the logstash as belows configuration. and I have tested two sample json data.

{"timestamp":"2016-07-21T01:20:04.392799-0400","in_iface":"docker0","event_type":"alert","src_ip":"172.17.0.2","dest_ip":"172.17.0.3","proto":"ICMP","icmp_type":0,"icmp_code":0,"alert":{"action":"allowed","gid":2,"signature_id":2,"rev":0,"signature":"ICMP msg","category":"","severity":3},"payload":"hFuQVwAA","payload_printable":"kk"}
{"timestamp":"2016-07-21T01:20:04.392799-0400","in_iface":"docker0","event_type":"alert","src_ip":"172.17.0.2","dest_ip":"172.17.0.3","proto":"ICMP","icmp_type":0,"icmp_code":0,"alert":{"action":"allowed","gid":3,"signature_id":2,"rev":0,"signature":"ICMP msg","category":"","severity":3},"payload":"hFuQVwAA","payload_printable":"kk"}



input {
  file {
        path => "/etc/logstash/jsonSample.log"
       start_position => "beginning"
       sincedb_path => "/dev/null"
   }
}

filter {
                json {
                        source => "message"
                        target => "doc"
                        add_field => {"alert.gid" => "%{[doc][alert][gid]}"}
                        add_tag => ["tagName_%{[doc][alert][gid]}"]
                }


}


output {
if "tagName_2" in [tags] {
 stdout {codec => rubydebug}
}else if "tagName_3" in [tags] {
}

}

Then you can see the result

{
       "message" => "{\"timestamp\":\"2016-07-21T01:20:04.392799-0400\",\"in_iface\":\"docker0\",\"event_type\":\"alert\",\"src_ip\":\"172.17.0.2\",\"dest_ip\":\"172.17.0.3\",\"proto\":\"ICMP\",\"icmp_type\":0,\"icmp_code\":0,\"alert\":{\"action\":\"allowed\",\"gid\":2,\"signature_id\":2,\"rev\":0,\"signature\":\"ICMP msg\",\"category\":\"\",\"severity\":3},\"payload\":\"hFuQVwAA\",\"payload_printable\":\"kk\"}",
      "@version" => "1",
    "@timestamp" => "2016-07-25T04:41:11.980Z",
          "path" => "/etc/logstash/jsonSample.log",
          "host" => "baklava",
           "doc" => {
                "timestamp" => "2016-07-21T01:20:04.392799-0400",
                 "in_iface" => "docker0",
               "event_type" => "alert",
                   "src_ip" => "172.17.0.2",
                  "dest_ip" => "172.17.0.3",
                    "proto" => "ICMP",
                "icmp_type" => 0,
                "icmp_code" => 0,
                    "alert" => {
                  "action" => "allowed",
                     "gid" => 2,
            "signature_id" => 2,
                     "rev" => 0,
               "signature" => "ICMP msg",
                "category" => "",
                "severity" => 3
        },
                  "payload" => "hFuQVwAA",
        "payload_printable" => "kk"
    },
     "alert.gid" => 2,
          "tags" => [
        [0] "tagName_2"
    ]
}

You can also change the configuration applied as above it.

Regards.

You can refer the configuration of event and json filter https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html

https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html