Skip to content

Apache Log Analysis

Full Recipe

Shared by: Josh Cody

A simple recipe that parses incoming text for each line of an Apache web server access log and structures it into a graph. A useful introduction to standing queries, the powerful feature that makes Quine unique.

Apache Log Analysis Recipe
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
version: 2
title: Apache Log Analytics
contributor: https://github.com/joshcody
summary: ''
description: ''

ingestStreams:
  - name: apache-log-ingest
    source:
      type: File
      path: $in_file
      format:
        type: Line
    query: |-
      WITH text.regexFirstMatch($that, '(\S+)\s+\S+\s+(\S+)\s+\[(.+)\]\s+"(.*)\s+(.*)\s+(.*)"\s+([0-9]+)\s+(\S+)\s+"(.*)"\s+"(.*)"')
      AS r
      CREATE ({
        sourceIp: r[1],
        user: r[2],
        time: datetime(r[3], 'dd/MMM/yyyy:HH:mm:ss Z'),
        verb: r[4],
        path: r[5],
        httpVersion: r[6],
        status: r[7],
        size: r[8],
        referrer: r[9],
        agent: r[10],
        type: 'log'
      })

standingQueries:
  - name: log-to-verb
    pattern:
      type: Cypher
      query: MATCH (l) WHERE l.type = 'log' RETURN DISTINCT id(l) AS id
      mode: DISTINCT_ID
    outputs:
      - name: verb
        preEnrichmentTransformation:
          type: InlineData
        resultEnrichment:
          query: |-
            MATCH (l) WHERE id(l) = $that.id
            MATCH (v) WHERE id(v) = idFrom('verb', l.verb)
            SET v.type = 'verb',
              v.verb = l.verb
            CREATE (l)-[:verb]->(v)
            RETURN null
          parameter: that
        destinations:
          - type: Drop

nodeAppearances: []
quickQueries: []
sampleQueries:
  - name: Count HTTP GET Requests
    query: >-
      MATCH (l)-[rel:verb]->(v)
      WHERE l.type = 'log' AND v.type = 'verb' AND v.verb = 'GET'
      RETURN count(rel) AS get_count
statusQuery:
  cypherQuery: >-
    MATCH (l)-[rel:verb]->(v)
    WHERE l.type = 'log' AND v.type = 'verb' AND v.verb = 'GET'
    RETURN count(rel) AS get_count

Download Recipe

Shared by: Josh Cody

A simple recipe that parses incoming text for each line of an Apache web server access log and structures it into a graph. A useful introduction to standing queries, the powerful feature that makes Quine unique.

No download required

This version streams the sample data directly from data.thatdot.com, so you can skip the Sample Data step below.

See Recipe Data Service if you want to adjust how this recipe loads: take a slice of the data, or slow the stream down to watch the graph build.

Apache Log Analysis Recipe
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
version: 2
title: Apache Log Analytics
contributor: https://github.com/joshcody
summary: ''
description: ''

ingestStreams:
  - name: apache-log-ingest
    source:
      type: ServerSentEvent
      url: https://data.thatdot.com/recipe/apache.log
      format:
        type: Raw
    query: |-
      WITH text.regexFirstMatch(text.utf8Decode($that), '(\S+)\s+\S+\s+(\S+)\s+\[(.+)\]\s+"(.*)\s+(.*)\s+(.*)"\s+([0-9]+)\s+(\S+)\s+"(.*)"\s+"(.*)"')
      AS r
      CREATE ({
        sourceIp: r[1],
        user: r[2],
        time: datetime(r[3], 'dd/MMM/yyyy:HH:mm:ss Z'),
        verb: r[4],
        path: r[5],
        httpVersion: r[6],
        status: r[7],
        size: r[8],
        referrer: r[9],
        agent: r[10],
        type: 'log'
      })

standingQueries:
  - name: log-to-verb
    pattern:
      type: Cypher
      query: MATCH (l) WHERE l.type = 'log' RETURN DISTINCT id(l) AS id
      mode: DISTINCT_ID
    outputs:
      - name: verb
        preEnrichmentTransformation:
          type: InlineData
        resultEnrichment:
          query: |-
            MATCH (l) WHERE id(l) = $that.id
            MATCH (v) WHERE id(v) = idFrom('verb', l.verb)
            SET v.type = 'verb',
              v.verb = l.verb
            CREATE (l)-[:verb]->(v)
            RETURN null
          parameter: that
        destinations:
          - type: Drop

nodeAppearances: []
quickQueries: []
sampleQueries:
  - name: Count HTTP GET Requests
    query: >-
      MATCH (l)-[rel:verb]->(v)
      WHERE l.type = 'log' AND v.type = 'verb' AND v.verb = 'GET'
      RETURN count(rel) AS get_count
statusQuery:
  cypherQuery: >-
    MATCH (l)-[rel:verb]->(v)
    WHERE l.type = 'log' AND v.type = 'verb' AND v.verb = 'GET'
    RETURN count(rel) AS get_count

Download Recipe

Scenario

This recipe loads a sample Apache log file and manifests disconnected nodes in a graph for basic analysis and metric reporting.

Sample Data

Note

Download the sample data to the same directory where you will run Quine.

A sample Apache web server access logs dataset downloaded from https://recipes.quine.io/sample_apache_logs or using the command below.

curl -L https://recipes.quine.io/sample_apache_logs -o apache.log

How it Works

The recipe reads log entries from the sample data files using an ingest stream to manifest a graph in Quine. A regular expression inside the ingest stream Cypher query parses the logline and populates parameters in the node.

