forked from AndreaGreco/prometeus_uwsgi_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
231 lines (192 loc) · 4.7 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"path"
"runtime"
"github.com/op/go-logging"
"gopkg.in/yaml.v2"
)
type StatsSocketConf_t struct {
Domain string `yaml:"domain"`
Socket string `yaml:"socket"`
Tcp string `yaml:"tcp"`
}
type Config_t struct {
Port string `yaml:"port"`
SocketDir string `yaml:"socket_dir"`
PIDPath string `yaml:"pidfile"`
StatsSockets []StatsSocketConf_t `yaml:"stats_sockets"`
}
// LOGGER
var log = logging.MustGetLogger("uwsg_exporter")
var format = logging.MustStringFormatter(
`%{color}%{time:15:04:05} %{shortfunc} %{level:.4s} %{id:03x}%{color:reset} %{message}`,
)
func SetUpLogger() {
BkStdOut := logging.NewLogBackend(os.Stderr, "", 0)
BkStdoutFormatter := logging.NewBackendFormatter(BkStdOut, format)
// Set the backends to be used.
logging.SetBackend(BkStdoutFormatter)
}
/**
* @brief map domain, full path
*/
var FileMap map[string]string
/**
* @brief Flag config
*/
var config_path = flag.String("c", "config.yaml", "Path to a config file")
/**
* Do not deploy PID FileMap
*/
var noPID = flag.Bool("n", false, "Not deploy PID file")
/**
* Version flag, if true version will be printed
*/
var Version_flag = flag.Bool("v", false, "Version")
/**
* @brief Configuration struct
*/
var Conf Config_t
var VERSION_BUILD_GIT_HASH = "NOT SET"
var VERSION_BUILD_GIT_VERSION = "NOT SET"
var VERSION_BUILD_TIME = "NOT SET"
func Print_Version() {
fmt.Printf("Git Version:\t%s\n", VERSION_BUILD_GIT_VERSION)
fmt.Printf("Build Time :\t%s\n", VERSION_BUILD_TIME)
fmt.Printf("Git Hash:\t%s\n", VERSION_BUILD_GIT_HASH)
}
/**
* @brief Parse yaml config passed as flag parameter
* @return False if found error
*/
func ParseConfig() {
data, err := ioutil.ReadFile(*config_path)
if err != nil {
log.Fatalf("Impossible read file:%s Error%v\n", *config_path, err)
}
err = yaml.Unmarshal([]byte(data), &Conf)
if err != nil {
log.Fatalf("Config file:%s Error, %v\n", *config_path, err)
}
}
/**
* @brief Check if unix socket exist, and if file is Unix Socket
*
*/
func CheckUnixSocket(FullPath string) bool {
FoundError := false
if len(findIP(FullPath)) > 0 {
return FoundError
}
// Check path exist
_, err := os.Stat(FullPath)
if err != nil {
if os.IsNotExist(err) {
/* Error is not fatal, socket could be removed, uWSGI restared, Then log it, and continue */
log.Errorf("Could not open %s. This error is not critical will be SKIP\n", FullPath)
FoundError = true
} else {
log.Fatalf("%v\n", err)
}
}
// Let open File descriptor, check error
if err != nil {
FoundError = true
}
return FoundError
}
/**
* @brief callback handler GET request
*/
func GET_Handling(w http.ResponseWriter, r *http.Request) {
w.Write(ReadStatsSocket_uWSGI())
w.Write([]byte(fmt.Sprintf("uwsgiexpoter_subroutine %d\n", runtime.NumGoroutine())))
}
/**
* @brief Validate config file and fist open of FD
*/
func ValidateConfig() {
FoundError := false
FileMap = make(map[string]string)
log.Info("Start check configuration file\n")
// Check if folder exist
_, err := ioutil.ReadDir(Conf.SocketDir)
if err != nil {
log.Fatalf("Error %v\n", err)
}
// Check path fist start polling
//fmt.Println(Conf.StatsSockets)
for _, SocketPath := range Conf.StatsSockets {
// Calculate full path
var FullPath string
var TcpPath string
TcpPath = SocketPath.Tcp
if path.IsAbs(SocketPath.Socket) {
// Support socket with absolute path
FullPath = SocketPath.Socket
} else {
FullPath = path.Join(Conf.SocketDir, SocketPath.Socket)
}
if len(SocketPath.Socket) == 0 {
FullPath = TcpPath
}
log.Infof("Socket Path:%s", FullPath)
if CheckUnixSocket(FullPath) {
FoundError = true
}
FileMap[SocketPath.Domain] = FullPath
}
fmt.Println(FileMap)
if !FoundError {
log.Info("Configuration correct, no error detect\n")
} else {
log.Info("Error found check log\n")
}
}
/**
*
* @brief Deploy pid file
* Deploy pid file, Correct true, else false
*/
func DeployPID() bool {
if *noPID {
return true
}
// So ugly but it work
PID := os.Getpid()
pidFile, err := os.Open(Conf.PIDPath)
if err != nil {
log.Fatalf("Impossible open file, %s\n", err)
}
pidFile.WriteString(string(PID))
pidFile.Sync()
pidFile.Close()
return true
}
func main() {
// Setup env
flag.Parse()
// If user is requesting version print it
if *Version_flag {
Print_Version()
return
}
ParseConfig()
SetUpLogger()
DeployPID()
// End setup, from here all will be moved in second fork
ValidateConfig()
l, err := net.Listen("tcp", fmt.Sprintf("%v", Conf.Port))
if err != nil {
log.Fatal(err)
}
log.Infof("Bind port:%d\n", Conf.Port)
http.HandleFunc("/metrics", GET_Handling)
http.Serve(l, nil)
}