/
status.go
84 lines (75 loc) · 1.97 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* status.dat handling */
package nagios
import (
"bufio"
"io"
"regexp"
"sync"
)
var blockStartRegex = regexp.MustCompile(`^(contactstatus|hoststatus|info|programstatus|servicecomment|servicestatus)\s\{`)
var blockEndRegex = regexp.MustCompile(`^\s}$`)
var kvRegex = regexp.MustCompile(`^\s*(\S+?)=(.*)$`)
type Status struct {
Host map[string]Host `json:"host"`
Service map[string]map[string]Service `json:"service"`
Summary Summary `json:"summary"`
sync.RWMutex
}
func LoadStatus(r io.Reader) (Status, error) {
var status Status
var err error
scanner := bufio.NewScanner(r)
status.Host = make(map[string]Host)
status.Service = make(map[string]map[string]Service)
block_type := ""
block_content := make(map[string]string)
for scanner.Scan() {
t := scanner.Text()
match := blockStartRegex.FindStringSubmatch(t)
if len(match) > 1 {
block_type = match[1]
continue
}
match = kvRegex.FindStringSubmatch(t)
if len(match) > 1 {
block_content[match[1]] = match[2]
continue
}
if blockEndRegex.MatchString(t) {
if block_type == "servicestatus" {
s, err := NewServiceFromMap(block_content)
if err == nil {
if status.Service[s.Hostname] == nil {
status.Service[s.Hostname] = make(map[string]Service)
}
status.Service[s.Hostname][s.Description] = s
} else {
return status, err
}
} else if block_type == "hoststatus" {
h, err := NewHostFromMap(block_content)
if err == nil {
status.Host[h.Hostname] = h
} else {
return status, err
}
}
//end of block summary ,cleanup vars
block_type = ""
block_content = make(map[string]string)
continue
}
}
status.Summary.UpdateHost(status.Host)
status.Summary.UpdateService(status.Service)
return status, err
}
func (s *Status) UpdateStatus(r io.Reader) error {
status, err := LoadStatus(r)
s.Lock()
s.Host = status.Host
s.Service = status.Service
s.Summary = status.Summary
s.Unlock()
return err
}