agent

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2020 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// MaxPingProcs is the simultaneous fping process limit for this agent
	MaxPingProcs int

	// PingPacketCount is number of ping requests to send to target (-C param of fping)
	PingPacketCount int
)
View Source
var (
	// MockMode activates mock snmp polling mode.
	MockMode bool

	// MaxSNMPRequests is the maximum number of parallel polling requests.
	MaxSNMPRequests int

	// GracefulQuitMode rejects new poll jobs, waiting for ongoing ones to finish before exiting.
	GracefulQuitMode bool

	// StatsUpdFreq is the frequency (in seconds) at wich various stats are retrieved and logged.
	StatsUpdFreq int

	// InterPollDelay is the delay to sleep before starting a new poll to smooth load
	InterPollDelay time.Duration

	// StopCtx is a context used to stop the agent gracefully.
	StopCtx context.Context
)
View Source
var MaxAllowedLoad float64

MaxAllowedLoad is memory load treshold to reject new snmp requests.

Functions

func AddPingRequest

func AddPingRequest(req model.PingRequest) bool

AddPingRequest adds a new ping request to the queue. Returns true if it was successfuly added (i.e. the queue is not full)

func AddSnmpRequest

func AddSnmpRequest(req *SnmpRequest) bool

AddSnmpRequest adds a new snmp request to the queue. Returns true if it was added i.e. a worker slot was acquired.

func CurrentMemLoad

func CurrentMemLoad() float64

CurrentMemLoad returns the current relative memory usage of the agent.

func CurrentSNMPLoad

func CurrentSNMPLoad() float64

CurrentSNMPLoad returns the current snmp load of the agent. It is calculated as the current number of all snmp requests in queue over the queue size.

func ErrIsRefused

func ErrIsRefused(err error) bool

ErrIsRefused tells whether the error is an snmp connection refused error.

func ErrIsTimeout

func ErrIsTimeout(err error) bool

ErrIsTimeout tells whether the error is an snmp timeout error.

func ErrIsUnreachable

func ErrIsUnreachable(err error) bool

ErrIsUnreachable tells whether the error is an snmp timeout or connection refused.

func HandleCheck

func HandleCheck(w http.ResponseWriter, r *http.Request)

HandleCheck responds to keep-alive checks. Returns current worker count in body.

func HandleOngoing

func HandleOngoing(w http.ResponseWriter, r *http.Request)

HandleOngoing returns the list of ongoing snmp requests, their count, and the total workers count.

func HandlePingRequest

func HandlePingRequest(w http.ResponseWriter, r *http.Request)

HandlePingRequest handles ping job requests. Returns a status 202 when the job is accepted, a 4XX error status otherwise.

func HandleSnmpRequest

func HandleSnmpRequest(w http.ResponseWriter, r *http.Request)

HandleSnmpRequest handles snmp polling job requests.

func Init

func Init() error

Init initializes the worker queue and starts the job dispatcher and the result handler.

func InitCollectors

func InitCollectors(maxResAge, sweepFreq int) error

InitCollectors initializes the snmp and ping collectors with retention time and cleanup frequency. We have three collectors: - /metrics for internal poll related metrics - /snmpmetrics for snmp polling results - /pingmetrics for ping results

func NewInfluxClient

func NewInfluxClient(host, user, passwd, db, rp string, timeout, retries int) error

NewInfluxClient creates a new influx client and connects to the influx db.

func NewKafkaClient

func NewKafkaClient(hosts []string, topic string, partition int) error

NewKafkaClient creates a new kafka client and connects to the broker.

func SnmpScrapeCount

func SnmpScrapeCount() int

SnmpScrapeCount returns the number of prometheus snmp scrapes. Returns 0 if the collector is not initialized.

Types

type IndexedResults

type IndexedResults struct {
	// Name is the measure name.
	Name string `json:"name"`

	// Results is an 2-dimensional array of all results for this indexed measure
	// with the index as first dimension and the oid as second dimension.
	Results [][]Result `json:"metrics"`

	// ToInflux tells wether this measure is exported to influxDB
	ToInflux bool `json:"to_influx,omitempty"`

	// ToKafka tells wether this measure is exported to kafka
	ToKafka bool `json:"to_kafka,omitempty"`

	// ToProm tells wether this measure is exported to prometheus
	ToProm bool `json:"to_prom,omitempty"`

	// LabelsOnly tells wether the measure is label-only
	LabelsOnly bool `json:"labels_only,omitempty"`
}

IndexedResults is an indexed measure results.

func MakeIndexed

func MakeIndexed(uid string, meas model.IndexedMeasure, tabResults []TabularResults) IndexedResults

MakeIndexed builds an indexed results set from a TabularResults array. All results at the same key are grouped together. Note: tabResults[i] is an array of results for a given oid on all indexes and tabResults is a list of these results for all oids.

func (*IndexedResults) DedupDesc

func (x *IndexedResults) DedupDesc()

