@@ -20,12 +20,14 @@ import (
2020)
2121
2222type configStore struct {
23- urls []string
24- listen string
25- certFile string
26- certKey string
27- clientCAs * x509.CertPool
28- crl * pkix.CertificateList
23+ urls []* url.URL
24+ listen string
25+
26+ disableTLS bool
27+ certFile string
28+ certKey string
29+ clientCAs * x509.CertPool
30+ crl * pkix.CertificateList
2931}
3032
3133var (
@@ -34,56 +36,80 @@ var (
3436)
3537
3638func init () {
37- urls := getEnv ("URLS" , "http://localhost:9100" )
39+ urlsString := getEnv ("URLS" , "http://localhost:9100/metrics" )
40+ urls := []* url.URL {}
41+ for _ , urlString := range strings .Split (urlsString , "," ) {
42+ u , err := url .Parse (urlString )
43+ if err != nil {
44+ log .Fatal (err )
45+ }
46+ urls = append (urls , u )
47+ }
48+
3849 listen := getEnv ("LISTEN" , "0.0.0.0:9443" )
50+ _ , disableTLS := os .LookupEnv ("DISABLE_TLS" )
3951
40- caFile := getEnv ("CA" , "" )
41- caData , err := ioutil .ReadFile (caFile )
42- if err != nil {
43- log .Fatal ("Unable to load ca from $CA." )
44- }
52+ certFile := ""
53+ keyFile := ""
4554 clientCAs := x509 .NewCertPool ()
46- if ok := clientCAs .AppendCertsFromPEM (caData ); ! ok {
47- log .Fatal ("Unable to parse $CA as ca." )
55+ crl := & pkix.CertificateList {}
4856
49- }
57+ if ! disableTLS {
58+ caFile := getEnv ("CA" , "" )
59+ caData , err := ioutil .ReadFile (caFile )
60+ if err != nil {
61+ log .Fatal ("Unable to load ca from $CA." )
62+ }
63+ if ok := clientCAs .AppendCertsFromPEM (caData ); ! ok {
64+ log .Fatal ("Unable to parse $CA as ca." )
5065
51- crlFile := getEnv ("CRL" , "" )
52- crlData , err := ioutil .ReadFile (crlFile )
53- if err != nil {
54- log .Fatal ("Unable to load crl from $CRL." )
55- }
56- crl , err := x509 .ParseCRL (crlData )
57- if err != nil {
58- log .Fatal ("Unable to parse $CRL as crl." )
59- }
66+ }
67+
68+ crlFile := getEnv ("CRL" , "" )
69+ crlData , err := ioutil .ReadFile (crlFile )
70+ if err != nil {
71+ log .Fatal ("Unable to load crl from $CRL." )
72+ }
73+ crl , err = x509 .ParseCRL (crlData )
74+ if err != nil {
75+ log .Fatal ("Unable to parse $CRL as crl." )
76+ }
6077
61- certFile := getEnv ("CERT" , "" )
62- keyFile := getEnv ("KEY" , "" )
78+ certFile = getEnv ("CERT" , "" )
79+ keyFile = getEnv ("KEY" , "" )
80+ }
6381
6482 config = configStore {
65- strings . Split ( urls , "," ) ,
83+ urls ,
6684 listen ,
85+ disableTLS ,
6786 certFile ,
6887 keyFile ,
6988 clientCAs ,
7089 crl ,
7190 }
7291
73- re = regexp .MustCompile ("^(?P<name>[^#][^ {]+)({(?P<labels>.*)})? (?P<value>.* )" )
92+ re = regexp .MustCompile ("^(?P<name>[^#][^ {]+)(?: {(?P<labels>.*)})? (?P<value>[0-9]+(?: \\ .[0-9]+)? )" )
7493}
7594
7695func main () {
77- tlsConfig := & tls.Config {
78- ClientAuth : tls .RequireAndVerifyClientCert ,
79- ClientCAs : config .clientCAs ,
80- }
81- s := & http.Server {
82- Addr : config .listen ,
83- TLSConfig : tlsConfig ,
96+ if config .disableTLS {
97+ log .Printf ("Running WITHOUT TLS!" )
98+ http .HandleFunc ("/metrics" , metrics )
99+ s := & http.Server {Addr : config .listen }
100+ log .Fatal (s .ListenAndServe ())
101+ } else {
102+ tlsConfig := & tls.Config {
103+ ClientAuth : tls .RequireAndVerifyClientCert ,
104+ ClientCAs : config .clientCAs ,
105+ }
106+ s := & http.Server {
107+ Addr : config .listen ,
108+ TLSConfig : tlsConfig ,
109+ }
110+ http .HandleFunc ("/metrics" , withTlsClientCheck (metrics ))
111+ log .Fatal (s .ListenAndServeTLS (config .certFile , config .certKey ))
84112 }
85- http .HandleFunc ("/metrics" , withTlsClientCheck (metrics ))
86- log .Fatal (s .ListenAndServeTLS (config .certFile , config .certKey ))
87113}
88114
89115func getEnv (key string , fallback string ) string {
@@ -117,9 +143,9 @@ func metrics(w http.ResponseWriter, r *http.Request) {
117143
118144 c := make (chan string )
119145 var wg sync.WaitGroup
120- for _ , url := range config .urls {
146+ for _ , urlParsed := range config .urls {
121147 wg .Add (1 )
122- go fetch (url , c , ctx , & wg )
148+ go fetch (urlParsed , c , ctx , & wg )
123149 }
124150 go func () {
125151 wg .Wait ()
@@ -131,21 +157,16 @@ func metrics(w http.ResponseWriter, r *http.Request) {
131157 }
132158}
133159
134- func extend (line string , source string ) (string , error ) {
135- url , err := url .Parse (source )
136- if err != nil {
137- return line , err
138- }
139- label := fmt .Sprintf ("%s%s" , url .Host , url .Path )
160+ func extend (line string , label string ) (string , error ) {
140161
141162 match := re .FindStringSubmatch (line )
142- if len (match ) != 5 {
163+ if len (match ) != 4 {
143164 return line , errors .New ("Invalid Line." )
144165 }
145166
146167 lineName := match [1 ]
147- lineLabels := match [3 ]
148- lineValue := match [4 ]
168+ lineLabels := match [2 ]
169+ lineValue := match [3 ]
149170 if lineLabels == "" {
150171 lineLabels = fmt .Sprintf ("sub_instance=\" %s\" " , label )
151172 } else {
@@ -155,26 +176,37 @@ func extend(line string, source string) (string, error) {
155176 return line , nil
156177}
157178
158- func fetch (url string , c chan string , ctx context.Context , wg * sync.WaitGroup ) {
179+ func fetch (u * url. URL , c chan string , ctx context.Context , wg * sync.WaitGroup ) {
159180 defer wg .Done ()
160- req , err := http .NewRequest ("GET" , url , nil )
181+ urlString := u .String ()
182+ label := fmt .Sprintf ("%s%s" , u .Host , u .Path )
183+
184+ up := 0
185+ err := error (nil )
186+ defer func () {
187+ c <- fmt .Sprintf ("up {sub_instance=\" %s\" } %d" , label , up )
188+ if err != nil {
189+ log .Printf ("Error: %s" , err )
190+ }
191+ }()
192+
193+ req , err := http .NewRequest ("GET" , urlString , nil )
161194 if err != nil {
162- log .Printf ("Request Error %s." , err )
163195 return
164196 }
165197 req = req .WithContext (ctx )
166198 resp , err := http .DefaultClient .Do (req )
167199 if err != nil {
168- log .Printf ("Context Error %s." , err )
169200 return
170201 }
171202 scanner := bufio .NewScanner (resp .Body )
172203 for scanner .Scan () {
173204 line := scanner .Text ()
174- line , err = extend (line , url )
205+ line , err = extend (line , label )
175206 c <- line
176207 }
177208 if err := scanner .Err (); err != nil {
178- log . Printf ( "Scanner Error %s." , err )
209+ return
179210 }
211+ up = 1
180212}
0 commit comments