diff options
| author | Shulhan <ms@kilabit.info> | 2025-08-21 01:09:31 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-08-21 01:09:31 +0700 |
| commit | ea4ba5169e61b17d2e666ab3bae4899346ed08ac (patch) | |
| tree | 37d41922e8f2e0ea3b26e49f5f4e093600483edc | |
| parent | 3f6ce287761776fd19de7793ff4dcd4bb0b6a3ba (diff) | |
| download | lilin-ea4ba5169e61b17d2e666ab3bae4899346ed08ac.tar.xz | |
all: store the service file name as service ID
Instead of using name, which may contains space, use the
file name as service ID.
In this way, the log file name will match with the service
file name.
| -rw-r--r-- | _www/index.tmpl | 4 | ||||
| -rw-r--r-- | reports.go | 2 | ||||
| -rw-r--r-- | scan_report.go | 2 | ||||
| -rw-r--r-- | service.go | 4 | ||||
| -rw-r--r-- | service_config.go | 13 | ||||
| -rw-r--r-- | service_report.go | 8 | ||||
| -rw-r--r-- | service_report_test.go | 2 | ||||
| -rw-r--r-- | service_test.go | 4 | ||||
| -rw-r--r-- | testdata/etc/lilin/service.d/http.cfg | 3 | ||||
| -rw-r--r-- | testdata/etc/lilin/service.d/tcp.cfg | 3 | ||||
| -rw-r--r-- | testdata/etc/lilin/service.d/udp.cfg | 3 | ||||
| -rw-r--r-- | worker.go | 26 | ||||
| -rw-r--r-- | worker_test.go | 9 |
13 files changed, 46 insertions, 37 deletions
diff --git a/_www/index.tmpl b/_www/index.tmpl index ae7d7a2..4880ec8 100644 --- a/_www/index.tmpl +++ b/_www/index.tmpl @@ -56,7 +56,7 @@ {{ $status := statusString .Last.Success }} <div class="service {{ $status }}"> <span class="at"> {{.Last.At}}</span> - <span class="name"> {{.Name}} </span> + <span class="name"> {{.ID}} </span> <span class="status">{{ $status }}</span> <span class="error">{{ .Last.Error }}</span> </div> @@ -75,7 +75,7 @@ {{ range $list }} <div class="service"> <span class="at">{{ .At }}</span> - <span class="name">{{ .Name }}</span> + <span class="name">{{ .ID }}</span> <span class="status"><span class="fail"> DOWN </span></span> <span class="error">{{ .Error }}</span> </div> @@ -19,7 +19,7 @@ type Reports struct { // Store the service scan report. func (reports *Reports) Store(scanReport ScanReport) { reports.Lock() - var svcReport = reports.Services[scanReport.Name] + var svcReport = reports.Services[scanReport.ID] svcReport.Store(scanReport) if !scanReport.Success { reports.Fail = append(reports.Fail, scanReport) diff --git a/scan_report.go b/scan_report.go index 4b4938d..c9946f6 100644 --- a/scan_report.go +++ b/scan_report.go @@ -7,7 +7,7 @@ import "time" // ScanReport contains the result of scanning service. type ScanReport struct { - Name string + ID string // The time when the scan started. At time.Time @@ -37,7 +37,7 @@ func NewService(cfg ServiceConfig) (svc *Service, err error) { func (svc *Service) Scan() (report ScanReport) { var err error - report.Name = svc.cfg.Name + report.ID = svc.cfg.ID report.At = internal.Now() if !svc.isReady { err = svc.connect() @@ -89,7 +89,7 @@ func (svc *Service) Start(reportq chan<- ScanReport) { for { _, ok := <-svc.ticker.C if !ok { - log.Printf("Service: %s not ok", svc.cfg.Name) + log.Printf("Service: %s not ok", svc.cfg.ID) return } reportq <- svc.Scan() diff --git a/service_config.go b/service_config.go index 9f9e0fc..b1261fa 100644 --- a/service_config.go +++ b/service_config.go @@ -26,24 +26,27 @@ const defInterval = 10 * time.Second type ServiceConfig struct { scanURL *url.URL - Name string + // ID of service, derived from config file without ".cfg" extension. + ID string + + Name string `ini:"service::name"` // The Address of service, using scheme based, for example // - http://example.com/health for HTTP service, // - tcp://127.0.0.1:22 for TCP service, // - udp://127.0.0.1:53 for UDP service. - Address string `ini:"::address"` + Address string `ini:"service::address"` // HTTPMethod define HTTP method to be used to scan the HTTP service. // Valid value is either DELETE, GET, HEAD, PATCH, POST, or PUT. - HTTPMethod string `ini:"::method"` + HTTPMethod string `ini:"service::method"` // Timeout for connecting and reading response from service. - Timeout string `ini:"::timeout"` + Timeout string `ini:"service::timeout"` // Interval between each scan. // The minimum value is 60 seconds or 1 minute. - Interval string `ini:"::interval"` + Interval string `ini:"service::interval"` timeout time.Duration interval time.Duration diff --git a/service_report.go b/service_report.go index 492e368..0959522 100644 --- a/service_report.go +++ b/service_report.go @@ -23,19 +23,19 @@ type ServiceReport struct { writer *csv.Writer logPath string - Name string + ID string Last ScanReport History []ScanReport } -func NewServiceReport(cfg ServerConfig, name string) ( +func NewServiceReport(id string, cfg ServerConfig) ( svcReport *ServiceReport, err error, ) { var logp = `NewServiceReport` svcReport = &ServiceReport{ - logPath: filepath.Join(cfg.logServiceDir, name+`.log`), - Name: name, + logPath: filepath.Join(cfg.logServiceDir, id+`.log`), + ID: id, History: make([]ScanReport, 0, historyMax), } diff --git a/service_report_test.go b/service_report_test.go index c61f543..350c7f3 100644 --- a/service_report_test.go +++ b/service_report_test.go @@ -21,7 +21,7 @@ func TestServiceReport_Store(t *testing.T) { } var svcReport *ServiceReport - svcReport, err = NewServiceReport(serverCfg, `testStore`) + svcReport, err = NewServiceReport(`testStore`, serverCfg) if err != nil { t.Fatal(err) } diff --git a/service_test.go b/service_test.go index aeb1562..ba31192 100644 --- a/service_test.go +++ b/service_test.go @@ -19,11 +19,11 @@ func TestServiceStart_HTTP(t *testing.T) { var listCase = []testCase{{ cfg: lilin.ServiceConfig{ - Name: `http_service`, + ID: `http_service`, Address: `http://` + dummyHTTPAddress + `/health`, }, expReport: lilin.ScanReport{ - Name: `http_service`, + ID: `http_service`, At: internal.Now(), Success: true, }, diff --git a/testdata/etc/lilin/service.d/http.cfg b/testdata/etc/lilin/service.d/http.cfg index 8b4b402..4f446d1 100644 --- a/testdata/etc/lilin/service.d/http.cfg +++ b/testdata/etc/lilin/service.d/http.cfg @@ -1,7 +1,8 @@ ## SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> ## SPDX-License-Identifier: GPL-3.0-only -[service "example http"] +[service] +name = example http method = GET address = http://127.0.0.1:6121/health timeout = 5s diff --git a/testdata/etc/lilin/service.d/tcp.cfg b/testdata/etc/lilin/service.d/tcp.cfg index e5ed060..2a8d4e4 100644 --- a/testdata/etc/lilin/service.d/tcp.cfg +++ b/testdata/etc/lilin/service.d/tcp.cfg @@ -1,6 +1,7 @@ ## SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> ## SPDX-License-Identifier: GPL-3.0-only -[service "example tcp"] +[service] +name = example tcp address = tcp://127.0.0.1:6122 timeout = 5s diff --git a/testdata/etc/lilin/service.d/udp.cfg b/testdata/etc/lilin/service.d/udp.cfg index e7ff083..d1e8891 100644 --- a/testdata/etc/lilin/service.d/udp.cfg +++ b/testdata/etc/lilin/service.d/udp.cfg @@ -1,6 +1,7 @@ ## SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> ## SPDX-License-Identifier: GPL-3.0-only -[service "example udp"] +[service] +name = example udp address = udp://127.0.0.1:6123 timeout = 5s @@ -35,8 +35,8 @@ func newWorker(cfg ServerConfig) (wrk *worker, err error) { wrk.Reports = Reports{ Services: make(map[string]*ServiceReport, len(wrk.Services)), } - for name := range wrk.Services { - wrk.Reports.Services[name], err = NewServiceReport(cfg, name) + for id := range wrk.Services { + wrk.Reports.Services[id], err = NewServiceReport(id, cfg) if err != nil { return nil, err } @@ -56,22 +56,22 @@ func (wrk *worker) loadServiceDir(cfg ServerConfig) (err error) { return err } - var svcConfigs serviceConfigs var de os.DirEntry + var extCfg = `.cfg` for _, de = range listde { if de.IsDir() { continue } - var name = de.Name() - if name[0] == '.' { + var fileName = de.Name() + if fileName[0] == '.' { // Exclude hidden file. continue } - if !strings.HasSuffix(name, `.cfg`) { + if !strings.HasSuffix(fileName, extCfg) { continue } - var serviceCfg = filepath.Join(cfg.configServiceDir, name) + var serviceCfg = filepath.Join(cfg.configServiceDir, fileName) var rawcfg []byte rawcfg, err = os.ReadFile(serviceCfg) @@ -79,19 +79,19 @@ func (wrk *worker) loadServiceDir(cfg ServerConfig) (err error) { return err } - err = ini.Unmarshal(rawcfg, &svcConfigs) + var svcConfig ServiceConfig + err = ini.Unmarshal(rawcfg, &svcConfig) if err != nil { return err } - } - var svc *Service - for name, svcConfig := range svcConfigs.Config { - svcConfig.Name = name + svcConfig.ID = strings.TrimSuffix(fileName, extCfg) + + var svc *Service svc, err = NewService(svcConfig) if err != nil { return err } - wrk.Services[name] = svc + wrk.Services[svcConfig.ID] = svc } return nil } diff --git a/worker_test.go b/worker_test.go index 8863542..c00339c 100644 --- a/worker_test.go +++ b/worker_test.go @@ -33,13 +33,14 @@ func TestNewWorker(t *testing.T) { BaseDir: `testdata/`, }, expServices: map[string]*Service{ - `example http`: &Service{ + `http`: &Service{ cfg: ServiceConfig{ scanURL: &url.URL{ Scheme: `http`, Host: `127.0.0.1:6121`, Path: `/health`, }, + ID: `http`, Name: `example http`, HTTPMethod: `GET`, Address: `http://127.0.0.1:6121/health`, @@ -48,12 +49,13 @@ func TestNewWorker(t *testing.T) { interval: defInterval, }, }, - `example tcp`: &Service{ + `tcp`: &Service{ cfg: ServiceConfig{ scanURL: &url.URL{ Scheme: `tcp`, Host: `127.0.0.1:6122`, }, + ID: `tcp`, Name: `example tcp`, Address: `tcp://127.0.0.1:6122`, Timeout: `5s`, @@ -61,12 +63,13 @@ func TestNewWorker(t *testing.T) { interval: defInterval, }, }, - `example udp`: &Service{ + `udp`: &Service{ cfg: ServiceConfig{ scanURL: &url.URL{ Scheme: `udp`, Host: `127.0.0.1:6123`, }, + ID: `udp`, Name: `example udp`, Address: `udp://127.0.0.1:6123`, Timeout: `5s`, |
