-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (81 loc) · 1.79 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"path"
"runtime"
"strconv"
"time"
_ "github.com/go-sql-driver/mysql"
log "github.com/sirupsen/logrus"
)
func main() {
initlog()
skipState := flag.Bool("skip-state", false, "Skip state")
onlyOnce := flag.Bool("once", false, "run once")
shVer := flag.Bool("version", false, "show version")
flag.Parse()
if *shVer {
fmt.Println(BuildVersion())
return
}
config := new(Config)
config.Load("config.yml")
// fmt.Println(serverconfig)
nsu := NSUpdater{}
nsu.Init(*config)
state := NewState("state.json")
if *skipState {
state.MaxId = 0
}
ipamdb := IpamDB{}
ipamdb.Open(config.DSN)
defer ipamdb.Close()
for {
ipamdb.ProcessChangelogRecords(state.MaxId, func(record ChangelogRecord) bool {
log.Infof(
"changeid=%v action=%v object[type=%v id=%v] => hostname:%v ip:%v\n",
record.cid,
record.caction,
record.ctype,
record.coid,
record.hostname,
record.ip,
)
if record.caction == "delete" {
nsu.Delete(record.hostname, record.ip)
}
if record.caction == "add" || record.caction == "edit" {
nsu.Ensure(record.hostname, record.ip)
}
return true
})
state.SetMaxId(ipamdb.maxlogid).Save()
if *onlyOnce {
break
}
time.Sleep(1 * time.Second)
}
}
func initlog() {
log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
CallerPrettyfier: func(frame *runtime.Frame) (function string, file string) {
fileName := path.Base(frame.File) + ":" + strconv.Itoa(frame.Line)
//return frame.Function, fileName
return "", fileName
},
})
log.SetReportCaller(true)
lvl, ok := os.LookupEnv("LOG_LEVEL")
if !ok || lvl == "" {
lvl = "info"
}
ll, err := log.ParseLevel(lvl)
if err != nil {
ll = log.DebugLevel
}
log.SetLevel(ll)
}