DedupDesc strips the description field from all entries of an indexed result, except the first one. This is essential to reduce the size of the json pushed to kafka.

func (*IndexedResults) Filter

func (x *IndexedResults) Filter(meas model.IndexedMeasure)

Filter filters the indexed result against the regex filter..

func (IndexedResults) String

func (x IndexedResults) String() string

String returns a string representation of an IndexedResults.

type InfluxClient

type InfluxClient struct {
	// Host is the influx server address
	Host string

	// User is the influx authentication user
	User string

	// Password is the user password
	Password string

	// Database is the influx measurements database
	Database string

	// RetentionPolicy is the retention policy applied to measures
	RetentionPolicy string

	// Timeout is the influx connection and push timeout
	Timeout int

	// WriteRetries is the number of write retries in case of failure
	WriteRetries int

	influxclient.Client
	// contains filtered or unexported fields
}

InfluxClient is the influxdb (v1) result pusher

func (*InfluxClient) Close

func (c *InfluxClient) Close()

Close closes the db connection.

func (*InfluxClient) Push

func (c *InfluxClient) Push(res PollResult)

Push pushes a new poll result to the influx server. It actually converts the poll result to influx batchpoints and send the latter to the batchpoints channel to be consumed by sendData()

type KafkaClient

type KafkaClient struct {
	// Hosts is the list of kafka brokers addresses
	Hosts []string

	// Topic is the kafka topic
	Topic string

	// Partition is the kafka partition number
	Partition int32

	kafka.Producer
	// contains filtered or unexported fields
}

KafkaClient is a kafka Producer sink for snmp results.

func (*KafkaClient) Close

func (c *KafkaClient) Close()

Close ends the kafka connection

func (*KafkaClient) Push

func (c *KafkaClient) Push(res PollResult)

Push pushes a poll result to the kafka result channel.

type PingCollector

type PingCollector struct {
	*PromCollector
}

PingCollector is a prometheus collector

func (*PingCollector) Push

func (c *PingCollector) Push(meas PingMeasure)

Push converts a ping measure to prometheus samples and pushes them to the sample queue.

type PingMeasure

type PingMeasure struct {
	// HostID is the host db id
	HostID int

	// Hostname is the pinged host name
	Hostname string

	// IPAddr is the ip address of the pinged host
	IPAddr string

	// Category is the device category (for profile identification)
	Category string

	// Vendor is the device vendor (for profile identification)
	Vendor string

	// Model is the device model (for profile identification)
	Model string

	// Min is the minimal RTT in seconds
	Min float64

	// Max is the maximal RTT in seconds
	Max float64

	// Avg is the average RTT in seconds
	Avg float64

	// Loss is the packet loss percentage
	Loss float64

	// Stamp is the ping request datetime
	Stamp time.Time
}

PingMeasure is the result of a ping request.

type PollResult

type PollResult struct {
	// RequestID is the polling job id
	RequestID string `json:"request_id"`

	// AgentID is the poller agent id
	AgentID int `json:"agent_id"`

	// IPAddr is the polled device IP address
	IPAddr string `json:"device_ipaddr"`

	// Scalar is the set of scalar measures results
	Scalar []ScalarResults `json:"scalar_measures,omitempty"`

	// Indexed is the set of indexed measures results
	Indexed []IndexedResults `json:"indexed_measures,omitempty"`

	// PollStart is the poll starting time
	PollStart time.Time `json:"poll_start"`

	// Duration is the total polling duration in ms
	Duration int64 `json:"poll_duration"`

	// PollErr is the error message returned by the poll request
	PollErr string `json:"poll_error,omitempty"`

	// Tags is the tag map associated with the result
	Tags map[string]string `json:"tags,omitempty"`

	// IsPartial tells if the result is partial due to a mid-request snmp timeout.
	IsPartial bool `json:"is_partial,omitempty"`
	// contains filtered or unexported fields
}

PollResult is the complete result set of a polling job

func (PollResult) Copy

func (p PollResult) Copy() PollResult

Copy returns a deep copy of PollResult.

func (*PollResult) PruneForKafka

func (p *PollResult) PruneForKafka()

PruneForKafka prunes PollResult to keep only metrics to be exported to kafka.

type PromCollector

type PromCollector struct {
	// Samples is the map of last samples kept in memory.
	Samples map[uint64]*PromSample

	// MaxResultAge is the max time a sample is kept in memory.
	MaxResultAge time.Duration

	// SweepFreq is the cleanup goroutine frequency to remove old metrics.
	SweepFreq time.Duration

	sync.Mutex
	// contains filtered or unexported fields
}

PromCollector represents a prometheus collector

func NewCollector

func NewCollector(maxResAge, sweepFreq int, endpoint string) (*PromCollector, error)

NewCollector creates a new prometheus collector

func (*PromCollector) Collect

func (c *PromCollector) Collect(ch chan<- prometheus.Metric)

Collect implements Prometheus.Collector

