0

I'm running Cilium inside an Azure Kubernetes Cluster and want to parse the cilium log messages in the Azure Log Analytics. The log messages have a format like

key1=value1 key2=value2 key3="if the value contains spaces, it's wrapped in quotation marks"

For example:

level=info msg="Identity of endpoint changed" containerID=a4566a3e5f datapathPolicyRevision=0 

I couldn't find a matching parse_xxx method in the docs (e.g. https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/parsecsvfunction ). Is there a possibility to write a custom function to parse this kind of log messages?

lmr2391
  • 571
  • 1
  • 7
  • 14

2 Answers2

2

Not a fun format to parse... But this should work:

let LogLine = "level=info msg=\"Identity of endpoint changed\" containerID=a4566a3e5f datapathPolicyRevision=0";
print LogLine
| extend KeyValuePairs = array_concat(
    extract_all("([a-zA-Z_]+)=([a-zA-Z0-9_]+)", LogLine),
    extract_all("([a-zA-Z_]+)=\"([a-zA-Z0-9_ ]+)\"", LogLine))
| mv-apply KeyValuePairs on 
(
    extend p = pack(tostring(KeyValuePairs[0]), tostring(KeyValuePairs[1]))
    | summarize dict=make_bag(p)
)

The output will be:

| print_0            | dict                                    |
|--------------------|-----------------------------------------|
| level=info msg=... | {                                       |
|                    |   "level": "info",                      |
|                    |   "containerID": "a4566a3e5f",          |
|                    |   "datapathPolicyRevision": "0",        |
|                    |   "msg": "Identity of endpoint changed" |
|                    | }                                       |
|--------------------|-----------------------------------------|
Slavik N
  • 4,705
  • 17
  • 23
  • Great, that helped a lot to get started. I had to change the regexes a bit further, because the real logs contained some quirks that were not present in the example. I'll add another answer with the query I ended up with. – lmr2391 Jan 04 '21 at 17:36
0

With the help of Slavik N, I came with a query that works for me:

let containerIds = KubePodInventory
| where Namespace startswith "cilium"
| distinct ContainerID
| summarize make_set(ContainerID);
ContainerLog
| where ContainerID in (containerIds)
| extend KeyValuePairs = array_concat(
    extract_all("([a-zA-Z0-9_-]+)=([^ \"]+)", LogEntry),
    extract_all("([a-zA-Z0-9_]+)=\"([^\"]+)\"", LogEntry))
| mv-apply KeyValuePairs on 
(
    extend p = pack(tostring(KeyValuePairs[0]), tostring(KeyValuePairs[1]))
    | summarize JSONKeyValuePairs=parse_json(make_bag(p))
)
| project TimeGenerated, Level=JSONKeyValuePairs.level, Message=JSONKeyValuePairs.msg, PodName=JSONKeyValuePairs.k8sPodName, Reason=JSONKeyValuePairs.reason, Controller=JSONKeyValuePairs.controller, ContainerID=JSONKeyValuePairs.containerID, Labels=JSONKeyValuePairs.labels, Raw=LogEntry
lmr2391
  • 571
  • 1
  • 7
  • 14