snmp

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PidFilePath       = "/var/run/snmptrapd/snmptrapd.pid"
	DefaultBufferSize = 64000
)

Variables

View Source
var Functions = []expr.Option{
	expr.Function(
		"SHA256",
		func(params ...any) (any, error) {
			p, err := json.Marshal(params, json.Deterministic(true))
			if err != nil {
				return nil, err
			}
			h := sha256.New()
			h.Write(p)
			return hex.EncodeToString(h.Sum(nil)), nil
		},
		new(func(...any) string),
	),
	expr.Function(
		"MergeMap",
		func(params ...any) (any, error) {
			if val, err := prepareValues(params[0]); err != nil {
				return nil, err
			} else {
				res := make(map[string]any)
				for _, m := range val {
					for k, v := range m {
						res[k] = v
					}
				}
				return res, nil
			}
		},
		new(func([]map[string]any) map[string]any),
	),
	expr.Function(
		"OidValueAny",
		func(params ...any) (any, error) {
			if val, ok := params[0].([]Value); !ok {
				return nil, errors.Errorf(
					"unexpected error, invalid first param type %s",
					reflect.TypeOf(params[0]),
				)
			} else {
				prefix, ok := params[1].(string)
				if !ok {
					return nil, errors.Errorf(
						"unexpected error, invalid second param type %s",
						reflect.TypeOf(params[1]),
					)
				}
				valOid := getOidValue(val, prefix)
				return valOid, nil
			}
		},
		new(func([]Value, string) any),
	),
	expr.Function(
		"OidValueNumber",
		func(params ...any) (any, error) {
			if val, ok := params[0].([]Value); !ok {
				return nil, errors.Errorf(
					"unexpected error, invalid first param type %s",
					reflect.TypeOf(params[0]),
				)
			} else {
				prefix, ok := params[1].(string)
				if !ok {
					return nil, errors.Errorf(
						"unexpected error, invalid second param type %s",
						reflect.TypeOf(params[1]),
					)
				}
				tryCast, ok := params[2].(bool)
				if !ok {
					return nil, errors.Errorf(
						"unexpected error, invalid third param type %s",
						reflect.TypeOf(params[2]),
					)
				}
				valOid := getOidValue(val, prefix)
				switch v := valOid.(type) {
				case int:
					vFloat := float64(v)
					return &vFloat, nil
				case int8:
					vFloat := float64(v)
					return &vFloat, nil
				case int16:
					vFloat := float64(v)
					return &vFloat, nil
				case int32:
					vFloat := float64(v)
					return &vFloat, nil
				case int64:
					vFloat := float64(v)
					return &vFloat, nil
				case uint:
					vFloat := float64(v)
					return &vFloat, nil
				case uint8:
					vFloat := float64(v)
					return &vFloat, nil
				case uint16:
					vFloat := float64(v)
					return &vFloat, nil
				case uint32:
					vFloat := float64(v)
					return &vFloat, nil
				case uint64:
					vFloat := float64(v)
					return &vFloat, nil
				case float32:
					vFloat := float64(v)
					return &vFloat, nil
				case float64:
					return &v, nil
				case nil:
					return nil, nil
				default:
					if tryCast {
						if s, err := strconv.ParseFloat(fmt.Sprint(v), 64); err != nil {
							return nil, nil
						} else {
							return &s, nil
						}
					} else {
						return nil, nil
					}
				}
			}
		},
		new(func([]Value, string, bool) *float64),
	),
	expr.Function(
		"OidValueString",
		func(params ...any) (any, error) {
			if val, ok := params[0].([]Value); !ok {
				return nil, errors.Errorf(
					"unexpected error, invalid first param type %s",
					reflect.TypeOf(params[0]),
				)
			} else {
				prefix, ok := params[1].(string)
				if !ok {
					return nil, errors.Errorf(
						"unexpected error, invalid second param type %s",
						reflect.TypeOf(params[1]),
					)
				}
				tryCast, ok := params[2].(bool)
				if !ok {
					return nil, errors.Errorf(
						"unexpected error, invalid third param type %s",
						reflect.TypeOf(params[2]),
					)
				}
				valOid := getOidValue(val, prefix)
				switch v := valOid.(type) {
				case string:
					return &v, nil
				case nil:
					return nil, nil
				default:
					if tryCast {
						s := fmt.Sprint(v)
						return &s, nil
					} else {
						return nil, nil
					}
				}
			}
		},
		new(func([]Value, string, bool) *string),
	),
}

