将ClamAV日志输出转换为JSON

时间:2018-03-11 16:59:58

标签: json

我正在开发一个项目,使用ClamAV扫描几台CentOS 7机器和一台Ubuntu 16.04机器。我们需要将输出集成到SIEM解决方案中,但Clam的输出是不兼容的;

----------- SCAN SUMMARY -----------
Known viruses: 33840
Scanned directories: 145
Scanned files: 226
Infected files: 1
Data scanned: 54.22 MB
I/O buffer size: 131072 bytes
Time: 20.831 sec (0 m 20 s)

我想可以使用pythong将此输出转换为json,但对python来说相对较新我不知道从哪里开始。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

使用

$ awk -vmax=$(wc -l < file) -F: '
    BEGIN{print "{"}
    NR>1{
        gsub(/^ /, "", $2);
        printf "%s",  "\t\042"$1"\042:\042"$2"\042"
        if (NR<max) {print ","}else{print "\n"}
    }
    END{print "}"}
' file

输出:

{
    "Known viruses":"33840",
    "Scanned directories":"145",
    "Scanned files":"226",
    "Infected files":"1",
    "Data scanned":"54.22 MB",
    "I/O buffer size":"131072 bytes",
    "Time":"20.831 sec (0 m 20 s)"

}