First commit

Adding Design Doc and the basic config file to this repository for now,
I will remove them later down the line
master
Ronald 4 months ago
commit c61a67d46e

@ -0,0 +1,58 @@
# Design
## Protocol
### Types | TYPE
0 => Request, a new request
1 => Result, the result
#### Request types | REQUEST
0 => New server client
1 => New desktop client
2 => Data
#### Response types | RESPONSE
0 => OK
1 => Created
2 => Not OK
#### Data types | DATA
CPU_USAGE => FLOAT => Percentage of CPU usage
### Server Client
1. When a client wants to connect to the server it sends an initial request detailing the type of it is, e.g. desktop client or server client.
```json
{
"TYPE": 0,
"REQUEST": 0,
"HOSTNAME": "ASGARD"
}
```
2. Server sends back the following response
```json
{
"TYPE": 1,
"RESULT": 0
}
```
3. Client starts
```json
{
"TYPE": 0,
"REQUEST": 2,
"HOSTNAME": "ASGARD",
"DATA": {
"TIMESTAMP": ,
"CPU_USAGE": 50,
"MEMORY_USAGE": 50,
"MEMORY_USED_GB": 1
}
}
```

@ -0,0 +1,8 @@
app_mode = development
[database]
type = "sqlite"
path = "sqlite.db"
[connection]
port = 7778

@ -0,0 +1,20 @@
package main
func GetAllHosts() (hosts []Host) {
DB.Find(&hosts)
return
}
func CreateHost(hostname string) bool {
host := Host{}
host.Hostname = hostname
result := DB.Create(&host)
// TODO: Handle errors
if result.Error != nil {
return false
}
return true
}

@ -0,0 +1,23 @@
module mon-server
go 1.25.0
require (
gopkg.in/ini.v1 v1.67.0
gorm.io/driver/postgres v1.6.0
gorm.io/driver/sqlite v1.6.0
gorm.io/gorm v1.30.3
)
require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/text v0.21.0 // indirect
)

@ -0,0 +1,42 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
gorm.io/gorm v1.30.3 h1:QiG8upl0Sg9ba2Zatfjy0fy4It2iNBL2/eMdvEkdXNs=
gorm.io/gorm v1.30.3/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=

