icinga

Icinga2 API client in Go
git clone https://git.olowe.co/icinga
Log | Files | Refs | README | LICENSE

commit 8b3445266f32f04c7bd46718dd5948a6e1543f27
parent 9016a0fbf4855edbd3463ae2edf332d3a8f44466
Author: Oliver Lowe <o@olowe.co>
Date:   Wed,  8 Mar 2023 20:13:02 +1100

delete experimental object watcher

Diffstat:
Dcmd/i2cache/i2cache.go | 65-----------------------------------------------------------------
Dwatch.go | 71-----------------------------------------------------------------------
2 files changed, 0 insertions(+), 136 deletions(-)

diff --git a/cmd/i2cache/i2cache.go b/cmd/i2cache/i2cache.go @@ -1,65 +0,0 @@ -package main - -type Cache struct { - client *icinga.Client - hosts map[string]*hostEntry - services map[string]*serviceEntry -} - -func NewCache(client *icinga.Client) *Cache { - return &Cache{ - client: client, - hosts: make(map[string]*hostEntry), - services: make(map[string]*serviceEntry), - } -} - -type serviceEntry struct { - accessTime time.Time - watcher *ServiceWatcher - services []Service -} - -func (c *Cache) GetServices(expr string) []icinga.Service { - entry, ok := c.services[expr] - if !ok { - return nil - } - entry.accessTime = time.Now() - if entry.wacher == nil { - entry := entry - entry.watcher = WatchServices(c.client, expr) - go func() { - for msg := range entry.watcher.Updates { - if msg.Err != nil { - // TODO(otl) ?? - } - entry.services = msg.Services - } - }() - } - return entry.services -} - -type hostEntry struct { - accessTime time.Time - hosts []Host -} - -func (c *Cache) GetHosts(expr string) *hostEntry {} - -func (c *Cache) evictOlderThan(d time.Duration) { - t := time.Now().Sub(d) - for _, entry := range c.services { - if entry.accessTime.Before(t) { - entry.kill <- true - delete(c.services[expr]) - } - } - for _, entry := range c.Hosts { - if entry.accessTime.Before(t) { - entry.kill <- true - delete(c.hosts[expr]) - } - } -} diff --git a/watch.go b/watch.go @@ -1,71 +0,0 @@ -package icinga - -// A ServiceWatcher continuously queries Icinga about a set of Services. -// A ServiceWatcher uses the next check time of Services to determine when to query Icinga again. -// If it cannot determine the next check time, the Watcher waits 1 minute before trying again. -type ServiceWatcher struct { - kill chan bool - Updates chan ServiceMsg -} - -type HostWatcher struct { - Kill chan bool - Updates chan HostMsg -} - -type ServiceMsg struct { - Services []Service - Err error -} - -type HostMsg struct { - Hosts []Host - Err error -} - -// Kill stops the Watcher irrevocably. -// A new Watcher must be created with WatchServices. -func (w *ServiceWatcher) Kill() { - w.kill <- true -} - -// WatchServices returns a new Watcher which uses client to -// continuously query Icinga for Services matching the given filter -// expression. -func WatchServices(client *Client, expr string) *Watcher { - timer := time.NewTimer(0) - kill := make(chan bool) - ch := make(chan ServiceMsg) - go func() { - select { - case <-kill: - close(ch) - return - case <-timer.C: - services, err := client.Services(expr) - ch <- ServiceMsg{services, err} - if err != nil { - timer.Reset(1*time.Minute) - } else { - timer.Reset(time.Until(nextCheck(services))) - } - } - } - return &Watcher{ - kill: kill - Updates: ch - } -} - -func WatchHosts(expr string, ch chan HostMsg, stop chan bool) { - timer := time.NewTimer(0) - select { - case <-stop: - close(ch) - return - case <-timer.C: - hosts, err := client.Hosts(expr) - ch <- HostMsg{hosts, err} - timer.Reset(time.Until(NextCheck(hosts))) - } -}