lua

package
v0.0.171 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const TriggerTypeCron = "cron"

TriggerTypeCron 表示定时触发

View Source
const TriggerTypeManual = "manual"

TriggerTypeManual 表示手动触发

Variables

This section is empty.

Functions

func InitClusterInspection

func InitClusterInspection()

Types

type CheckEvent

type CheckEvent struct {
	Status     string         `json:"status"` // 检查状态(如“正常”、“失败”)
	Msg        string         `json:"msg"`
	Extra      map[string]any `json:"extra,omitempty"`
	ScriptName string         `json:"scriptName"` // 检测脚本名称
	Kind       string         `json:"kind"`       // 检查的资源类型
	CheckDesc  string         `json:"checkDesc"`  // 检查脚本内容描述
	Namespace  string         `json:"ns"`         // 资源命名空间
	Name       string         `json:"name"`       // 资源名称
}

CheckEvent 用于记录每次检测的详细信息,包括检测状态、消息、额外上下文、脚本名称、资源类型、描述、命名空间和资源名。

type CheckResult

type CheckResult struct {
	Name         string
	StartTime    time.Time
	EndTime      time.Time
	LuaRunOutput string
	LuaRunError  error
	Events       []CheckEvent
}

type Inspection

type Inspection struct {
	Cluster string // 集群名称

	Schedule *models.InspectionSchedule // 巡检计划ID
	// contains filtered or unexported fields
}

func NewLuaInspection

func NewLuaInspection(schedule *models.InspectionSchedule, cluster string) *Inspection

func (*Inspection) Start

func (p *Inspection) Start() []CheckResult

type Kubectl

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

type ScheduleBackground

type ScheduleBackground struct {
}

func (*ScheduleBackground) Add

func (s *ScheduleBackground) Add(scheduleID uint)

Add 添加一个新的定时巡检任务到cron调度器中 该方法会从数据库读取指定的巡检计划,并创建对应的定时任务

重要:为了避免Go闭包变量捕获问题,该方法会创建局部变量副本 这确保每个定时任务都有自己独立的数据副本,不会被后续的Add调用影响 如果不这样做,多个巡检计划可能会错误地使用相同的集群配置

参数:

scheduleID - 要添加的巡检计划ID

func (*ScheduleBackground) AutoGenerateSummary added in v0.0.169

func (s *ScheduleBackground) AutoGenerateSummary(recordID uint)

AutoGenerateSummary 如果启用了AI总结,则自动生成总结 该方法在巡检执行完成后被调用,生成的AI总结会保存到数据库中 调用时机:巡检完成后立即调用,在发送webhook之前 设计原则:AI总结生成与webhook发送分离,确保职责单一

func (*ScheduleBackground) GetSummaryMsg added in v0.0.132

func (s *ScheduleBackground) GetSummaryMsg(recordID uint) (*SummaryMsg, error)

GetSummaryMsg 获取巡检记录的汇总信息 该函数根据记录ID查询巡检记录详情,包括关联的调度任务信息和失败事件统计 返回的数据中,record_date字段会将原始的UTC时间转换为本地时间,以便前端正确显示 参数:

  • recordID: 巡检记录的唯一标识符

返回值:

  • *SummaryMsg: 包含巡检汇总信息的结构体,包括记录日期、调度信息、错误统计等
  • error: 如果查询失败或记录不存在则返回错误

func (*ScheduleBackground) IsEventStatusPass

func (s *ScheduleBackground) IsEventStatusPass(status string) bool

IsEventStatusPass 判断事件状态是否为通过 这里的通过状态包括:正常、pass、ok、success、通过 入库前将状态描述文字统一为正常、失败两种

func (*ScheduleBackground) PushToHooksByRecordID added in v0.0.130

func (s *ScheduleBackground) PushToHooksByRecordID(recordID uint) ([]*webhook.SendResult, error)

PushToHooksByRecordID 根据巡检记录ID发送webhook通知 该方法从数据库中获取已生成的AI总结,然后发送到所有关联的webhook 调用时机:在AutoGenerateSummaryIfEnabled()完成后调用 设计原则:单纯的webhook发送功能,不负责AI总结生成

func (*ScheduleBackground) Remove

func (s *ScheduleBackground) Remove(scheduleID uint)

func (*ScheduleBackground) RunByCluster

func (s *ScheduleBackground) RunByCluster(ctx context.Context, scheduleID *uint, cluster string, triggerType string) (*models.InspectionRecord, error)

RunByCluster 启动一次巡检任务,并记录执行及每个脚本的结果到数据库 scheduleID: 可选,定时任务ID(手动触发时为nil) cluster: 目标集群 triggerType: 触发类型(manual/cron)

