forked from floyd871/prometheus_oracle_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alertlog.go
164 lines (149 loc) · 3.92 KB
/
alertlog.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"bufio"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/prometheus/common/log"
)
type Client struct {
Ip string `yaml:"ip"`
Date string `yaml:"date"`
}
type Lastlog struct {
Instance string `yaml:"instance"`
Clients []Client `yaml:"clients"`
}
type Lastlogs struct {
Cfgs []Lastlog `yaml:"lastlog"`
}
type oraerr struct {
ora string
text string
ignore string
count int
}
var (
Errors []oraerr
oralayout = "Mon Jan 02 15:04:05 2006"
lastlog Lastlogs
)
// Get individual ScrapeTime per Prometheus instance for alertlog
func (e *Exporter) GetLastScrapeTime(conf int) time.Time {
for i, _ := range lastlog.Cfgs {
if lastlog.Cfgs[i].Instance == config.Cfgs[conf].Instance {
for n, _ := range lastlog.Cfgs[i].Clients {
if lastlog.Cfgs[i].Clients[n].Ip == e.lastIp {
t, _ := time.Parse("2006-01-02 15:04:05 -0700 MST", string(lastlog.Cfgs[i].Clients[n].Date))
return t
}
}
}
}
return time.Now()
}
// Set individual ScrapeTime per Prometheus instance for alertlog
func (e *Exporter) SetLastScrapeTime(conf int, t time.Time) {
var indInst int = -1
var indIp int = -1
for i, _ := range lastlog.Cfgs {
if lastlog.Cfgs[i].Instance == config.Cfgs[conf].Instance {
indInst = i
for n, _ := range lastlog.Cfgs[i].Clients {
if lastlog.Cfgs[i].Clients[n].Ip == e.lastIp {
indIp = n
}
}
}
}
if indInst == -1 {
cln := Client{Ip: e.lastIp, Date: t.Format("2006-01-02 15:04:05 -0700 MST")}
lastlog.Cfgs = append(lastlog.Cfgs, Lastlog{Instance: config.Cfgs[conf].Instance,
Clients: []Client{cln}})
} else {
if indIp == -1 {
cln := Client{Ip: e.lastIp, Date: t.Format("2006-01-02 15:04:05 -0700 MST")}
lastlog.Cfgs[indInst].Clients = append(lastlog.Cfgs[indInst].Clients, cln)
} else {
lastlog.Cfgs[indInst].Clients[indIp].Date = t.Format("2006-01-02 15:04:05 -0700 MST")
}
}
}
func addError(conf int, ora string, text string) {
var found bool = false
for i, _ := range Errors {
if Errors[i].ora == ora {
Errors[i].count++
found = true
}
}
if !found {
ignore := "0"
for _, e := range config.Cfgs[conf].Alertlog[0].Ignoreora {
if e == ora {
ignore = "1"
}
}
is := strings.Index(text, " ")
ip := strings.Index(text, ". ")
if is < 0 {
is = 0
}
if ip < 0 {
ip = len(text)
}
ora := oraerr{ora: ora, text: text[is+1 : ip], ignore: ignore, count: 1}
Errors = append(Errors, ora)
}
}
func (e *Exporter) ScrapeAlertlog() {
loc := time.Now().Location()
re := regexp.MustCompile(`O(RA|GG)-[0-9]+`)
ReadAccess()
for conf, _ := range config.Cfgs {
if len(config.Cfgs[conf].Alertlog) > 0 {
var lastTime time.Time
Errors = nil
lastScrapeTime := e.GetLastScrapeTime(conf).Add(time.Second)
info, err := os.Stat(config.Cfgs[conf].Alertlog[0].File)
file, err := os.Open(config.Cfgs[conf].Alertlog[0].File)
if err != nil {
log.Infoln(err)
} else {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
t, err := time.ParseInLocation(oralayout, scanner.Text(), loc)
if err == nil {
lastTime = t
} else {
if lastTime.After(lastScrapeTime) {
if re.MatchString(scanner.Text()) {
ora := re.FindString(scanner.Text())
addError(conf, ora, scanner.Text())
}
}
}
}
e.SetLastScrapeTime(conf, lastTime)
for i, _ := range Errors {
e.alertlog.WithLabelValues(config.Cfgs[conf].Database,
config.Cfgs[conf].Instance,
Errors[i].ora,
strings.ToValidUTF8(Errors[i].text,""),
Errors[i].ignore).Set(float64(Errors[i].count))
WriteLog(config.Cfgs[conf].Instance + " " + e.lastIp +
" (" + Errors[i].ignore + "/" + strconv.Itoa(Errors[i].count) + "): " +
Errors[i].ora + " - " + Errors[i].text)
}
e.alertdate.WithLabelValues(config.Cfgs[conf].Database,
config.Cfgs[conf].Instance).Set(float64(info.ModTime().Unix()))
}
if file != nil {
file.Close()
}
}
}
WriteAccess()
}