Functions

func InitMIBTranslator

func InitMIBTranslator(mibPath string) ([]string, error)

func ParserWorker

func ParserWorker(
	i int,
	wg *sync.WaitGroup,
	parseChan <-chan []byte,
	messageChan chan<- *Message,
)

Types

type AuthConfig

type AuthConfig struct {
	// Enable auth for v1 and v2. v3 still needs users to be defined
	Enable bool
	// Community for snmp V1 and V2
	Community []Community
	// User for snmp v3
	User []User
}

type AuthType

type AuthType int8
const (
	AuthSHA AuthType = iota
	AuthMD5
	AuthSHA128
	AuthSHA224
	AuthSHA256
	AuthSHA384
	AuthSHA512
)

func (*AuthType) String

func (a *AuthType) String() string

func (*AuthType) UnmarshalText

func (a *AuthType) UnmarshalText(text []byte) error

type Community

type Community struct {
	Name string
}

type Config

type Config struct {
	Auth             AuthConfig
	Listening        []string
	AdditionalConfig string `mapstructure:"additional_config"`
	MagicBegin       string `mapstructure:"magic_begin"`
	MagicEnd         string `mapstructure:"magic_end"`
	BufferSize       string `mapstructure:"buffer_size"`
}

func (*Config) GetBufferSize added in v0.4.6

func (c *Config) GetBufferSize() (int, error)

func (*Config) Serialize

func (c *Config) Serialize(path string) error

type Correlate added in v0.6.0

type Correlate struct {
	ID              string          `json:"id" expr:"id"`
	RaisedTime      time.Time       `json:"raised_time" expr:"raised_time"`
	Duration        helper.Duration `json:"duration" expr:"duration"`
	DurationSeconds float64         `json:"duration_seconds" expr:"duration_seconds"`
}
type Header int8
const (
	HeaderAgentAddress Header = iota
	HeaderConnection
	HeaderTime
	HeaderUptime
	HeaderDescription
	HeaderEnterprise
	HeaderSecurity
	HeaderTrapType
	HeaderTrapSubType
	HeaderVarBinds
)

type Message

type Message struct {
	Payload  *Payload
	Metadata Metadata
}

func (*Message) Compare added in v0.2.0

func (m *Message) Compare(other queue.Item) int

func (*Message) Compile added in v0.2.0

func (m *Message) Compile(conf MessageCompiler)

func (*Message) ComputeEta added in v0.2.0

func (m *Message) ComputeEta(minDelay, maxDelay time.Duration) time.Time

func (*Message) Copy added in v0.2.0

func (m *Message) Copy() Message

Copy is only a shallow copy, only the metadata is different between messages

func (*Message) Eta added in v0.2.0

func (m *Message) Eta() time.Time

func (*Message) UnmarshalText

func (m *Message) UnmarshalText(text []byte) error

type MessageCompiler added in v0.2.0

type MessageCompiler struct {
	Filter     *vm.Program
	JSONFormat *vm.Program
	Logger     zerolog.Logger
}

type Metadata added in v0.6.0

type Metadata struct {
	Retries        int
	Skip           bool
	MessageJSON    []byte
	Eta            time.Time
	Compiled       bool
	TimeAsTimezone string
	TimeFormat     string
}

type Payload added in v0.6.0