func (*ScheduleBackground) SaveSummaryBack added in v0.0.132

func (s *ScheduleBackground) SaveSummaryBack(id uint, summary string, summaryErr error, resultRaw string) error

SaveSummaryBack 保存AI总结结果到数据库 参数:id 巡检记录ID,summary AI总结内容,summaryErr AI总结错误,resultRaw 原始巡检结果JSON字符串

func (*ScheduleBackground) StartFromDB

func (s *ScheduleBackground) StartFromDB()

StartFromDB 后台自动执行调度

func (*ScheduleBackground) SummaryByAI added in v0.0.130

func (s *ScheduleBackground) SummaryByAI(ctx context.Context, msg *SummaryMsg) (string, error)

SummaryByAI 生成巡检总结 参数:msg 包含巡检数据和AI配置的消息 返回:总结内容和错误信息

type SummaryMsg added in v0.0.169

type SummaryMsg struct {
	RecordDate       string                         `json:"record_date"`        // 巡检记录日期(本地时间格式)
	RecordID         uint                           `json:"record_id"`          // 巡检记录ID
	ScheduleID       *uint                          `json:"schedule_id"`        // 巡检计划ID
	ScheduleName     string                         `json:"schedule_name"`      // 巡检计划名称
	Cluster          string                         `json:"cluster"`            // 集群名称
	TotalRules       int                            `json:"total_rules"`        // 总规则数
	FailedCount      int                            `json:"failed_count"`       // 失败数量
	FailedList       []*models.InspectionCheckEvent `json:"failed_list"`        // 失败事件列表
	AIEnabled        bool                           `json:"ai_enabled"`         // 是否启用AI汇总
	AIPromptTemplate string                         `json:"ai_prompt_template"` // AI提示模板
}

SummaryMsg 巡检记录汇总信息结构体 用于替代原来的 map[string]any 返回类型,提供类型安全和更好的性能

type TaskControlManager added in v0.0.169

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

TaskControlManager 任务控制管理器 负责管理所有任务的执行控制状态

func (*TaskControlManager) GetTaskByEntryID added in v0.0.169

func (tcm *TaskControlManager) GetTaskByEntryID(entryID cron.EntryID) (*TaskExecutionControl, bool)

GetTaskByEntryID 根据EntryID获取任务控制信息

func (*TaskControlManager) GetTaskByScheduleID added in v0.0.169

func (tcm *TaskControlManager) GetTaskByScheduleID(scheduleID uint) (*TaskExecutionControl, bool)

GetTaskByScheduleID 根据ScheduleID获取任务控制信息

func (*TaskControlManager) IsTaskDeleted added in v0.0.169

func (tcm *TaskControlManager) IsTaskDeleted(token string) bool

IsTaskDeleted 检查任务是否已被删除 在任务执行前调用,如果任务已被删除则不执行

func (*TaskControlManager) ListActiveTasks added in v0.0.169

func (tcm *TaskControlManager) ListActiveTasks() []*TaskExecutionControl

ListActiveTasks 列出所有活跃的任务

func (*TaskControlManager) MarkTaskDeleted added in v0.0.169

func (tcm *TaskControlManager) MarkTaskDeleted(scheduleID uint) bool

MarkTaskDeleted 标记任务为已删除状态 当任务被删除时调用,防止任务继续执行

func (*TaskControlManager) RegisterTask added in v0.0.169

func (tcm *TaskControlManager) RegisterTask(scheduleID uint, entryID cron.EntryID, token string)

RegisterTask 注册任务控制信息 在AddFunc执行前调用,建立随机字符串与EntryID的映射关系

func (*TaskControlManager) RemoveTask added in v0.0.169

func (tcm *TaskControlManager) RemoveTask(scheduleID uint)

RemoveTask 完全移除任务控制信息 在任务被彻底清理时调用

type TaskExecutionControl added in v0.0.169

type TaskExecutionControl struct {
	RandomToken string       `json:"random_token"` // 随机字符串,用于在AddFunc中标识任务
	EntryID     cron.EntryID `json:"entry_id"`     // cron任务的EntryID
	IsDeleted   bool         `json:"is_deleted"`   // 是否已删除
	ScheduleID  uint         `json:"schedule_id"`  // 关联的巡检计划ID
	CreatedAt   time.Time    `json:"created_at"`   // 创建时间
}

TaskExecutionControl 任务执行控制结构体 用于管理定时任务的执行状态,防止已删除的任务继续执行

Jump to

Keyboard shortcuts

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