The ingest stream processes the apache.log file:

ingestStreams:
  - name: apache-log-ingest
    source:
      type: File
      path: $in_file
      format:
        type: Line
    query: |-
      WITH text.regexFirstMatch($that, '(\S+)\s+\S+\s+(\S+)\s+\[(.+)\]\s+"(.*)\s+(.*)\s+(.*)"\s+([0-9]+)\s+(\S+)\s+"(.*)"\s+"(.*)"')
      AS r
      CREATE ({
        sourceIp: r[1],
        user: r[2],
        time: datetime(r[3], 'dd/MMM/yyyy:HH:mm:ss Z'),
        verb: r[4],
        path: r[5],
        httpVersion: r[6],
        status: r[7],
        size: r[8],
        referrer: r[9],
        agent: r[10],
        type: 'log'
      })
POST /api/v2/graph/quine/ingests
{
  "name": "apache-log-ingest",
  "source": {
    "type": "File",
    "path": "$in_file",
    "format": {
      "type": "Line"
    }
  },
  "query": "WITH text.regexFirstMatch($that, '(\\S+)\\s+\\S+\\s+(\\S+)\\s+\\[(.+)\\]\\s+\"(.*)\\s+(.*)\\s+(.*)\"\\s+([0-9]+)\\s+(\\S+)\\s+\"(.*)\"\\s+\"(.*)\"') AS r CREATE ({ sourceIp: r[1], user: r[2], time: datetime(r[3], 'dd/MMM/yyyy:HH:mm:ss Z'), verb: r[4], path: r[5], httpVersion: r[6], status: r[7], size: r[8], referrer: r[9], agent: r[10], type: 'log' })"
}

A standing query is configured to detect nodes that have a type of log and then create relationships between the nodes and their verbs.

standingQueries:
  - name: log-to-verb
    pattern:
      type: Cypher
      query: MATCH (l) WHERE l.type = 'log' RETURN DISTINCT id(l) AS id
      mode: DISTINCT_ID
    outputs:
      - name: verb
        preEnrichmentTransformation:
          type: InlineData
        resultEnrichment:
          query: |-
            MATCH (l) WHERE id(l) = $that.id
            MATCH (v) WHERE id(v) = idFrom('verb', l.verb)
            SET v.type = 'verb',
              v.verb = l.verb
            CREATE (l)-[:verb]->(v)
            RETURN null
          parameter: that
        destinations:
          - type: Drop
POST /api/v2/graph/quine/standingQueries
{
  "name": "log-to-verb",
  "pattern": {
    "type": "Cypher",
    "query": "MATCH (l) WHERE l.type = 'log' RETURN DISTINCT id(l) AS id",
    "mode": "DISTINCT_ID"
  },
  "outputs": [
    {
      "name": "verb",
      "preEnrichmentTransformation": {
        "type": "InlineData"
      },
      "resultEnrichment": {
        "query": "MATCH (l) WHERE id(l) = $that.id MATCH (v) WHERE id(v) = idFrom('verb', l.verb) SET v.type = 'verb', v.verb = l.verb CREATE (l)-[:verb]->(v) RETURN null",
        "parameter": "that"
      },
      "destinations": [
        {
          "type": "Drop"
        }
      ]
    }
  ]
}

Running the Recipe

❯ java -jar quine-2.2.1.jar -r apache_log.yaml --recipe-value in_file=apache.log
Graph is ready
Using 1 sample queries
Running Standing Query log-to-verb
Ingest Stream apache-log-ingest: Could not verify that the provided ingest query is idempotent. If timeouts occur, query execution may be retried and duplicate data may be created.
Running Ingest Stream apache-log-ingest
Status query URL is http://127.0.0.1:8080#MATCH%20(l)-%5Brel:verb%5D-%3E(v)%20WHERE%20l.type%20=%20'log'%20AND%20v.type%20=%20'verb'%20AND%20v.verb%20=%20'GET'%20RETURN%20count(rel)%20AS%20get_count
Quine web server available at http://127.0.0.1:8080
Default API version: v2
apache-log-ingest status is completed and ingested 10000
---[ Status Query result 1 ]------------
get_count | 9951
----------+-----------------------------
 | => log-to-verb count 10000

Summary

The recipe contains a status query that will emit a link in the console window to view the results for the count of GET requests in the log file. The status query will also update the console with a running results count of GET entries as they are encountered.

Status query URL is http://127.0.0.1:8080#MATCH%20(l)-%5Brel:verb%5D-%3E(v)%20WHERE%20l.type%20=%20'log'%20AND%20v.type%20=%20'verb'%20AND%20v.verb%20=%20'GET'%20RETURN%20count(rel)%20AS%20get_count

At the time of writing this recipe, the sample data file contains 9951 GET requests.

---[ Status Query result 1 ]------------
get_count | 9951
----------+-----------------------------

Tip

Quick Queries are available by right clicking on a node.

Quick Query Node Type Description
Adjacent Nodes All Display the nodes that are adjacent to this node.
Refresh All Refresh the content stored in a node

Build your skills

What Cypher query could you write to return a count for other HTTP verbs in the log file?

Solution

We solved this by modifying the status query to be less specific and to return unique node verb parameters as part of the results.

Enter this query into the Exploration UI and press Ctrl+Shift+Enter to run it as a text query.

MATCH (l)-[rel:verb]->(v)
WHERE l.type = 'log' AND v.type = 'verb'
RETURN DISTINCT v.verb, count(rel)

Our results:

v.verb count(rel)
"OPTIONS" 1
"POST" 5
"HEAD" 42
"GET" 9951