Skip to content

Commit 9bfb86d

Browse files
committed
docs: add plugin docs
1 parent 3efe640 commit 9bfb86d

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed

docs/plugin/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY : test build clean format
2+
3+
build:
4+
go build -buildmode=plugin -o templateplugin.so

docs/plugin/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Tob Plugin
2+
3+
https://pkg.go.dev/plugin
4+
5+
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.
6+
7+
There are several limitations currently at https://pkg.go.dev/plugin.
8+
- Binary Plugins must be built with the same code as the code you use to build the main binary.
9+
10+
- 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.
11+
12+
### Getting started
13+
14+
- Clone the latest Tob code
15+
```shell
16+
https://github.com/telkomdev/tob.git
17+
```
18+
19+
- create your plugin folder, for example `dummyplugin`
20+
```shell
21+
mkdir dummyplugin
22+
```
23+
24+
- copy the `templateplugin.go` and `Makefile` into your `dummyplugin` folder
25+
```shell
26+
cp docs/plugin/templateplugin.go dummyplugin/
27+
cp docs/plugin/Makefile dummyplugin/
28+
```
29+
30+
- build the binary
31+
```shell
32+
cd dummyplugin/
33+
make build
34+
```
35+
36+
- 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`.
37+
38+
```json
39+
"dummy_plugin_one": {
40+
"kind": "plugin",
41+
"url": "https://www.google.com",
42+
"checkInterval": 5,
43+
"enable": false,
44+
"tags": ["product 1", "product 2"],
45+
"pics": ["bob", "john"],
46+
"pluginPath": "/home/john/tob/dummyplugin/dummyplugin.so",
47+
.....
48+
```

docs/plugin/templateplugin.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"math/rand"
6+
"time"
7+
8+
"github.com/telkomdev/tob"
9+
"github.com/telkomdev/tob/config"
10+
"github.com/telkomdev/tob/util"
11+
)
12+
13+
func main() {}
14+
15+
// TemplatePlugin service
16+
type TemplatePlugin struct {
17+
url string
18+
recovered bool
19+
lastDownTime string
20+
enabled bool
21+
verbose bool
22+
logger *log.Logger
23+
checkInterval int
24+
stopChan chan bool
25+
message string
26+
notificatorConfig config.Config
27+
}
28+
29+
// NewDummy Dummy's constructor
30+
func NewTemplatePlugin(verbose bool, logger *log.Logger) *TemplatePlugin {
31+
stopChan := make(chan bool, 1)
32+
return &TemplatePlugin{
33+
logger: logger,
34+
verbose: verbose,
35+
36+
// by default service is recovered
37+
recovered: true,
38+
checkInterval: 0,
39+
stopChan: stopChan,
40+
}
41+
}
42+
43+
// Name the name of the service
44+
func (d *TemplatePlugin) Name() string {
45+
return "plugin"
46+
}
47+
48+
// Ping will try to ping the service
49+
func (d *TemplatePlugin) Ping() []byte {
50+
n := rand.Intn(100)
51+
if n < 50 {
52+
d.SetMessage("dummy plugin has an error")
53+
return []byte("NOT_OK")
54+
}
55+
56+
return []byte("OK")
57+
}
58+
59+
// SetURL will set the service URL
60+
func (d *TemplatePlugin) SetURL(url string) {
61+
d.url = url
62+
}
63+
64+
// Connect to service if needed
65+
func (d *TemplatePlugin) Connect() error {
66+
if d.verbose {
67+
d.logger.Println("connect dummy")
68+
}
69+
70+
return nil
71+
}
72+
73+
// Close will close the service resources if needed
74+
func (d *TemplatePlugin) Close() error {
75+
if d.verbose {
76+
d.logger.Println("close dummy plugin")
77+
}
78+
79+
return nil
80+
}
81+
82+
// SetRecover will set recovered status
83+
func (d *TemplatePlugin) SetRecover(recovered bool) {
84+
d.recovered = recovered
85+
}
86+
87+
// IsRecover will return recovered status
88+
func (d *TemplatePlugin) IsRecover() bool {
89+
return d.recovered
90+
}
91+
92+
// LastDownTime will set last down time of service to current time
93+
func (d *TemplatePlugin) SetLastDownTimeNow() {
94+
if d.recovered {
95+
d.lastDownTime = time.Now().Format(util.YYMMDD)
96+
}
97+
}
98+
99+
// GetDownTimeDiff will return down time service difference in minutes
100+
func (d *TemplatePlugin) GetDownTimeDiff() string {
101+
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
102+
}
103+
104+
// SetCheckInterval will set check interval to service
105+
func (d *TemplatePlugin) SetCheckInterval(interval int) {
106+
d.checkInterval = interval
107+
}
108+
109+
// GetCheckInterval will return check interval to service
110+
func (d *TemplatePlugin) GetCheckInterval() int {
111+
return d.checkInterval
112+
}
113+
114+
// Enable will set enabled status to service
115+
func (d *TemplatePlugin) Enable(enabled bool) {
116+
d.enabled = enabled
117+
}
118+
119+
// IsEnabled will return enable status
120+
func (d *TemplatePlugin) IsEnabled() bool {
121+
return d.enabled
122+
}
123+
124+
// SetMessage will set additional message
125+
func (d *TemplatePlugin) SetMessage(message string) {
126+
d.message = message
127+
}
128+
129+
// GetMessage will return additional message
130+
func (d *TemplatePlugin) GetMessage() string {
131+
return d.message
132+
}
133+
134+
// SetConfig will set config
135+
func (d *TemplatePlugin) SetConfig(configs config.Config) {
136+
137+
}
138+
139+
// SetNotificatorConfig will set config
140+
func (d *TemplatePlugin) SetNotificatorConfig(configs config.Config) {
141+
d.notificatorConfig = configs
142+
}
143+
144+
// GetNotificators will return notificators
145+
func (d *TemplatePlugin) GetNotificators() []tob.Notificator {
146+
return tob.InitNotificatorFactory(d.notificatorConfig, d.verbose)
147+
}
148+
149+
// Stop will receive stop channel
150+
func (d *TemplatePlugin) Stop() chan bool {
151+
if d.stopChan == nil {
152+
d.stopChan = make(chan bool, 1)
153+
}
154+
155+
return d.stopChan
156+
}
157+
158+
// Exported
159+
var Service TemplatePlugin

0 commit comments

Comments
 (0)