-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (79 loc) · 2.06 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
/*
The MIT License (MIT)
mysqldiff - mysql diff tool
invoke sys-mysql-diff scripts
zhe.chen<chenzhe07@gmail.com>
*/
package main
import (
"flag"
"log"
"os"
"strings"
"time"
)
var config struct {
Conf string
Debug bool
Useremail string
}
func main() {
logpath := flag.String("log", "", "")
conf := flag.String("conf", "", "config file to verify database and firewall info")
verbose := flag.Bool("verbose", false, "wheather print verbose message")
flag.Parse()
config.Conf = *conf
if len(config.Conf) <= 0 {
log.Printf("Error: You must secipfy the conf file")
os.Exit(1)
}
if *logpath != "" {
f, err := os.OpenFile(*logpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
}
conf_fh, err := get_config(config.Conf)
if err != nil {
log.Printf("Error: read config file error")
os.Exit(1)
}
backend_dsn, err := get_backend_dsn(conf_fh)
backend_dbh, err := dbh(backend_dsn)
if err != nil {
log.Printf("get backend dbh error: %s", err)
os.Exit(1)
}
log.Printf("begin ---------------------------")
sections := get_mysql_list(conf_fh)
for _, section := range sections {
if !strings.EqualFold(section, "default") &&
!strings.EqualFold(section, "backend") {
//check mysql list
startdiff := time.Now()
mysqlval, err := get_mysql_instance(conf_fh, section)
if err != nil {
log.Printf("Warn: get instance for section [%s] error: %v\n", section, err)
continue
}
mysqlval.changes, err = startDiff(&mysqlval)
if err != nil {
log.Printf("Warn: get changes for section [%s] error: %v\n", section, err)
continue
}
elapsed := time.Since(startdiff)
if *verbose {
log.Printf("=> changes from %s:%d, elapsed %s\n%s", mysqlval.host, mysqlval.port, elapsed, mysqlval.changes)
}
if len(mysqlval.changes) == 0 {
continue
}
if insertlog(backend_dbh, &mysqlval) {
log.Printf("insert %s:%d/%s ok", mysqlval.host, mysqlval.port, mysqlval.db)
}
}
}
log.Printf("end ---------------------------")
}