func (*PromCollector) Describe

func (c *PromCollector) Describe(ch chan<- *prometheus.Desc)

Describe implements Prometheus.Collector

type PromSample

type PromSample struct {
	// Name is the prometheus metric name in the form of <snmp measurement name>_<snmp metric name>.
	Name string

	// Desc is the metric description (usually the snmp oid).
	Desc string

	// Value is the metric value.
	Value float64

	// Labels is the metric label map.
	Labels map[string]string

	// Stamp is the metric timestamp (the snmp poll start time).
	Stamp time.Time
}

PromSample is a prometheus metric.

type Result

type Result struct {
	// Oid is the metric OID as returned by the device.
	Oid string `json:"oid"`

	// Name is the metric name (from SNMP MIB usually).
	Name string `json:"name"`

	// ExportedName is the name of the exported metric.
	ExportedName string `json:"exported_name"`

	// Description is the metric description copied from request.
	Description string `json:"description,omitempty"`

	// Value is the metric value converted to the corresponding Go type.
	Value interface{} `json:"value"`

	// AsLabel tells if the result is exported as a prometheus label.
	AsLabel bool `json:"as_label,omitempty"`

	// Index is the result index as extracted from the oid according to the index_pattern.
	Index string `json:"index,omitempty"`
	// contains filtered or unexported fields
}

Result represents a single snmp result

func MakeResult

func MakeResult(pdu gosnmp.SnmpPDU, metric model.Metric) (Result, error)

MakeResult builds a Result from a gosnmp PDU. The value is casted to its corresponding Go type when necessary. In particular, Counter64 values are converted to float as influx does not support them out of the box. Returns an error on snmp NoSuchObject reply or nil value.

func (Result) String

func (r Result) String() string

String returns a string representation of a Result.

type ScalarResults

type ScalarResults struct {
	// Name is the name of the result group
	Name string `json:"name"`

	// Results is the list of results of this measure
	Results []Result `json:"metrics"`

	// ToInflux tells wether this measure is exported to influxDB
	ToInflux bool `json:"to_influx,omitempty"`

	// ToKafka tells wether this measure is exported to kafka
	ToKafka bool `json:"to_kafka,omitempty"`

	// ToProm tells wether this measure is exported to prometheus
	ToProm bool `json:"to_prom,omitempty"`
}

ScalarResults is a scalar measure results.

type SnmpCollector

type SnmpCollector struct {
	*PromCollector
}

SnmpCollector is a prometheus collector for snmp datas.

func (*SnmpCollector) Push

func (c *SnmpCollector) Push(pollRes PollResult)

Push convert a poll result to prometheus samples and push them to the sample queue.

type SnmpRequest

type SnmpRequest struct {
	model.SnmpRequest

	// logger is the internal gosnmp compatible glog Logger.
	log.Logger
	// contains filtered or unexported fields
}

SnmpRequest is a model.SnmpRequest with a snmp connection handler and a logger.

func (*SnmpRequest) Close

func (s *SnmpRequest) Close()

Close closes all the opened snmp connections.

func (*SnmpRequest) Dial

func (s *SnmpRequest) Dial(ctx context.Context) error

Dial opens all the needed snmp connections to the device.

func (*SnmpRequest) Get

func (s *SnmpRequest) Get(ctx context.Context) (results []ScalarResults, err error)

Get fetches all the scalar measures results. Returns the last non-nil error from gosnmp.

func (SnmpRequest) MakePollResult

func (r SnmpRequest) MakePollResult() PollResult

MakePollResult builds a PollResult from an SnmpRequest.

func (*SnmpRequest) Poll

func (s *SnmpRequest) Poll(ctx context.Context) PollResult

Poll queries all metrics of the request and returns them in a PollResult. If there was a timeout while getting scalar results, we stop there, there is no Walk attempted to get the indexed results.

func (*SnmpRequest) UnmarshalJSON

func (s *SnmpRequest) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json unmarshaler for SnmpRequest.

func (*SnmpRequest) Walk

func (s *SnmpRequest) Walk(ctx context.Context) ([]IndexedResults, error)

Walk polls all the indexed measures and returns an array of IndexedResults in the same order as each indexed measure. On error, a partial result is still returned.

type TabularResults

type TabularResults map[string][]Result

TabularResults is a map of Result array containing all values for a given indexed oid. The map key is the result index extracted from the result oid: if IndexRegex is not defined, its the suffix of the base oid; otherwise, its the concatenation of all parenthesized subexpressions extracted from the result oid. For example, if a walk result of `oid` returns oid.i1->res1 oid.i1.i12->res11, oid.i1.i13->res12, oid.i2->res2, oid.i3.xxx->res3,... with i1,i2... as the index and i12,i13 the sub-index, the corresponding TabularResults is {i1=>[res1], i1.i12=>[res11], i1.i13=>[res12], i2=>[res2], i3=>[res3], ...}

Jump to

Keyboard shortcuts

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