Skip to content

Commit

Permalink
Merge f2f879f into e15ea62
Browse files Browse the repository at this point in the history
  • Loading branch information
dhamith93 committed Oct 31, 2021
2 parents e15ea62 + f2f879f commit 5e723f4
Show file tree
Hide file tree
Showing 18 changed files with 1,376 additions and 520 deletions.
25 changes: 11 additions & 14 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Custom timeseries data can be added using the agent as below. Current server tim
This can be automated using a cron or a script

### API documentation

#### API Authenticatication
* Client app
* Since the API call authentication is handled through the client app, API calls via the client app can be used without any authentication.
#### End points
* `/system`
* Returns system info
Expand All @@ -80,29 +84,29 @@ This can be automated using a cron or a script
* Return disk info/usage
```json
{
Time: "time",
Disks: [["file system", "mount point", "type", "size", "free", "used", "used%", "inodes", "inodes free", "inodes used", "inodes used%"]]
"Time": "time",
"Disks": [["file system", "mount point", "type", "size", "free", "used", "used%", "inodes", "inodes free", "inodes used", "inodes used%"]]
}
```
* `/network`
* Return disk info/usage
```json
{
Time: "time",
Networks: [["ip", "interface", "rx", "tx"]]
"Time": "time",
"Networks": [["ip", "interface", "rx", "tx"]]
}
```
* `/processes`
* Returns list of processes using most CPU/RAM (10 each)
```json

{
CPU: [
"CPU": [
["pid", "cpu", "mem", "command"],
["pid", "cpu", "mem", "command"]
],

Memory: [
"Memory": [
["pid", "cpu", "mem", "command"],
["pid", "cpu", "mem", "command"]
]
Expand All @@ -126,7 +130,7 @@ This can be automated using a cron or a script
* `/custom-metric-names`
* Names of custom metrics
```json
{CustomMetrics: ["active-http-requests",]}
{"CustomMetrics": ["active-http-requests",]}
```
* `/custom?custom-metric=name`
* Custom metric values
Expand All @@ -139,13 +143,6 @@ This can be automated using a cron or a script
* `from=[unix timestamp]&to=[unix timestamp]` values within a time range
* `time=[unix timestamp]` value(s) at a certain time (depends on availability of data)

#### API Authenticatication
* Client app
* Since the API call authentication is handled through the client app, API calls via the client app can be used without any authentication.

* Collector app
* To use with collector app, API calls must have JWT in header derived from the key file.

## Todo
* Disk I/O graphs
* Alerts
Expand Down
2 changes: 1 addition & 1 deletion agent/config-example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ServerId": "unique-id",
"MonitorEndpoint": "http://localhost:9900/collect",
"MonitorEndpoint": "localhost:9900",
"LogFileEnabled": false,
"LogFilePath": "",
"SQLiteDBLoggingEnabled": false,
Expand Down
76 changes: 63 additions & 13 deletions agent/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
Expand All @@ -10,9 +11,13 @@ import (
"sync"
"time"

"github.com/dhamith93/SyMon/internal/api"
"github.com/dhamith93/SyMon/internal/auth"
"github.com/dhamith93/SyMon/internal/config"
"github.com/dhamith93/SyMon/internal/logger"
"github.com/dhamith93/SyMon/internal/monitor"
"github.com/dhamith93/SyMon/internal/send"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

func main() {
Expand All @@ -37,11 +42,11 @@ func main() {
flag.Parse()

if *initPtr {
initAgent(config)
initAgent(&config)
return
} else if *customPtr {
if len(name) > 0 && len(value) > 0 && len(unit) > 0 {
sendCustomMetric(name, unit, value, config)
sendCustomMetric(name, unit, value, &config)
} else {
fmt.Println("Metric name, unit, and value all required")
}
Expand All @@ -56,8 +61,8 @@ func main() {
for {
select {
case <-ticker.C:
monitorData := monitor.MonitorAsJSON(config)
send.SendPost(config.MonitorEndpoint, monitorData)
monitorData := monitor.MonitorAsJSON(&config)
sendMonitorData(monitorData, &config)
case <-quit:
ticker.Stop()
return
Expand All @@ -68,16 +73,33 @@ func main() {
fmt.Println("Exiting")
}

func initAgent(config config.Config) {
_, code, response := send.SendGet(config.MonitorEndpoint + "-init?serverId=" + config.ServerId + "&timezone=" + monitor.GetSystem().TimeZone)
if code == -1 {
fmt.Println("Cannot connect to collector, make sure config.json is correct and collector is running.")
} else {
fmt.Println(response)
func initAgent(config *config.Config) {
conn, c, ctx, cancel := createClient(config)
defer conn.Close()
defer cancel()
response, err := c.InitAgent(ctx, &api.ServerInfo{
ServerName: config.ServerId,
Timezone: monitor.GetSystem().TimeZone,
})
if err != nil {
logger.Log("error", "error adding agent: "+err.Error())
os.Exit(1)
}
fmt.Printf("%s \n", response.Body)
}

func sendMonitorData(monitorData string, config *config.Config) {
conn, c, ctx, cancel := createClient(config)
defer conn.Close()
defer cancel()
_, err := c.HandleMonitorData(ctx, &api.MonitorData{MonitorData: monitorData})
if err != nil {
logger.Log("error", "error sending data: "+err.Error())
os.Exit(1)
}
}

func sendCustomMetric(name string, unit string, value string, config config.Config) {
func sendCustomMetric(name string, unit string, value string, config *config.Config) {
customMetric := monitor.CustomMetric{
Name: name,
Unit: unit,
Expand All @@ -90,5 +112,33 @@ func sendCustomMetric(name string, unit string, value string, config config.Conf
fmt.Println(err.Error())
return
}
send.SendPost(config.MonitorEndpoint+"-custom", string(jsonData))
conn, c, ctx, cancel := createClient(config)
defer conn.Close()
defer cancel()
_, err = c.HandleCustomMonitorData(ctx, &api.MonitorData{MonitorData: string(jsonData)})
if err != nil {
logger.Log("error", "error sending custom data: "+err.Error())
os.Exit(1)
}
}

func generateToken() string {
token, err := auth.GenerateJWT()
if err != nil {
logger.Log("error", "error generating token: "+err.Error())
os.Exit(1)
}
return token
}

func createClient(config *config.Config) (*grpc.ClientConn, api.MonitorDataServiceClient, context.Context, context.CancelFunc) {
conn, err := grpc.Dial(config.MonitorEndpoint, grpc.WithInsecure())
if err != nil {
logger.Log("error", "connection error: "+err.Error())
os.Exit(1)
}
c := api.NewMonitorDataServiceClient(conn)
token := generateToken()
ctx, cancel := context.WithTimeout(metadata.NewOutgoingContext(context.Background(), metadata.New(map[string]string{"jwt": token})), time.Second*1)
return conn, c, ctx, cancel
}
2 changes: 1 addition & 1 deletion client/config-example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Port": "9000",
"MonitorEndpoint": "http://localhost:9900/",
"MonitorEndpoint": "localhost:9900",
"LogFileEnabled": true,
"LogFilePath": "/home/dhamith93/tmp/symon_collector.log"
}
Loading

0 comments on commit 5e723f4

Please sign in to comment.