-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
122 lines (109 loc) · 3.06 KB
/
main.go
File metadata and controls
122 lines (109 loc) · 3.06 KB
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
package main
import (
"flag"
"fmt"
"github.com/BurntSushi/toml"
"github.com/fiorix/go-smpp/smpp"
"io"
logger "log"
"net/http"
"os"
"text/template"
)
var (
log *logger.Logger
tx *smpp.Transmitter
grafanaTmpl *template.Template
prometheusTmpl *template.Template
configFile, logFile string
debug bool
config struct {
ListenAddr string `toml:"listen_address"`
Sms struct {
Host string `toml:"host"`
Port int `toml:"port"`
SystemID string `toml:"system_id"`
Password string `toml:"password"`
} `toml:"sms"`
Grafana struct {
Enable bool `toml:"enable"`
Recipient []string `toml:"recipient"`
From string `toml:"from"`
User string `toml:"user"`
Password string `toml:"password"`
Template string `toml:"template"`
} `toml:"grafana"`
Prometheus struct {
Enable bool `toml:"enable"`
Recipient []string `toml:"recipient"`
From string `toml:"from"`
User string `toml:"user"`
Password string `toml:"password"`
Template string `toml:"template"`
} `toml:"prometheus"`
JSON struct {
Enable bool `toml:"enable"`
User string `toml:"user"`
Password string `toml:"password"`
} `toml:"json"`
Raw struct {
Enable bool `toml:"enable"`
User string `toml:"user"`
Password string `toml:"password"`
} `toml:"raw"`
}
)
func init() {
flag.StringVar(&configFile, "c", "config.ini", "Config file")
flag.StringVar(&logFile, "l", "", "Log file, if blank: not write log to file")
flag.BoolVar(&debug, "d", false, "Debug on")
flag.Parse()
}
func main() {
var (
err error
logWriter io.Writer
)
if logFile != "" {
lf, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
logger.Printf("error opening log file: %v", err)
os.Exit(2)
}
defer lf.Close()
logWriter = io.MultiWriter(lf, os.Stdout)
} else {
logWriter = os.Stdout
}
log = logger.New(logWriter, "", logger.Ldate|logger.Ltime)
if _, err = toml.DecodeFile(configFile, &config); err != nil {
log.Printf("Parse config error: %s", err)
os.Exit(2)
}
grafanaTmpl = template.Must(template.New("grafana").Parse(config.Grafana.Template))
prometheusTmpl = template.Must(template.New("prometheus").Parse(config.Prometheus.Template))
tx = &smpp.Transmitter{
Addr: fmt.Sprintf("%s:%d", config.Sms.Host, config.Sms.Port),
User: config.Sms.SystemID,
Passwd: config.Sms.Password,
}
conn := tx.Bind()
var status smpp.ConnStatus
if status = <-conn; status.Error() != nil {
log.Printf("Unable SMPP connect, aborting: %s", status.Error().Error())
} else {
log.Println("SMPP connection completed, status:", status.Status().String())
}
go func() {
for c := range conn {
if debug {
log.Println("SMPP connection status:", c.Status())
}
}
}()
http.HandleFunc("/grafana", grafanaHandler)
http.HandleFunc("/prometheus", prometheusHandler)
http.HandleFunc("/json", jsonHandler)
log.Printf("Listen on %s", config.ListenAddr)
log.Printf("Listen error: %s", http.ListenAndServe(config.ListenAddr, nil))
}