Skip to content

Commit

Permalink
supply for lock_image
Browse files Browse the repository at this point in the history
  • Loading branch information
Gui-Yue committed Sep 15, 2023
1 parent b5bc417 commit 74c6ec4
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 44 deletions.
3 changes: 3 additions & 0 deletions eBPF_Visualization/eBPF_prometheus/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ all:

start_service:
./runimages.sh

clean_data:
rm ./dao/data.db
14 changes: 13 additions & 1 deletion eBPF_Visualization/eBPF_prometheus/collector/collect_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ func init() {
log.Fatalf("Failed to load ... error:%s\n", err)
return
}
tmuxSvc := Aservice{
Name: "tmuxCollectData",
Desc: "collect data from lock_image",
NewInst: newTmuxCmd}
if err := AddAService(&tmuxSvc); err != nil {
log.Fatalf("Failed to load ... error:%s\n", err)
return
}
}

func newCollectCmd(ctx *cli.Context, opts ...interface{}) (interface{}, error) {
Expand All @@ -110,6 +118,10 @@ func newProcCmd(ctx *cli.Context, opts ...interface{}) (interface{}, error) {
return proc_imageCommand, nil
}

func newTmuxCmd(ctx *cli.Context, opts ...interface{}) (interface{}, error) {
return tmux_command, nil
}

type BPF_name struct {
Name string
}
Expand Down Expand Up @@ -162,7 +174,7 @@ func (b *BPF_name) Run(filePath string) error {
mapchan := make(chan []map[string]interface{}, 2)

if checker.IsTcpObjection(cmdStr) {
log.Println("I am TCPWatch")
log.Println("I am NETWatch")
go RedirectTcpWatch(stdout, mapchan)
} else {
go redirectStdout(stdout, mapchan)
Expand Down
127 changes: 89 additions & 38 deletions eBPF_Visualization/eBPF_prometheus/collector/collect_proc_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ import (
"log"
"os"
"os/exec"
"regexp"
"strings"
"syscall"
"time"
)

// Pro_Setting 定义了设置项,通过读取同目录下的proc_setting.yaml实现对基本信息的设置。
type Proc_Setting struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
Pid string `yaml:"pid"`
Max_Records int `yaml:"max_records"`
Name string `yaml:"proc_name"`
Path string `yaml:"proc_path"`
Pid string `yaml:"proc_pid"`
Max_Records int `yaml:"proc_max_records"`
}

var proc_imageCommand = cli.Command{
Expand All @@ -50,33 +51,48 @@ var proc_imageCommand = cli.Command{
}

// Get_Setting 函数用于获取设置的信息
func Get_Setting() (error, string, int) {
func Get_Setting(which string) (error, string, int) {
currentDir, _ := os.Getwd()
content, err := os.ReadFile(currentDir + "/collector/proc_setting.yaml")
content, err := os.ReadFile(currentDir + "/collector/tmux_proc_setting.yaml")
if err != nil {
log.Fatalf("Error reading file: %v", err)
return err, "", 0
}
var setting Proc_Setting
err = yaml.Unmarshal(content, &setting)
if err != nil {
log.Fatalf("Error unmarshaling YAML :%v", err)
return err, "", 0
}
command := ""
maxrecords := 0
if which == "proc" {
var setting Proc_Setting
err = yaml.Unmarshal(content, &setting)
if err != nil {
log.Fatalf("Error unmarshaling YAML :%v", err)
return err, "", 0
}

command = setting.Path + " -p " + setting.Pid
maxrecords = setting.Max_Records
} else if which == "tmux" {
var setting Tmux_Setting
err = yaml.Unmarshal(content, &setting)
if err != nil {
log.Fatalf("Error unmarshaling YAML :%v", err)
return err, "", 0
}

command := setting.Path + " -p " + setting.Pid
maxrecords := setting.Max_Records
command = setting.Path + " -p " + setting.Pid
maxrecords = setting.Max_Records
} else {
log.Fatalf("select setting failed.")
}
return nil, command, maxrecords
}

func procCollect(ctx *cli.Context) error {
_, command, _ := Get_Setting()
_, command, _ := Get_Setting("proc")
return ProcRun(command)
}

// ProcRun 是收集器的主函数,通过goroutin的方式实现数据收集,重定向,与prom_core包实现通信。
func ProcRun(command string) error {
_, _, maxrecords := Get_Setting()
cmdStr := CheckFileType(command)
cmd := exec.Command("sh", "-c", cmdStr)

Expand All @@ -93,30 +109,65 @@ func ProcRun(command string) error {

loc, _ := time.LoadLocation("Asia/Shanghai")
currenttime := float64(time.Now().In(loc).UnixNano()) / 1e9
go redirectProc(stdout, mapchan)

procdata := prom_core.ProcMetrics{Max_records: maxrecords, NowTime: currenttime}
sqlobj := &dao.Sqlobj{Tablename: "proc_image_data"}
procdata.Sqlobj = sqlobj
// process chan from redirectProc Stdout
go procdata.BootProcService()

go func() {
for {
select {
case <-mapchan:
procdata.Getorigindata(mapchan)
if procdata.Sqlinted {
procdata.UpdateSql()
} else {
procdata.Initsql()

pathlist := strings.Split(command, "/")
is_proc, _ := regexp.MatchString(`proc`, pathlist[len(pathlist)-1])
is_lifecycle, _ := regexp.MatchString(`lifecycle`, pathlist[len(pathlist)-1])
is_lock, _ := regexp.MatchString(`lock`, pathlist[len(pathlist)-1])

if is_proc || is_lifecycle {
log.Println("This is lifecycle")
_, _, maxrecords := Get_Setting("proc")
go redirectProc(stdout, mapchan)

procdata := prom_core.ProcMetrics{Max_records: maxrecords, NowTime: currenttime}
sqlobj := &dao.Sqlobj{Tablename: "proc_image_data"}
procdata.Sqlobj = sqlobj
// process chan from redirectProc Stdout
go procdata.BootProcService()

go func() {
for {
select {
case <-mapchan:
procdata.Getorigindata(mapchan)
if procdata.Sqlinted {
procdata.UpdateSql()
} else {
procdata.Initsql()
}
procdata.UpdateRecords()
<-mapchan
default:
}
procdata.UpdateRecords()
<-mapchan
default:
}
}
}()
}()

} else if is_lock {
log.Println("This is lock_image.")
_, _, maxrecords := Get_Setting("tmux")
go redirectTmux(stdout, mapchan)
tmuxdata := prom_core.TmuxMetrics{Max_records: maxrecords, NowTime: currenttime}
sqlobj := &dao.Sqlobj{Tablename: "tmux_data"}
tmuxdata.Sqlobj = sqlobj
go tmuxdata.BootProcService()
go func() {
for {
select {
case <-mapchan:
tmuxdata.Getorigindata(mapchan)
if tmuxdata.Sqlinted {
tmuxdata.UpdateSql()
} else {
tmuxdata.Initsql()
}
tmuxdata.UpdateRecords()
<-mapchan
default:
}
}
}()
}

err = cmd.Start()
if err != nil {
Expand Down
78 changes: 78 additions & 0 deletions eBPF_Visualization/eBPF_prometheus/collector/collect_tmux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2023 The LMP Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// author: Gui-Yue
//
// 为lock_image所做的适配的收集器

package collector

import (
"bufio"
"ebpf_prometheus/checker"
"github.com/urfave/cli/v2"
"io"
"strings"
)

type Tmux_Setting struct {
Name string `yaml:"tmux_name"`
Path string `yaml:"tmux_path"`
Pid string `yaml:"tmux_pid"`
Max_Records int `yaml:"tmux_max_records"`
}

var tmux_command = cli.Command{
Name: "tmux",
Usage: "Special collect data out from lock_image",
Action: tmuxCollect,
}

func tmuxCollect(ctx *cli.Context) error {
_, command, _ := Get_Setting("tmux")
return ProcRun(command)
}

func redirectTmux(stdout io.ReadCloser, mapchan chan map[string]interface{}) {
controler := 0
scanner := bufio.NewScanner(stdout)
onemap := make(map[string]interface{})

for scanner.Scan() {
line := scanner.Text()
line = checker.CutunexceptedSpace(line)

if controler == 0 {
if checker.Istmuxlineone(line) {
parms := strings.Fields(line)
for _, value := range parms {
parts := strings.Split(value, ":")
onemap[parts[0]] = parts[1]
controler = 1
}
}
} else {
if checker.Istmuxlinetwo(line) {
parms := strings.Fields(line)
for _, value := range parms {
parts := strings.Split(value, ":")
onemap[parts[0]] = parts[1]
controler = 0
}
mapchan <- onemap
onemap = make(map[string]interface{})
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
proc_name: proc_image
proc_path: ~/tmp/proc_image/proc_image
proc_pid: 22896
proc_max_records: 20 # 一次展示的最大数量

tmux_name: tmux
tmux_path: /root/lmp/eBPF_Supermarket/eBPF_proc_image/lock_image
tmux_pid: 20080
tmux_max_records: 200 # 一次展示的最大数量
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//
// author: Gui-Yue
//
// 数据可视化处理的核心逻辑
// proc_image数据可视化处理的核心逻辑

package prom_core

Expand Down Expand Up @@ -50,6 +50,7 @@ func (p *ProcMetrics) Getorigindata(originalvalue chan map[string]interface{}) {
p.OriginalValue = <-originalvalue
}

// 数据库操作
func (p *ProcMetrics) UpdateSql() {
p.Sqlobj.Data = p.OriginalValue
p.Sqlobj.CreateRow()
Expand Down
Loading

0 comments on commit 74c6ec4

Please sign in to comment.