forked from jehiah/go-daemontools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svstat.go
81 lines (74 loc) · 1.47 KB
/
svstat.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
package daemontools
import (
"encoding/binary"
"fmt"
"os"
"path"
"time"
)
type Status struct {
Service string
PID int
Paused bool
Want byte
When time.Time
NormallyUp bool
}
func (s *Status) String() (o string) {
hasPid := s.PID > 0
o = fmt.Sprintf("%s: ", s.Service)
if hasPid {
o += fmt.Sprintf("up (pid %d) ", s.PID)
} else {
o += "down "
}
seconds := time.Now().Unix() - s.When.Unix()
o += fmt.Sprintf("%d seconds ", seconds)
if hasPid && !s.NormallyUp {
o += " normally down"
}
if !hasPid && s.NormallyUp {
o += " normally up"
}
if hasPid && s.Paused {
o += " paused"
}
if !hasPid && s.Want == 'u' {
o += " want up"
}
if hasPid && s.Want == 'd' {
o += "want down"
}
return
}
func Svstat(service string) (s *Status, err error) {
var f *os.File
f, err = os.OpenFile(path.Join(service, "supervise", "ok"), os.O_WRONLY, 0)
if err != nil {
return nil, err
}
f.Close()
f, err = os.Open(path.Join(service, "supervise", "status"))
if err != nil {
return nil, err
}
b := make([]byte, 18)
n, err := f.Read(b)
if err != nil || n != 18 {
return nil, err
}
f.Close()
var normallyUp bool
if _, err := os.Stat(path.Join(service, "down")); err != nil && os.IsNotExist(err) {
normallyUp = true
}
s = &Status{
Service: service,
PID: int(binary.LittleEndian.Uint32(b[12:16])),
Paused: b[16] == '1',
Want: b[17],
When: taiUnpack(b[:8]),
NormallyUp: normallyUp,
}
return s, nil
}