@ -0,0 +1,202 @@
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"gopkg.in/ini.v1"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// Global variables
var DB *gorm.DB
type Data struct {
T *int `json:"TYPE"` // TYPE
Request *int `json:"REQUEST"` // REQUEST TYPE
Result *int `json:"RESPONSE,omitempty"` // RESPONSE TYPE
Hostname *string `json:"HOSTNAME"` // HOSTNAME
Data *map[string]any `json:"DATA,omitempty"` // DATA
}
func handleInitialConnection(conn net.Conn) {
initial_request := make([]byte, 256)
n, err := conn.Read(initial_request)
if err == io.EOF {
fmt.Fprintf(os.Stderr, "ERROR: client has disconnected: %s\n", conn.RemoteAddr())
conn.Close()
return
} else if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s failed with the error %v, closing connection\n", conn.RemoteAddr(), err)
conn.Close()
return
}
if n == 0 {
fmt.Fprintf(os.Stderr, "ERROR: received zero bytes, closing connection\n")
conn.Close()
return
}
data := Data{}
err = json.Unmarshal(initial_request[:n], &data)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: failed to parse data in initial request, closing connection\n")
conn.Close()
return
}
if *data.T != 0 || *data.Request != 0 || len(*data.Hostname) < 1 {
fmt.Fprintf(os.Stderr, "data: %v\n", data)
fmt.Fprintf(os.Stderr, "data.Hostname: \"%s\"\n", data.Hostname)
fmt.Fprintf(os.Stderr, "ERROR: invalid initial request, closing connection from %s\n", conn.RemoteAddr())
conn.Close()
return
}
switch *data.Request {
case 0:
var connectedResponse Data
connectedResponse.T = new(int)
connectedResponse.Result = new(int)
*connectedResponse.T = 1
foundHost := false
for _, host := range GetAllHosts() {
if strings.ToLower(host.Hostname) == strings.ToLower(*data.Hostname) {
*connectedResponse.T = 1
*connectedResponse.Result = 0
fmt.Printf("Found host in database\n")
foundHost = true
break
}
}
if !foundHost {
if ok := CreateHost(*data.Hostname); !ok {
fmt.Fprintf(os.Stderr, "ERROR: failed to create host in database, disconnecting client\n")
conn.Close()
return
}
fmt.Printf("Created host in database")
}
bytes, err := json.Marshal(connectedResponse)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: failed to create JSON to send back to client, closing connection\n")
conn.Close()
return
}
conn.Write(bytes)
// TODO: Handle client connections differently
handleServerConnection(conn)
}
}
func handleServerConnection(conn net.Conn) {
data := make([]byte, 4028)
for {
n, err := conn.Read(data)
if err == io.EOF {
fmt.Fprintf(os.Stderr, "ERROR: client has disconnected: %s\n", conn.RemoteAddr())
conn.Close()
return
} else if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s failed with the error %v, closing connection\n", conn.RemoteAddr(), err)
conn.Close()
return
}
if n == 0 {
continue
}
fmt.Printf("Read %d bytes from %s\n", n, conn.RemoteAddr())
fmt.Printf("data:\n\t'%s'\n", data)
}
}
func main() {
cfg, err := ini.Load("config.ini")
if err != nil {
log.Fatal("Fail to read file: ", err)
}
// Classic read of values, default section can be represented as empty string
log.Println("App Mode: ", cfg.Section("").Key("app_mode").String())
log.Println("Database Type: ", cfg.Section("database").Key("type").String())
if strings.ToLower(cfg.Section("database").Key("type").String()) == "postgres" {
log.Println("Database Host: ", cfg.Section("database").Key("host").String())
log.Println("Database Port: ", cfg.Section("database").Key("port").String())
log.Println("Database Username:", cfg.Section("database").Key("username").String())
log.Println("Database Name: ", cfg.Section("database").Key("DB_name").String())
databaseConnectString := fmt.Sprintf(
"host=%s port=%s user=%s DBname=%s password=%s sslmode=disable",
cfg.Section("database").Key("host"),
cfg.Section("database").Key("port"),
cfg.Section("database").Key("user"),
cfg.Section("database").Key("DB_name"),
cfg.Section("database").Key("password"),
)
log.Println(databaseConnectString)
DB, err = gorm.Open(postgres.Open(databaseConnectString), &gorm.Config{TranslateError: true})
if err != nil {
log.Fatal("Failed to connect to PostgreSQL DB")
}
} else if strings.ToLower(cfg.Section("database").Key("type").String()) == "sqlite" {
sqlite_path := cfg.Section("database").Key("path").String()
log.Println("sqlite DB Path: ", sqlite_path)
DB, err = gorm.Open(sqlite.Open(sqlite_path), &gorm.Config{TranslateError: true})
if err != nil {
log.Fatal("ERROR: failed to open sqlite database")
}
}
port := cfg.Section("connection").Key("port").String()
log.Println("Successfully connected to database")
err = DB.AutoMigrate(&CPU{})
if err != nil {
log.Fatal("Failed to migrate CPU schema")
}
err = DB.AutoMigrate(&Memory{})
if err != nil {
log.Fatal("Failed to migrate Memory schema")
}
err = DB.AutoMigrate(&Host{})
if err != nil {
log.Fatal("Failed to migrate Host schema")
}
addr := fmt.Sprintf(":%s", port)
ln, err := net.Listen("tcp", addr)
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: failed to listen on port with error: ", err)
os.Exit(-1)
}
fmt.Printf("Listening on %s\n", addr)
for {
conn, err := ln.Accept()
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: failed to connect the error: ", err)
continue
}
go handleInitialConnection(conn)
}
}

@ -0,0 +1,59 @@
package main
import (
"time"
"gorm.io/gorm"
)
type CPU struct {
gorm.Model `json:"-"`
ID uint `json:"id" gorm:"primary_key"`
Time time.Time `json:"time" gorm:"autoCreateTime"`
Name string `json:"name"`
Socket int `json:"socket"`
Cores int `json:"cores"`
HostID uint `json:"host_id"`
Host Host `json:"-" gorm:"foreignKey:ID;references:HostID"`
}
type CPUUsage struct {
gorm.Model `json:"-"`
ID uint `json:"id" gorm:"primary_key"`
Time time.Time `json:"time" gorm:"autoCreateTime"`
Core int `json:"core"`
Usage float64 `json:"usage"`
HostID uint `json:"host_id"`
Host Host `json:"-" gorm:"foreignKey:ID;references:HostID"`
}
type Memory struct {
gorm.Model `json:"-"`
ID uint `json:"id" gorm:"primary_key"`
Time time.Time `json:"time" gorm:"autoCreateTime"`
Type string `json:"type"`
Total int `json:"total"`
Used int `json:"used"`
Usage float64 `json:"usage"`
HostID uint `json:"host_id"`
Host Host `json:"-" gorm:"foreignKey:ID;references:HostID"`
}
type Host struct {
gorm.Model `json:"-"`
ID uint `json:"id" gorm:"primary_key;autoIncrement"`
Hostname string `json:"hostname" gorm:"unique"`
Description string `json:"description"`
Cpus []CPU `json:"cpus" gorm:"foreignKey:ID"`
Memory []Memory `json:"memory" gorm:"foreignKey:ID"`
CPUUsageID uint `json:"cpu_usage_id"`
}
type AddHost struct {
Hostname string `json:"hostname"`
}
type Error struct {
Status string `json:"status"`
Error string `json:"error"`
}
Loading…
Cancel
Save