action

package
v0.0.0-...-7ff62be Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: AGPL-3.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TaskNew = iota
	TaskNotMatched
	TaskCancelled
	TaskRpcError
)
View Source
const MaxRetries = 5
View Source
const PbehaviorOrigin = "scenario"

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	Type                     string     `bson:"type" json:"type"`
	Comment                  string     `bson:"comment" json:"comment"`
	Parameters               Parameters `bson:"parameters,omitempty" json:"parameters,omitempty"`
	DropScenarioIfNotMatched bool       `bson:"drop_scenario_if_not_matched" json:"drop_scenario_if_not_matched"`
	EmitTrigger              bool       `bson:"emit_trigger" json:"emit_trigger"`

	savedpattern.EntityPatternFields `bson:",inline"`
	savedpattern.AlarmPatternFields  `bson:",inline"`
}

Action represents a canopsis Action on alarms.

func (Action) Match

func (a Action) Match(entity types.Entity, alarm types.Alarm) (bool, error)

TODO: change arguments to pointers, check any other places where changing event, alarm and entity structs to pointers gives a performance boost.

type Adapter

type Adapter interface {
	GetEnabled(ctx context.Context) ([]Scenario, error)
	GetEnabledById(ctx context.Context, id string) (Scenario, error)
	GetEnabledByIDs(ctx context.Context, ids []string) ([]Scenario, error)
}

func NewAdapter

func NewAdapter(dbClient mongo.DbClient) Adapter

type AdditionalData

type AdditionalData struct {
	Trigger   string `json:"trigger"`
	Author    string `json:"author"`
	User      string `json:"user"`
	Initiator string `json:"initiator"`
	Output    string `json:"event_output"`
	RuleName  string `json:"rule_name"`

	// Deprecated: use Trigger instead of AlarmChangeType
	AlarmChangeType string `json:"alarm_change_type"`
}

type DelayedScenario

type DelayedScenario struct {
	ID            string           `json:"id"`
	ScenarioID    string           `json:"scenario_id"`
	AlarmID       string           `json:"alarm_id"`
	ExecutionTime datetime.CpsTime `json:"execution_time"`
	Paused        bool             `json:"paused"`
	TimeLeft      time.Duration    `json:"time_left"`

	AdditionalData AdditionalData `json:"additional_data"`
}

type DelayedScenarioManager

type DelayedScenarioManager interface {
	AddDelayedScenario(context.Context, types.Alarm, Scenario, AdditionalData) error
	PauseDelayedScenarios(context.Context, types.Alarm) error
	ResumeDelayedScenarios(context.Context, types.Alarm) error
	Run(context.Context) (<-chan DelayedScenarioTask, error)
}

func NewDelayedScenarioManager

func NewDelayedScenarioManager(
	adapter Adapter,
	alarmAdapter libalarm.Adapter,
	storage DelayedScenarioStorage,
	periodicalTimeout time.Duration,
	logger zerolog.Logger,
) DelayedScenarioManager

type DelayedScenarioStorage

type DelayedScenarioStorage interface {
	Add(ctx context.Context, scenario DelayedScenario) (string, error)
	GetAll(ctx context.Context) ([]DelayedScenario, error)
	Get(ctx context.Context, id string) (*DelayedScenario, error)
	Delete(ctx context.Context, id string) (bool, error)
	Update(ctx context.Context, scenario DelayedScenario) (bool, error)
}

func NewRedisDelayedScenarioStorage

func NewRedisDelayedScenarioStorage(
	key string,
	client redis.Cmdable,
	encoder encoding.Encoder,
	decoder encoding.Decoder,
) DelayedScenarioStorage

type DelayedScenarioTask

type DelayedScenarioTask struct {
	Alarm    types.Alarm
	Entity   types.Entity
	Scenario Scenario

	AdditionalData AdditionalData
}

type ExecuteScenariosTask

type ExecuteScenariosTask struct {
	Triggers             []string
	DelayedScenarioID    string
	Entity               types.Entity
	Alarm                types.Alarm
	AdditionalData       AdditionalData
	FifoAckEvent         types.Event
	IsMetaAlarmUpdated   bool
	IsInstructionMatched bool
	Start                time.Time

	AbandonedExecutionCacheKey string
}

type Execution

type Execution struct {
	Action   Action `json:"a"`
	Executed bool   `json:"e"`
}

type Parameters

