26
type FunctionTp struct {
27
Time string `json:"time"`
28
Key string `json:"key"`
29
HostName string `json:"hostname"`
30
ProcessState string `json:"processState"`
31
ElapsedTime string `json:"elapsedTime"`
34
type SystemAlive struct {
35
Key string `json:"key"`
36
HostName string `json:"hostname"`
37
Time string `json:"time"`
40
type BusinessAlarm struct {
41
Time string `json:"time"`
42
Key string `json:"key"`
43
HostName string `json:"hostname"`
44
BusinessType string `json:"type"`
45
Value string `json:"value"`
46
Detail string `json:"detail"`
50
FunctionTpSufixx = "tp.log"
51
SystemAliveSufixx = "alive.log"
52
BusinessAlarmSufixx = "business.log"
53
LogFileOpt = os.O_RDWR | os.O_CREATE | os.O_APPEND
55
BusinessAlarmType = "BusinessAlarm"
56
SystemAliveType = "SystemAlive"
57
FunctionTpType = "FunctionTp"
58
HostNameFile = "/proc/sys/kernel/hostname"
59
MaxLogSize = 1024 * 1024 * 10
63
FunctionTpLogWrite = &LogWrite{logCh: make(chan interface{}, ChSize)}
64
SystemAliveLogWrite = &LogWrite{logCh: make(chan interface{}, ChSize)}
65
BusinessAlarmLogWrite = &LogWrite{logCh: make(chan interface{}, ChSize)}
66
UmpDataDir = "/export/home/tomcat/UMP-Monitor/logs/"
70
logCh chan interface{}
78
jsonEncoder *json.Encoder
81
func (lw *LogWrite) initLogFp(sufixx string) (err error) {
84
lw.sigCh = make(chan bool, 1)
86
lw.logName = fmt.Sprintf("%s%s%s", UmpDataDir, "ump_", lw.logSufixx)
87
lw.bf = bytes.NewBuffer([]byte{})
88
lw.jsonEncoder = json.NewEncoder(lw.bf)
89
lw.jsonEncoder.SetEscapeHTML(false)
90
if lw.logFp, err = os.OpenFile(lw.logName, LogFileOpt, 0o666); err != nil {
93
if fi, err = lw.logFp.Stat(); err != nil {
96
lw.logSize = fi.Size()
101
func (lw *LogWrite) backGroundCheckFile() (err error) {
102
if lw.logSize <= MaxLogSize {
111
name := fmt.Sprintf("%s%s%s.%d", UmpDataDir, "ump_", lw.logSufixx, lw.seq)
112
if _, err = os.Stat(name); err == nil {
115
os.Rename(lw.logName, name)
117
if lw.logFp, err = os.OpenFile(lw.logName, LogFileOpt, 0o666); err != nil {
121
if err = os.Truncate(lw.logName, 0); err != nil {
130
func (lw *LogWrite) backGroundWrite(umpType string) {
136
tp := obj.(*FunctionTp)
137
lw.jsonEncoder.Encode(tp)
138
body = append(body, lw.bf.Bytes()...)
140
FunctionTpPool.Put(tp)
141
case SystemAliveType:
142
alive := obj.(*SystemAlive)
143
lw.jsonEncoder.Encode(alive)
144
body = append(body, lw.bf.Bytes()...)
146
SystemAlivePool.Put(alive)
147
case BusinessAlarmType:
148
alarm := obj.(*BusinessAlarm)
149
lw.jsonEncoder.Encode(alarm)
150
body = append(body, lw.bf.Bytes()...)
156
if lw.backGroundCheckFile() != nil {
159
body = append(body, []byte("\n")...)
161
lw.logSize += (int64)(len(body))
165
func initLogName(module, dataDir string) (err error) {
168
if !strings.HasSuffix(UmpDataDir, "/") {
172
return fmt.Errorf("warnLogDir dir not config")
174
if err = os.MkdirAll(UmpDataDir, 0o755); err != nil {
178
if HostName, err = GetLocalIpAddr(); err != nil {
182
if err = FunctionTpLogWrite.initLogFp(module + "_" + FunctionTpSufixx); err != nil {
186
if err = SystemAliveLogWrite.initLogFp(module + "_" + SystemAliveSufixx); err != nil {
190
if err = BusinessAlarmLogWrite.initLogFp(module + "_" + BusinessAlarmSufixx); err != nil {
197
func GetLocalIpAddr() (localAddr string, err error) {
198
addrs, err := net.InterfaceAddrs()
202
for _, addr := range addrs {
203
if ipNet, isIpNet := addr.(*net.IPNet); isIpNet && !ipNet.IP.IsLoopback() {
204
if ipv4 := ipNet.IP.To4(); ipv4 != nil {
205
localAddr = ipv4.String()
210
err = fmt.Errorf("cannot get local ip")
214
func backGroudWrite() {
215
go FunctionTpLogWrite.backGroundWrite(FunctionTpType)
216
go SystemAliveLogWrite.backGroundWrite(SystemAliveType)
217
go BusinessAlarmLogWrite.backGroundWrite(BusinessAlarmType)