-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins_cloudlinux.go
More file actions
426 lines (352 loc) · 12.8 KB
/
plugins_cloudlinux.go
File metadata and controls
426 lines (352 loc) · 12.8 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package directadmin
import (
"fmt"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
// CloudLinuxGetUsageCharts retrieves the CloudLinux usage charts for the given period and ID.
//
// period: e.g., "1h", "1d", "1w", "1m"
// id: the user ID or LVE ID (e.g., "1000001025")
func (c *UserContext) CloudLinuxGetUsageCharts(period string, id string) ([]*CloudLinuxChartData, error) {
rawChart, err := c.cloudLinuxGetUsageCharts(period, id, "svg")
if err != nil {
return nil, err
}
// There is a non-SVG endpoint; however, it's significantly slower.
charts, err := parseCloudLinuxSVG(rawChart)
if err != nil {
return nil, fmt.Errorf("failed to parse SVG chart: %w", err)
}
return charts, nil
}
// CloudLinuxGetUsageChartsAsImage retrieves the CloudLinux usage charts for the given period and ID.
//
// period: e.g., "1h", "1d", "1w", "1m"
// id: the user ID or LVE ID (e.g., "1000001025")
// format: SVG or PNG
func (c *UserContext) CloudLinuxGetUsageChartsAsImage(period string, id string, format string) (string, error) {
format = strings.ToLower(format)
if format != "png" && format != "svg" {
return "", fmt.Errorf("unsupported format: %s", format)
}
chart, err := c.cloudLinuxGetUsageCharts(period, id, format)
if err != nil {
return "", err
}
return chart, nil
}
// CloudLinuxGetUsageCharts retrieves the CloudLinux usage charts for the given period and ID.
//
// period: e.g., "1h", "1d", "1w", "1m"
// id: the user ID or LVE ID (e.g., "1000001025")
func (c *UserContext) cloudLinuxGetUsageCharts(period string, id string, format string) (string, error) {
if err := c.CreateSession(); err != nil {
return "", fmt.Errorf("failed to create user session: %w", err)
}
csrfToken, err := c.cloudlinuxCreateCSRFToken()
if err != nil {
return "", fmt.Errorf("failed to create CSRF token: %w", err)
}
body := url.Values{}
body.Set("command", "cloudlinux-charts")
body.Set("params[period]", period)
body.Set("params[format]", format)
body.Set("params[id]", id)
body.Set("csrftoken", csrfToken)
var resp struct {
Chart string `json:"chart"`
Result string `json:"result"`
}
if _, err = c.makeRequestOld(http.MethodPost, "PLUGINS_RESELLER/lvemanager_spa/index.raw?c=send-request", body, &resp); err != nil {
return "", err
}
if resp.Result != "success" {
return "", fmt.Errorf("failed to retrieve charts: %s", resp.Result)
}
return resp.Chart, nil
}
type CloudLinuxUser struct {
CageFS string `json:"cageFS"`
Domain string `json:"domain"`
ID int `json:"id"`
Limits struct {
CPU struct {
All string `json:"all"`
} `json:"cpu"`
EP string `json:"ep"`
IO struct {
All string `json:"all"`
} `json:"io"`
IOPS string `json:"iops"`
NProc string `json:"nproc"`
PMem string `json:"pmem"`
VMem string `json:"vmem"`
} `json:"limits"`
Package string `json:"package"`
Username string `json:"username"`
}
// CloudLinuxGetUsers retrieves all accessible CLoudLinux users with their resource limits.
func (c *UserContext) CloudLinuxGetUsers() ([]*CloudLinuxUser, error) {
if err := c.CreateSession(); err != nil {
return nil, fmt.Errorf("failed to create user session: %w", err)
}
csrfToken, err := c.cloudlinuxCreateCSRFToken()
if err != nil {
return nil, fmt.Errorf("failed to create CSRF token: %w", err)
}
body := url.Values{}
body.Set("command", "cloudlinux-limits")
body.Set("method", "get")
body.Set("csrftoken", csrfToken)
var resp struct {
Result string `json:"result"`
Users []*CloudLinuxUser `json:"users"`
}
if _, err = c.makeRequestOld(http.MethodPost, "PLUGINS_RESELLER/lvemanager_spa/index.raw?c=send-request", body, &resp); err != nil {
return nil, err
}
if resp.Result != "success" {
return nil, fmt.Errorf("failed to retrieve users: %s", resp.Result)
}
return resp.Users, nil
}
// cloudlinuxCreateCSRFToken creates a CSRF token for the CloudLinux plugin if one doesn't already exist.
func (c *UserContext) cloudlinuxCreateCSRFToken() (string, error) {
const endpoint = "PLUGINS_RESELLER/lvemanager_spa/index.raw"
csrfToken := c.getCSRFToken(c.getRequestURLOld(endpoint))
if csrfToken != "" {
return csrfToken, nil
}
// Retrieve CSRF token.
if _, err := c.makeRequestOld(http.MethodGet, "PLUGINS_RESELLER/lvemanager_spa/index.raw?a=cookie", nil, nil); err != nil {
return "", err
}
return c.getCSRFToken(c.getRequestURLOld(endpoint)), nil
}
// dataPoint represents a single time-series data point with multiple series values.
type dataPoint struct {
Fault bool `json:"fault,omitempty"`
Timestamp string `json:"timestamp"`
Values map[string]float64 `json:"values"`
}
// CloudLinuxChartData represents the parsed data for a single chart.
type CloudLinuxChartData struct {
ChartTitle string `json:"chartTitle"`
DataPoints []dataPoint `json:"dataPoints"`
FaultEvents []string `json:"faultEvents,omitempty"`
Unit string `json:"unit"`
}
// parseCloudLinuxSVG extracts time-series data from a CloudLinux SVG chart.
func parseCloudLinuxSVG(svgContent string) ([]*CloudLinuxChartData, error) {
var charts []*CloudLinuxChartData
// Regex to find individual chart SVG blocks.
chartBlockRegex := regexp.MustCompile(`<svg[^>]*id="(svg-\d+)"[^>]*>([\s\S]*?)</svg>`)
chartBlocks := chartBlockRegex.FindAllStringSubmatch(svgContent, -1)
// Regex to find the chart title within a block.
titleRegex := regexp.MustCompile(`<text[^>]*x="55"[^>]*y="10"[^>]*>([^<]+)</text>`)
// Regex to find legend entries within a block.
legendRegex := regexp.MustCompile(`<rect[^>]*fill="([^"]+)"[^>]*/>\s*<text[^>]*>([^<]+)</text>`)
// Regex to extract data from line elements with onmousemove handlers.
lineRegex := regexp.MustCompile(`<line[^>]*onmousemove="show_tip\(evt,\s*'(svg-\d+)',\s*[\d.]+,\s*[\d.]+,\s*[\d.]+,\s*[\d.]+,\s*'([^']+)',\s*'([^']+)',\s*'([^']+)',\s*'([^']+)'\)"[^>]*stroke="([^"]+)"[^>]*/>`)
// Regex to extract fault lines (vertical lines without onmousemove, typically with stroke="aquamarine").
// These are lines where x1 == x2 (vertical) within a chart's clip-path group.
faultLineRegex := regexp.MustCompile(`<line[^>]*stroke="aquamarine"[^>]*x1="([\d.]+)"[^>]*x2="([\d.]+)"[^>]*y1="([\d.]+)"[^>]*y2="([\d.]+)"[^>]*/>`)
// Build per-chart legends and titles.
chartTitles := make(map[string]string)
chartLegends := make(map[string]map[string]string) // svgID -> color -> name.
chartContents := make(map[string]string) // svgID -> block content.
for _, block := range chartBlocks {
if len(block) < 3 {
continue
}
svgID := block[1]
content := block[2]
chartContents[svgID] = content
// Extract title.
if titleMatch := titleRegex.FindStringSubmatch(content); len(titleMatch) >= 2 {
chartTitles[svgID] = titleMatch[1]
}
// Extract legend for this chart.
chartLegends[svgID] = make(map[string]string)
legendMatches := legendRegex.FindAllStringSubmatch(content, -1)
for _, match := range legendMatches {
if len(match) >= 3 {
chartLegends[svgID][match[1]] = match[2]
}
}
}
// Extract all data points from lines with onmousemove handlers.
matches := lineRegex.FindAllStringSubmatch(svgContent, -1)
// Group data points by chart ID, then by timestamp, with series values.
chartDataMap := make(map[string]map[string]map[string]float64)
chartUnits := make(map[string]string)
for _, match := range matches {
if len(match) < 7 {
continue
}
svgID := match[1]
t1, v1 := match[2], match[3]
t2, v2 := match[4], match[5]
color := match[6]
// Map color to series name using this chart's legend.
seriesName := color
if legend, ok := chartLegends[svgID]; ok {
if name, ok := legend[color]; ok {
seriesName = name
}
}
if chartDataMap[svgID] == nil {
chartDataMap[svgID] = make(map[string]map[string]float64)
}
// Store unit for this chart.
if chartUnits[svgID] == "" {
chartUnits[svgID] = extractUnitFromCloudLinuxChart(v1)
}
ts1, _ := parseCloudLinuxTimestamp(t1)
if chartDataMap[svgID][ts1] == nil {
chartDataMap[svgID][ts1] = make(map[string]float64)
}
chartDataMap[svgID][ts1][seriesName] = parseCloudLinuxChartValue(v1)
ts2, _ := parseCloudLinuxTimestamp(t2)
if chartDataMap[svgID][ts2] == nil {
chartDataMap[svgID][ts2] = make(map[string]float64)
}
chartDataMap[svgID][ts2][seriesName] = parseCloudLinuxChartValue(v2)
}
// Extract fault events per chart.
chartFaultTimestamps := make(map[string]map[string]bool)
for svgID, content := range chartContents {
faultMatches := faultLineRegex.FindAllStringSubmatch(content, -1)
for _, match := range faultMatches {
if len(match) < 5 {
continue
}
x1, _ := strconv.ParseFloat(match[1], 64)
x2, _ := strconv.ParseFloat(match[2], 64)
// Only process vertical lines (fault markers have x1 == x2).
if x1 != x2 {
continue
}
// Find the nearest timestamp for this x position.
timestamp := findTimestampForXPosition(svgID, x1, svgContent)
// Mark this timestamp as having a fault.
if chartFaultTimestamps[svgID] == nil {
chartFaultTimestamps[svgID] = make(map[string]bool)
}
chartFaultTimestamps[svgID][timestamp] = true
}
}
// Convert maps to sorted slices.
for svgID, timestampMap := range chartDataMap {
chart := &CloudLinuxChartData{
ChartTitle: chartTitles[svgID],
Unit: chartUnits[svgID],
DataPoints: make([]dataPoint, 0, len(timestampMap)),
}
for timestamp, values := range timestampMap {
chart.DataPoints = append(chart.DataPoints, dataPoint{
Timestamp: timestamp,
Values: values,
Fault: chartFaultTimestamps[svgID][timestamp],
})
}
// Sort data points by timestamp.
sort.Slice(chart.DataPoints, func(i, j int) bool {
return chart.DataPoints[i].Timestamp < chart.DataPoints[j].Timestamp
})
charts = append(charts, chart)
}
return charts, nil
}
// findTimestampForXPosition finds the timestamp corresponding to a given x position in the SVG.
// It does this by finding the nearest data line and interpolating from its timestamps.
func findTimestampForXPosition(svgID string, xPos float64, svgContent string) string {
// Regex to extract x positions and timestamps from lines in this chart.
lineRegex := regexp.MustCompile(`<line[^>]*onmousemove="show_tip\(evt,\s*'` + regexp.QuoteMeta(svgID) + `',\s*([\d.]+),\s*[\d.]+,\s*([\d.]+),\s*[\d.]+,\s*'([^']+)',\s*'[^']+',\s*'([^']+)',\s*'[^']+'\)"[^>]*/>`)
matches := lineRegex.FindAllStringSubmatch(svgContent, -1)
var closestTimestamp string
closestDistance := -1.0
for _, match := range matches {
if len(match) < 5 {
continue
}
x1, _ := strconv.ParseFloat(match[1], 64)
x2, _ := strconv.ParseFloat(match[2], 64)
t1 := match[3]
t2 := match[4]
// Check distance to x1.
dist1 := abs(x1 - xPos)
if closestDistance < 0 || dist1 < closestDistance {
closestDistance = dist1
ts, _ := parseCloudLinuxTimestamp(t1)
closestTimestamp = ts
}
// Check distance to x2.
dist2 := abs(x2 - xPos)
if dist2 < closestDistance {
closestDistance = dist2
ts, _ := parseCloudLinuxTimestamp(t2)
closestTimestamp = ts
}
}
return closestTimestamp
}
// abs returns the absolute value of a float64.
func abs(x float64) float64 {
if x < 0 {
return -x
}
return x
}
// extractUnitFromCloudLinuxChart extracts the unit suffix from a value string.
func extractUnitFromCloudLinuxChart(rawValue string) string {
rawValue = strings.TrimSpace(rawValue)
for _, suffix := range []string{"%", "MB", "GB", "KB", "KB/s", "MB/s", "ms", "s", "B"} {
if strings.HasSuffix(rawValue, suffix) {
return suffix
}
}
return ""
}
// parseCloudLinuxChartValue extracts numeric value from strings like "113%", "57MB", "1.5GB".
func parseCloudLinuxChartValue(rawValue string) float64 {
// Remove common suffixes.
cleaned := strings.TrimSpace(rawValue)
cleaned = strings.TrimSuffix(cleaned, "%")
cleaned = strings.TrimSuffix(cleaned, "MB")
cleaned = strings.TrimSuffix(cleaned, "GB")
cleaned = strings.TrimSuffix(cleaned, "KB")
cleaned = strings.TrimSuffix(cleaned, "B")
cleaned = strings.TrimSuffix(cleaned, "ms")
cleaned = strings.TrimSuffix(cleaned, "s")
val, err := strconv.ParseFloat(cleaned, 64)
if err != nil {
return 0
}
return val
}
// parseCloudLinuxTimestamp converts CloudLinux timestamp format to RFC3339.
// Input format: "Jan-29 04:28PM"
// Output format: "2025-01-29T16:28:00Z"
func parseCloudLinuxTimestamp(ts string) (string, error) {
// CloudLinux format: "Jan-29 04:28PM"
// Go parse layout: "Jan-02 03:04PM"
parsed, err := time.Parse("Jan-02 03:04PM", ts)
if err != nil {
return ts, err
}
// The SVG doesn't include the year, so we assume the current year.
now := time.Now().UTC()
parsed = time.Date(now.Year(), parsed.Month(), parsed.Day(), parsed.Hour(), parsed.Minute(), 0, 0, time.UTC)
// Handle year boundary (e.g., if it's January and the timestamp is from December).
if parsed.After(now.AddDate(0, 0, 1)) {
parsed = parsed.AddDate(-1, 0, 0)
}
return parsed.Format(time.RFC3339), nil
}