type Payload struct {
	Time              time.Time  `json:"time" expr:"time"`
	UptimeSeconds     *float64   `json:"uptime_seconds" expr:"uptime_seconds"`
	SrcAddress        string     `json:"src_address" expr:"src_address"`
	SrcPort           int        `json:"src_port" expr:"src_port"`
	DstAddress        string     `json:"dst_address" expr:"dst_address"`
	DstPort           int        `json:"dst_port" expr:"dst_port"`
	AgentAddress      *string    `json:"agent_address" expr:"agent_address"`
	PDUVersion        string     `json:"pdu_version" expr:"pdu_version"`
	SNMPVersion       string     `json:"snmp_version" expr:"snmp_version"`
	Community         *string    `json:"community" expr:"community"`
	EnterpriseOID     *string    `json:"enterprise_oid" expr:"enterprise_oid"`
	EnterpriseMIBName *string    `json:"enterprise_mib_name" expr:"enterprise_mib_name"`
	User              *string    `json:"user" expr:"user"`
	Context           *string    `json:"context" expr:"context"`
	Description       *string    `json:"description" expr:"description"`
	TrapType          *int64     `json:"trap_type" expr:"trap_type"`
	TrapSubType       *int64     `json:"trap_sub_type" expr:"trap_sub_type"`
	Values            []Value    `json:"values" expr:"value_list"`
	Correlate         *Correlate `json:"correlate" expr:"correlate"`
}

type PrivacyProtocol

type PrivacyProtocol int8
const (
	PrivAES PrivacyProtocol = iota
	PrivDES
	PrivAES128
	PrivAES192
	PrivAES256
)

func (*PrivacyProtocol) String

func (p *PrivacyProtocol) String() string

func (*PrivacyProtocol) UnmarshalText

func (p *PrivacyProtocol) UnmarshalText(text []byte) error

type User

type User struct {
	Username          string
	NoAuth            bool            `mapstructure:"no_auth"`
	RequirePrivacy    bool            `mapstructure:"require_privacy"`
	EngineID          string          `mapstructure:"engine_id"`
	AuthType          AuthType        `mapstructure:"auth_type"`
	AuthPassphrase    string          `mapstructure:"auth_passphrase"`
	PrivacyProtocol   PrivacyProtocol `mapstructure:"privacy_protocol"`
	PrivacyPassphrase string          `mapstructure:"privacy_passphrase"`
}

func (User) SecurityLevel

func (u User) SecurityLevel() string

type Value

type Value struct {
	OID         string      `json:"oid" expr:"oid"`
	MIBName     string      `json:"mib_name" expr:"mib_name"`
	Type        ValueType   `json:"type" expr:"type"`
	NativeType  string      `json:"native_type" expr:"native_type"`
	Value       any         `json:"value" expr:"value"`
	ValueDetail ValueDetail `json:"value_detail" expr:"value_detail"`
}

func (Value) HasOIDPrefix

func (v Value) HasOIDPrefix(prefix string) bool

func (Value) SnmpCmd

func (v Value) SnmpCmd() (cmd []string)

type ValueDetail

type ValueDetail struct {
	Raw any    `json:"raw,omitempty" expr:"raw"`
	Hex string `json:"hex,omitempty" expr:"hex"`
}

type ValueType

type ValueType int8
const (
	TypeUnknown ValueType = iota
	TypeInteger
	TypeDuration
	TypeEnum
	TypeIpAddress
	TypeOID
	TypeString
	TypeBytes
	TypeBits
	TypeDateAndTime
	TypeNull
)

func (*ValueType) FromMIB

func (v *ValueType) FromMIB(text string) error

func (*ValueType) FromSNMP

func (v *ValueType) FromSNMP(text string) error

func (*ValueType) MarshalJSON

func (v *ValueType) MarshalJSON() ([]byte, error)

func (*ValueType) Parse

func (v *ValueType) Parse(text string) (any, ValueDetail, error)

func (*ValueType) String

func (v *ValueType) String() string

Jump to

Keyboard shortcuts

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