-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (175 loc) · 4.86 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
// Copyright 2021 Jérôme Velociter
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate sqlboiler psql
package main
import (
"context"
"crypto/tls"
"database/sql"
"encoding/json"
"fmt"
"github.com/jessevdk/go-flags"
"github.com/jvelo/icescraper/config"
database "github.com/jvelo/icescraper/db"
"github.com/jvelo/icescraper/updater"
"github.com/pkg/errors"
"github.com/prometheus/common/log"
"github.com/volatiletech/sqlboiler/v4/boil"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
_ "github.com/joho/godotenv/autoload"
_ "github.com/lib/pq"
)
type options struct {
Config string `short:"c" long:"config" description:"Path to configuration file" default:"./config.yml"`
}
type Source struct {
AudioInfo string `json:"audio_info"`
Bitrate int `json:"bitrate"`
Genre string `json:"genre"`
ListenersPeak int `json:"listeners_peak"`
Listeners int `json:"listeners"`
ListenURL string `json:"listenurl"`
Description string `json:"server_description"`
Name string `json:"server_name"`
Type string `json:"server_type"`
Url string `json:"server_url"`
StreamStart string `json:"stream_start_iso8601"`
Title string `json:"title"`
}
type Icestats struct {
Admin string `json:"admin"`
Host string `json:"host"`
Location string `json:"location"`
Id string `json:"server_id"`
ServerStart string `json:"server_start_iso8601"`
Source Source `json:"source"`
}
type Response struct {
Stats Icestats `json:"icestats"`
}
var (
opts options
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "myapp_processed_ops_total",
Help: "The total number of processed events",
})
)
func main() {
if err := run(); err != nil {
panic(err)
}
}
func run() error {
parser := flags.NewParser(&opts, flags.HelpFlag|flags.PrintErrors)
_, err := parser.Parse()
if err != nil {
parser.WriteHelp(os.Stderr)
os.Exit(1)
}
url := os.Getenv("DATABASE_URL")
db, err := sql.Open("postgres", url)
if err != nil {
panic(err)
}
boil.SetDB(db)
defer func() {
if err := db.Close(); err != nil {
panic(err)
}
}()
conf, err := config.LoadFile(opts.Config)
if err != nil {
return err
}
log.Infof("conf: %v", conf)
ticker := time.NewTicker(conf.ScrapeInterval)
defer ticker.Stop()
insecureTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
secureTransport := &http.Transport{}
c := http.Client{Transport: secureTransport}
stream := make(chan *database.Record)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go updater.Update(ctx, stream)
go func() {
for {
select {
case <-ticker.C:
go func() {
for _, target := range conf.Servers {
if target.SkipCertCheck {
c.Transport = insecureTransport
} else {
c.Transport = secureTransport
}
body, err := doRequest(target, c)
if err != nil {
log.Errorf("polling target: %v", err)
continue
}
var response Response
err = json.Unmarshal(body, &response)
if err != nil {
log.Errorf("unmarshalling target: %v", err)
continue
}
cast := database.NewCast(
response.Stats.Source.Name,
response.Stats.Source.Description,
target.Url,
)
track := database.NewTrack(
response.Stats.Source.Title,
response.Stats.Source.Listeners,
)
go func() {
stream <- &database.Record{
Stream: cast,
Track: track,
}
}()
}
}()
}
}
}()
http.Handle("/metrics", promhttp.Handler())
return http.ListenAndServe("0.0.0.0:2112", nil)
}
func doRequest(target config.IcecastServer, c http.Client) ([]byte, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%v/status-json.xsl", target.Url), nil)
if err != nil {
return nil, errors.Wrap(err, "creating request")
}
resp, err := c.Do(req)
if err != nil {
return nil, errors.Wrap(err, "doing request")
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("didn't get a OK status: %v", resp.StatusCode))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "reading body")
}
return body, nil
}