-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipamdb.go
92 lines (79 loc) · 2.17 KB
/
ipamdb.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
package main
import (
"database/sql"
"regexp"
"time"
log "github.com/sirupsen/logrus"
)
type ChangelogRecord struct {
cid int
ctype string
coid int
caction string
cdate string
hostname string
ip string
cdiff string
}
type IpamDB struct {
db *sql.DB
maxlogid int
}
func (i *IpamDB) Close() {
if i != nil {
i.db.Close()
}
}
func (i *IpamDB) Open(dsn string) *IpamDB {
if dsn == "" {
panic("No DSN configured")
}
db, err := sql.Open("mysql", dsn)
i.db = db
if err != nil {
panic(err)
}
log.Info("Connected to database")
db.SetConnMaxIdleTime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
return i
}
var changelogHostnameFinder = regexp.MustCompile(`\[hostname\]. (.+)\r`)
var changelogIPFinder = regexp.MustCompile(`\[ip_addr\]. (.*?)\r`)
func (i *IpamDB) ProcessChangelogRecords(resumeFrom int, clrprocessor func(ChangelogRecord) bool) {
i.db.QueryRow("SELECT MAX(cid) FROM changelog").Scan(&i.maxlogid)
log.Debug("max id found in DB: ", i.maxlogid, "\tmax id previous run: ", resumeFrom)
rows, err := i.db.Query(`
SELECT cid, ctype, coid, caction, cdate, COALESCE(hostname, ""), COALESCE(INET_NTOA(ip_addr), INET_NTOA(ip_addr), "") AS ip, cdiff
FROM changelog
LEFT JOIN ipaddresses ON ctype = 'ip_addr' AND ipaddresses.id = coid
WHERE 1
AND ( hostname IS NOT NULL OR caction = "delete" )
AND cid > ? AND cid <= ?
AND cresult = 'success'
AND ctype = 'ip_addr'
AND ( cdiff LIKE '%\n[hostname]_ %' OR cdiff LIKE '[hostname]_ %' )
`, resumeFrom, i.maxlogid)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
clrecord := ChangelogRecord{}
if err := rows.Scan(&clrecord.cid, &clrecord.ctype, &clrecord.coid, &clrecord.caction, &clrecord.cdate, &clrecord.hostname, &clrecord.ip, &clrecord.cdiff); err != nil {
panic(err)
}
if clrecord.ip == "" {
matches := changelogIPFinder.FindAllStringSubmatch(clrecord.cdiff, -1)
clrecord.ip = matches[0][1]
}
if clrecord.hostname == "" {
matches := changelogHostnameFinder.FindAllStringSubmatch(clrecord.cdiff, -1)
if matches[0][1] != "/ " {
clrecord.hostname = matches[0][1]
}
}
clrprocessor(clrecord)
}
}