#!/bin/awk -f ############################# # clean data and filter some to rediect to new file # env: rlx linux with origin-awk and /bin/sh # Author: Liao # modify time: 2022/2/18 16:05 (+8:00) ############################# BEGIN{ fileSize = 1024*1024*1 upload_ip = "192.168.1.5" uploadCount = 0 # filePath = "/data/log_dir/mySimpleLog" log2file("{ \"event\":\"gateway startup\"}") } $0~/ dp rept_type:[0-9]+, data:/ { atime = match($0,/[0-9]{1,2}-[0-9]{1,2} ([0-9]{1,2}:){3}[0-9]{1,3}/) ? substr($0,RSTART,RLENGTH):null; adps = match($0,/\{.+\}/) ? substr($0,RSTART,RLENGTH):null; aLogJson = "{ \"time\":"atime", \"event\":\"dp report\", \"dps\":"adps"}" # log2file(aLogJson) checkFileSize() } $0~/rev id:\w+ cmd:\{.*\} type:0/ { atime = match($0,/[0-9]{1,2}-[0-9]{1,2} ([0-9]{1,2}:){3}[0-9]{1,3}/) ? substr($0,RSTART,RLENGTH) : null; adps = match($0,/cmd:\{.+\} /) ? substr(substr($0,RSTART,RLENGTH),5,RLENGTH-5) : "\"unkown\""; devMac = match($0,/rev id:\w+/) ? substr(substr($0,RSTART,RLENGTH),8) : null if((getline conditionLine) > 0){ isCondition = match(conditionLine,/(no id match|no dp match)/) ? "false" : match(conditionLine,/dp [0-9]+ match/) ? "true" : "\"unkown\"" condition = "\"isCondition\":"isCondition if (isCondition=="true"){ conditionDp = match(conditionLine,/\] dp [0-9]+ match/) ? substr(substr(conditionLine,RSTART,RLENGTH),6,RLENGTH-11) : "\"unkown\"" condition = condition",\"conditionDp\":"conditionDp } }else{ print "no more line" } aLogJson = "{ \"time\":"atime", \"event\":\"linkage recv dp\", \"dps\":"adps",\"devMac\":\""devMac"\","condition"}" log2file(aLogJson) } $0~/condition set is ok,then execute action set\./ { # print $0 atime = match($0,/[0-9]{1,2}-[0-9]{1,2} ([0-9]{1,2}:){3}[0-9]{1,3}/) ? substr($0,RSTART,RLENGTH) : null; aruleid = match($0,/scene:\w+,/) ? substr(substr($0,RSTART,RLENGTH),7,RLENGTH-7) : "false" log2file("{ \"time\":"atime", \"event\":\"trigger\", \"ruleid\":\""aruleid"\",\"triggerNow\":true}") } $0~/action<[0-9]+> is dp cmd:/ { atime = match($0,/[0-9]{1,2}-[0-9]{1,2} ([0-9]{1,2}:){3}[0-9]{1,3}/) ? substr($0,RSTART,RLENGTH) : null; adps = match($0,/cmd:\{\".+\}/) ? substr(substr($0,RSTART,RLENGTH),5,RLENGTH-4) : ""$0; aLogJson = "{ \"time\":"atime", \"event\":\"action\", \"cmd\":"adps"}" log2file(aLogJson) } END{ splitAndUpload() print "upload "uploadCount" count" } function log2file(logJson){ system("echo -e '"logJson"' >> /data/log_dir/mySimpleLog") } function checkFileSize(){ checkState = system("ls -l /data/log_dir/mySimpleLog | awk '{ if($5 >"fileSize"){exit 0}else{exit 1} }'") if(!checkState){ splitAndUpload() } } function splitAndUpload(){ newFileName = "mySimpleLog_"systime() print "uploading..." ret = system("mv /data/log_dir/mySimpleLog /data/log_dir/"newFileName" && cd /data/log_dir/ && tftp "upload_ip" -p -l "newFileName" > /dev/null && rm "newFileName) if(!ret){ print "upload success!" uploadCount++ }else{ print "upload fail!" } } function notify2python(){ # system("curl http://"upload_ip"/api") }