af

package module
v1.5.3-0...-e9feef0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2020 License: MIT Imports: 29 Imported by: 0

README

Agent Framework

系统配置

  1. system.max_memory : 设置最大占用内存, 默认100m
  2. system.max_cpu_quota : 设置最大cpu使用率, 默认10%
  3. system.max_load_limit : 设置系统负载自杀阈值,默认0.7
  4. system.register.api : agent注册api地址, 必填
  5. system.register.save_file : agent注册完信息保存文件路径, 默认:存放到当前目录下data.toml文件中
  6. system.max_cpu_usage_limit : 设置cpu使用率自杀阈值,默认80(%)
  7. system.cgroup_enable : 是否启用cgroup, 默认启用, 参数 on/off

稳定性

控制 windows linux
负载监控
资源限制(cpu/memory)
CPU使用率

TODO

  • 资源限制(内存/CPU)
  • 资源监控/自杀
  • 统一日志
  • 插件扩展
  • 统一注册
  • 服务注册
  • 进程守护
  • 升级卸载
  • 配置变更
  • 指令通讯

框架使用

main.go



// Copyright 2020 Dean.
// Authors: Dean <dean@csoio.com>
// Date: 2020/5/19 6:17 下午

package main

import (
	"github.com/dean2021/af"
	"github.com/dean2021/af/_example/logger"
	"github.com/dean2021/af/_example/plugin"
	"github.com/sirupsen/logrus"
	"log"
	"path"
	"time"
)

func main() {

	// 新建一个agent
	agent := af.NewAgent("hs-agent")

	// 限制100M内存
	agent.Config.Set("system.max_memory", "104857600")

	// 限制10% CPU使用率
	agent.Config.Set("system.max_cpu_quota", "10000")

	// 系统负载阈值, 超过此阈值则退出程序
	agent.Config.Set("system.max_load_limit", "0.7")

	// 系统cpu使用率阈值,超过此阈值则
	agent.Config.Set("system.max_cpu_usage_limit", "80")

	// agent注册api
	agent.Config.Set("system.register.api", "http://soc.qa.csoio.com/api/hostsecurity/agent/register")

	// agent注册信息保存文件
	agent.Config.Set("system.register.save_file", "./data.toml")

	// 用户自定义配置
	agent.Config.Set("user.mysql", "mysql://127.0.0.1:3306")
	agent.Config.Set("service.grpc.addr", "localhost:50001")

	// 添加服务
	//af.AddService("grpc", new(service.DataService))

	// 添加插件
	agent.Plugin(new(plugin.TestPlugin))
	agent.Plugin(new(plugin.TestPlugin2))
	//agent.Plugin(new(plugin.TestService))

	// 替换日志组件
	l := logrus.New()
	// 设置日志格式
	l.SetFormatter(&logger.JSONFormatter{})
	// 添加log http hook
	l.AddHook(logger.NewHttpHook(logrus.AllLevels, &logger.JSONFormatter{}, "http://www.baidu.com/logserver"))
	// 添加log滚动文件切割hook
	hook, err := logger.NewRotateHook(path.Join("./", "logs"), "debug.log", time.Hour*24, time.Second*60)
	if err != nil {
		panic(err)
	}
	l.AddHook(hook)
	agent.SetLogger(l)

	// 运行agent
	err = agent.Run()
	if err != nil {
		log.Fatal(err)
	}
}



Documentation

Index

Constants

View Source
const (
	CgroupRootPath = "/cgroup"
)

Variables

This section is empty.

Functions

func AddService

func AddService(name string, service Service)

func GetService

func GetService(name string, service Service) error

func NewSystemService

func NewSystemService(config *service.Config, agent *Agent) (service.Service, error)

初始化一个服务

func Register

func Register(agent *Agent) error

注册agent

func SystemCpuUsageCheck

func SystemCpuUsageCheck(agent *Agent)

系统cpu使用率 超过系统设置好的阈值, agent则自动停止

func SystemLoadCheck

func SystemLoadCheck(agent *Agent)

系统负载检查 超过设定好的阈值, agent则自动停止

func SystemResourceLimit

func SystemResourceLimit(agent *Agent) error

根据进程添加资源限制

Types

type Agent

type Agent struct {
	// 唯一ID
	ID string `toml:"id"`
	// Agent名称
	Name string `toml:"name"`

	// agent配置
	Config *Config `toml:"-"`
	// 通讯管道
	Notify *Notify `toml:"-"`
	// contains filtered or unexported fields
}

func NewAgent

func NewAgent(name string) *Agent

初始化

func (*Agent) Plugin

func (a *Agent) Plugin(plugin Plugin)

注册插件

func (*Agent) Run

func (a *Agent) Run() error

运行

func (*Agent) SetLogger

func (a *Agent) SetLogger(logger Logger)

func (*Agent) Start

func (a *Agent) Start() error

启动

func (*Agent) StartCommandWatch

func (a *Agent) StartCommandWatch()

启动插件指令监控 支持配置变更及命令通知

func (*Agent) StartPlugin

func (a *Agent) StartPlugin()

启动所有插件

func (*Agent) Stop

func (a *Agent) Stop() error

停止agent

type Config

type Config struct {
	sync.Map
}

func (*Config) Get

func (c *Config) Get(key string) string

获取配置

func (*Config) Set

func (c *Config) Set(key, value string)

设置配置

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
	Print(v ...interface{})
	Println(v ...interface{})
	Fatal(v ...interface{})
	Fatalf(format string, v ...interface{})
	Fatalln(v ...interface{})
	Panic(v ...interface{})
	Panicf(format string, v ...interface{})
	Panicln(v ...interface{})
}

type Notify

type Notify struct {
	// contains filtered or unexported fields
}

func NewNotify

func NewNotify(namespace string, uuid string, client *clientv3.Client) *Notify

func (*Notify) GetConfig

func (n *Notify) GetConfig(pluginName string) (string, error)

获取配置信息 路径设计: /hs/uuid-xxx/system/plugin-xxx/command/config

func (*Notify) Watch

func (n *Notify) Watch(ReceiveHandle func(path string, value string))

监听指令 路径设计:/hs/uuid-xxx

type Plugin

type Plugin interface {

	// 插件名称
	Name() string

	// 收到指令被动调用
	Command(name string, body string)

	// 插件入口函数
	Entry(config *Config, notify *Notify, logger Logger) error
}

type Service

type Service interface{}

Directories

Path Synopsis
util
retry
Simple library for retry mechanism slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry) SYNOPSIS http get with retry: url := "http://example.com" var body []byte err := retry.Do( func() error { resp, err := http.Get(url) if err != nil { return err } defer resp.Body.Close() body, err = ioutil.ReadAll(resp.Body) if err != nil { return err } return nil }, ) fmt.Println(body) [next examples](https://github.com/avast/retry-go/tree/master/examples) SEE ALSO * [giantswarm/retry-go](https://github.com/giantswarm/retry-go) - slightly complicated interface.
Simple library for retry mechanism slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry) SYNOPSIS http get with retry: url := "http://example.com" var body []byte err := retry.Do( func() error { resp, err := http.Get(url) if err != nil { return err } defer resp.Body.Close() body, err = ioutil.ReadAll(resp.Body) if err != nil { return err } return nil }, ) fmt.Println(body) [next examples](https://github.com/avast/retry-go/tree/master/examples) SEE ALSO * [giantswarm/retry-go](https://github.com/giantswarm/retry-go) - slightly complicated interface.
str

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL