Skip to content

Commit

Permalink
docs: add plugin docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wuriyanto48 committed Nov 3, 2024
1 parent 3efe640 commit 9bfb86d
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/plugin/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY : test build clean format

build:
go build -buildmode=plugin -o templateplugin.so
48 changes: 48 additions & 0 deletions docs/plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Tob Plugin

https://pkg.go.dev/plugin

You can create custom functionality for services. This is needed when you want to create a service that is not currently available on Tob, or when you want to create a custom message that will appear on the monitoring Dashboard or a message that will appear on the Notificator.

There are several limitations currently at https://pkg.go.dev/plugin.
- Binary Plugins must be built with the same code as the code you use to build the main binary.

- The version of the operating system used to run the plugin binary and main binary must be the same as the one you used to build the plugin binary and main binary. For example, if you want to run the plugin binary and main binary on Ubuntu 20.04, you have to build it using Ubuntu 20.04.

### Getting started

- Clone the latest Tob code
```shell
https://github.com/telkomdev/tob.git
```

- create your plugin folder, for example `dummyplugin`
```shell
mkdir dummyplugin
```

- copy the `templateplugin.go` and `Makefile` into your `dummyplugin` folder
```shell
cp docs/plugin/templateplugin.go dummyplugin/
cp docs/plugin/Makefile dummyplugin/
```

- build the binary
```shell
cd dummyplugin/
make build
```

- add configuration to your `json config`, by adjusting 2 fields. The `kind` field is filled with `plugin`, and `pluginPath` is filled with plugin files with the extension `.so`.

```json
"dummy_plugin_one": {
"kind": "plugin",
"url": "https://www.google.com",
"checkInterval": 5,
"enable": false,
"tags": ["product 1", "product 2"],
"pics": ["bob", "john"],
"pluginPath": "/home/john/tob/dummyplugin/dummyplugin.so",
.....
```
159 changes: 159 additions & 0 deletions docs/plugin/templateplugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package main

import (
"log"
"math/rand"
"time"

"github.com/telkomdev/tob"
"github.com/telkomdev/tob/config"
"github.com/telkomdev/tob/util"
)

func main() {}

// TemplatePlugin service
type TemplatePlugin struct {
url string
recovered bool
lastDownTime string
enabled bool
verbose bool
logger *log.Logger
checkInterval int
stopChan chan bool
message string
notificatorConfig config.Config
}

// NewDummy Dummy's constructor
func NewTemplatePlugin(verbose bool, logger *log.Logger) *TemplatePlugin {
stopChan := make(chan bool, 1)
return &TemplatePlugin{
logger: logger,
verbose: verbose,

// by default service is recovered
recovered: true,
checkInterval: 0,
stopChan: stopChan,
}
}

// Name the name of the service
func (d *TemplatePlugin) Name() string {
return "plugin"
}

// Ping will try to ping the service
func (d *TemplatePlugin) Ping() []byte {
n := rand.Intn(100)
if n < 50 {
d.SetMessage("dummy plugin has an error")
return []byte("NOT_OK")
}

return []byte("OK")
}

// SetURL will set the service URL
func (d *TemplatePlugin) SetURL(url string) {
d.url = url
}

// Connect to service if needed
func (d *TemplatePlugin) Connect() error {
if d.verbose {
d.logger.Println("connect dummy")
}

return nil
}

// Close will close the service resources if needed
func (d *TemplatePlugin) Close() error {
if d.verbose {
d.logger.Println("close dummy plugin")
}

return nil
}

// SetRecover will set recovered status
func (d *TemplatePlugin) SetRecover(recovered bool) {
d.recovered = recovered
}

// IsRecover will return recovered status
func (d *TemplatePlugin) IsRecover() bool {
return d.recovered
}

// LastDownTime will set last down time of service to current time
func (d *TemplatePlugin) SetLastDownTimeNow() {
if d.recovered {
d.lastDownTime = time.Now().Format(util.YYMMDD)
}
}

// GetDownTimeDiff will return down time service difference in minutes
func (d *TemplatePlugin) GetDownTimeDiff() string {
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
}

// SetCheckInterval will set check interval to service
func (d *TemplatePlugin) SetCheckInterval(interval int) {
d.checkInterval = interval
}

// GetCheckInterval will return check interval to service
func (d *TemplatePlugin) GetCheckInterval() int {
return d.checkInterval
}

// Enable will set enabled status to service
func (d *TemplatePlugin) Enable(enabled bool) {
d.enabled = enabled
}

// IsEnabled will return enable status
func (d *TemplatePlugin) IsEnabled() bool {
return d.enabled
}

// SetMessage will set additional message
func (d *TemplatePlugin) SetMessage(message string) {
d.message = message
}

// GetMessage will return additional message
func (d *TemplatePlugin) GetMessage() string {
return d.message
}

// SetConfig will set config
func (d *TemplatePlugin) SetConfig(configs config.Config) {

}

// SetNotificatorConfig will set config
func (d *TemplatePlugin) SetNotificatorConfig(configs config.Config) {
d.notificatorConfig = configs
}

// GetNotificators will return notificators
func (d *TemplatePlugin) GetNotificators() []tob.Notificator {
return tob.InitNotificatorFactory(d.notificatorConfig, d.verbose)
}

// Stop will receive stop channel
func (d *TemplatePlugin) Stop() chan bool {
if d.stopChan == nil {
d.stopChan = make(chan bool, 1)
}

return d.stopChan
}

// Exported
var Service TemplatePlugin

0 comments on commit 9bfb86d

Please sign in to comment.