type Parameters struct {
	Output string `json:"output,omitempty" bson:"output,omitempty" binding:"max=1000"`

	ForwardAuthor *bool  `json:"forward_author,omitempty" bson:"forward_author,omitempty"`
	Author        string `json:"author,omitempty" bson:"author,omitempty"`

	// State is used in changestate action.
	//   * `0` - Info
	//   * `1` - Minor
	//   * `2` - Major
	//   * `3` - Critical
	State *types.CpsNumber `json:"state,omitempty" bson:"state,omitempty"`
	// Ticket is used in assocticket action.
	Ticket string `json:"ticket,omitempty" binding:"max=255" bson:"ticket,omitempty"`
	// TicketURL is used in assocticket action.
	TicketURL string `json:"ticket_url,omitempty" binding:"max=255" bson:"ticket_url,omitempty"`
	// TicketSystemName is used in assocticket and webhook action.
	TicketSystemName string `json:"ticket_system_name,omitempty" binding:"max=255" bson:"ticket_system_name,omitempty"`
	// TicketData is used in assocticket action.
	TicketData map[string]string `json:"ticket_data,omitempty" bson:"ticket_data,omitempty"`
	// Duration is used in snooze and pbehavior actions.
	Duration *datetime.DurationWithUnit `json:"duration,omitempty" bson:"duration,omitempty"`
	// Name is used in pbehavior action.
	Name string `json:"name,omitempty" binding:"max=255" bson:"name,omitempty"`
	// Reason is used in pbehavior action.
	Reason string `json:"reason,omitempty" bson:"reason,omitempty"`
	// Type is used in pbehavior action.
	Type string `json:"type,omitempty" bson:"type,omitempty"`
	// RRule is used in pbehavior action.
	RRule string `json:"rrule,omitempty" bson:"rrule,omitempty"`
	// Tstart is used in pbehavior action.
	Tstart *datetime.CpsTime `json:"tstart,omitempty" bson:"tstart,omitempty" swaggertype:"integer"`
	// Tstop is used in pbehavior action.
	Tstop *datetime.CpsTime `json:"tstop,omitempty" bson:"tstop,omitempty" swaggertype:"integer"`
	// StartOnTrigger is used in pbehavior action.
	StartOnTrigger *bool `json:"start_on_trigger,omitempty" bson:"start_on_trigger,omitempty"`
	// Color is used in pbehavior action.
	Color string `json:"color,omitempty" bson:"color,omitempty"`
	// Request is used in webhook action.
	Request *request.Parameters `json:"request,omitempty" bson:"request,omitempty"`
	// SkipForChild is used in webhook action.
	SkipForChild *bool `json:"skip_for_child,omitempty" bson:"skip_for_child,omitempty"`
	// SkipForInstruction is used in webhook action.
	SkipForInstruction *bool `json:"skip_for_instruction,omitempty" bson:"skip_for_instruction,omitempty"`
	// DeclareTicket is used in webhook action.
	DeclareTicket *request.WebhookDeclareTicket `json:"declare_ticket,omitempty" bson:"declare_ticket,omitempty"`
}

type RpcResult

type RpcResult struct {
	CorrelationID   string
	Alarm           *types.Alarm
	AlarmChangeType types.AlarmChangeType
	Error           error
}

type Scenario

type Scenario struct {
	ID                   string                     `bson:"_id,omitempty" json:"_id,omitempty"`
	Name                 string                     `bson:"name" json:"name"`
	Author               string                     `bson:"author" json:"author"`
	Enabled              bool                       `bson:"enabled" json:"enabled"`
	DisableDuringPeriods []string                   `bson:"disable_during_periods" json:"disable_during_periods"`
	Triggers             []string                   `bson:"triggers" json:"triggers"`
	Actions              []Action                   `bson:"actions" json:"actions"`
	Priority             int64                      `bson:"priority" json:"priority"`
	Delay                *datetime.DurationWithUnit `bson:"delay" json:"delay"`
	Created              datetime.CpsTime           `bson:"created,omitempty" json:"created,omitempty"`
	Updated              datetime.CpsTime           `bson:"updated,omitempty" json:"updated,omitempty"`
}

func (Scenario) IsTriggered

func (s Scenario) IsTriggered(triggers []string) string

type ScenarioExecution

type ScenarioExecution struct {
	ID                   string         `json:"_id"`
	ScenarioID           string         `json:"sid"`
	ScenarioName         string         `json:"sn"`
	AlarmID              string         `json:"aid"`
	Entity               types.Entity   `json:"e"`
	ActionExecutions     []Execution    `json:"ae"`
	LastUpdate           int64          `json:"u"`
	Tries                int64          `json:"t"`
	AdditionalData       AdditionalData `json:"ad"`
	FifoAckEvent         types.Event    `json:"fev"`
	IsMetaAlarmUpdated   bool           `json:"mau,omitempty"`
	IsInstructionMatched bool           `json:"im,omitempty"`
	StartEventProcessing int64          `json:"sep"`
}

func (ScenarioExecution) GetCacheKey

func (e ScenarioExecution) GetCacheKey() string

type ScenarioExecutionStorage

type ScenarioExecutionStorage interface {
	Get(ctx context.Context, key string) (*ScenarioExecution, error)
	GetAbandoned(ctx context.Context) ([]ScenarioExecution, error)
	Create(ctx context.Context, execution ScenarioExecution) (bool, error)
	Update(ctx context.Context, execution ScenarioExecution) error
	Del(ctx context.Context, key string) error
	IncExecutingCount(ctx context.Context, key string, inc int64, drop bool) (int64, error)
	DelExecutingCount(ctx context.Context, key string) (int64, error)
	IncExecutedCount(ctx context.Context, key string, inc int64, drop bool) (int64, error)
	DelExecutedCount(ctx context.Context, key string) (int64, error)
	IncExecutedWebhookCount(ctx context.Context, key string, inc int64, drop bool) (int64, error)
	DelExecutedWebhookCount(ctx context.Context, key string) (int64, error)
}

func NewRedisScenarioExecutionStorage

func NewRedisScenarioExecutionStorage(
	redisKeyPrefix string,
	redisClient redis.Cmdable,
	encoder encoding.Encoder,
	decoder encoding.Decoder,
	lastRetryInterval time.Duration,
	logger zerolog.Logger,
) ScenarioExecutionStorage

type ScenarioResult

type ScenarioResult struct {
	Alarm            types.Alarm
	Err              error
	ActionExecutions []Execution
	FifoAckEvent     types.Event

	StartEventProcessing time.Time
	ExecutedRuleCount    int64
	ExecutedWebhookCount int64

	// EntityType is needed to send activation event with right source type.
	EntityType string
}

type ScenarioStorage

type ScenarioStorage interface {
	// ReloadScenarios trigger a refresh on scenarios cache from DB
	ReloadScenarios(ctx context.Context) error

	// GetTriggeredScenarios returns scenarios which are triggered by triggers.
	GetTriggeredScenarios(
		triggers []string,
		alarm types.Alarm,
	) (map[string][]Scenario, error)

	// RunDelayedScenarios starts delay timeout for scenarios which are triggered by triggers.
	RunDelayedScenarios(
		ctx context.Context,
		triggers []string,
		alarm types.Alarm,
		entity types.Entity,
		additionalData AdditionalData,
	) error

	// GetScenario returns scenario.
	GetScenario(id string) *Scenario
}

ScenarioStorage is used to provide scenarios.

func NewScenarioStorage

func NewScenarioStorage(
	actionAdapter Adapter,
	delayedScenarioManager DelayedScenarioManager,
	logger zerolog.Logger,
) ScenarioStorage

type Service

type Service interface {
	// Process parse an event to see if an action is suitable.
	Process(ctx context.Context, event *types.Event) error

	// ListenScenarioFinish receives message when all scenarios for event are finished
	// and acknowledges fifo.
	ListenScenarioFinish(ctx context.Context, channel <-chan ScenarioResult)

	// ProcessAbandonedExecutions checks execution storage and processes executions which
	// weren't updated for a long time
	ProcessAbandonedExecutions(ctx context.Context) error
}

Service allows you to manipulate actions.

func NewService

func NewService(
	alarmAdapter libalarm.Adapter,
	scenarioInputChan chan<- ExecuteScenariosTask,
	delayedScenarioManager DelayedScenarioManager,
	storage ScenarioExecutionStorage,
	encoder encoding.Encoder,
	decoder encoding.Decoder,
	fifoChan libamqp.Channel,
	fifoExchange string,
	fifoQueue string,
	activationService libalarm.ActivationService,
	techMetricsSender techmetrics.Sender,
	logger zerolog.Logger,
) Service

NewService gives the correct action adapter.

type Task

type Task struct {
	Source               string
	Action               Action
	Alarm                types.Alarm
	Entity               types.Entity
	Step                 int
	ExecutionCacheKey    string
	ExecutionID          string
	ScenarioID           string
	ScenarioName         string
	SkipForChild         bool
	IsMetaAlarmUpdated   bool
	SkipForInstruction   bool
	IsInstructionMatched bool
	AdditionalData       AdditionalData
}

type TaskManager

type TaskManager interface {
	Run(ctx context.Context, rpcResultChannel <-chan RpcResult,
		inputChannel <-chan ExecuteScenariosTask) (<-chan ScenarioResult, error)
}

TaskManager is used to execute scenarios.

func NewTaskManager

func NewTaskManager(
	workerPool WorkerPool,
	executionStorage ScenarioExecutionStorage,
	scenarioStorage ScenarioStorage,
	logger zerolog.Logger,
) TaskManager

type TaskResult

type TaskResult struct {
	Source            string
	Alarm             types.Alarm
	Step              int
	ExecutionCacheKey string
	AlarmChangeType   types.AlarmChangeType
	Status            int
	Err               error
}

type WorkerPool

type WorkerPool interface {
	RunWorkers(ctx context.Context, taskChannel <-chan Task) (<-chan TaskResult, error)
}

func NewWorkerPool

func NewWorkerPool(
	size int,
	dbClient mongo.DbClient,
	axeRpcClient engine.RPCClient,
	webhookRpcClient engine.RPCClient,
	encoder encoding.Encoder,
	logger zerolog.Logger,
	templateExecutor template.Executor,
	alarmConfigProvider config.AlarmConfigProvider,
) WorkerPool

Jump to

Keyboard shortcuts

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