Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .mk/easyjson.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
GEN_EASYJSON_FILES = $(patsubst %.go,%_easyjson.go,$(shell git grep //go:generate | grep "easyjson" | grep -v Makefile | cut -d ":" -f 1))

%_easyjson.go: %.go
go generate -run easyjson $<

.PHONY: .easyjson
.easyjson: $(GEN_EASYJSON_FILES)

.PHONY: .easyjson.clean
.easyjson.clean:
find . \( -name *_easyjson.go ! -path './vendor/*' \) -exec rm {} \;

11 changes: 11 additions & 0 deletions .mk/gendecoder.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GEN_DECODER_FILES = $(patsubst %.go,%_gendecoder.go,$(shell git grep //go:generate | grep "gendecoder" | grep -v Makefile | cut -d ":" -f 1))

%_gendecoder.go: %.go
go generate -run gendecoder $<

.PHONY: .gendecoder
.gendecoder: $(GEN_DECODER_FILES)

.PHONY: .gendecoder.clean
.gendecoder.clean:
find . \( -name *_gendecoder.go \) -exec rm {} \;
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SUBDIRS := $(wildcard */.)

all: $(SUBDIRS)
$(SUBDIRS):
$(MAKE) -C $@

.PHONY: all $(SUBDIRS)
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Skydive plugin probes repository

Work In Progress

This repository hosts probe plugins. Probe Plugins can be Agent and/or Analyzer
probe, please check probe README file for more details.

# Installation

Just run `make` to compile all the plugins or enter to sub directory to compile specific one.

# Examples

You can have a look at the `sample` plugin which brings the minimal required to write a probe or the `memory` one,
a bit more complex but still simple registering real data structure and live updating the Skydive graph.

## Contributing

Your contributions are more than welcome. Please check
https://github.com/skydive-project/skydive/blob/master/CONTRIBUTING.md
to know about the process.

## License

This software is licensed under the Apache License, Version 2.0 (the
"License"); you may not use this software except in compliance with the
License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
10 changes: 10 additions & 0 deletions memory/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export GO111MODULE?=on

include $(abspath ../.mk/easyjson.mk)
include $(abspath ../.mk/gendecoder.mk)

memory.so: genlocalfiles
go build -buildmode=plugin ./...

.PHONY: genlocalfiles
genlocalfiles: .gendecoder .easyjson
15 changes: 15 additions & 0 deletions memory/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/skydive-project/skydive-plugins

require (
github.com/mailru/easyjson v0.7.0
github.com/shirou/gopsutil v0.0.0-20180111024713-6a368fb7cd12
github.com/skydive-project/skydive v0.0.0-20191009170754-872a2c0bfd34
github.com/spf13/viper v1.4.0
)

replace (
github.com/skydive-project/skydive => /home/safchain/code/gocode/src/github.com/skydive-project/skydive
github.com/spf13/viper v1.4.0 => github.com/lebauce/viper v0.0.0-20190903114911-3b7a98e30843
golang.org/x/sys => golang.org/x/sys v0.0.0-20190412213103-97732733099d
golang.org/x/tools => golang.org/x/tools v0.0.0-20190925230517-ea99b82c7b93
)
727 changes: 727 additions & 0 deletions memory/go.sum

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions memory/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//go:generate go run github.com/skydive-project/skydive/scripts/gendecoder --package github.com/skydive-project/skydive-plugins/memory
//go:generate go run github.com/mailru/easyjson/easyjson $GOFILE

/*
* Copyright (C) 2019 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// This package implement a probe which reports memory usage to the host node.
// This probe is fairly basic and provide a good example
// to start writting a probe.
package main

import (
json "encoding/json"
"fmt"
"time"

"github.com/shirou/gopsutil/mem"

"github.com/skydive-project/skydive/common"
"github.com/skydive-project/skydive/graffiti/graph"
"github.com/skydive-project/skydive/probe"
"github.com/skydive-project/skydive/topology/probes"
)

// ProbeHandler describes a memory probe handler.
// It implements the probe.Handler interface.
type ProbeHandler struct {
Ctx probes.Context
quit chan struct{}
}

// Usage defines how the topology metadata will look like. Tags below
// are used in order to generate marshal/unmarshal functions and getters.
// easyjson:json
// gendecoder
type Usage struct {
Total int64
Free int64
UsedPercent int64
}

// Start the handler. Implements the probe.Handler interface.
func (p *ProbeHandler) Start() error {
// start a goroutine in order to update the graph
go func() {
// update the graph each five seconds
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()

for {
select {
case <-p.quit:
// got a message on the quit chan
return
case <-ticker.C:
v, err := mem.VirtualMemory()
if err != nil {
p.Ctx.Logger.Errorf("unable to retrieve memory information: %s", err)
continue
}

usage := &Usage{
Total: int64(v.Total),
Free: int64(v.Free),
UsedPercent: int64(v.UsedPercent),
}

// lock the graph for modification
p.Ctx.Graph.Lock()

// add metadata entry to the root node
p.Ctx.Graph.AddMetadata(p.Ctx.RootNode, "Memory", usage)

// release the graph lock
p.Ctx.Graph.Unlock()
}
}
}()

return nil
}

// Stop the handler. Implements the probe.Handler interface.
func (p *ProbeHandler) Stop() {
p.quit <- struct{}{}
}

// NewAgentProbe returns a new topology memory probe
func NewAgentProbe(ctx probes.Context, bundle *probe.Bundle) (probes.Handler, error) {
return &ProbeHandler{
Ctx: ctx,
quit: make(chan struct{}),
}, nil
}

// MetadataDecoder implements a json message raw decoder. It is used
// by the analyzer to decode properly the JSON message with the proper types.
// the Getter interface functions will be generated by the gendecoder generator.
// See the first line and the tag at the struct declaration.
func MetadataDecoder(raw json.RawMessage) (common.Getter, error) {
var usage Usage
if err := json.Unmarshal(raw, &usage); err != nil {
return nil, fmt.Errorf("unable to unmarshal routing table %s: %s", string(raw), err)
}

return &usage, nil
}

// RegisterDecoders registers metadata decoders
func RegisterDecoders() {
// register the MemoryDecoder so that the graph knows how to decode the
// Memory metadata field.
graph.NodeMetadataDecoders["Memory"] = MetadataDecoder
}

func main() {
}
99 changes: 99 additions & 0 deletions memory/memory_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions memory/memory_gendecoder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading