master
go 153 lines 3.5 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package monit
4
5 // status_xml(): https://bitbucket.org/tildeslash/monit/src/5467d37d70c3c63c5760cddb93831bde4e17c14b/src/http/xml.c#lines-631
6 type monitStatus struct {
7 Server *statusServer `xml:"server"`
8 Services []statusServiceCheck `xml:"service"`
9 }
10
11 type statusServer struct {
12 ID string `xml:"id"`
13 Version string `xml:"version"`
14 Uptime int64 `xml:"uptime"`
15 LocalHostname string `xml:"localhostname"`
16 }
17
18 // status_service(): https://bitbucket.org/tildeslash/monit/src/5467d37d70c3c63c5760cddb93831bde4e17c14b/src/http/xml.c#lines-196
19 // struct Service_T: https://bitbucket.org/tildeslash/monit/src/5467d37d70c3c63c5760cddb93831bde4e17c14b/src/monit.h#lines-1212
20 type statusServiceCheck struct {
21 Type string `xml:"type,attr"`
22 Name string `xml:"name"`
23
24 Status int `xml:"status"` // Error flags bitmap
25
26 // https://bitbucket.org/tildeslash/monit/src/5467d37d70c3c63c5760cddb93831bde4e17c14b/src/monit.h#lines-269
27 MonitoringStatus int `xml:"monitor"`
28
29 // https://bitbucket.org/tildeslash/monit/src/5467d37d70c3c63c5760cddb93831bde4e17c14b/src/monit.h#lines-254
30 MonitorMode int `xml:"monitormode"`
31
32 // https://bitbucket.org/tildeslash/monit/src/5467d37d70c3c63c5760cddb93831bde4e17c14b/src/monit.h#lines-261
33 OnReboot int `xml:"onreboot"`
34
35 // https://bitbucket.org/tildeslash/monit/src/5467d37d70c3c63c5760cddb93831bde4e17c14b/src/monit.h#lines-248
36 PendingAction int `xml:"pendingaction"`
37 }
38
39 func (s *statusServiceCheck) id() string {
40 return s.svcType() + ":" + s.Name
41 }
42
43 func (s *statusServiceCheck) svcType() string {
44 // See enum Service_Type https://bitbucket.org/tildeslash/monit/src/master/src/monit.h
45
46 switch s.Type {
47 case "0":
48 return "filesystem"
49 case "1":
50 return "directory"
51 case "2":
52 return "file"
53 case "3":
54 return "process"
55 case "4":
56 return "host"
57 case "5":
58 return "system"
59 case "6":
60 return "fifo"
61 case "7":
62 return "program"
63 case "8":
64 return "network"
65 default:
66 return "unknown"
67 }
68 }
69
70 func (s *statusServiceCheck) status() string {
71 // https://bitbucket.org/tildeslash/monit/src/5467d37d70c3c63c5760cddb93831bde4e17c14b/src/http/cervlet.c#lines-2866
72
73 switch st := s.monitoringStatus(); st {
74 case "not_monitored", "initializing":
75 return st
76 default:
77 if s.Status != 0 {
78 return "error"
79 }
80 return "ok"
81 }
82 }
83
84 func (s *statusServiceCheck) monitoringStatus() string {
85 switch s.MonitoringStatus {
86 case 0:
87 return "not_monitored"
88 case 1:
89 return "monitored"
90 case 2:
91 return "initializing"
92 case 4:
93 return "waiting"
94 default:
95 return "unknown"
96 }
97 }
98
99 func (s *statusServiceCheck) monitorMode() string {
100 switch s.MonitorMode {
101 case 0:
102 return "active"
103 case 1:
104 return "passive"
105 default:
106 return "unknown"
107 }
108 }
109
110 func (s *statusServiceCheck) onReboot() string {
111 switch s.OnReboot {
112 case 0:
113 return "start"
114 case 1:
115 return "no_start"
116 default:
117 return "unknown"
118 }
119 }
120
121 func (s *statusServiceCheck) pendingAction() string {
122 switch s.PendingAction {
123 case 0:
124 return "ignored"
125 case 1:
126 return "alert"
127 case 2:
128 return "restart"
129 case 3:
130 return "stop"
131 case 4:
132 return "exec"
133 case 5:
134 return "unmonitor"
135 case 6:
136 return "start"
137 case 7:
138 return "monitor"
139 default:
140 return "unknown"
141 }
142 }
143
144 func (s *statusServiceCheck) hasServiceStatus() bool {
145 // https://bitbucket.org/tildeslash/monit/src/5467d37d70c3c63c5760cddb93831bde4e17c14b/src/util.c#lines-1721
146
147 const eventNonExist = 512
148 const eventData = 2048
149
150 return s.monitoringStatus() == "monitored" &&
151 !(s.Status&eventNonExist != 0) &&
152 !(s.Status&eventData != 0)
153 }