From 049d8681cc4d54b50a869f932e4af0c96136c79e Mon Sep 17 00:00:00 2001 From: liaozhaoyan Date: Tue, 9 May 2023 17:01:50 +0800 Subject: [PATCH 1/6] add virtiostat. --- .../unity/collector/execEngine/forkRun.lua | 31 +-- .../monitor/unity/collector/plugin/Makefile | 2 +- .../plugin/net_retrans/net_retrans.c | 18 +- .../plugin/sum_retrans/sum_retrans.c | 13 +- .../collector/plugin/virtiostat/Makefile | 8 + .../plugin/virtiostat/virtiostat.bpf.c | 105 +++++++ .../collector/plugin/virtiostat/virtiostat.c | 43 +++ .../collector/plugin/virtiostat/virtiostat.h | 18 ++ .../unity/collector/plugin/virtout/virtout.c | 6 +- .../unity/collector/postEngine/engine.lua | 34 +++ .../unity/collector/postEngine/execBase.lua | 27 +- .../unity/collector/postEngine/execDiag.lua | 10 + source/tools/monitor/unity/common/exec.lua | 33 +++ source/tools/monitor/unity/httplib/coCli.lua | 3 +- .../monitor/unity/test/curl/postDiag.lua | 15 + .../monitor/unity/test/lab/tcpServer/cli.py | 11 + .../monitor/unity/test/lab/tcpServer/serv.py | 28 ++ .../monitor/unity/test/pcap/capLoop/Makefile | 16 ++ .../monitor/unity/test/pcap/capLoop/capLoop.c | 58 ++++ .../monitor/unity/test/pcap/capOne/Makefile | 16 ++ .../monitor/unity/test/pcap/capOne/capOne.c | 56 ++++ .../monitor/unity/test/pcap/caps/Makefile | 16 ++ .../tools/monitor/unity/test/pcap/caps/caps.c | 257 ++++++++++++++++++ .../monitor/unity/test/pcap/dump/Makefile | 16 ++ .../tools/monitor/unity/test/pcap/dump/dump.c | 85 ++++++ .../monitor/unity/test/pcap/filter/Makefile | 16 ++ .../monitor/unity/test/pcap/filter/filter.c | 63 +++++ .../unity/test/pcap/lookupdev/Makefile | 16 ++ .../unity/test/pcap/lookupdev/lookupdev.c | 23 ++ source/tools/monitor/unity/test/readlink.lua | 14 + .../tools/monitor/unity/tools/nsping/nsping.c | 96 +++++++ 31 files changed, 1080 insertions(+), 73 deletions(-) create mode 100644 source/tools/monitor/unity/collector/plugin/virtiostat/Makefile create mode 100644 source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.bpf.c create mode 100644 source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c create mode 100644 source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.h create mode 100644 source/tools/monitor/unity/collector/postEngine/execDiag.lua create mode 100644 source/tools/monitor/unity/common/exec.lua create mode 100644 source/tools/monitor/unity/test/curl/postDiag.lua create mode 100644 source/tools/monitor/unity/test/lab/tcpServer/cli.py create mode 100644 source/tools/monitor/unity/test/lab/tcpServer/serv.py create mode 100644 source/tools/monitor/unity/test/pcap/capLoop/Makefile create mode 100644 source/tools/monitor/unity/test/pcap/capLoop/capLoop.c create mode 100644 source/tools/monitor/unity/test/pcap/capOne/Makefile create mode 100644 source/tools/monitor/unity/test/pcap/capOne/capOne.c create mode 100644 source/tools/monitor/unity/test/pcap/caps/Makefile create mode 100644 source/tools/monitor/unity/test/pcap/caps/caps.c create mode 100644 source/tools/monitor/unity/test/pcap/dump/Makefile create mode 100644 source/tools/monitor/unity/test/pcap/dump/dump.c create mode 100644 source/tools/monitor/unity/test/pcap/filter/Makefile create mode 100644 source/tools/monitor/unity/test/pcap/filter/filter.c create mode 100644 source/tools/monitor/unity/test/pcap/lookupdev/Makefile create mode 100644 source/tools/monitor/unity/test/pcap/lookupdev/lookupdev.c create mode 100644 source/tools/monitor/unity/test/readlink.lua create mode 100644 source/tools/monitor/unity/tools/nsping/nsping.c diff --git a/source/tools/monitor/unity/collector/execEngine/forkRun.lua b/source/tools/monitor/unity/collector/execEngine/forkRun.lua index 5ca34e9d..0e6c0f6a 100644 --- a/source/tools/monitor/unity/collector/execEngine/forkRun.lua +++ b/source/tools/monitor/unity/collector/execEngine/forkRun.lua @@ -11,37 +11,17 @@ --- require("common.class") -local unistd = require("posix.unistd") +local exec = require("common.exec") local pwait = require("posix.sys.wait") -local signal = require("posix.signal") local pystring = require("common.pystring") local system = require("common.system") local CvProc = require("collector.vproc") local CforkRun = class("execBase", CvProc) -local function run(cmd, args) - local pid, err = unistd.fork() - if pid > 0 then -- for self - return pid - elseif pid == 0 then -- for child - local errno - prctl_death_kill() - _, err, errno = unistd.exec(cmd, args) - assert(not errno, "exec failed." .. err .. errno) - else - error("fork report" .. err) - end -end - -local function kill(pid) - signal.kill(pid, signal.SIGKILL) -- force to kill task - pwait.wait(pid) -- wait task -end - function CforkRun:_init_(opts, proto, pffi, mnt) CvProc._init_(self, proto, pffi, mnt) - self._pid = run(opts.cmd, opts.args) + self._pid = exec.run(opts.cmd, opts.args) self._opts = opts print("fork run: ", self._pid) @@ -49,7 +29,7 @@ end function CforkRun:_del_() if self._pid then - kill(self._pid) + exec.kill(self._pid) end print("kill " .. self._pid) end @@ -65,9 +45,4 @@ function CforkRun:proc(elapsed, lines) end end -local function kill(pid) - signal.kill(pid, signal.SIGKILL) -- force to kill task - pwait.wait(pid) -- wait task -end - return CforkRun diff --git a/source/tools/monitor/unity/collector/plugin/Makefile b/source/tools/monitor/unity/collector/plugin/Makefile index 1523bc19..7efdddc1 100644 --- a/source/tools/monitor/unity/collector/plugin/Makefile +++ b/source/tools/monitor/unity/collector/plugin/Makefile @@ -5,7 +5,7 @@ OBJS := proto_sender.o LIB := libproto_sender.a -DEPMOD=sample threads kmsg proc_schedstat proc_loadavg unity_nosched unity_irqoff cpudist cpu_bled net_health net_retrans netlink cpufreq gpuinfo pmu_events virtout sum_retrans +DEPMOD=sample threads kmsg proc_schedstat proc_loadavg unity_nosched unity_irqoff cpudist cpu_bled net_health net_retrans netlink cpufreq gpuinfo pmu_events virtout sum_retrans virtiostat all: $(LIB) $(DEPMOD) diff --git a/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c b/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c index 415a9cfb..49b2225c 100644 --- a/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c +++ b/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c @@ -91,7 +91,6 @@ int call(int t, struct unity_lines *lines) { for (i = 0; i < NET_RETRANS_TYPE_MAX; i ++) { unity_set_value(line, i, net_title[i], values[i]); } - return 0; } @@ -101,9 +100,7 @@ void deinit(void) DESTORY_SKEL_BOJECT(net_retrans); } -#define LOG_MAX 256 -static char log[LOG_MAX]; - +#define LOG_MAX 1024 static int transIP(unsigned long lip, char *result, int size) { inet_ntop(AF_INET, (void *) &lip, result, size); return 0; @@ -169,6 +166,7 @@ static const char * resetActive(int stack_fd, struct data_t *e){ int proc(int stack_fd, struct data_t *e, struct unity_line *line) { char sip[32]; char dip[32]; + char log[LOG_MAX]; transIP(e->ip_src, sip, 32); transIP(e->ip_dst, dip, 32); @@ -182,30 +180,30 @@ int proc(int stack_fd, struct data_t *e, struct unity_line *line) { case NET_RETRANS_TYPE_SYN: case NET_RETRANS_TYPE_SYN_ACK: { - char buf[LOG_MAX - 1]; - snprintf(buf, LOG_MAX - 1, "rcv_nxt:%u, rcv_wup:%u, snd_nxt:%u, snd_una:%u, copied_seq:%u, " + char buf[LOG_MAX/2]; + snprintf(buf, LOG_MAX/2, "rcv_nxt:%u, rcv_wup:%u, snd_nxt:%u, snd_una:%u, copied_seq:%u, " "snd_wnd:%u, rcv_wnd:%u, lost_out:%u, packets_out:%u, retrans_out:%u, " "sacked_out:%u, reordering:%u", e->rcv_nxt, e->rcv_wup, e->snd_nxt, e->snd_una, e->copied_seq, e->snd_wnd, e->rcv_wnd, e->lost_out, e->packets_out, e->retrans_out, e->sacked_out, e->reordering ); - strncat(log, buf, LOG_MAX -1); + strncat(log, buf, LOG_MAX - 1 - strlen(log)); } break; case NET_RETRANS_TYPE_RST: - strncat(log, "noport", LOG_MAX - 1); + strncat(log, "noport", LOG_MAX - 1 - strlen(log)); break; case NET_RETRANS_TYPE_RST_SK: { const char *type = resetSock(stack_fd, e); - strncat(log, type, LOG_MAX - 1); + strncat(log, type, LOG_MAX - 1 - strlen(log)); } break; case NET_RETRANS_TYPE_RST_ACTIVE: { const char *type = resetActive(stack_fd, e); - strncat(log, type, LOG_MAX - 1); + strncat(log, type, LOG_MAX - 1 - strlen(log)); } break; default: diff --git a/source/tools/monitor/unity/collector/plugin/sum_retrans/sum_retrans.c b/source/tools/monitor/unity/collector/plugin/sum_retrans/sum_retrans.c index ea11ba99..61c9537b 100644 --- a/source/tools/monitor/unity/collector/plugin/sum_retrans/sum_retrans.c +++ b/source/tools/monitor/unity/collector/plugin/sum_retrans/sum_retrans.c @@ -27,6 +27,7 @@ int init(void *arg) return ret; } +#define BUFF_SIZE 64 #define LOG_MAX 4096 static char log[LOG_MAX]; @@ -37,7 +38,7 @@ static int transIP(unsigned long lip, char *result, int size) { static void pack_dip() { char ips[32]; - char buff[64]; + char buff[BUFF_SIZE]; unsigned long value; unsigned int ip, ip_next; @@ -48,8 +49,8 @@ static void pack_dip() { ip = ip_next; transIP(ip, ips, 32); - snprintf(buff, 64, "%s:%ld,", ips, value); - strncat(log, buff, 4096); + snprintf(buff, BUFF_SIZE, "%s:%ld,", ips, value); + strncat(log, buff, LOG_MAX - 1 - strlen(log)); ip = ip_next; } @@ -61,7 +62,7 @@ static void pack_dip() { } static void pack_inum() { - char buff[64]; + char buff[BUFF_SIZE]; unsigned long value; unsigned int inum, inum_next; @@ -70,8 +71,8 @@ static void pack_inum() { inum = 0; while (coobpf_key_next(inum_fd, &inum, &inum_next) == 0) { bpf_map_lookup_elem(inum_fd, &inum_next, &value); - snprintf(buff, 64, "%u:%ld,", inum_next, value); - strncat(log, buff, 4096); + snprintf(buff, BUFF_SIZE, "%u:%ld,", inum_next, value); + strncat(log, buff, LOG_MAX - 1 - strlen(log)); inum = inum_next; } diff --git a/source/tools/monitor/unity/collector/plugin/virtiostat/Makefile b/source/tools/monitor/unity/collector/plugin/virtiostat/Makefile new file mode 100644 index 00000000..51b1e9da --- /dev/null +++ b/source/tools/monitor/unity/collector/plugin/virtiostat/Makefile @@ -0,0 +1,8 @@ + +newdirs := $(shell find ./ -type d) + +bpfsrcs := virtiostat.bpf.c +csrcs := virtiostat.c +so := libvirtiostat.so + +include ../bpfso.mk \ No newline at end of file diff --git a/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.bpf.c b/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.bpf.c new file mode 100644 index 00000000..de54bb42 --- /dev/null +++ b/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.bpf.c @@ -0,0 +1,105 @@ +// +// Created by 廖肇燕 on 2023/5/7. +// + +#define BPF_NO_GLOBAL_DATA + +#include +#include +#include "virtiostat.h" + +BPF_HASH(stats, u64, virtio_stat_t, 256); + +struct virtio_device_id { + u32 device; + u32 vendor; +}; + +struct virtio_device { + int index; + bool failed; + bool config_enabled; + bool config_change_pending; + spinlock_t config_lock; + struct device dev; + struct virtio_device_id id; + void *config; + void *vringh_config; + struct list_head vqs; + u64 features; + void *priv; +}; + +struct virtqueue { + struct list_head list; + void (*callback)(struct virtqueue *vq); + const char *name; + struct virtio_device *vdev; + unsigned int index; + unsigned int num_free; + void *priv; +}; + +static inline void add_value(virtio_stat_t *vs, struct scatterlist **sgs, + u32 out_sgs, u32 in_sgs) { + vs->out_sgs += out_sgs; + vs->in_sgs += in_sgs; +} + +static inline void record(struct virtqueue *vq, struct scatterlist **sgs, + unsigned int out_sgs, unsigned int in_sgs) +{ + virtio_stat_t newvs = {0}; + virtio_stat_t *vs; + u64 key = (u64)vq; + + vs = bpf_map_lookup_elem(&stats, &key); + if (!vs) { + bpf_probe_read_kernel_str(newvs.driver, sizeof(newvs.driver), BPF_CORE_READ(vq, vdev, dev.driver, name)); + bpf_probe_read_kernel_str(newvs.dev, sizeof(newvs.dev), BPF_CORE_READ(vq, vdev, dev.kobj.name)); + bpf_probe_read_kernel_str(newvs.vqname, sizeof(newvs.vqname), BPF_CORE_READ(vq, name)); + + add_value(&newvs, sgs, out_sgs, in_sgs); + bpf_map_update_elem(&stats, &key, &newvs, BPF_ANY); + } else { + add_value(vs, sgs, out_sgs, in_sgs); + } +} + +SEC("kprobe/virtqueue_add_sgs") +int trace_virtqueue_add_sgs(struct pt_regs *ctx) +{ + struct virtqueue *vq = (struct virtqueue *)PT_REGS_PARM1(ctx); + struct scatterlist **sgs = (struct scatterlist **)PT_REGS_PARM2(ctx); + u32 out_sgs = PT_REGS_PARM3(ctx); + u32 in_sgs = PT_REGS_PARM4(ctx); + record(vq, sgs, out_sgs, in_sgs); + return 0; +} + +SEC("kprobe/virtqueue_add_outbuf") +int trace_virtqueue_add_outbuf(struct pt_regs *ctx) +{ + struct virtqueue *vq = (struct virtqueue *)PT_REGS_PARM1(ctx); + struct scatterlist **sgs = (struct scatterlist **)PT_REGS_PARM2(ctx); + record(vq, sgs, 1, 0); + return 0; +} + +SEC("kprobe/virtqueue_add_inbuf") +int trace_virtqueue_add_inbuf(struct pt_regs *ctx) +{ + struct virtqueue *vq = (struct virtqueue *)PT_REGS_PARM1(ctx); + struct scatterlist **sgs = (struct scatterlist **)PT_REGS_PARM2(ctx); + record(vq, sgs, 0, 1); + return 0; +} + +SEC("kprobe/virtqueue_add_inbuf_ctx") +int trace_virtqueue_add_inbuf_ctx(struct pt_regs *ctx) +{ + struct virtqueue *vq = (struct virtqueue *)PT_REGS_PARM1(ctx); + struct scatterlist **sgs = (struct scatterlist **)PT_REGS_PARM2(ctx); + record(vq, sgs, 0, 1); + return 0; +} diff --git a/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c b/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c new file mode 100644 index 00000000..34e4e069 --- /dev/null +++ b/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c @@ -0,0 +1,43 @@ +// +// Created by 廖肇燕 on 2023/5/7. +// + +#include "virtiostat.h" +#include "../bpf_head.h" +#include "virtiostat.skel.h" + +DEFINE_SEKL_OBJECT(virtiostat); +static int stats_fd = 0; + +int init(void *arg) +{ + int ret; + printf("virtiostat plugin install.\n"); + ret = LOAD_SKEL_OBJECT(virtiostat, perf); + stats_fd = coobpf_map_find(virtiostat->obj, "stats"); + return ret; +} + +void walk_virtio(void) { + unsigned long key, next; + struct virtio_stat stat; + + key = 0; + while (coobpf_key_next(stats_fd, &key, &next) == 0) { + bpf_map_lookup_elem(stats_fd, &next, &stat); + key = next; + printf("driver:%s dev:%s, name:%s, in:%d, out:%d\n", stat.driver, stat.dev, stat.vqname, stat.in_sgs, stat.out_sgs); + } +} + +int call(int t, struct unity_lines *lines) { + printf("call 2.\n"); + walk_virtio(); + return 0; +} + +void deinit(void) +{ + printf("virtiostat plugin uninstall.\n"); + DESTORY_SKEL_BOJECT(virtiostat); +} diff --git a/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.h b/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.h new file mode 100644 index 00000000..2e037b85 --- /dev/null +++ b/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.h @@ -0,0 +1,18 @@ +// +// Created by 廖肇燕 on 2023/5/7. +// + +#ifndef UNITY_VIRTIOSTAT_H +#define UNITY_VIRTIOSTAT_H + +#define CMPMAX 16 + +typedef struct virtio_stat { + char driver[CMPMAX]; + char dev[12]; + char vqname[12]; + unsigned int in_sgs; + unsigned int out_sgs; +} virtio_stat_t; + +#endif //UNITY_VIRTIOSTAT_H diff --git a/source/tools/monitor/unity/collector/plugin/virtout/virtout.c b/source/tools/monitor/unity/collector/plugin/virtout/virtout.c index e3b32702..c05cc95b 100644 --- a/source/tools/monitor/unity/collector/plugin/virtout/virtout.c +++ b/source/tools/monitor/unity/collector/plugin/virtout/virtout.c @@ -214,11 +214,11 @@ int proc(int stack_fd, struct data_t *e, struct unity_line *line) { if (addr[i] > 0) { cell = ksym_search(addr[i]); if (cell != NULL) { - strncat(log, cell->func, LOG_MAX); + strncat(log, cell->func, LOG_MAX - 1 - strlen(log)); } else { - strncat(log, "!nil", LOG_MAX); + strncat(log, "!nil", LOG_MAX - - 1 - strlen(log)); } - strncat(log, ",", LOG_MAX); + strncat(log, ",", LOG_MAX - 1 - strlen(log)); } else { break; } diff --git a/source/tools/monitor/unity/collector/postEngine/engine.lua b/source/tools/monitor/unity/collector/postEngine/engine.lua index c3de1f82..ab58d07a 100644 --- a/source/tools/monitor/unity/collector/postEngine/engine.lua +++ b/source/tools/monitor/unity/collector/postEngine/engine.lua @@ -15,11 +15,16 @@ local CexecBase = require("collector.postEngine.execBase") local Cengine = class("engine", CvProto) +local diagExec = { + io_hang = {block = 60, time = 15, cmd = "../../../iosdiag"} +} + function Cengine:_init_(que, proto_q, fYaml, tid) CvProto._init_(self, CprotoData.new(que)) self._fYaml = fYaml self._tid = tid self._task = nil + self._diags = {} end function Cengine:setTask(taskMons) @@ -40,6 +45,20 @@ function Cengine:pushTask(e, msgs) local second = res.second or 1 local exec = CexecBase.new(execCmd, args, second) exec:addEvents(e) + elseif cmd == "diag" then + cmd = res.exec + local diag = diagExec[cmd] + if diag then + if self._diags[cmd] then + print("cmd " .. cmd .. " is blocking.") + else + local args = res.args + local second = res.second or diag.time + local exec = CexecBase.new(diag.cmd, args, second) + exec:addEvents(e) + self._diags[cmd] = diag.block + end + end end end end @@ -56,11 +75,26 @@ function Cengine:proc(t, event, msgs) self:pushTask(event, msgs) end +function Cengine:checkDiag() + local toDel = {} + for k, v in pairs(self._diags) do + if v > 0 then + self._diags[k] = v - 1 + else + table.insert(toDel, k) + end + end + for _, k in ipairs(toDel) do + self._diags[k] = nil + end +end + function Cengine:work(t, event) local msgs = postQue.pull() if msgs then self:proc(t, event, msgs) end + self:checkDiag() end return Cengine diff --git a/source/tools/monitor/unity/collector/postEngine/execBase.lua b/source/tools/monitor/unity/collector/postEngine/execBase.lua index 59c3ac40..0e8b497f 100644 --- a/source/tools/monitor/unity/collector/postEngine/execBase.lua +++ b/source/tools/monitor/unity/collector/postEngine/execBase.lua @@ -5,47 +5,26 @@ --- require("common.class") -local unistd = require("posix.unistd") +local exec = require("common.exec") local pwait = require("posix.sys.wait") -local signal = require("posix.signal") local pystring = require("common.pystring") local system = require("common.system") local CexecBase = class("execBase") local interval = 5 --- poll for every 5 second -local function run(cmd, args) - local pid, err = unistd.fork() - if pid > 0 then -- for self - print("pid: " .. pid) - return pid - elseif pid == 0 then -- for child - local errno - prctl_death_kill() - _, err, errno = unistd.exec(cmd, args) - assert(not errno, "exec failed." .. err .. errno) - else - error("fork report" .. err) - end -end - function CexecBase:_init_(cmd, args, seconds) self.cmd = cmd self._cnt = 0 self._loop = seconds / interval - self._pid = run(cmd, args) + self._pid = exec.run(cmd, args) end function CexecBase:addEvents(e) e:addEvent(self.cmd, self, interval, true, self._loop) end -local function kill(pid) - signal.kill(pid, signal.SIGKILL) -- force to kill task - pwait.wait(pid) -- wait task -end - function CexecBase:work() local cnt = self._cnt if cnt >= self._loop then @@ -55,7 +34,7 @@ function CexecBase:work() end if not exit then -- process not exit print("force to kill " .. self._pid) - kill(self._pid) + exec.kill(self._pid) end return -1 -- delete from task list. end diff --git a/source/tools/monitor/unity/collector/postEngine/execDiag.lua b/source/tools/monitor/unity/collector/postEngine/execDiag.lua new file mode 100644 index 00000000..d5c89386 --- /dev/null +++ b/source/tools/monitor/unity/collector/postEngine/execDiag.lua @@ -0,0 +1,10 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/5/3 23:04 +--- + +require("common.class") +local unistd = require("posix.unistd") +local pwait = require("posix.sys.wait") +local signal = require("posix.signal") \ No newline at end of file diff --git a/source/tools/monitor/unity/common/exec.lua b/source/tools/monitor/unity/common/exec.lua new file mode 100644 index 00000000..ad98d639 --- /dev/null +++ b/source/tools/monitor/unity/common/exec.lua @@ -0,0 +1,33 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/5/4 10:50 +--- + +local unistd = require("posix.unistd") +local pwait = require("posix.sys.wait") +local signal = require("posix.signal") + +local module = {} + +function module.run(cmd, args) + local pid, err = unistd.fork() + if pid > 0 then -- for self + print("pid: " .. pid) + return pid + elseif pid == 0 then -- for child + local errno + prctl_death_kill() + _, err, errno = unistd.exec(cmd, args) + assert(not errno, "exec failed." .. err .. errno) + else + error("fork report" .. err) + end +end + +function module.kill(pid) + signal.kill(pid, signal.SIGKILL) -- force to kill task + pwait.wait(pid) -- wait task +end + +return module diff --git a/source/tools/monitor/unity/httplib/coCli.lua b/source/tools/monitor/unity/httplib/coCli.lua index 0f352ffb..327c111d 100644 --- a/source/tools/monitor/unity/httplib/coCli.lua +++ b/source/tools/monitor/unity/httplib/coCli.lua @@ -68,7 +68,8 @@ function CcoCli:coQueFunc(cli, cffi, efd) end body = nil else --> other stat, only record - print("http stat:", stat, enumStat.connected) + ok = nil + --print("http stat:", stat, enumStat.connected) end end end diff --git a/source/tools/monitor/unity/test/curl/postDiag.lua b/source/tools/monitor/unity/test/curl/postDiag.lua new file mode 100644 index 00000000..eaa62132 --- /dev/null +++ b/source/tools/monitor/unity/test/curl/postDiag.lua @@ -0,0 +1,15 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/5/4 13:21 +--- + +package.path = package.path .. ";../../?.lua;" +local system = require("common.system") +local ChttpCli = require("httplib.httpCli") + +local cli = ChttpCli.new() +local url = "http://127.0.0.1:8400/api/trig" +local req = {cmd = "diag", exec = "io_hang", args = {"hangdetect", "vda"}} +local res = cli:postTable(url, req) +system:dumps(res) \ No newline at end of file diff --git a/source/tools/monitor/unity/test/lab/tcpServer/cli.py b/source/tools/monitor/unity/test/lab/tcpServer/cli.py new file mode 100644 index 00000000..f1c9da80 --- /dev/null +++ b/source/tools/monitor/unity/test/lab/tcpServer/cli.py @@ -0,0 +1,11 @@ +from socket import socket, AF_INET, SOCK_STREAM + + +def cli(): + s = socket(AF_INET, SOCK_STREAM) + s.connect(('172.16.0.129', 4321)) + s.send(b'Hello') + + +if __name__ == '__main__': + cli() diff --git a/source/tools/monitor/unity/test/lab/tcpServer/serv.py b/source/tools/monitor/unity/test/lab/tcpServer/serv.py new file mode 100644 index 00000000..f22527a7 --- /dev/null +++ b/source/tools/monitor/unity/test/lab/tcpServer/serv.py @@ -0,0 +1,28 @@ +from socketserver import BaseRequestHandler, TCPServer +from nsenter import Namespace +import socket + + +def getIp(): + hostname = socket.gethostname() + print(hostname) + return socket.gethostbyname(hostname) + + +class EchoHandler(BaseRequestHandler): + def handle(self): + print('Got connection from', self.client_address) + with Namespace('/var/run/netns/tang1', 'net'): + print(getIp()) + while True: + msg = self.request.recv(8192) + print(msg) + if not msg: + break + self.request.send(msg) + + +if __name__ == '__main__': + print(getIp()) + serv = TCPServer(('', 4321), EchoHandler) + serv.serve_forever() diff --git a/source/tools/monitor/unity/test/pcap/capLoop/Makefile b/source/tools/monitor/unity/test/pcap/capLoop/Makefile new file mode 100644 index 00000000..07c38d34 --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/capLoop/Makefile @@ -0,0 +1,16 @@ +CC := gcc +CFLAG := -g +LDFLAG := -g -lpcap +OBJS = capLoop.o +EXEC = capLoop + +all: $(EXEC) + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAG) + +$(EXEC): $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAG) + +clean: + rm -f *.o $(EXEC) \ No newline at end of file diff --git a/source/tools/monitor/unity/test/pcap/capLoop/capLoop.c b/source/tools/monitor/unity/test/pcap/capLoop/capLoop.c new file mode 100644 index 00000000..66fc2309 --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/capLoop/capLoop.c @@ -0,0 +1,58 @@ +// +// Created by 廖肇燕 on 2023/5/4. +// + +#include +#include +#include +#include + + +void processPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet) +{ + int *count = (int *)arg; + + printf("Packet Count: %d\n", ++(*count)); + printf("Received Packet Size: %d\n", pkthdr->len); + printf("Payload:\n"); + + for(int i=0; i < pkthdr->len; ++i) + { + printf("%02x ", packet[i]); + if ((i + 1) % 16 == 0) + { + printf("\n"); + } + } + printf("\n\n"); + return; +} + +int main() +{ + char errBuf[PCAP_ERRBUF_SIZE], * devStr; + + devStr = pcap_lookupdev(errBuf); + if (devStr) + printf("success: device: %s\n", devStr); + else + { + printf("error: %s\n", errBuf); + exit(1); + } + + /* open a device, wait until a packet arrives */ + pcap_t * device = pcap_open_live(devStr, 64, 1, 0, errBuf); + if (!device) + { + printf("error: pcap_open_live(): %s\n", errBuf); + exit(1); + } + + int count = 0; + /*Loop forever & call processPacket() for every received packet.*/ + pcap_loop(device, 30, processPacket, (u_char *)&count); + + pcap_close(device); + return 0; +} diff --git a/source/tools/monitor/unity/test/pcap/capOne/Makefile b/source/tools/monitor/unity/test/pcap/capOne/Makefile new file mode 100644 index 00000000..76ac043e --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/capOne/Makefile @@ -0,0 +1,16 @@ +CC := gcc +CFLAG := -g +LDFLAG := -g -lpcap +OBJS = capOne.o +EXEC = capOne + +all: $(EXEC) + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAG) + +$(EXEC): $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAG) + +clean: + rm -f *.o $(EXEC) \ No newline at end of file diff --git a/source/tools/monitor/unity/test/pcap/capOne/capOne.c b/source/tools/monitor/unity/test/pcap/capOne/capOne.c new file mode 100644 index 00000000..c1a59b41 --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/capOne/capOne.c @@ -0,0 +1,56 @@ +// +// Created by 廖肇燕 on 2023/5/4. +// + +#include +#include +#include +#include + +int main() +{ + char errBuf[PCAP_ERRBUF_SIZE], * devStr; + + devStr = pcap_lookupdev(errBuf); + if (devStr) + printf("success: device: %s\n", devStr); + else + { + printf("error: %s\n", errBuf); + exit(1); + } + + /* open a device, wait until a packet arrives */ + pcap_t * device = pcap_open_live(devStr, 65536, 1, 1000, errBuf); + if (!device) + { + printf("error: pcap_open_live(): %s\n", errBuf); + exit(1); + } + + /* wait a packet to arrive */ + struct pcap_pkthdr packet; + const u_char * pktStr = pcap_next(device, &packet); + + if (!pktStr) + { + printf("did not capture a packet!\n"); + exit(1); + } + + printf("Packet len:%d, Bytes:%d, Received time:%s\n", packet.len, + packet.caplen, ctime((const time_t *)&packet.ts.tv_sec)); + + for(int i=0; i < packet.len; ++i) + { + printf(" %02x", pktStr[i]); + if ((i + 1) % 16 == 0) + { + printf("\n"); + } + } + printf("\n"); + + pcap_close(device); + return 0; +} diff --git a/source/tools/monitor/unity/test/pcap/caps/Makefile b/source/tools/monitor/unity/test/pcap/caps/Makefile new file mode 100644 index 00000000..fc90df5e --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/caps/Makefile @@ -0,0 +1,16 @@ +CC := gcc +CFLAG := -g +LDFLAG := -g -lpcap +OBJS = caps.o +EXEC = caps + +all: $(EXEC) + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAG) + +$(EXEC): $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAG) + +clean: + rm -f $(SO) $(EXEC) \ No newline at end of file diff --git a/source/tools/monitor/unity/test/pcap/caps/caps.c b/source/tools/monitor/unity/test/pcap/caps/caps.c new file mode 100644 index 00000000..6ffb2328 --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/caps/caps.c @@ -0,0 +1,257 @@ +// +// Created by 廖肇燕 on 2023/5/4. +// + +#include +#include +#include +#include + +#define u_char unsigned char +#define u_short unsigned short +#define u_int unsigned int +#define uint16_t unsigned short int + +/* 以太网帧头部 */ +struct sniff_ethernet { +#define ETHER_ADDR_LEN 6 + u_char ether_dhost[ETHER_ADDR_LEN]; /* 目的主机的地址 */ + u_char ether_shost[ETHER_ADDR_LEN]; /* 源主机的地址 */ + u_short ether_type; /* IP:0x0800;IPV6:0x86DD; ARP:0x0806;RARP:0x8035 */ +}; +#define ETHERTYPE_IPV4 (0x0800) +#define ETHERTYPE_IPV6 (0x86DD) +#define ETHERTYPE_ARP (0x0806) +#define ETHERTYPE_RARP (0x8035) + + +/* IP数据包的头部 */ +struct sniff_ip { +#if BYTE_ORDER == LITTLE_ENDIAN + u_int ip_hl:4, /* 头部长度 */ + ip_v:4; /* 版本号 */ +#if BYTE_ORDER == BIG_ENDIAN + u_int ip_v:4, /* 版本号 */ + ip_hl:4; /* 头部长度 */ +#endif +#endif /* not _IP_VHL */ + u_char ip_tos; /* 服务的类型 */ + u_short ip_len; /* 总长度 */ + u_short ip_id; /*包标志号 */ + u_short ip_off; /* 碎片偏移 */ +#define IP_RF 0x8000 /* 保留的碎片标志 */ +#define IP_DF 0x4000 /* dont fragment flag */ +#define IP_MF 0x2000 /* 多碎片标志*/ +#define IP_OFFMASK 0x1fff /*分段位 */ + u_char ip_ttl; /* 数据包的生存时间 */ + u_char ip_p; /* 所使用的协议:1 ICMP;2 IGMP;4 IP;6 TCP;17 UDP;89 OSPF */ + u_short ip_sum; /* 校验和 */ + struct in_addr ip_src,ip_dst; /* 源地址、目的地址*/ +}; +#define IPTYPE_ICMP (1) +#define IPTYPE_IGMP (2) +#define IPTYPE_IP (4) +#define IPTYPE_TCP (6) +#define IPTYPE_UDP (17) +#define IPTYPE_OSPF (89) + +typedef u_int tcp_seq; +/* TCP 数据包的头部 */ +struct sniff_tcp { + u_short th_sport; /* 源端口 */ + u_short th_dport; /* 目的端口 */ + tcp_seq th_seq; /* 包序号 */ + tcp_seq th_ack; /* 确认序号 */ +#if BYTE_ORDER == LITTLE_ENDIAN + u_int th_x2:4, /* 还没有用到 */ + th_off:4; /* 数据偏移 */ +#endif +#if BYTE_ORDER == BIG_ENDIAN + u_int th_off:4, /* 数据偏移*/ + th_x2:4; /*还没有用到 */ +#endif + u_char th_flags; +#define TH_FIN 0x01 +#define TH_SYN 0x02 +#define TH_RST 0x04 +#define TH_PUSH 0x08 +#define TH_ACK 0x10 +#define TH_URG 0x20 +#define TH_ECE 0x40 +#define TH_CWR 0x80 +#define TH_FLAGS (TH_FINTH_SYNTH_RSTTH_ACKTH_URGTH_ECETH_CWR) + u_short th_win; /* TCP滑动窗口 */ + u_short th_sum; /* 头部校验和 */ + u_short th_urp; /* 紧急服务位 */ +}; + + +/* UDP header */ +struct sniff_udp{ + uint16_t sport; /* source port */ + uint16_t dport; /* destination port */ + uint16_t udp_length; + uint16_t udp_sum; /* checksum */ +}; + +int pcap_protocal(const struct pcap_pkthdr *pkthdr, const u_char *packet,pcap_dumper_t* arg) +{ + pcap_dump((char *)arg, pkthdr, packet); + printf("packet size:%u, data len:%u\n", pkthdr->len, pkthdr->caplen); //数据包实际的长度, 抓到时的数据长度 + + struct sniff_ethernet *ethernet = (struct sniff_ethernet*)packet; + unsigned char* src_mac = ethernet->ether_shost; + unsigned char* dst_mac = ethernet->ether_dhost; + + printf("src_mac:%x:%x:%x:%x:%x:%x\n",src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]); + printf("dst_mac:%x:%x:%x:%x:%x:%x\n",dst_mac[0],dst_mac[1],dst_mac[2],dst_mac[3],dst_mac[4],dst_mac[5]); + printf("ether_type:%u\n",ethernet->ether_type); + + int eth_len = sizeof(struct sniff_ethernet); //以太网头的长度 + int ip_len = sizeof(struct sniff_ip); //ip头的长度 + int tcp_len = sizeof(struct sniff_tcp); //tcp头的长度 + int udp_len = sizeof(struct sniff_udp); //udp头的长度 + printf("eth_len: %d\n",eth_len); + printf("ip_len: %d\n",ip_len); + printf("tcp_len: %d\n",tcp_len); + printf("udp_len: %d\n",udp_len); + printf("/************************************/\n"); + + /*解析网络层 IP头*/ + //ntohs()是一个函数名,作用是将一个16位数由网络字节顺序转换为主机字节顺序。 + if(ntohs(ethernet->ether_type) == ETHERTYPE_IPV4) + { //IPV4 + printf("/**********************************************************************/\n"); + printf("It's IPv4!\n"); + struct sniff_ip* ip = (struct sniff_ip*)(packet + eth_len); + printf("ip->ip_hl:%d\n",ip->ip_hl); + printf("ip->ip_hl & 0x0f:%x\n",ip->ip_hl & 0x0f); + ip_len = (ip->ip_hl & 0x0f)*4; //ip头的长度 + printf("ip->ip_v:%d\n",ip->ip_v); + // printf("ip->ip_tos:%s\n",ip->ip_tos); + printf("ip->ip_len:%d\n",ip->ip_len); + // struct in_addr + // { + // in_addr_t s_addr; + // }; + unsigned char *saddr = (unsigned char*)&ip->ip_src.s_addr; //网络字节序转换成主机字节序 + unsigned char *daddr = (unsigned char*)&ip->ip_dst.s_addr; + + //printf("eth_len:%u ip_len:%u tcp_len:%u udp_len:%u\n", eth_len, ip_len, tcp_len, udp_len); + printf("src_ip:%d.%d.%d.%d\n", saddr[0], saddr[1],saddr[2],saddr[3]/*InttoIpv4str(saddr)*/); //源IP地址 + printf("dst_ip:%d.%d.%d.%d\n", daddr[0],daddr[1],daddr[2],daddr[3]/*InttoIpv4str(daddr)*/); //目的IP地址 + + /*解析传输层 TCP、UDP、ICMP*/ + + if(ip->ip_p == IPTYPE_TCP) + { //TCP + printf("ip->proto:TCP\n"); //传输层用的哪一个协议 + struct sniff_tcp* tcp = (struct sniff_tcp*)(packet + eth_len + ip_len); + printf("tcp_sport = %u\n", tcp->th_sport); + printf("tcp_dport = %u\n", tcp->th_dport); + for(int i=0;*(packet + eth_len + ip_len+tcp_len+i)!='\0';i++) + { + printf("%02x ",*(packet + eth_len + ip_len+tcp_len+i)); + } + + /**********(pcaket + eth_len + ip_len + tcp_len)就是TCP协议传输的正文数据了***********/ + } + else if(ip->ip_p == IPTYPE_UDP) + { //UDP + printf("ip->proto:UDP\n"); //传输层用的哪一个协议 + struct sniff_udp* udp = (struct sniff_udp*)(packet + eth_len + ip_len); + printf("udp_sport = %u\n", udp->sport); + printf("udp_dport = %u\n", udp->dport); + /**********(pcaket + eth_len + ip_len + udp_len)就是UDP协议传输的正文数据了***********/ + } + else if(ip->ip_p == IPTYPE_ICMP) + { //ICMP + printf("ip->proto:CCMP\n"); //传输层用的哪一个协议 + } + + } + else if(ntohs(ethernet->ether_type) == ETHERTYPE_IPV6) + { //IPV6 + printf("It's IPv6!\n"); + } + else{ + printf("既不是IPV4也不是IPV6\n"); + } + printf("============================================\n"); + return 0; +} + +int main() +{ + int ret32 = -1; + pcap_t *handle = NULL; /* 会话的句柄 */ + char *dev = "lo"; /* 执行嗅探的设备 */ + char errbuf[PCAP_ERRBUF_SIZE]; /* 存储错误 信息的字符串 */ + struct bpf_program filter; /*已经编译好的过滤表达式*/ + char filter_app[] = "port 7890"; /* 过滤表达式*/ + bpf_u_int32 mask; /* 执行嗅探的设备的网络掩码 */ + bpf_u_int32 net; /* 执行嗅探的设备的IP地址 */ + const u_char *packet; /* 实际的包 */ + struct pcap_pkthdr header; /* 由pcap.h定义 */ + struct in_addr ip_addr; + int pcapnum=0; + + /*开启支持PCAP的设备嗅探*/ + // dev = pcap_lookupdev(errbuf); + ret32 = pcap_lookupnet(dev, &net, &mask, errbuf);//获取指定设备的网络号与掩码,如果出错,返回-1,errbuf存放错误信息 + printf("Device: %s\n", dev); + if(ret32 < 0) + { + printf("pcap_lookupnet return %d, errbuf:%s\n", ret32, errbuf); + } + printf("sizeof(mask) = %d, mask:%#x, net:%#x\n",sizeof(mask), mask, net); + ip_addr.s_addr= net; + printf("ipaddress is :%s\n",inet_ntoa(ip_addr)); + + + handle = pcap_open_live(dev, 10*1024, 1, 0, errbuf); + // printf("handle = %s\n",handle); + //捕获网络数据包的数据包捕获描述字,打开名为DEV的网络设备,最大捕获字节为1024字节, + //将网络接口设定为混杂模式,超时时间为0ms意味着一直嗅探直到捕获到数据 + if(handle == NULL) + { + printf("pcap_open_live return err,errbuf:%s...\n", errbuf); + return -1; + } + + pcap_dumper_t* out_pcap; + out_pcap = pcap_dump_open(handle,"protocal.pcap"); + + while(1) + { + pcapnum++; + if(pcapnum>100) + { + break; + } + /* 截获一个包 */ + packet = pcap_next(handle, &header); + if(packet) + { + /* 打印它的长度 */ + printf("Jacked a packet with length of [%d]\n", header.len); + //数据包协议解析 + pcap_protocal(&header, packet,out_pcap); + } + else + { + printf("pcap_next return err, errbuf:%s\n", errbuf); + break; + } + } + + /*flush buff*/ + pcap_dump_flush(out_pcap); + + pcap_dump_close(out_pcap); + /* 关闭会话 */ + pcap_close(handle); + + return 0; +} + diff --git a/source/tools/monitor/unity/test/pcap/dump/Makefile b/source/tools/monitor/unity/test/pcap/dump/Makefile new file mode 100644 index 00000000..d29279e9 --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/dump/Makefile @@ -0,0 +1,16 @@ +CC := gcc +CFLAG := -g +LDFLAG := -g -lpcap +OBJS = dump.o +EXEC = dump + +all: $(EXEC) + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAG) + +$(EXEC): $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAG) + +clean: + rm -f *.o $(EXEC) \ No newline at end of file diff --git a/source/tools/monitor/unity/test/pcap/dump/dump.c b/source/tools/monitor/unity/test/pcap/dump/dump.c new file mode 100644 index 00000000..c6889d7b --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/dump/dump.c @@ -0,0 +1,85 @@ +// +// Created by 廖肇燕 on 2023/5/5. +// + +#include +#include +#include +#include + +static int pack_cnt = 0; + +void processPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet) +{ + pcap_dump(arg, pkthdr, packet); + pack_cnt ++; + return; +} + +int main(int argc, char * argv[]) { + int ret; + time_t hope; + char errBuf[PCAP_ERRBUF_SIZE], *devStr; + + if (argc < 2) { + printf("argc %d is less than 2\n", argc); + exit(1); + } + + devStr = pcap_lookupdev(errBuf); + if (devStr) + printf("success: device: %s\n", devStr); + else + { + printf("error: %s\n", errBuf); + exit(1); + } + + /* open a device, wait until a packet arrives */ + pcap_t * device = pcap_open_live(NULL, 96, 1, 1000, errBuf); + if (!device) + { + fprintf(stderr, "error: pcap_open_live(): %s\n", errBuf); + exit(1); + } + + struct bpf_program filter; +// ret = pcap_compile(device, &filter, "tcp and not port 22", 1, 0); + printf("filter: %s\n", argv[1]); + ret = pcap_compile(device, &filter, argv[1], 1, 0); + if (ret < 0) { + fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(device)); + exit(1); + } + ret = pcap_setfilter(device, &filter); + if (ret < 0) { + fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(device)); + exit(1); + } + + /*open pcap write output file*/ + pcap_dumper_t* out_pcap; + out_pcap = pcap_dump_open(device,"pack.pcap"); + + hope = 30 + time(NULL); + while (time(NULL) <= hope) { + int ret = pcap_dispatch(device, -1, processPacket, (u_char *)out_pcap); + if (ret == -1) { + fprintf(stderr, "pcap_dispatch: %s\n", pcap_geterr(device)); + break; + } + + if (ret == -2) { + printf("pcap_breakloop called\n"); + break; + } + } + printf("cap %d\n", pack_cnt); + + /*flush buff*/ + pcap_dump_flush(out_pcap); + + pcap_dump_close(out_pcap); + pcap_close(device); + return 0; +} diff --git a/source/tools/monitor/unity/test/pcap/filter/Makefile b/source/tools/monitor/unity/test/pcap/filter/Makefile new file mode 100644 index 00000000..d781bc08 --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/filter/Makefile @@ -0,0 +1,16 @@ +CC := gcc +CFLAG := -g +LDFLAG := -g -lpcap +OBJS = filter.o +EXEC = filter + +all: $(EXEC) + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAG) + +$(EXEC): $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAG) + +clean: + rm -f *.o $(EXEC) \ No newline at end of file diff --git a/source/tools/monitor/unity/test/pcap/filter/filter.c b/source/tools/monitor/unity/test/pcap/filter/filter.c new file mode 100644 index 00000000..98ede821 --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/filter/filter.c @@ -0,0 +1,63 @@ +// +// Created by 廖肇燕 on 2023/5/5. +// + +#include +#include +#include +#include + + +void processPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet) +{ + int *count = (int *)arg; + + printf("Packet Count: %d\n", ++(*count)); + printf("Received Packet Size: %d\n", pkthdr->len); + printf("Payload:\n"); + + for(int i=0; i < pkthdr->len; ++i) + { + printf("%02x ", packet[i]); + if ((i + 1) % 16 == 0) + { + printf("\n"); + } + } + printf("\n\n"); + return; +} + +int main() +{ + char errBuf[PCAP_ERRBUF_SIZE], * devStr; + + devStr = pcap_lookupdev(errBuf); + if (devStr) + printf("success: device: %s\n", devStr); + else + { + printf("error: %s\n", errBuf); + exit(1); + } + + /* open a device, wait until a packet arrives */ + pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf); + if (!device) + { + printf("error: pcap_open_live(): %s\n", errBuf); + exit(1); + } + + /* construct a filter */ + struct bpf_program filter; + pcap_compile(device, &filter, "tcp", 1, 0); + pcap_setfilter(device, &filter); + + int count = 0; + /*Loop forever & call processPacket() for every received packet.*/ + pcap_loop(device, 30, processPacket, (u_char *)&count); + + pcap_close(device); + return 0; +} diff --git a/source/tools/monitor/unity/test/pcap/lookupdev/Makefile b/source/tools/monitor/unity/test/pcap/lookupdev/Makefile new file mode 100644 index 00000000..9b8b54e7 --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/lookupdev/Makefile @@ -0,0 +1,16 @@ +CC := gcc +CFLAG := -g +LDFLAG := -g -lpcap +OBJS = lookupdev.o +EXEC = lookupdev + +all: $(EXEC) + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAG) + +$(EXEC): $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAG) + +clean: + rm -f $(SO) $(EXEC) \ No newline at end of file diff --git a/source/tools/monitor/unity/test/pcap/lookupdev/lookupdev.c b/source/tools/monitor/unity/test/pcap/lookupdev/lookupdev.c new file mode 100644 index 00000000..9a97b9a1 --- /dev/null +++ b/source/tools/monitor/unity/test/pcap/lookupdev/lookupdev.c @@ -0,0 +1,23 @@ +// +// Created by 廖肇燕 on 2023/5/4. +// + +#include +#include +#include + +int main() +{ + char errBuf[PCAP_ERRBUF_SIZE], * devStr; + + devStr = pcap_lookupdev(errBuf); + if (devStr) + printf("success: device: %s\n", devStr); + else + { + printf("error: %s\n", errBuf); + exit(1); + } + + return 0; +} diff --git a/source/tools/monitor/unity/test/readlink.lua b/source/tools/monitor/unity/test/readlink.lua new file mode 100644 index 00000000..d95f5838 --- /dev/null +++ b/source/tools/monitor/unity/test/readlink.lua @@ -0,0 +1,14 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/5/5 11:33 +--- + +local stat = require("posix.sys.stat") + +local res, err, errno = stat.stat("/proc/1/fd/36") +print(err, errno) +print(stat.S_ISSOCK(res.st_mode)) +for a, b in pairs(res) do + print(a, b) +end diff --git a/source/tools/monitor/unity/tools/nsping/nsping.c b/source/tools/monitor/unity/tools/nsping/nsping.c new file mode 100644 index 00000000..696dd6c1 --- /dev/null +++ b/source/tools/monitor/unity/tools/nsping/nsping.c @@ -0,0 +1,96 @@ +// +// Created by 廖肇燕 on 2023/4/29. +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ICMP_ECHOREPLY 0 +#define ICMP_ECHO + +#define BUFSIZE 1500 +#define DEFAULT_LEN 56 + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; + +struct icmphdr { + u8 type; + u8 code; + u16 checksum; + union{ + struct{ + u16 id; + u16 sequence; + }echo; + u32 gateway; + struct{ + u16 unsed; + u16 mtu; + }frag; + }un; + u8 data[0]; +#define icmp_id un.echo.id +#define icmp_seq un.echo.sequence +}; +#define ICMP_HSIZE sizeof(struct icmphdr) + +struct iphdr { + u8 hlen:4, ver:4; + u8 tos; + u16 tot_len; + u16 id; + u16 frag_off; + u8 ttl; + u8 protocol; + u16 check; + u32 saddr; + u32 daddr; +}; + +#define IP_HSIZE sizeof(struct iphdr) +#define IPVERSION 4 + +char *hostname; +int datalen = DEFAULT_LEN; +char sendbuf[BUFSIZE]; +char recvbuf[BUFSIZE]; +int nsent; +int nrecv; +pid_t pid; +struct timeval recvtime; +int sockfd; +struct sockaddr_in dest; +struct sockaddr_in from; +struct sigaction act_alarm; +struct sigaction act_int; + +void alarm_handler(int); +void int_handler(int); +void set_sighandler(); +void send_ping(); +void recv_reply(); +u16 checksum(u8 *buf, int len); +int handle_pkt(); +void bail(const char *); + + +struct itimerval val_alarm = { + .it_interval.tv_sec = 1, + .it_interval.tv_usec = 0, + .it_value.tv_sec = 0, + .it_value.tv_usec = 1 +}; + + -- Gitee From 8e6de008b0681190512b5f85bc7b42acb3be60cf Mon Sep 17 00:00:00 2001 From: liaozhaoyan Date: Wed, 10 May 2023 19:47:09 +0800 Subject: [PATCH 2/6] add netedge. --- .../tools/monitor/unity/beeQ/collectors.lua | 1 + source/tools/monitor/unity/collector/loop.lua | 4 +- .../tools/monitor/unity/collector/plugin.lua | 12 +- .../collector/plugin/virtiostat/virtiostat.c | 17 ++- .../unity/collector/plugin/virtout/virtout.c | 3 +- .../monitor/unity/collector/pluginManager.lua | 12 ++ .../unity/collector/postEngine/engine.lua | 38 ++++-- .../unity/collector/proc_snmp_stat.lua | 121 +++++++++++++++--- .../monitor/unity/test/curl/postDiag.lua | 3 +- .../tools/monitor/unity/tools/nsping/nsping.c | 96 -------------- 10 files changed, 169 insertions(+), 138 deletions(-) delete mode 100644 source/tools/monitor/unity/tools/nsping/nsping.c diff --git a/source/tools/monitor/unity/beeQ/collectors.lua b/source/tools/monitor/unity/beeQ/collectors.lua index 10206def..b083d7a6 100644 --- a/source/tools/monitor/unity/beeQ/collectors.lua +++ b/source/tools/monitor/unity/beeQ/collectors.lua @@ -133,6 +133,7 @@ function work(que, proto_q, yaml, tid) engine, unit = setupPostEngine(que, proto_q, fYaml, tid) engine:setTask(main.postPlugin.tasks) e:addEvent("postEngine", engine, unit) + engine:setMainloop(main) return e:proc() end diff --git a/source/tools/monitor/unity/collector/loop.lua b/source/tools/monitor/unity/collector/loop.lua index 8197b3b5..517dfb9a 100644 --- a/source/tools/monitor/unity/collector/loop.lua +++ b/source/tools/monitor/unity/collector/loop.lua @@ -30,7 +30,7 @@ function Cloop:_init_(que, proto_q, fYaml, tid) local jperiod = calcJiffies.calc(res.config.proc_path, procffi) -- self._guardSched = CguardSched.new(tid, self._procs, self._names, jperiod) - self._soPlugins = CpluginManager.new(procffi, proto_q, res, tid, jperiod) + self.soPlugins = CpluginManager.new(procffi, proto_q, res, tid, jperiod) self._guardStat = CguardSelfStat.new(self._proto, procffi, "/", res, jperiod) self.postPlugin = CpostPlugin.new(self._proto, procffi, res) end @@ -68,7 +68,7 @@ function Cloop:work(t) local lines = self._proto:protoTable() self._guardSched:proc(t, lines) - self._soPlugins:proc(t, lines) + self.soPlugins:proc(t, lines) self._guardStat:proc(t, lines) self.postPlugin:proc(t, lines) diff --git a/source/tools/monitor/unity/collector/plugin.lua b/source/tools/monitor/unity/collector/plugin.lua index a7590294..a37801f4 100644 --- a/source/tools/monitor/unity/collector/plugin.lua +++ b/source/tools/monitor/unity/collector/plugin.lua @@ -10,11 +10,12 @@ local dockerinfo = require("common.dockerinfo") local cjson = require("cjson.safe") local json = cjson.new() -function Cplugin:_init_(resYaml, ffi, proto_q, so) +function Cplugin:_init_(resYaml, ffi, proto_q, so, loop) self._ffi = ffi self._cffi = self._ffi.load(so) self._cffi.init(proto_q) self._so = so + self._loop = loop or -1 self.proc_fs = resYaml.config["proc_path"] or "/" self.fill_arg = {["podname"]="pid"} end @@ -103,6 +104,15 @@ function Cplugin:proc(t, lines) self:_proc(unity_lines, lines) end self._ffi.C.free(unity_lines.line) -- should free memory. + + local loop = self._loop + if loop > 0 then + loop = loop - 1 + if loop == 0 then + return -1 -- to remove + end + self._loop = loop + end end return Cplugin diff --git a/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c b/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c index 34e4e069..1b857566 100644 --- a/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c +++ b/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c @@ -18,21 +18,30 @@ int init(void *arg) return ret; } -void walk_virtio(void) { +#define BUF_MAX 128 +#define LOG_MAX 4096 +void walk_virtio(struct unity_lines *lines) { + struct unity_line *line; unsigned long key, next; struct virtio_stat stat; + char log[LOG_MAX] = {'\0'}; key = 0; while (coobpf_key_next(stats_fd, &key, &next) == 0) { + char buf[BUF_MAX]; bpf_map_lookup_elem(stats_fd, &next, &stat); key = next; - printf("driver:%s dev:%s, name:%s, in:%d, out:%d\n", stat.driver, stat.dev, stat.vqname, stat.in_sgs, stat.out_sgs); + snprintf(buf, BUF_MAX, "driver:%s,dev:%s,name:%s,in:%d,out:%d;", stat.driver, stat.dev, stat.vqname, stat.in_sgs, stat.out_sgs); + strncat(log, buf, LOG_MAX - 1 - strlen(log)); } + unity_alloc_lines(lines, 1); + line = unity_get_line(lines, 0); + unity_set_table(line, "virtios"); + unity_set_log(line, "log", log); } int call(int t, struct unity_lines *lines) { - printf("call 2.\n"); - walk_virtio(); + walk_virtio(lines); return 0; } diff --git a/source/tools/monitor/unity/collector/plugin/virtout/virtout.c b/source/tools/monitor/unity/collector/plugin/virtout/virtout.c index c05cc95b..b611b8de 100644 --- a/source/tools/monitor/unity/collector/plugin/virtout/virtout.c +++ b/source/tools/monitor/unity/collector/plugin/virtout/virtout.c @@ -199,10 +199,9 @@ void deinit(void) } #define LOG_MAX 4096 -static char log[LOG_MAX]; - int proc(int stack_fd, struct data_t *e, struct unity_line *line) { int i; + char log[LOG_MAX] = {'\0'}; unsigned long addr[128]; int id = e->stack_id; //last stack struct ksym_cell* cell; diff --git a/source/tools/monitor/unity/collector/pluginManager.lua b/source/tools/monitor/unity/collector/pluginManager.lua index 49431121..501fea88 100644 --- a/source/tools/monitor/unity/collector/pluginManager.lua +++ b/source/tools/monitor/unity/collector/pluginManager.lua @@ -8,6 +8,7 @@ require("common.class") local CpluginManager = class("pluginManager") local CguardSched = require("collector.guard.guardSched") local Cplugin = require("collector.plugin") +local system = require("common.system") function CpluginManager:_init_(procffi, proto_q, resYaml, tid, jperiod) local res = resYaml @@ -20,6 +21,9 @@ function CpluginManager:_init_(procffi, proto_q, resYaml, tid, jperiod) self._names = {} self:setup(res, proto_q) self._guardSched = CguardSched.new(tid, self._plugins, self._names, jperiod) + + self._resYaml = resYaml -- for add function + self._proto_q = proto_q end function CpluginManager:_del_() @@ -42,6 +46,14 @@ function CpluginManager:setup(resYaml, proto_q) end end +function CpluginManager:add(name, loops) + local pluginFFI = require("collector.native.plugincffi") + if not system:valueIsIn(self._names, name) then + table.insert(self._plugins, Cplugin.new(self._resYaml, pluginFFI, self._proto_q, name, loops)) + table.insert(self._names, name) + end +end + function CpluginManager:setProcSys(procFFI, config) local proc = config["proc_path"] or "/" local sys = config["sys_path"] or "/" diff --git a/source/tools/monitor/unity/collector/postEngine/engine.lua b/source/tools/monitor/unity/collector/postEngine/engine.lua index ab58d07a..29b14742 100644 --- a/source/tools/monitor/unity/collector/postEngine/engine.lua +++ b/source/tools/monitor/unity/collector/postEngine/engine.lua @@ -12,11 +12,11 @@ local pystring = require("common.pystring") local system = require("common.system") local cjson = require("cjson.safe") local CexecBase = require("collector.postEngine.execBase") - local Cengine = class("engine", CvProto) local diagExec = { - io_hang = {block = 60, time = 15, cmd = "../../../iosdiag"} + io_hang = {block = 60, time = 15, cmd = "../../../iosdiag"}, + net_edge = {block = 5 * 60, time = 60, so = {virtiostat = 5 * 3}}, } function Cengine:_init_(que, proto_q, fYaml, tid) @@ -27,10 +27,30 @@ function Cengine:_init_(que, proto_q, fYaml, tid) self._diags = {} end +function Cengine:setMainloop(main) + self._main = main +end + function Cengine:setTask(taskMons) self._task = taskMons end +function Cengine:run(e, res, diag) + local args = res.args + local second = res.second or diag.time + if diag.cmd then + local exec = CexecBase.new(diag.cmd, args, second) + exec:addEvents(e) + end + local so = diag.so + if so then + for plugin, loop in pairs(so) do + self._main.soPlugins:add(plugin, loop) + end + end + self._diags[res.exec] = diag.block +end + function Cengine:pushTask(e, msgs) local events = pystring:split(msgs, '\n') for _, msg in ipairs(events) do @@ -46,17 +66,13 @@ function Cengine:pushTask(e, msgs) local exec = CexecBase.new(execCmd, args, second) exec:addEvents(e) elseif cmd == "diag" then - cmd = res.exec - local diag = diagExec[cmd] + local exec = res.exec + local diag = diagExec[exec] if diag then - if self._diags[cmd] then - print("cmd " .. cmd .. " is blocking.") + if self._diags[exec] then + print("cmd " .. exec .. " is blocking.") else - local args = res.args - local second = res.second or diag.time - local exec = CexecBase.new(diag.cmd, args, second) - exec:addEvents(e) - self._diags[cmd] = diag.block + self:run(e, res, diag) end end end diff --git a/source/tools/monitor/unity/collector/proc_snmp_stat.lua b/source/tools/monitor/unity/collector/proc_snmp_stat.lua index 432db4a5..7d17261e 100644 --- a/source/tools/monitor/unity/collector/proc_snmp_stat.lua +++ b/source/tools/monitor/unity/collector/proc_snmp_stat.lua @@ -17,41 +17,120 @@ function CprocSnmpStat:_init_(proto, pffi, mnt, pFile) self._rec = nil end -function CprocSnmpStat:retransRate(titles, values) +local ipHeads = {"InReceives", "OutRequests"} +function CprocSnmpStat:ipCount(titles, values) + local vs = {} for i = 1, titles.no do local cell = self._ffi.string(titles.s[i]) - if cell == "OutSegs" then - self._outSegs = tonumber(values.value[i - 1]) - elseif cell == "RetransSegs" then - self._retransSegs = tonumber(values.value[i - 1]) + if cell == "Forwarding" then + table.insert(vs, {name = cell, value = tonumber(values.value[i - 1])}) + elseif system:valueIsIn(ipHeads, cell) then + local name = "v_ip" .. cell + local v = tonumber(values.value[i - 1]) + if self[name] then + table.insert(vs, {name = cell, value = v - self[name]}) + end + self[name] = v end end + self:appendLine(self:_packProto("net_ip_count", nil, vs)) +end - if self._lastOutSegs then - local segs = self._outSegs - self._lastOutSegs - local rate = 0 - local retrans = 0 - if segs > 0 then - retrans = self._retransSegs - self._lastRetransSegs - rate = (self._retransSegs - self._lastRetransSegs) / segs * 100.0 +local icmpHeads = {"InMsgs", "InErrors", "OutMsgs", "OutErrors"} +function CprocSnmpStat:icmpCount(titles, values) + local vs = {} + for i = 1, titles.no do + local cell = self._ffi.string(titles.s[i]) + if system:valueIsIn(icmpHeads, cell) then + local name = "v_icmp" .. cell + local v = tonumber(values.value[i - 1]) + if self[name] then + table.insert(vs, {name = cell, value = v - self[name]}) + end + self[name] = v end - local vs = { - { name = "rate", value = rate}, - { name = "segs", value = segs}, - { name = "retrans", value = retrans} - } - self:appendLine(self:_packProto("retrans", nil, vs)) end - self._lastOutSegs = self._outSegs - self._lastRetransSegs = self._retransSegs + if #vs > 0 then + self:appendLine(self:_packProto("net_icmp_count", nil, vs)) + end +end + +local udpHeads = {"InDatagrams", "InErrors", "OutDatagrams", + "RcvbufErrors", "SndbufErrors", "NoPorts"} +function CprocSnmpStat:udpCount(titles, values) + local vs = {} + for i = 1, titles.no do + local cell = self._ffi.string(titles.s[i]) + if system:valueIsIn(udpHeads, cell) then + local name = "v_udp" .. cell + local v = tonumber(values.value[i - 1]) + if self[name] then + table.insert(vs, {name = cell, value = v - self[name]}) + end + self[name] = v + end + end + if #vs > 0 then + self:appendLine(self:_packProto("net_udp_count", nil, vs)) + end +end + +local tcpHeads = {"InSegs", "OutMsgs", "RetransSegs", "InErrs", "CurrEstab"} +function CprocSnmpStat:tcpCount(titles, values) + local vs = {} + for i = 1, titles.no do + local cell = self._ffi.string(titles.s[i]) + if system:valueIsIn(tcpHeads, cell) then + local name = "v_tcp" .. cell + local v = tonumber(values.value[i - 1]) + if self[name] then + table.insert(vs, {name = cell, value = v - self[name]}) + end + self[name] = v + end + end + if #vs > 0 then + self:appendLine(self:_packProto("net_tcp_count", nil, vs)) + end +end + +local tcpExtHeads = {"TCPSynRetrans", "ListenDrops", "ListenOverflows", + "SyncookiesSent", "SyncookiesRecv", "SyncookiesFailed", + "ActiveOpens", "PassiveOpens"} +function CprocSnmpStat:tcpExtCount(titles, values) + local vs = {} + for i = 1, titles.no do + local cell = self._ffi.string(titles.s[i]) + if system:valueIsIn(tcpExtHeads, cell) then + local name = "v_tcpExt" .. cell + local v = tonumber(values.value[i - 1]) + if self[name] then + table.insert(vs, {name = cell, value = v - self[name]}) + end + self[name] = v + end + end + if #vs > 0 then + self:appendLine(self:_packProto("net_tcp_ext_count", nil, vs)) + end end function CprocSnmpStat:createTable(titles, values, now) local head = string.gsub(self._ffi.string(titles.s[0]), ":", "") assert(self._ffi.string(titles.s[0]), self._ffi.string(values.s)) + if head == "Tcp" then - self:retransRate(titles, values) + self:tcpCount(titles, values) + elseif head == "Udp" then + self:udpCount(titles, values) + elseif head == "Ip" then + self:ipCount(titles, values) + elseif head == "Icmp" then + self:icmpCount(titles, values) + elseif head == "TcpExt" then + self:tcpExtCount(titles, values) end + for i = 1, titles.no do local cell = self._ffi.string(titles.s[i]) local low = string.lower(cell) diff --git a/source/tools/monitor/unity/test/curl/postDiag.lua b/source/tools/monitor/unity/test/curl/postDiag.lua index eaa62132..bc175c71 100644 --- a/source/tools/monitor/unity/test/curl/postDiag.lua +++ b/source/tools/monitor/unity/test/curl/postDiag.lua @@ -10,6 +10,7 @@ local ChttpCli = require("httplib.httpCli") local cli = ChttpCli.new() local url = "http://127.0.0.1:8400/api/trig" -local req = {cmd = "diag", exec = "io_hang", args = {"hangdetect", "vda"}} +--local req = {cmd = "diag", exec = "io_hang", args = {"hangdetect", "vda"}} +local req = {cmd = "diag", exec = "net_edge", args = {"hangdetect", "vda"}} local res = cli:postTable(url, req) system:dumps(res) \ No newline at end of file diff --git a/source/tools/monitor/unity/tools/nsping/nsping.c b/source/tools/monitor/unity/tools/nsping/nsping.c deleted file mode 100644 index 696dd6c1..00000000 --- a/source/tools/monitor/unity/tools/nsping/nsping.c +++ /dev/null @@ -1,96 +0,0 @@ -// -// Created by 廖肇燕 on 2023/4/29. -// - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define ICMP_ECHOREPLY 0 -#define ICMP_ECHO - -#define BUFSIZE 1500 -#define DEFAULT_LEN 56 - -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned int u32; - -struct icmphdr { - u8 type; - u8 code; - u16 checksum; - union{ - struct{ - u16 id; - u16 sequence; - }echo; - u32 gateway; - struct{ - u16 unsed; - u16 mtu; - }frag; - }un; - u8 data[0]; -#define icmp_id un.echo.id -#define icmp_seq un.echo.sequence -}; -#define ICMP_HSIZE sizeof(struct icmphdr) - -struct iphdr { - u8 hlen:4, ver:4; - u8 tos; - u16 tot_len; - u16 id; - u16 frag_off; - u8 ttl; - u8 protocol; - u16 check; - u32 saddr; - u32 daddr; -}; - -#define IP_HSIZE sizeof(struct iphdr) -#define IPVERSION 4 - -char *hostname; -int datalen = DEFAULT_LEN; -char sendbuf[BUFSIZE]; -char recvbuf[BUFSIZE]; -int nsent; -int nrecv; -pid_t pid; -struct timeval recvtime; -int sockfd; -struct sockaddr_in dest; -struct sockaddr_in from; -struct sigaction act_alarm; -struct sigaction act_int; - -void alarm_handler(int); -void int_handler(int); -void set_sighandler(); -void send_ping(); -void recv_reply(); -u16 checksum(u8 *buf, int len); -int handle_pkt(); -void bail(const char *); - - -struct itimerval val_alarm = { - .it_interval.tv_sec = 1, - .it_interval.tv_usec = 0, - .it_value.tv_sec = 0, - .it_value.tv_usec = 1 -}; - - -- Gitee From e0abb4e7904287ddced49a8e8cac85e8e79a675e Mon Sep 17 00:00:00 2001 From: liaozhaoyan Date: Fri, 19 May 2023 14:49:11 +0800 Subject: [PATCH 3/6] fix bug for hist10 push. --- .../collector/plugin/cpudist/cpudist.bpf.c | 2 +- .../plugin/net_health/net_health.bpf.c | 2 +- .../plugin/net_retrans/net_retrans.bpf.c | 5 +- .../plugin/net_retrans/net_retrans.c | 22 ++++--- .../collector/plugin/virtout/virtout.bpf.c | 7 +-- .../unity/collector/plugin/virtout/virtout.c | 8 +-- .../unity/collector/plugin/virtout/virtout.h | 1 + .../unity/collector/postEngine/engine.lua | 9 ++- .../unity/collector/postEngine/execBase.lua | 35 +++++++++++- .../unity/collector/postEngine/execDiag.lua | 57 ++++++++++++++++++- .../unity/collector/proc_snmp_stat.lua | 6 +- source/tools/monitor/unity/common/system.lua | 18 ++++++ .../monitor/unity/test/curl/postDiag.lua | 2 +- 13 files changed, 146 insertions(+), 28 deletions(-) diff --git a/source/tools/monitor/unity/collector/plugin/cpudist/cpudist.bpf.c b/source/tools/monitor/unity/collector/plugin/cpudist/cpudist.bpf.c index 1dc01053..cd3da8ae 100644 --- a/source/tools/monitor/unity/collector/plugin/cpudist/cpudist.bpf.c +++ b/source/tools/monitor/unity/collector/plugin/cpudist/cpudist.bpf.c @@ -33,7 +33,7 @@ int sched_switch_hook(struct sched_switch_args *args){ } pv = bpf_map_lookup_elem(&start, &prev); if (pv && ts > *pv) { - hist10_push((struct bpf_map_def *)&cpudist, (ts - *pv) / 1000); + hist10_push((struct bpf_map_def *)&cpudist, (ts - *pv) / 1000 * 10); } return 0; } diff --git a/source/tools/monitor/unity/collector/plugin/net_health/net_health.bpf.c b/source/tools/monitor/unity/collector/plugin/net_health/net_health.bpf.c index fedd6b09..5bd2eabc 100644 --- a/source/tools/monitor/unity/collector/plugin/net_health/net_health.bpf.c +++ b/source/tools/monitor/unity/collector/plugin/net_health/net_health.bpf.c @@ -15,7 +15,7 @@ int j_tcp_validate_incoming(struct pt_regs *ctx) { if (ms > 0) { add_hist((struct bpf_map_def *)&outCnt, 0, ms); add_hist((struct bpf_map_def *)&outCnt, 1, 1); - hist10_push((struct bpf_map_def *)&netHist, ms); + hist10_push((struct bpf_map_def *)&netHist, ms * 10); } return 0; } diff --git a/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.bpf.c b/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.bpf.c index d9d9428b..6be0e8ab 100644 --- a/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.bpf.c +++ b/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.bpf.c @@ -26,12 +26,15 @@ struct liphdr { BPF_PERF_OUTPUT(perf, 1024); BPF_STACK_TRACE(stack, MAX_ENTRY); -BPF_HASH(outCnt, int, u64, NET_RETRANS_TYPE_MAX); +BPF_ARRAY(outCnt, u64, NET_RETRANS_TYPE_MAX + 1); static inline void addCnt(int k, u64 val) { + k += 1; u64 *pv = bpf_map_lookup_elem(&outCnt, &k); if (pv) { __sync_fetch_and_add(pv, val); + } else { + bpf_map_update_elem(&outCnt, &k, &val, BPF_ANY); } } diff --git a/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c b/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c index 49b2225c..89988a7e 100644 --- a/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c +++ b/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c @@ -56,9 +56,17 @@ int init(void *arg) static int get_count(unsigned long *locals) { int i = 0; - - for (i = 0; i < NET_RETRANS_TYPE_MAX; i ++) { - coobpf_key_value(cnt_fd, &i, &locals[i]); + unsigned long value = 0; + int key, key_next; + + key = 0; + while (coobpf_key_next(cnt_fd, &key, &key_next) == 0) { + coobpf_key_value(cnt_fd, &key_next, &value); + locals[i ++] = value; + if (i > NET_RETRANS_TYPE_MAX) { + break; + } + key = key_next; } return i; } @@ -69,7 +77,7 @@ static int cal_retrans(unsigned long *values) { unsigned long locals[NET_RETRANS_TYPE_MAX]; get_count(locals); - for (i = 0; i < NET_RETRANS_TYPE_MAX; i ++) { + for (i = NET_RETRANS_TYPE_RTO; i < NET_RETRANS_TYPE_MAX; i ++) { values[i] = locals[i] - rec[i]; rec[i] = locals[i]; } @@ -78,7 +86,7 @@ static int cal_retrans(unsigned long *values) { int call(int t, struct unity_lines *lines) { int i; - unsigned long values[NET_RETRANS_TYPE_MAX]; + unsigned long values[NET_RETRANS_TYPE_MAX] = {0}; struct unity_line* line; budget = t; //release log budget @@ -88,8 +96,8 @@ int call(int t, struct unity_lines *lines) { unity_set_table(line, "net_retrans_count"); cal_retrans(values); - for (i = 0; i < NET_RETRANS_TYPE_MAX; i ++) { - unity_set_value(line, i, net_title[i], values[i]); + for (i = NET_RETRANS_TYPE_RTO; i < NET_RETRANS_TYPE_MAX; i ++) { + unity_set_value(line, i - NET_RETRANS_TYPE_RTO, net_title[i], values[i]); } return 0; } diff --git a/source/tools/monitor/unity/collector/plugin/virtout/virtout.bpf.c b/source/tools/monitor/unity/collector/plugin/virtout/virtout.bpf.c index 25d3e612..69ca70ad 100644 --- a/source/tools/monitor/unity/collector/plugin/virtout/virtout.bpf.c +++ b/source/tools/monitor/unity/collector/plugin/virtout/virtout.bpf.c @@ -43,8 +43,8 @@ static inline void check_time(struct bpf_perf_event_data *ctx, if (last && delta > 2 * PERIOD_TIME) { delta -= PERIOD_TIME; - bpf_printk("delta %llu.\n", delta); - hist10_push((struct bpf_map_def *)&virtdist, delta / PERIOD_TIME); + int res = delta / PERIOD_TIME; + hist10_push((struct bpf_map_def *)&virtdist, res * 10); if (delta >= THRESHOLD_TIME) { struct task_struct* task = (struct task_struct *)bpf_get_current_task(); struct data_t data = {}; @@ -52,10 +52,9 @@ static inline void check_time(struct bpf_perf_event_data *ctx, data.pid = pid(); data.stack_id = bpf_get_stackid(ctx, &stack, KERN_STACKID_FLAGS); data.delta = delta; + data.us = t / 1000; data.cpu = cpu(); bpf_get_current_comm(&data.comm, TASK_COMM_LEN); - bpf_printk("delta %llu, pid:%d.\n", delta, data.pid); - bpf_perf_event_output(ctx, event, BPF_F_CURRENT_CPU, &data, sizeof(data)); } } diff --git a/source/tools/monitor/unity/collector/plugin/virtout/virtout.c b/source/tools/monitor/unity/collector/plugin/virtout/virtout.c index b611b8de..6034db5a 100644 --- a/source/tools/monitor/unity/collector/plugin/virtout/virtout.c +++ b/source/tools/monitor/unity/collector/plugin/virtout/virtout.c @@ -142,8 +142,8 @@ static int get_dist(unsigned long *locals) { key = 0; while (coobpf_key_next(dist_fd, &key, &key_next) == 0) { coobpf_key_value(dist_fd, &key_next, &value); - locals[i ++] = value; - if (i > DIST_ARRAY_SIZE) { + locals[key] = value; + if (key > DIST_ARRAY_SIZE) { break; } key = key_next; @@ -155,7 +155,7 @@ static int cal_dist(unsigned long* values) { int i, j; int size; static unsigned long rec[DIST_ARRAY_SIZE] = {0}; - unsigned long locals[DIST_ARRAY_SIZE]; + unsigned long locals[DIST_ARRAY_SIZE] = {0}; size = get_dist(locals); for (i = 0; i < CPU_DIST_INDEX - 1; i ++) { @@ -206,7 +206,7 @@ int proc(int stack_fd, struct data_t *e, struct unity_line *line) { int id = e->stack_id; //last stack struct ksym_cell* cell; - snprintf(log, LOG_MAX, "task:%d(%s);cpu:%d;delayed:%ld;callstack:", e->pid, e->comm, e->cpu, e->delta); + snprintf(log, LOG_MAX, "task:%d(%s);us:%ld;cpu:%d;delayed:%ld;callstack:", e->pid, e->comm, e->us, e->cpu, e->delta); coobpf_key_value(stack_fd, &id, &addr); for (i = 0; i < 128; i ++) { diff --git a/source/tools/monitor/unity/collector/plugin/virtout/virtout.h b/source/tools/monitor/unity/collector/plugin/virtout/virtout.h index a0acb6c8..42822d96 100644 --- a/source/tools/monitor/unity/collector/plugin/virtout/virtout.h +++ b/source/tools/monitor/unity/collector/plugin/virtout/virtout.h @@ -10,6 +10,7 @@ struct data_t { int pid; int cpu; + unsigned long us; unsigned int stack_id; unsigned long delta; char comm[TASK_COMM_LEN]; diff --git a/source/tools/monitor/unity/collector/postEngine/engine.lua b/source/tools/monitor/unity/collector/postEngine/engine.lua index 29b14742..44ecc2f9 100644 --- a/source/tools/monitor/unity/collector/postEngine/engine.lua +++ b/source/tools/monitor/unity/collector/postEngine/engine.lua @@ -12,15 +12,20 @@ local pystring = require("common.pystring") local system = require("common.system") local cjson = require("cjson.safe") local CexecBase = require("collector.postEngine.execBase") +local CexecDiag = require("collector.postEngine.execDiag") local Cengine = class("engine", CvProto) local diagExec = { - io_hang = {block = 60, time = 15, cmd = "../../../iosdiag"}, + 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 = 5 * 60, time = 60, so = {virtiostat = 5 * 3}}, } function Cengine:_init_(que, proto_q, fYaml, tid) CvProto._init_(self, CprotoData.new(que)) + self._que = que self._fYaml = fYaml self._tid = tid self._task = nil @@ -39,7 +44,7 @@ function Cengine:run(e, res, diag) local args = res.args local second = res.second or diag.time if diag.cmd then - local exec = CexecBase.new(diag.cmd, args, second) + local exec = CexecDiag.new(diag.cmd, args, second, self._que, diag.report, res.uid) exec:addEvents(e) end local so = diag.so diff --git a/source/tools/monitor/unity/collector/postEngine/execBase.lua b/source/tools/monitor/unity/collector/postEngine/execBase.lua index 0e8b497f..77b7cb94 100644 --- a/source/tools/monitor/unity/collector/postEngine/execBase.lua +++ b/source/tools/monitor/unity/collector/postEngine/execBase.lua @@ -7,17 +7,40 @@ require("common.class") local exec = require("common.exec") local pwait = require("posix.sys.wait") +local unistd = require("posix.unistd") local pystring = require("common.pystring") local system = require("common.system") local CexecBase = class("execBase") local interval = 5 --- poll for every 5 second +local function checkChild(ppid, pid) + local path = "/proc/" .. pid .. "/status" + local ret = false + if unistd.access(path) then + local f = io.open(path) + + for line in f:lines() do + if pystring:startswith(line, "PPid:") then + local _, s = pystring:split(line, ":", 1) + if tonumber(pystring:strip(s)) == ppid then + ret = true + end + break + end -- end startswith + end -- end for + + f:close() + end + return ret +end + function CexecBase:_init_(cmd, args, seconds) self.cmd = cmd self._cnt = 0 self._loop = seconds / interval + self._ppid = unistd.getpid() self._pid = exec.run(cmd, args) end @@ -27,16 +50,22 @@ end function CexecBase:work() local cnt = self._cnt + local pid, stat, exit = pwait.wait(self._pid, pwait.WNOHANG) + + if exit then + return -1, exit + end if cnt >= self._loop then - local pid, stat, exit = pwait.wait(self._pid, pwait.WNOHANG) if pid == nil then error("wait failed " .. stat .. exit) end if not exit then -- process not exit print("force to kill " .. self._pid) - exec.kill(self._pid) + if checkChild(self._ppid, self._ppid) then -- confirm child process + exec.kill(self._pid) + end end - return -1 -- delete from task list. + return -1, nil -- delete from task list. end self._cnt = cnt + 1 end diff --git a/source/tools/monitor/unity/collector/postEngine/execDiag.lua b/source/tools/monitor/unity/collector/postEngine/execDiag.lua index d5c89386..f9a5c249 100644 --- a/source/tools/monitor/unity/collector/postEngine/execDiag.lua +++ b/source/tools/monitor/unity/collector/postEngine/execDiag.lua @@ -5,6 +5,59 @@ --- require("common.class") +local system = require("common.system") local unistd = require("posix.unistd") -local pwait = require("posix.sys.wait") -local signal = require("posix.signal") \ No newline at end of file +local cjson = require("cjson.safe") +local CprotoData = require("common.protoData") +local CexecBase = require("collector.postEngine.execBase") + +local CexecDiag = class("execDiag", CexecBase) + +function CexecDiag:_init_(cmd, args, seconds, que, report, uid) + CexecBase._init_(self, cmd, args, seconds) + self._proto = CprotoData.new(que) + self._report = report + if uid then + self._uid = uid + else + self._uid = system:guid() + end +end + +function CexecDiag:work() + local ret, exit = CexecBase.work(self) + if ret == -1 and exit and self._report then -- need to report + local c = 0 + local ss = {} + for _, fName in ipairs(self._report.files) do + if unistd.access(fName) then + c = c + 1 + local f = io.open(fName, 'r') + ss[c] = f:read("*a") + end + end + + local stream = table.concat(ss) + local logs = { + { + name = "uid", + log = cjson.encode(self._uid), + }, { + name = "diag", + log = cjson.encode(stream) + } + } + local line = {line = self._report.title, log = logs} + + local lines = self._proto:protoTable() + table.insert(lines.lines, line) + system:dumps(lines) + + local bytes = self._proto:encode(lines) + print(#bytes) + self._proto:que(bytes) + end + return ret +end + +return CexecDiag \ No newline at end of file diff --git a/source/tools/monitor/unity/collector/proc_snmp_stat.lua b/source/tools/monitor/unity/collector/proc_snmp_stat.lua index 7d17261e..262d5433 100644 --- a/source/tools/monitor/unity/collector/proc_snmp_stat.lua +++ b/source/tools/monitor/unity/collector/proc_snmp_stat.lua @@ -75,12 +75,14 @@ function CprocSnmpStat:udpCount(titles, values) end end -local tcpHeads = {"InSegs", "OutMsgs", "RetransSegs", "InErrs", "CurrEstab"} +local tcpHeads = {"InSegs", "OutSegs", "RetransSegs", "InErrs"} function CprocSnmpStat:tcpCount(titles, values) local vs = {} for i = 1, titles.no do local cell = self._ffi.string(titles.s[i]) - if system:valueIsIn(tcpHeads, cell) then + if cell == "CurrEstab" then + table.insert(vs, {name = cell, value = tonumber(values.value[i - 1])}) + elseif system:valueIsIn(tcpHeads, cell) then local name = "v_tcp" .. cell local v = tonumber(values.value[i - 1]) if self[name] then diff --git a/source/tools/monitor/unity/common/system.lua b/source/tools/monitor/unity/common/system.lua index c43a7ffd..b1220ade 100644 --- a/source/tools/monitor/unity/common/system.lua +++ b/source/tools/monitor/unity/common/system.lua @@ -207,4 +207,22 @@ function system:Enum(tbl, index) return eTbl; end +function system:guid() + math.randomseed(os.time()) + local seed={'e','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'} + local tb={} + for i = 1,32 do + table.insert(tb,seed[math.random(1,16)]) + end + + local sid = table.concat(tb) + return string.format('%s-%s-%s-%s-%s', + string.sub(sid,1,8), + string.sub(sid,9,12), + string.sub(sid,13,16), + string.sub(sid,17,20), + string.sub(sid,21,32) + ) +end + return system \ No newline at end of file diff --git a/source/tools/monitor/unity/test/curl/postDiag.lua b/source/tools/monitor/unity/test/curl/postDiag.lua index bc175c71..8082eb06 100644 --- a/source/tools/monitor/unity/test/curl/postDiag.lua +++ b/source/tools/monitor/unity/test/curl/postDiag.lua @@ -11,6 +11,6 @@ local ChttpCli = require("httplib.httpCli") local cli = ChttpCli.new() local url = "http://127.0.0.1:8400/api/trig" --local req = {cmd = "diag", exec = "io_hang", args = {"hangdetect", "vda"}} -local req = {cmd = "diag", exec = "net_edge", args = {"hangdetect", "vda"}} +local req = {cmd = "diag", exec = "io_hang", args = {"hangdetect", "vda"}, uid = system:guid()} local res = cli:postTable(url, req) system:dumps(res) \ No newline at end of file -- Gitee From 5a46dbc8618c908f83e6c5df2b58c9db6125b97a Mon Sep 17 00:00:00 2001 From: liaozhaoyan Date: Mon, 22 May 2023 15:04:57 +0800 Subject: [PATCH 4/6] fix bug for hist10 push. --- .../tools/monitor/unity/collector/plugin/cpudist/cpudist.c | 4 +++- .../monitor/unity/collector/plugin/net_health/net_health.c | 6 ++++-- .../unity/collector/plugin/net_retrans/net_retrans.c | 6 ++++-- .../unity/collector/plugin/sum_retrans/sum_retrans.c | 6 ++++-- .../monitor/unity/collector/plugin/virtiostat/virtiostat.c | 4 +++- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/source/tools/monitor/unity/collector/plugin/cpudist/cpudist.c b/source/tools/monitor/unity/collector/plugin/cpudist/cpudist.c index 5ea28bac..0dfa928a 100644 --- a/source/tools/monitor/unity/collector/plugin/cpudist/cpudist.c +++ b/source/tools/monitor/unity/collector/plugin/cpudist/cpudist.c @@ -17,7 +17,9 @@ int init(void *arg) int ret; printf("cpudist plugin install.\n"); ret = LOAD_SKEL_OBJECT(cpudist, perf); - dist_fd = coobpf_map_find(cpudist->obj, "cpudist"); + if (ret >= 0) { + dist_fd = coobpf_map_find(cpudist->obj, "cpudist"); + } return ret; } diff --git a/source/tools/monitor/unity/collector/plugin/net_health/net_health.c b/source/tools/monitor/unity/collector/plugin/net_health/net_health.c index 4e77dc4c..41b61a1c 100644 --- a/source/tools/monitor/unity/collector/plugin/net_health/net_health.c +++ b/source/tools/monitor/unity/collector/plugin/net_health/net_health.c @@ -20,8 +20,10 @@ int init(void *arg) int ret; printf("net_health plugin install.\n"); ret = LOAD_SKEL_OBJECT(net_health, perf); - cnt_fd = coobpf_map_find(net_health->obj, "outCnt"); - dist_fd = coobpf_map_find(net_health->obj, "netHist"); + if (ret >= 0) { + cnt_fd = coobpf_map_find(net_health->obj, "outCnt"); + dist_fd = coobpf_map_find(net_health->obj, "netHist"); + } return ret; } diff --git a/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c b/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c index 89988a7e..7a20ceda 100644 --- a/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c +++ b/source/tools/monitor/unity/collector/plugin/net_retrans/net_retrans.c @@ -49,8 +49,10 @@ int init(void *arg) printf("net_retrans plugin install.\n"); ret = LOAD_SKEL_OBJECT(net_retrans, perf); - cnt_fd = coobpf_map_find(net_retrans->obj, "outCnt"); - stack_fd = coobpf_map_find(net_retrans->obj, "stack"); + if (ret >= 0) { + cnt_fd = coobpf_map_find(net_retrans->obj, "outCnt"); + stack_fd = coobpf_map_find(net_retrans->obj, "stack"); + } return ret; } diff --git a/source/tools/monitor/unity/collector/plugin/sum_retrans/sum_retrans.c b/source/tools/monitor/unity/collector/plugin/sum_retrans/sum_retrans.c index 61c9537b..d411e59d 100644 --- a/source/tools/monitor/unity/collector/plugin/sum_retrans/sum_retrans.c +++ b/source/tools/monitor/unity/collector/plugin/sum_retrans/sum_retrans.c @@ -22,8 +22,10 @@ int init(void *arg) int ret; printf("sum_retrans plugin install.\n"); ret = LOAD_SKEL_OBJECT(sum_retrans, perf); - inum_fd = coobpf_map_find(sum_retrans->obj, "inums"); - dip_fd = coobpf_map_find(sum_retrans->obj, "dips"); + if (ret >= 0) { + inum_fd = coobpf_map_find(sum_retrans->obj, "inums"); + dip_fd = coobpf_map_find(sum_retrans->obj, "dips"); + } return ret; } diff --git a/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c b/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c index 1b857566..cbe5c03d 100644 --- a/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c +++ b/source/tools/monitor/unity/collector/plugin/virtiostat/virtiostat.c @@ -14,7 +14,9 @@ int init(void *arg) int ret; printf("virtiostat plugin install.\n"); ret = LOAD_SKEL_OBJECT(virtiostat, perf); - stats_fd = coobpf_map_find(virtiostat->obj, "stats"); + if (ret >= 0) { + stats_fd = coobpf_map_find(virtiostat->obj, "stats"); + } return ret; } -- Gitee From 40cbf56644ddb439e712a334861f276c59919d1a Mon Sep 17 00:00:00 2001 From: liaozhaoyan Date: Tue, 23 May 2023 10:17:57 +0800 Subject: [PATCH 5/6] add test for post. --- .../tools/monitor/unity/collector/plugin.yaml | 21 ++++++++ .../unity/collector/postEngine/engine.lua | 9 +++- .../unity/collector/postEngine/execDiag.lua | 52 ++++++++++++------- .../monitor/unity/test/curl/diag/ioHang.py | 8 +++ .../monitor/unity/test/curl/diag/netCli.py | 8 +++ .../monitor/unity/test/curl/postJRuntime.lua | 16 ++++++ .../tools/monitor/unity/test/curl/postNet.lua | 16 ++++++ 7 files changed, 108 insertions(+), 22 deletions(-) create mode 100644 source/tools/monitor/unity/test/curl/diag/ioHang.py create mode 100644 source/tools/monitor/unity/test/curl/diag/netCli.py create mode 100644 source/tools/monitor/unity/test/curl/postJRuntime.lua create mode 100644 source/tools/monitor/unity/test/curl/postNet.lua diff --git a/source/tools/monitor/unity/collector/plugin.yaml b/source/tools/monitor/unity/collector/plugin.yaml index 45fda75b..7e1dc24c 100644 --- a/source/tools/monitor/unity/collector/plugin.yaml +++ b/source/tools/monitor/unity/collector/plugin.yaml @@ -26,6 +26,27 @@ forkRun: cmd: "/usr/bin/python" args: ["../test/curl/forkRun.py"] +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 diff --git a/source/tools/monitor/unity/collector/postEngine/engine.lua b/source/tools/monitor/unity/collector/postEngine/engine.lua index 44ecc2f9..4ec4fefb 100644 --- a/source/tools/monitor/unity/collector/postEngine/engine.lua +++ b/source/tools/monitor/unity/collector/postEngine/engine.lua @@ -29,6 +29,9 @@ function Cengine:_init_(que, proto_q, fYaml, tid) self._fYaml = fYaml self._tid = tid self._task = nil + + local res = system:parseYaml(fYaml) + self._resDiag = res.diagnose self._diags = {} end @@ -50,6 +53,7 @@ function Cengine:run(e, res, diag) local so = diag.so if so then for plugin, loop in pairs(so) do + print(plugin, loop) self._main.soPlugins:add(plugin, loop) end end @@ -70,10 +74,11 @@ function Cengine:pushTask(e, msgs) local second = res.second or 1 local exec = CexecBase.new(execCmd, args, second) exec:addEvents(e) - elseif cmd == "diag" then + elseif cmd == "diag" and self._resDiag then local exec = res.exec - local diag = diagExec[exec] + local diag = self._resDiag[exec] if diag then + system:dumps(diag) if self._diags[exec] then print("cmd " .. exec .. " is blocking.") else diff --git a/source/tools/monitor/unity/collector/postEngine/execDiag.lua b/source/tools/monitor/unity/collector/postEngine/execDiag.lua index f9a5c249..adb6d7ef 100644 --- a/source/tools/monitor/unity/collector/postEngine/execDiag.lua +++ b/source/tools/monitor/unity/collector/postEngine/execDiag.lua @@ -15,6 +15,7 @@ local CexecDiag = class("execDiag", CexecBase) function CexecDiag:_init_(cmd, args, seconds, que, report, uid) CexecBase._init_(self, cmd, args, seconds) + system:dumps(args) self._proto = CprotoData.new(que) self._report = report if uid then @@ -26,35 +27,46 @@ end function CexecDiag:work() local ret, exit = CexecBase.work(self) - if ret == -1 and exit and self._report then -- need to report - local c = 0 - local ss = {} - for _, fName in ipairs(self._report.files) do - if unistd.access(fName) then - c = c + 1 - local f = io.open(fName, 'r') - ss[c] = f:read("*a") + if ret == -1 and exit then -- -1 means should delete from exec list. exit and report need to report + local title = "diag_result" + local logs + if self._report then + local c = 0 + local ss = {} + for _, fName in ipairs(self._report.files) do + if unistd.access(fName) then + c = c + 1 + local f = io.open(fName, 'r') + ss[c] = f:read("*a") + end end - end - local stream = table.concat(ss) - local logs = { - { - name = "uid", - log = cjson.encode(self._uid), - }, { - name = "diag", - log = cjson.encode(stream) + local stream = table.concat(ss) + logs = { + { + name = "uid", + log = cjson.encode(self._uid), + }, { + name = "diag", + log = cjson.encode(stream) + } + } + title = self._report.title + else + logs = { + { + name = "uid", + log = cjson.encode(self._uid), + } } - } - local line = {line = self._report.title, log = logs} + end + local line = {line = title, log = logs} local lines = self._proto:protoTable() table.insert(lines.lines, line) system:dumps(lines) local bytes = self._proto:encode(lines) - print(#bytes) self._proto:que(bytes) end return ret diff --git a/source/tools/monitor/unity/test/curl/diag/ioHang.py b/source/tools/monitor/unity/test/curl/diag/ioHang.py new file mode 100644 index 00000000..12e76228 --- /dev/null +++ b/source/tools/monitor/unity/test/curl/diag/ioHang.py @@ -0,0 +1,8 @@ +import requests +import json +import uuid + +url = "http://127.0.0.1:8400/api/trig" +vs = {"cmd": "diag", "exec": "io_hang", "args": ["hangdetect", "vda"], "uid": str(uuid.uuid4())} +res = requests.post(url, json=vs) +print(res) diff --git a/source/tools/monitor/unity/test/curl/diag/netCli.py b/source/tools/monitor/unity/test/curl/diag/netCli.py new file mode 100644 index 00000000..16ab2153 --- /dev/null +++ b/source/tools/monitor/unity/test/curl/diag/netCli.py @@ -0,0 +1,8 @@ +import requests +import json +import uuid + +url = "http://127.0.0.1:8400/api/trig" +vs = {"cmd": "diag", "exec": "net_edge", "args": ["11.0.145.174", "host", "tcp port 80"], "uid": str(uuid.uuid4())} +res = requests.post(url, json=vs) +print(res) diff --git a/source/tools/monitor/unity/test/curl/postJRuntime.lua b/source/tools/monitor/unity/test/curl/postJRuntime.lua new file mode 100644 index 00000000..301fd11f --- /dev/null +++ b/source/tools/monitor/unity/test/curl/postJRuntime.lua @@ -0,0 +1,16 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/5/23 00:11 +--- + +package.path = package.path .. ";../../?.lua;" +local system = require("common.system") +local ChttpCli = require("httplib.httpCli") + +local cli = ChttpCli.new() +local url = "http://127.0.0.1:8400/api/trig" +--local req = {cmd = "diag", exec = "io_hang", args = {"hangdetect", "vda"}} +local req = {cmd = "diag", exec = "jruntime", args = {"-d", "5", "--top", "2"}, uid = system:guid()} +local res = cli:postTable(url, req) +system:dumps(res) \ No newline at end of file diff --git a/source/tools/monitor/unity/test/curl/postNet.lua b/source/tools/monitor/unity/test/curl/postNet.lua new file mode 100644 index 00000000..85b74a6a --- /dev/null +++ b/source/tools/monitor/unity/test/curl/postNet.lua @@ -0,0 +1,16 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/5/22 17:00 +--- + +package.path = package.path .. ";../../?.lua;" +local system = require("common.system") +local ChttpCli = require("httplib.httpCli") + +local cli = ChttpCli.new() +local url = "http://127.0.0.1:8400/api/trig" +--local req = {cmd = "diag", exec = "io_hang", args = {"hangdetect", "vda"}} +local req = {cmd = "diag", exec = "net_edge", args = {"172.16.0.118", "host", "tcp port 80"}, uid = system:guid()} +local res = cli:postTable(url, req) +system:dumps(res) \ No newline at end of file -- Gitee From c175d37e54006857c0be8722d8390febe1918a72 Mon Sep 17 00:00:00 2001 From: liaozhaoyan Date: Tue, 23 May 2023 14:31:59 +0800 Subject: [PATCH 6/6] fix a efd bug for co httpCli. --- source/tools/monitor/unity/httplib/coHttpCli.lua | 2 +- source/tools/monitor/unity/test/curl/diag/jRuntime.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 source/tools/monitor/unity/test/curl/diag/jRuntime.py diff --git a/source/tools/monitor/unity/httplib/coHttpCli.lua b/source/tools/monitor/unity/httplib/coHttpCli.lua index b8d40a73..9718385d 100644 --- a/source/tools/monitor/unity/httplib/coHttpCli.lua +++ b/source/tools/monitor/unity/httplib/coHttpCli.lua @@ -472,7 +472,7 @@ function CcoHttpCli:coWrite(cffi, efd, fd, stream) end ::continue:: end - res = cffi.mod_fd(self._efd, fd, 0) -- epoll read ev only + res = cffi.mod_fd(efd, fd, 0) -- epoll read ev only checkInt(-res, fd) if res < 0 then print("mod_fd socket failed") diff --git a/source/tools/monitor/unity/test/curl/diag/jRuntime.py b/source/tools/monitor/unity/test/curl/diag/jRuntime.py new file mode 100644 index 00000000..ece07f9c --- /dev/null +++ b/source/tools/monitor/unity/test/curl/diag/jRuntime.py @@ -0,0 +1,8 @@ +import requests +import json +import uuid + +url = "http://127.0.0.1:8400/api/trig" +vs = {"cmd": "diag", "exec": "jruntime", "args": ["-d", "5", "--top", "2"], "uid": str(uuid.uuid4())} +res = requests.post(url, json=vs) +print(res) -- Gitee