-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (163 loc) · 5.03 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
exporterPort = getEnvAsInt("EXPORTER_PORT", 17500)
fetchInterval = getEnvAsInt("FETCH_INTERVAL", 3600)
cookieString = os.Getenv("COOKIE_STRING")
url = os.Getenv("PROFILE_URL")
userAgent = os.Getenv("USER_AGENT")
dlElementSelector = os.Getenv("DL_ELEMENT_SELECTOR")
ulElementSelector = os.Getenv("UL_ELEMENT_SELECTOR")
telegramBotToken = os.Getenv("TELEGRAM_BOT_TOKEN")
telegramChatID = getEnvAsInt("TELEGRAM_CHAT_ID", 294967926)
uploadMetric = prometheus.NewGauge(prometheus.GaugeOpts{Name: "upload_value_bytes", Help: "Value extracted from the webpage in bytes"})
downloadMetric = prometheus.NewGauge(prometheus.GaugeOpts{Name: "download_value_bytes", Help: "Value extracted from the webpage in bytes"})
units = map[string]float64{
"TiB": 1 << 40, // TiB (Tebibytes)
"TB": 1e12, // TB (Terabytes)
"GiB": 1 << 30, // GiB
"GB": 1e9, // GB
"MiB": 1 << 20, // MiB
"MB": 1e6, // MB
"KiB": 1 << 10, // KiB
"KB": 1e3, // KB
}
)
func init() {
// Exit if required environment variables are not set
if cookieString == "" || url == "" || userAgent == "" || dlElementSelector == "" || ulElementSelector == "" {
log.Fatal("Required environment variables are not set")
os.Exit(1)
}
prometheus.MustRegister(uploadMetric)
prometheus.MustRegister(downloadMetric)
}
func getEnvAsInt(key string, defaultValue int) int {
if value, exists := os.LookupEnv(key); exists {
if intValue, err := strconv.Atoi(value); err == nil {
return intValue
}
}
return defaultValue
}
func ConvertHumanReadableSizeToBytes(input string) (float64, error) {
// Remove text in parentheses with parantheses
input = regexp.MustCompile(`\([^)]*\)`).ReplaceAllString(input, "")
// Split the input and trim any whitespace
parts := strings.Fields(input)
if len(parts) != 2 {
return 0, fmt.Errorf("invalid input format")
}
valueStr := parts[0]
unit := parts[1]
// Check if the unit is recognized
multiplier, ok := units[unit]
if !ok {
return 0, fmt.Errorf("unrecognized unit: %s", unit)
}
// Parse the numeric part
value, err := strconv.ParseFloat(valueStr, 64)
if err != nil {
return 0, err
}
// Calculate the number of bytes
bytes := value * multiplier
return bytes, nil
}
func sendTelegramMessage(message string) {
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", telegramBotToken)
payload := map[string]interface{}{
"chat_id": telegramChatID,
"text": message,
}
payloadBytes, _ := json.Marshal(payload)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
if err != nil {
log.Printf("Error creating Telegram request: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != http.StatusOK {
log.Printf("Failed to send message to Telegram: %v", err)
}
}
func fetchValue() {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("Error creating request: %v", err)
return
}
req.Header.Set("Cookie", cookieString)
req.Header.Set("User-Agent", userAgent)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Error fetching webpage: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Failed to fetch webpage: %d", resp.StatusCode)
return
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Printf("Error parsing HTML: %v", err)
return
}
uploadText := strings.TrimSpace(doc.Find(ulElementSelector).Text())
downloadText := strings.TrimSpace(doc.Find(dlElementSelector).Text())
// If there are no space between value and unit, add a space
uploadText = regexp.MustCompile(`(\d)([A-Za-z])`).ReplaceAllString(uploadText, "$1 $2")
downloadText = regexp.MustCompile(`(\d)([A-Za-z])`).ReplaceAllString(downloadText, "$1 $2")
log.Printf("Upload: '%s'\n", uploadText)
log.Printf("Download: '%s'\n", downloadText)
if uploadText != "" {
uploadBytes, err := ConvertHumanReadableSizeToBytes(uploadText)
if err != nil {
log.Printf("Invalid format for upload: %s", uploadText)
} else {
uploadMetric.Set(uploadBytes)
}
} else {
log.Println("Upload element not found")
}
if downloadText != "" {
downloadBytes, err := ConvertHumanReadableSizeToBytes(downloadText)
if err != nil {
log.Printf("Invalid format for download: %s", downloadText)
} else {
downloadMetric.Set(downloadBytes)
}
} else {
log.Println("Download element not found")
}
}
func main() {
http.Handle("/metrics", promhttp.Handler())
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", exporterPort), nil))
}()
log.Println("Starting exporter...")
log.Printf("Fetching URL: %s", url)
for {
fetchValue()
time.Sleep(time.Duration(fetchInterval) * time.Second)
}
}