This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
maintail.go
95 lines (83 loc) · 2.96 KB
/
maintail.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
85
86
87
88
89
90
91
92
93
94
95
// pmm-managed
// Copyright (C) 2017 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package supervisord
import (
"regexp"
"strings"
"time"
)
type eventType string
const (
// mirror http://supervisord.org/subprocess.html#process-states
stopped eventType = "STOPPED"
stopping eventType = "STOPPING"
starting eventType = "STARTING"
running eventType = "RUNNING"
exitedExpected eventType = "EXITED (expected)"
exitedUnexpected eventType = "EXITED (unexpected)"
fatal eventType = "FATAL"
unknown eventType = "unknown"
logReopen eventType = "logreopen"
)
var (
stoppedRE = regexp.MustCompile(`^stopped\: ([\w-]+) \(exit status \d+\)$`)
stoppingRE = regexp.MustCompile(`^waiting for ([\w-]+) to stop$`)
startingRE = regexp.MustCompile(`^spawned\: '([\w-]+)' with pid \d+$`)
runningRE = regexp.MustCompile(`^success\: ([\w-]+) entered RUNNING state, process has stayed up for > than \d+ seconds \(startsecs\)$`)
exitedExpectedRE = regexp.MustCompile(`^exited\: ([\w-]+) \(exit status \d+; expected\)$`)
exitedUnexpectedRE = regexp.MustCompile(`^exited\: ([\w-]+) \(exit status \d+; not expected\)$`)
fatalRE = regexp.MustCompile(`^gave up\: ([\w-]+) entered FATAL state, too many start retries too quickly$`)
logReopenRE = regexp.MustCompile(`^([\w-]+) logreopen$`)
events = map[*regexp.Regexp]eventType{
stoppedRE: stopped,
stoppingRE: stopping,
startingRE: starting,
runningRE: running,
exitedExpectedRE: exitedExpected,
exitedUnexpectedRE: exitedUnexpected,
fatalRE: fatal,
logReopenRE: logReopen,
}
)
// event represents supervisord program event.
type event struct {
Time time.Time
Type eventType
Program string
}
// parseEvent returns parsed event from supervisord maintail line, or nil.
func parseEvent(line string) *event {
parts := strings.SplitN(line, " ", 4)
if len(parts) != 4 {
return nil
}
// see https://github.com/golang/go/issues/6189
ts := strings.Replace(parts[0]+" "+parts[1], ",", ".", 1)
t, err := time.Parse("2006-01-02 15:04:05.000", ts)
if err != nil {
return nil
}
for re, typ := range events {
if m := re.FindStringSubmatch(parts[3]); m != nil {
return &event{
Time: t,
Type: typ,
Program: m[1],
}
}
}
return nil
}