Skip to content

Commit

Permalink
Merge pull request linuxkerneltravel#297 from Gui-Yue/develop
Browse files Browse the repository at this point in the history
Datacollector
  • Loading branch information
helight authored Aug 12, 2022
2 parents 3e0d47c + bac83e1 commit d6c2fdb
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,29 @@ package check

import (
"regexp"
"strings"
"text/template"
)

func VerifyCompleteIndexFormat(line string) bool {
elements := strings.Fields(line)
for _, element := range elements {
matched := VerifySingleIndexFormat(element)
if !matched {
return false
}
}
return true
}

func VerifySingleIndexFormat(element string) bool {
matched, _ := regexp.MatchString(".+\\|.+", element)
if !matched {
return false
}
return true
}

func EscapeData(line string) string {
return template.HTMLEscapeString(line)
}
Expand All @@ -14,11 +34,21 @@ func GetTypeFromData(element string) string {
if matched, _ := regexp.MatchString("^[-+]?[0-9]*\\.[0-9]+$", EscapeData(element)); matched {
ElementType = "REAL"
} else {
if matched, _ := regexp.MatchString("^[-+]?[1-9]\\d*$|0", EscapeData(element)); matched {
if matched, _ := regexp.MatchString("^[-+]?[1-9]\\d*$|^0$", EscapeData(element)); matched {
ElementType = "INTEGER"
} else {
ElementType = "TEXT"
}
}
return ElementType
}

func VerifyMultipleDataMatched(line string, index_parms []string) bool {
line_parms := strings.Fields(line)
for i, v := range line_parms {
if GetTypeFromData(v) != index_parms[i] {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"lmp/server/model/data_collector/check"
)

const SpliteCharacter = "|"

type TableInfo struct {
TableName string
IndexName []string
Expand Down Expand Up @@ -44,7 +46,25 @@ func (ti TableInfo) CreateTable() error {
return nil
}

func (ti TableInfo) AppendTable(index string, line string) (error, TableInfo) {
func (ti TableInfo) AppendTableByIndx(index string) (error, TableInfo) {
parms := strings.Fields(index)
ti.IndexName = make([]string, len(parms))
ti.IndexType = make([]string, len(parms))
for i, value := range parms {
info := strings.Split(value, SpliteCharacter)
ti.IndexName[i] = check.EscapeData(info[0])
ti.IndexType[i] = check.EscapeData(info[1])
}
for i, _ := range ti.IndexName {
addcollumnsql := fmt.Sprintf("alter table %s add column \"%s\" %s", ti.TableName, ti.IndexName[i], ti.IndexType[i])
if err := GLOBALDB.Exec(addcollumnsql).Error; err != nil {
return err, ti
}
}
return nil, ti
}

func (ti TableInfo) AppenTableByData(index string, line string) (error, TableInfo) {
index_parms := strings.Fields(index)
elements := strings.Fields(line)
type_parms := make([]string, len(index_parms))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package logic

import (
"fmt"

"lmp/server/model/data_collector/dao"
)

Expand All @@ -15,14 +14,28 @@ func InitCollectSqlite() error {
return nil
}

func DataCollectorIndexType(pluginname string, index string, line string) (error, dao.TableInfo) {
func DataCollectorIndexFromIndex(pluginname string, index string) (error, dao.TableInfo) {
var tableinfo dao.TableInfo
tableinfo.TableName = pluginname
var err error
if err = tableinfo.CreateTable(); err != nil {
return err, tableinfo
}
if err, tableinfo = tableinfo.AppendTableByIndx(index); err != nil {
return err, Tableinfo
}
fmt.Println(tableinfo)
return nil, tableinfo
}

func DataCollectorIndexFromData(pluginname string, index string, line string) (error, dao.TableInfo) {
var tableinfo dao.TableInfo
tableinfo.TableName = pluginname
var err error
if err = tableinfo.CreateTable(); err != nil {
return err, tableinfo
}
if err, tableinfo = tableinfo.AppendTable(index, line); err != nil {
if err, tableinfo = tableinfo.AppenTableByData(index, line); err != nil {
return err, Tableinfo
}
fmt.Println(tableinfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ebpfplugins
import (
"errors"
"io/ioutil"
"log"
"sync"

"lmp/server/global"
Expand Down Expand Up @@ -105,9 +104,8 @@ func (ebpf *EbpfpluginsService) LoadEbpfPlugins(e request.PluginInfo) (err error
}()
go func() {
select {
case out := <-outputChannel:
case <-outputChannel:
global.GVA_LOG.Info("Start run plugin!")
log.Println(out)
wg.Done()
case err = <-errorChannel:
global.GVA_LOG.Error("error in runSinglePlugin!")
Expand Down
58 changes: 45 additions & 13 deletions eBPF_Visualization/eBPF_server/service/ebpfplugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package ebpfplugins
import (
"bufio"
"context"
"errors"
"fmt"
"lmp/server/model/data_collector/check"
"lmp/server/model/data_collector/dao"
"os/exec"
"syscall"
"time"

"lmp/server/global"
"lmp/server/model/common/request"
"lmp/server/model/data_collector/dao"
"lmp/server/model/data_collector/logic"
"lmp/server/model/ebpfplugins"

Expand Down Expand Up @@ -166,29 +168,59 @@ func runSinglePlugin(e request.PluginInfo, out *chan bool, errch *chan error) {
var tableinfo dao.TableInfo
var indexname string
var counter int
default_create_table := true
counter = 1
for scanner.Scan() {
linechan <- scanner.Text()
select {
case line := <-linechan:
if counter == 1 {
fmt.Printf("%s-index-%d", line, counter) //todo 仅用于测试环境
indexname = line
*out <- true
} else {
if counter == 2 {
err, tableinfo = logic.DataCollectorIndexType(plugin.PluginName, indexname, line)
err = logic.DataCollectorRow(tableinfo, line)
if check.VerifyCompleteIndexFormat(line) {
err, tableinfo = logic.DataCollectorIndexFromIndex(plugin.PluginName, line)
if err != nil {
global.GVA_LOG.Error("error in DataCollectorIndexFromIndex:", zap.Error(err))
*errch <- err
return
}
default_create_table = false
global.GVA_LOG.Info("使用用户指定的数据类型建表!")
} else {
err = logic.DataCollectorRow(tableinfo, line)
if err != nil {
*errch <- err
global.GVA_LOG.Info("使用正则的方式自动建表!")
indexname = line
}
*out <- true
}
if counter > 1 {
if !default_create_table {
if err := logic.DataCollectorRow(tableinfo, line); err != nil {
global.GVA_LOG.Error("error in DataCollectorRow:", zap.Error(err))
}
}
if default_create_table {
if counter == 2 {
err, tableinfo = logic.DataCollectorIndexFromData(plugin.PluginName, indexname, line)
if err != nil {
global.GVA_LOG.Error("error in DataCollectorIndexFromData:", zap.Error(err))
return
}
if err := logic.DataCollectorRow(tableinfo, line); err != nil {
global.GVA_LOG.Error("error in DataCollectorIndexFromData:", zap.Error(err))
return
}
} else {
if check.VerifyMultipleDataMatched(line, tableinfo.IndexType) {
if err := logic.DataCollectorRow(tableinfo, line); err != nil {
global.GVA_LOG.Error("error in DataCollectorIndexFromData:", zap.Error(err))
return
}
} else {
mismatcheerr := fmt.Sprintf("第%d行数据无法自动匹配,请使用指定类型的ebpf程序!", counter)
err = errors.New(mismatcheerr)
global.GVA_LOG.Error("error:\n", zap.Error(err))
return
}
}
fmt.Printf("%s-rows-%d", line, counter)
} //todo 仅用于测试环境
}
}
if len(*out) >= 1 {
<-*out
Expand Down

0 comments on commit d6c2fdb

Please sign in to comment.