diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index b639f68ef81e47f5d1ca0c3e13497b3d586b058e..0000000000000000000000000000000000000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.gitignore b/.gitignore index 40a844cf97c51e9911797ce9870e063f2b71d5ed..cdce78c5fe3a2c389fcd5708e749ca9b27c58551 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,10 @@ *.ko *.ko.cmd *.idea +**/.idea/ +**/.DS_Store Module.symvers modules.builtin modules.order /out +sync.sh \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b81b018ad684f3a35fee301741b2734c8f4..0000000000000000000000000000000000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 53624c9e1f9ab0331fd10b32c26747519ace5794..0000000000000000000000000000000000000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddfbbc029bcab630581847471d7f238ec53..0000000000000000000000000000000000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/source/.DS_Store b/source/.DS_Store deleted file mode 100644 index fe62bc3d37d7e43ee3c765a76137456f136a5618..0000000000000000000000000000000000000000 Binary files a/source/.DS_Store and /dev/null differ diff --git a/source/tools/.DS_Store b/source/tools/.DS_Store deleted file mode 100644 index 12272494a9145ab77e6821d51a35aa9e656937e1..0000000000000000000000000000000000000000 Binary files a/source/tools/.DS_Store and /dev/null differ diff --git a/source/tools/monitor/.DS_Store b/source/tools/monitor/.DS_Store deleted file mode 100644 index 54403a1caa441bc5047ab36847f73aef327fef34..0000000000000000000000000000000000000000 Binary files a/source/tools/monitor/.DS_Store and /dev/null differ diff --git a/source/tools/monitor/unity/beeQ/pack.sh b/source/tools/monitor/unity/beeQ/pack.sh index 07b479ba32000502b5769d90fc5a59a15a1208c0..dfabfce33cb4029dea2684076f1f7c5059b48574 100755 --- a/source/tools/monitor/unity/beeQ/pack.sh +++ b/source/tools/monitor/unity/beeQ/pack.sh @@ -80,6 +80,12 @@ cp common/*.lua ${APP}/common/ mkdir ${APP}/httplib cp httplib/*.lua ${APP}/httplib/ +mkdir ${APP}/sls_metric +mkdir ${APP}/sls_metric/native +cp sls_metric/native/*.so* ${APP}/sls_metric/native/ +cp sls_metric/native/*.lua ${APP}/sls_metric/native/ +cp sls_metric/*.lua ${APP}/sls_metric/ + mkdir ${APP}/tsdb mkdir ${APP}/tsdb/native cp tsdb/native/*.so* ${APP}/tsdb/native/ diff --git a/source/tools/monitor/unity/beeQ/pushTo.lua b/source/tools/monitor/unity/beeQ/pushTo.lua index bf301ffedc8bbb7ed1e97d450ef577e55268960c..c4d68eff493db881fd85b998e74c2fb2905de1ee 100644 --- a/source/tools/monitor/unity/beeQ/pushTo.lua +++ b/source/tools/monitor/unity/beeQ/pushTo.lua @@ -6,13 +6,26 @@ package.path = package.path .. ";../?.lua;" +local system = require("common.system") local coCli = require("httplib.coCli") local coInflux = require("httplib.coInflux") +local coMetrics = require("httplib.coMetrics") +local coAutoMetrics =require("httplib.coAutoMetrics") function work(fd, fYaml) + local conf = system:parseYaml(fYaml) + local to = conf.pushTo.to + print(to) local frame = coCli.new(fd) - local c = coInflux.new(fYaml) + local _funcs = { + Influx = function(fYaml) return coInflux.new(fYaml) end, + Metrics = function(fYaml) return coMetrics.new(fYaml) end, + AutoMetrics = function(fYaml) return coAutoMetrics.new(fYaml) end + } + local c = _funcs[to](fYaml) + --local c = _funcs[to]("/etc/sysak/base.yaml") frame:poll(c) + print("end push.") return 0 end diff --git a/source/tools/monitor/unity/beeQ/run.sh b/source/tools/monitor/unity/beeQ/run.sh index 1f228ae736c9ee43efa024e219590a697d11f156..97b5be763dc2807fbf432511db096a36bc45a9db 100755 --- a/source/tools/monitor/unity/beeQ/run.sh +++ b/source/tools/monitor/unity/beeQ/run.sh @@ -18,7 +18,7 @@ yaml_path=$1 [ ! $yaml_path ] && yaml_path="/etc/sysak/base.yaml" #download sysak.ko -sysak -oss -d +#sysak -oss -d echo $yaml_yaml_path ./unity-mon $yaml_path diff --git a/source/tools/monitor/unity/collector/guard/guardSelfStat.lua b/source/tools/monitor/unity/collector/guard/guardSelfStat.lua index bbdc5e3ddab8e7f3a96b2693358dd810590c4e5c..f02d72e48b844770300a2ae3e7d52a025a08fd80 100644 --- a/source/tools/monitor/unity/collector/guard/guardSelfStat.lua +++ b/source/tools/monitor/unity/collector/guard/guardSelfStat.lua @@ -30,8 +30,17 @@ function CguardSelfStat:_init_(proto, pffi, mnt, resYaml, jperiod) self._lastUser, self._lastSys, _, _ = readProc(self._path) self._period = jperiod - self._cpuLimit = resYaml.config.limit.cpu * jperiod / 100 - self._memLimit = resYaml.config.limit.mem * 1024 * 1024 + + self._cpuLimit = nil + self._memLimit = nil + if resYaml.config.limit then + if resYaml.config.limit.cpu then + self._cpuLimit = resYaml.config.limit.cpu * jperiod / 100 + end + if resYaml.config.limit.mem then + self._memLimit = resYaml.config.limit.mem * 1024 * 1024 + end + end end local function rssRssAnon() @@ -54,17 +63,17 @@ function CguardSelfStat:proc(elapsed, lines) local user, sys, vsize, rss = readProc(self._path) local _user, _sys = user - self._lastUser, sys - self._lastSys - local cpus = _user, _sys + local cpus = _user + _sys self._lastUser, self._lastSys = user, sys - if cpus > self._cpuLimit * elapsed then - print("last cpu usage overflow." .. cpus) + if self._cpuLimit and cpus > self._cpuLimit * elapsed then + print("last cpu usage overflow. user + sys jiffies: " .. cpus) os.exit(1) end local anon = rssRssAnon() - if anon > self._memLimit then - print("last mem usage overflow." .. rss) + if self._memLimit and anon > self._memLimit then + print("last mem usage overflow. rss bytes: " .. rss) os.exit(1) end local vs = { diff --git a/source/tools/monitor/unity/collector/plugin.yaml b/source/tools/monitor/unity/collector/plugin.yaml deleted file mode 100644 index 07e9b3c0ce7a13a3fc1db8ed99c043befd6ede56..0000000000000000000000000000000000000000 --- a/source/tools/monitor/unity/collector/plugin.yaml +++ /dev/null @@ -1,333 +0,0 @@ -config: - freq: 20 # unit second - daemon: true - port: 8400 # bind port - bind_addr: 0.0.0.0 # bind ip - backlog: 32 # listen backlog - identity: # support hostip, curl(need url arg), hostname, file(need path arg), specify(need name arg), env(need name arg) - mode: curl - url: "http://100.100.100.200/latest/meta-data/instance-id" -# name: test_specify -# mode: hostip -# real_timestamps: true -# unix_socket: "/tmp/sysom_unity.sock" - proc_path: /mnt/host/ # in container mode, like -v /:/mnt/host , should use /mnt/host/ -# proc_path: / # in container mode, like -v /:/mnt/host , should use /mnt/host/ - db: - rotate: 7 # tsdb file retention time, unit day - budget: 200 # max query buffer from tsdb. - limit: - cpu: 30 # unit % - mem: 40 # unit mb - tasks: 10 # monitor 10 pid max. - -forkRun: - - - cmd: "/usr/bin/python" - args: ["../test/curl/forkRun.py"] - -oss: - bucket: "netinfo-shenzhen" - endPoint: "oss-cn-shenzhen.aliyuncs.com" - ak: "ak" - sk: "sk" - -diagnose: - io_hang: - block: 60 - time: 15 - cmd: "../../../iosdiag" - report: - title: "iosdiag" - files: - - "/var/log/sysak/iosdiag/hangdetect/result.log.stat" - - "/var/log/sysak/iosdiag/hangdetect/result.log.seq" - net_edge: - block: 300 - time: 60 - so: - virtiostat: 15 - cmd: "../../../netCli" - jruntime: - block: 60 - time: 30 - cmd: "../../../java_collect" - -outline: - - /var/sysom/outline - -pushTo: - to: "Influx" - host: "ld-wz9d17b514mg6kjkx-proxy-tsdb.lindorm.rds.aliyuncs.com" - port: 8242 - url: "/api/v2/write?db=lua" - -container: - mode: "pods" - luaPlugin: ["cg_cpu_stat_sample", "cg_cpuacct_stat"] - directCgPath: - - "/" - - "/kubepods.slice" - - "/kubepods.slice/kubepods-besteffort.slice" - - "/kubepods.slice/kubepods-burstable.slice" - - indirectCgPath: - - "kubepods.slice" - - "kubepods.slice/kubepods-besteffort.slice" - - "kubepods.slice/kubepods-burstable.slice" - - indirectCgPath1: - - path: "/kubepods.slice" - child1: "/kubepods%-pod" - child2: "/cri%-containerd" - - path: "/kubepods.slice/kubepods-besteffort.slice" - child1: "/kubepods%-besteffort%-pod" - child2: "/cri%-containerd" - - path: "/kubepods.slice/kubepods-burstable.slice" - child1: "/kubepods%-burstable%-pod" - -luaPlugins: ["proc_buddyinfo", "proc_diskstats", "proc_meminfo", "proc_mounts", "proc_netdev", - "proc_snmp_stat", "proc_sockstat", "proc_stat", "proc_statm", "proc_vmstat", - "proc_uptime"] - -plugins: - - so: kmsg - description: "collect dmesg info." - - - so: sample - description: "just a example." - - - so: sample_threads - description: "threads example." - - - so: bpfsample2 - description: "bpf threads example." - - - - so: proc_schedstat - description: "collect schedule stat info of percpu" - - - so: proc_loadavg - description: "collect load avg" - - - - so: unity_nosched - description: "nosched:sys hold cpu and didn't scheduling" - - so: net_health - description: "tcp net health." - - so: net_retrans - description: "tcp retrans monitor." - - - so: unity_irqoff - description: "irqoff:detect irq turned off and can't response" - #- - # so: numainfo - # description: "collect numainfo" - #- - # so: cpufreq - # description: "collect cpufreq" - - - so: gpuinfo - description: "collect gpuinfo" - #- - # so: pmu_events - # description: "collect pmu events" - -metrics: - - - title: sysak_proc_cpu_total - from: cpu_total - head: mode - help: "cpu usage info for total." - type: "gauge" - - title: sysak_proc_cpus - from: cpus - head: mode - help: "cpu usage info for per-cpu." - type: "gauge" - - title: sysak_proc_sirq - from: sirq - head: type - help: "system soft irq times." - type: "gauge" - - title: sysak_proc_stat_counters - from: stat_counters - head: counter - help: "system state counter." - type: "gauge" - - title: sysak_proc_meminfo - from: meminfo - head: value - help: "meminfo from /proc/meminfo." - type: "gauge" - - title: sysak_proc_vmstat - from: vmstat - head: value - help: "vmstat info from /proc/vmstat." - type: "gauge" - - title: sysak_proc_self_statm - from: self_statm - head: value - help: "statm info from /proc/self/statm." - type: "gauge" - - title: sysak_proc_networks - from: networks - head: counter - help: "networks info from /proc/net/dev." - type: "gauge" - - title: sysak_proc_disks - from: disks - head: counter - help: "disk info from /proc/diskstats." - type: "gauge" - - title: sysak_proc_pkt_status - from: pkt_status - head: counter - help: "net status info from /proc/net/snmp and /proc/net/status." - type: "gauge" - - title: sysak_fs_stat - from: fs_stat - head: counter - help: "file system information." - type: "gauge" - - title: sysak_sock_stat - from: sock_stat - head: value - help: "sock stat counters from /proc/net/sockstat" - type: "gauge" - - title: sysak_sample_tbl1 - from: sample_tbl1 - head: value - help: "example1 for develop." - type: "gauge" - - title: sysak_sample_tbl2 - from: sample_tbl2 - head: value - help: "example2 for develop." - type: "gauge" - - title: sysak_sample_bpfsample2 - from: bpfsample2 - head: value - help: "example for bpfsample2" - type: "gauge" - - title: sysak_proc_schedstat - from: proc_schedstat - head: value - help: "schedule state of percpu." - type: "gauge" - - title: sysak_proc_loadavg - from: proc_loadavg - head: value - help: "loadavg of system from /proc/loadavg" - type: "gauge" - - title: sysak_proc_buddyinfo - from: buddyinfo - head: value - help: "buddyinfo of system from /proc/buddyinfo" - type: "gauge" - - title: sysak_IOMonIndForDisksIO - from: IOMonIndForDisksIO - head: value - help: "Disk IO indicators and abnormal events" - type: "gauge" - - title: sysak_IOMonIndForSystemIO - from: IOMonIndForSystemIO - head: value - help: "System indicators and abnormal events about IO" - type: "gauge" - - title: sysak_IOMonDiagLog - from: IOMonDiagLog - head: value - help: "Diagnose log for IO exception" - type: "gauge" - - title: sysak_sched_moni_jitter - from: sched_moni_jitter - head: value - help: "nosched/irqoff:sys and irqoff hold cpu and didn't scheduling" - type: "gauge" - - title: sysak_cpu_dist - from: cpu_dist - head: value - help: "task cpu sched dist." - type: "gauge" - - title: sysak_net_health_hist - from: net_health_hist - head: value - help: "net_health_hist" - type: "gauge" - - title: sysak_net_health_count - from: net_health_count - head: value - help: "net_health_count" - type: "gauge" - - title: sysak_net_retrans_count - from: net_retrans_count - head: value - help: "net_retrans_count" - type: "gauge" - #- title: sysak_numainfo - # from: numainfo - # head: value - # help: "numainfo of system from /sys/devices/system/" - # type: "gauge" - #- title: sysak_proc_cpufreq - # from: cpufreq - # head: value - # help: "cpufreq of system from /proc/cpuinfo" - # type: "gauge" - - title: sysak_gpuinfo - from: gpuinfo - head: value - help: "gpuinfo of system from nvidia-smi" - type: "gauge" - - #- title: sysak_pod_alloc - #from: pod_alloc - #head: value - #help: "get pod alloc page used" - #type: "gauge" - - title: sysak_pmu_events - from: pmu_events - head: value - help: "pmu events, such as cycles/instructions, llc events" - type: "gauge" - - title: sysak_pmu_events_percpu - from: pmu_events_percpu - head: value - help: "pmu events of percpu" - type: "gauge" - - title: sysak_cg_memfail_cnt - from: cg_memfail_cnt - head: value - help: "sysak_cg_memFail_cnt" - type: "gauge" - - title: sysak_cg_memdrcm_latency - from: cg_memdrcm_latency - head: value - help: "sysak_cg_memdrcm_latency" - type: "gauge" - - title: sysak_cg_memmcmp_latency - from: cg_memmcmp_latency - head: value - help: "sysak_cg_memmcmp_latency" - type: "gauge" - - title: sysak_cg_wait_latency - from: cg_wait_latency - head: value - help: "sysak_cg_wait_latency" - type: "gauge" - - title: sysak_cg_cpuacct_proc_stat - from: cg_cpuacct_proc_stat - head: value - help: "sysak_cg_cpuacct_proc_stat" - type: "gauge" - - title: sysak_cg_cpu_stat - from: cg_cpu_stat - head: value - help: "sysak_cg_cpu_stat" - type: "gauge" - - title: sysak_cg_cpuacct_stat - from: cg_cpuacct_stat - head: value - help: "cpuacct/cpuacct.stat" - type: "gauge" diff --git a/source/tools/monitor/unity/collector/podMan/podsAll.lua b/source/tools/monitor/unity/collector/podMan/podsAll.lua index ae3b563bde418f462c058e934f53ac04825f8b8f..248a1a777ec8b908a2293f2eb911a5c35b30a192 100644 --- a/source/tools/monitor/unity/collector/podMan/podsAll.lua +++ b/source/tools/monitor/unity/collector/podMan/podsAll.lua @@ -174,14 +174,6 @@ local function setupPlugins(res, proto, pffi, mnt, ino) name = "podns", index = con.pod.namespace, }, - { - name = "uid", - index = con.pod.uid, - }, - { - name = "con_id", - index = string.sub(con.id, 1, 12) - }, } for _, plugin in ipairs(res.container.luaPlugin) do diff --git a/source/tools/monitor/unity/common/addition.lua b/source/tools/monitor/unity/common/addition.lua new file mode 100644 index 0000000000000000000000000000000000000000..e8a98515a72dccdd69a4d238dd891e45fd11b304 Binary files /dev/null and b/source/tools/monitor/unity/common/addition.lua differ diff --git a/source/tools/monitor/unity/common/transPro.lua b/source/tools/monitor/unity/common/transPro.lua new file mode 100644 index 0000000000000000000000000000000000000000..328e99988f53a9cb66ce8284c9f2d01dd90c24b8 --- /dev/null +++ b/source/tools/monitor/unity/common/transPro.lua @@ -0,0 +1,114 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by wrp. +--- DateTime: 2023/7/10 10:46 +--- + +local system = require("common.system") +local pystring = require("common.pystring") +require("common.class") + +local CtransPro = class("CtransPro") + +local function qFormData(from, tData) + local res = {} + local len = #tData + local last = 0 + local c = 0 + for i = len, 1, -1 do + local line = tData[i] + print(line.title) + if from == line.title then + if last == 0 or last == line.time then + c = c + 1 + res[c] = line + last = line.time + else + break + end + end + end + return res +end + +local function packLine_us(title, ls, v, time) + local tLs = {} + for k, v in pairs(ls) do + table.insert(tLs, string.format("%s=\"%s\"", k , v)) + end + local label = "" + if #tLs then + label = pystring:join(",", tLs) + label = "{" .. label .. "}" + end + return string.format("%s%s %.1f %d", title, label, v, time/1000) +end + +local function packLine(title, ls, v, time) + local tLs = {} + local c = 0 + for k, v in pairs(ls) do + c = c + 1 + tLs[c] = string.format("%s=\"%s\"", k , v) + end + local label = "" + if #tLs then + label = pystring:join(",", tLs) + label = "{" .. label .. "}" + end + return string.format("%s%s %.1f", title, label, v) +end + +function CtransPro:_init_(instance, fYaml, help, timestamps) + self._instance = instance + self._help = help or false + local ms = system:parseYaml(fYaml) + self._timestamps = timestamps or false + if self._timestamps == true then + self.pack_line = packLine_us + else + self.pack_line = packLine + end + self._tDescr = ms.metrics + print(self._tDescr) +end + +function CtransPro:export(datas) + local res = {} + local c = 0 + for _, line in ipairs(self._tDescr) do + --local from = line.from -- cpu_total + --local tFroms = qFormData(from, datas) + if #datas then + local title = line.title --sysak_proc_cpu_total + if self._help then + local help = string.format("# HELP %s %s", title, line.help) + c = c + 1 + res[c] = help + local sType = string.format("# TYPE %s %s", title, line.type) + c = c + 1 + res[c] = sType + end + + for _, tFrom in ipairs(datas) do + local labels = system:deepcopy(tFrom.labels) + if not labels then + labels = {} + end + labels.instance = self._instance + for k, v in pairs(tFrom.vs) do + labels[line.head] = v.name + c = c + 1 + res[c] = self.pack_line(title, labels, v.value, 1) + end + end + end + end + --c = c + 1 + --res[c] = "" + local lines = pystring:join("\n", res) + --print(lines) + return lines +end + +return CtransPro \ No newline at end of file diff --git a/source/tools/monitor/unity/etc/k8s.yaml b/source/tools/monitor/unity/etc/k8s.yaml index 19e6059ec0fe78268c4c0c31e6a926f4ff8cff96..f73d003b926edac23391bc3505c4930c01c71ddc 100644 --- a/source/tools/monitor/unity/etc/k8s.yaml +++ b/source/tools/monitor/unity/etc/k8s.yaml @@ -49,7 +49,7 @@ container: luaPlugins: ["podmem","proc_buddyinfo", "proc_diskstats", "proc_meminfo", "proc_mounts", "proc_netdev", - "proc_snmp_stat", "proc_sockstat", "proc_stat", "proc_statm", "proc_vmstat","pod_allocpage", + "proc_snmp_stat", "proc_sockstat", "proc_stat", "proc_statm", "proc_vmstat", "proc_uptime", "proc_arp", "proc_cgroups", "proc_softirqs", "proc_softnet_stat", ] diff --git a/source/tools/monitor/unity/httplib/coAutoMetrics.lua b/source/tools/monitor/unity/httplib/coAutoMetrics.lua new file mode 100644 index 0000000000000000000000000000000000000000..b7d87aa6d3d30c526434c176a4997cfdcaae3674 --- /dev/null +++ b/source/tools/monitor/unity/httplib/coAutoMetrics.lua @@ -0,0 +1,27 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by wrp. +--- DateTime: 2023/7/17 15:26 +--- + +require("common.class") + +local CcoMetrics = require("httplib.coMetrics") + +local CcoAutoMetrics = class("coAutoMetrics", CcoMetrics) + +function CcoAutoMetrics:_init_(fYaml) + CcoMetrics._init_(self,fYaml) + + local ts = io.popen('curl 100.100.100.200/latest/meta-data/region-id') + local regionid = ts:read("*all") + + self._project = "sysom-metrics-" .. regionid + self._endpoint = regionid .. "-intranet.log.aliyuncs.com" + self._metricstore = "auto" + self._url = "/prometheus/" ..self._project.."/"..self._metricstore.."/api/v1/write" + self._host = self._project .."." .. self._endpoint + +end + +return CcoAutoMetrics \ No newline at end of file diff --git a/source/tools/monitor/unity/httplib/coHttpCli.lua b/source/tools/monitor/unity/httplib/coHttpCli.lua index 9718385d2e36764b01e00e2b55265005ecf7db89..83c7b1a22da676def801b3ef1d8d1c76e36e8a70 100644 --- a/source/tools/monitor/unity/httplib/coHttpCli.lua +++ b/source/tools/monitor/unity/httplib/coHttpCli.lua @@ -113,18 +113,13 @@ local function setupSocket(host, port) end end -function CcoHttpCli:_init_(fYaml, persistent) - local res = system:parseYaml(fYaml) - local pushTo = res.pushTo +function CcoHttpCli:_init_(pushTo, persistent) + self._host = pushTo.host self._port = pushTo.port or 80 self._url = pushTo.url or "/" self._persistent = persistent - local Cidentity = require("beaver.identity") - local inst = Cidentity.new(fYaml) - self._instance = inst:id() - self.status = enumStat.closed end @@ -151,30 +146,6 @@ function CcoHttpCli:trans(msgs, body, filter) return "" end -function CcoHttpCli:addInstance(line) -- add instance id for line index. - local cells = line.ls - local hasInstance = false - - if cells then - for _, cell in ipairs(cells) do - if cell.name == "instance" then - hasInstance = true - end - end - end - - if not hasInstance then - local cell = { - name = "instance", - index = self._instance - } - if cells then - table.insert(cells, cell) - else - line.ls = {cell} - end - end -end function CcoHttpCli:pack(body) local line = self:packCliHead('GET', self._url) @@ -271,7 +242,7 @@ local function waitChuckSize(fread, s) if string.find(s, "\r\n") then return s end - local add = fread() + local add = fread() --add = nil,后续没有读到数据了 if add then s = s .. add else @@ -283,7 +254,7 @@ end local function readChunks(fread, tReq) local cells = {} local s = tReq.data - local ssize, size + local size local len = 1 local bodies, body @@ -325,8 +296,9 @@ local function waitHttpRest(fread, tReq) if waitDataRest(fread, rest, tReq) < 0 then return -2 end - else -- chunk mode - if #tReq.data > 0 then + elseif tReq.header["Transfer-Encoding"] then + -- chunk mode + if tReq.header["Transfer-Encoding"]=="chunked" and #tReq.data > 0 then if readChunks(fread, tReq) < 0 then return -3 end @@ -520,8 +492,8 @@ function CcoHttpCli:work(cffi, efd) goto failed end self.status = enumStat.receiving - local fread = self:closureRead(fd) - local tReq = self:result(fread) + local fread = self:closureRead(fd) -- fread() = nil + local tReq = self:result(fread) --nil,fread = nil if tReq then self.status = enumStat.connected self:echo(tReq) diff --git a/source/tools/monitor/unity/httplib/coHttpCliInst.lua b/source/tools/monitor/unity/httplib/coHttpCliInst.lua new file mode 100644 index 0000000000000000000000000000000000000000..4481f2d635fc554f254f17c3ab16e5e99305278a --- /dev/null +++ b/source/tools/monitor/unity/httplib/coHttpCliInst.lua @@ -0,0 +1,44 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by wrp. +--- DateTime: 2023/7/17 09:45 +--- + +require("common.class") + +local CcoHttpCli = require("httplib.coHttpCli") + +local CcoHttpCliInst = class("coHttpCliInst", CcoHttpCli) + +function CcoHttpCliInst:_init_(instance, pushTo, persistent) + CcoHttpCli._init_(self, pushTo ,persistent) + + self._instance = instance +end + +function CcoHttpCliInst:addInstance(line) -- add instance id for line index. + local cells = line.ls + local hasInstance = false + + if cells then + for _, cell in ipairs(cells) do + if cell.name == "instance" then + hasInstance = true + end + end + end + + if not hasInstance then + local cell = { + name = "instance", + index = self._instance + } + if cells then + table.insert(cells, cell) + else + line.ls = {cell} + end + end +end + +return CcoHttpCliInst \ No newline at end of file diff --git a/source/tools/monitor/unity/httplib/coInflux.lua b/source/tools/monitor/unity/httplib/coInflux.lua index 6b00a4c930c8b53f66d0f86efb6d90213545ac9f..61f8e8634563983894871a59aee443dfdd1e335d 100644 --- a/source/tools/monitor/unity/httplib/coInflux.lua +++ b/source/tools/monitor/unity/httplib/coInflux.lua @@ -5,14 +5,26 @@ --- require("common.class") -local CcoHttpCli = require("httplib.coHttpCli") +local CcoHttpCliInst = require("httplib.coHttpCliInst") local pystring = require("common.pystring") local lineParse = require("common.lineParse") +local system = require("common.system") -local CcoInflux = class("coInflux", CcoHttpCli) +local CcoInflux = class("coInflux", CcoHttpCliInst) function CcoInflux:_init_(fYaml) - CcoHttpCli._init_(self, fYaml) + local res = system:parseYaml(fYaml) + local pushInflux = { + host = res.pushTo.host, + url = res.pushTo.url, + port = res.pushTo.port + } + + local Cidentity = require("beaver.identity") + local inst = Cidentity.new(fYaml) + local instance = inst:id() + + CcoHttpCliInst._init_(self, instance, pushInflux) end function CcoInflux:echo(tReq) diff --git a/source/tools/monitor/unity/httplib/coMetrics.lua b/source/tools/monitor/unity/httplib/coMetrics.lua new file mode 100644 index 0000000000000000000000000000000000000000..510cd54e310df92198253c053524c621d1054dc4 --- /dev/null +++ b/source/tools/monitor/unity/httplib/coMetrics.lua @@ -0,0 +1,101 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by wrp. +--- DateTime: 2023/7/7 17:07 +--- + +require("common.class") + +local CcoHttpCliInst = require("httplib.coHttpCliInst") +local system = require("common.system") +local pystring = require("common.pystring") +local lineParse = require("common.lineParse") +local CtransPro = require("common.transPro") +local base64 = require("base64") +local addition = require("common.addition") + +local CcoMetrics = class("coMetrics", CcoHttpCliInst) + +function CcoMetrics:_init_(fYaml) + + local res = system:parseYaml(fYaml) + local _metrics = res.metrics + self._mhead = _metrics.head + self._title = _metrics.title + + local Cidentity = require("beaver.identity") + local inst = Cidentity.new(fYaml) + local instance = inst:id() + + local _addition = res.pushTo.addition + + self._key1, self._key2 = addition:decode(_addition) + self._project = res.pushTo.project + self._endpoint = res.pushTo.endpoint + self._metricstore = res.pushTo.metricstore + self._url = "/prometheus/" ..self._project.."/"..self._metricstore.."/api/v1/write" + self._host = self._project .."." .. self._endpoint + + local pushMetrics = { + host = self._host, + url = self._url, + port = 80 + } + CcoHttpCliInst._init_(self, instance, pushMetrics) + -- go ffi + local ffi = require("sls_metric.native.ffi_lua") + self.ffi = ffi.ffi + self.awesome = ffi.awesome + + --fox ffi + local foxFFI = require("tsdb.native.foxffi") + self.foxffi = foxFFI.ffi + self.foxcffi = foxFFI.cffi + + self._transPro = CtransPro.new(instance, fYaml, false, false) +end + +function CcoMetrics:echo(tReq) + --if tReq.code ~= "204" then + print(tReq.code, tReq.data) + --end +end + +function CcoMetrics:trans(msgs) + local res + local c = 0 + local lines + + lines = msgs.lines + res = self._transPro:export(lines) + local prome = self.ffi.new("GoString") + prome.p = res + prome.n = #res + local prome_ptr = self.ffi.cast("GoString*", prome) + local byte = self.ffi.new("GoSlice") + local byte_ptr = self.ffi.cast("GoSlice*", byte) --{ void *data; GoInt len; GoInt cap; } GoSlice + local data_len = self.awesome.metricSnappy(prome,byte_ptr) + data_len = tonumber(data_len) + local data = self.ffi.cast("GoUint8*", byte_ptr.data) + + return self.foxffi.string(data, data_len) +end + +function CcoMetrics:pack(body) + local line = self:packCliHead('POST', self._url) + local keys = self._key1 .. ":"..self._key2 + local keys64 = base64.encode(keys) + local head = { + Host = self._host, + ["Content-Encoding"] = "snappy", + ["Content-Type"] = "application/x-protobuf", + --["X-Prometheus-Remote-Write-Version"] = "0.1.0", + ["Content-Length"] = #body, + ["Authorization"] = "Basic " .. keys64, + } + local heads = self:packCliHeaders(head) + print("pack finish") + return pystring:join("\r\n", {line, heads, body}) +end + +return CcoMetrics \ No newline at end of file diff --git a/source/tools/monitor/unity/httplib/test.lua b/source/tools/monitor/unity/httplib/test.lua new file mode 100644 index 0000000000000000000000000000000000000000..50f6e3609d2b5223381f69853fa81c9d49e4d4ef --- /dev/null +++ b/source/tools/monitor/unity/httplib/test.lua @@ -0,0 +1,9 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by wrp. +--- DateTime: 2023/7/17 15:42 +--- + +local ts = io.popen('curl 100.100.100.200/latest/meta-data/region-id') +local s = ts:read("*all") +print("s "..s) diff --git a/source/tools/monitor/unity/sls_metric/native/README.md b/source/tools/monitor/unity/sls_metric/native/README.md new file mode 100644 index 0000000000000000000000000000000000000000..95e08e3e0ac7bd65c156cfd35a49b1ae706988dc --- /dev/null +++ b/source/tools/monitor/unity/sls_metric/native/README.md @@ -0,0 +1,10 @@ +模块功能: + 将prometheus格式字符串转化为snappy压缩后的字节流 + +run: + sh ./build_shared.sh + luajit ffi_lua.lua + +test: + 修改metricstore信息 + go run test_sls.go \ No newline at end of file diff --git a/source/tools/monitor/unity/sls_metric/native/build_shared.sh b/source/tools/monitor/unity/sls_metric/native/build_shared.sh new file mode 100644 index 0000000000000000000000000000000000000000..743e6b0d96c4ad872263d3088d98eb6a751093b9 --- /dev/null +++ b/source/tools/monitor/unity/sls_metric/native/build_shared.sh @@ -0,0 +1,7 @@ +rm go.mod +rm go.sum +go mod init metricSnappy.go +go mod tidy +go build -o libmetricSnappy.so -buildmode=c-shared metricSnappy.go +cp libmetricSnappy.so ../../beeQ/lib + diff --git a/source/tools/monitor/unity/sls_metric/native/ffi_lua.lua b/source/tools/monitor/unity/sls_metric/native/ffi_lua.lua new file mode 100644 index 0000000000000000000000000000000000000000..2c3649d619edfb765bfa938fed579eb225335af3 --- /dev/null +++ b/source/tools/monitor/unity/sls_metric/native/ffi_lua.lua @@ -0,0 +1,37 @@ +local ffi = require("ffi") +local awesome = ffi.load('metricSnappy') + +ffi.cdef[[ +typedef signed char GoInt8; +typedef unsigned char GoUint8; +typedef long long GoInt64; +typedef GoInt64 GoInt; +typedef double GoFloat64; +typedef struct { const char *p; GoInt n; } GoString; +typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; +extern GoInt metricSnappy(GoString* prome_ptr, GoSlice* ret); +]] + +return {ffi = ffi, awesome = awesome} +-- +---- input prometheus string +--local prome = ffi.new("GoString") +---- an example of prometheus string +--local s = 'sysak_proc_cpu_total{mode="user",instance="i-wz9d3tqjhpb8esj8ps4z"} 0.8\nsysak_proc_cpu_total{mode="total",instance="i-wz9d3tqjhpb8esj8ps4z"} 3960.0' +--prome.p = s; +--prome.n = #s; +--local prome_ptr = ffi.cast("GoString*", prome) +--local byte = ffi.new("GoSlice") +--local byte_ptr = ffi.cast("GoSlice*", byte) +-- +---- execute parse and snappy +--local data_len = awesome.metricSnappy(prome,byte_ptr) +-- +---- read data in lua +--data_len = tonumber(data_len) +--local data = ffi.cast("GoUint8*", byte_ptr.data) +-- +---- show data +--for i = 0, data_len do +-- print(data[i]) +--end diff --git a/source/tools/monitor/unity/sls_metric/native/metricSnappy.go b/source/tools/monitor/unity/sls_metric/native/metricSnappy.go new file mode 100644 index 0000000000000000000000000000000000000000..eeded249fcb4023609753761c2d985de4ec7f12a --- /dev/null +++ b/source/tools/monitor/unity/sls_metric/native/metricSnappy.go @@ -0,0 +1,88 @@ +package main + +import "C" + +import ( + "fmt" + "time" + "strings" + "strconv" + "github.com/gogo/protobuf/proto" + "github.com/prometheus/prometheus/prompb" + "github.com/golang/snappy" + "regexp" +) + +//export metricSnappy +func metricSnappy(prome_ptr *string, ret *[]byte) int { + /* + input: + prome_ptr, *string, prometheus data + ret, *[]byte, receive return byte data + output: + int, length of return data + */ + // initialize + var prome = strings.Split(*prome_ptr, "\n") + timeSeries := []prompb.TimeSeries{}; + timestamp := time.Now().UnixNano() + + for i:=0;i