This repository was archived by the owner on Nov 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 356
Add TM integration and separate docker-compose services for Varnish #7746
Merged
limited
merged 4 commits into
apache:master
from
AbdelrahmanElawady:varnish-traffic-monitor-integration
Sep 8, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f66ef62
Add TM integration and separate docker-compose services for Varnish
AbdelrahmanElawady 1d3a639
Add vstats parser tests
AbdelrahmanElawady 2963885
Get interface from TM and remove concurrency
AbdelrahmanElawady e840378
Remove . and / from interface names
AbdelrahmanElawady File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| # | ||
| # This compose file will run cache servers as Varnish instead of ATS. | ||
| # | ||
| # docker-compose -f docker-compose.yml -f docker-compose.varnish.yml up | ||
| # | ||
|
|
||
| --- | ||
| version: '3.8' | ||
|
|
||
| services: | ||
| edge: | ||
| build: | ||
| dockerfile: infrastructure/cdn-in-a-box/varnish/Dockerfile | ||
| mid-01: | ||
| build: | ||
| dockerfile: infrastructure/cdn-in-a-box/varnish/Dockerfile | ||
| mid-02: | ||
| build: | ||
| dockerfile: infrastructure/cdn-in-a-box/varnish/Dockerfile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package main | ||
|
|
||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "net/http" | ||
| "os" | ||
| "os/exec" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| type vstats struct { | ||
| ProcLoadavg string `json:"proc.loadavg"` | ||
| ProcNetDev string `json:"proc.net.dev"` | ||
| InfSpeed int64 `json:"inf_speed"` | ||
| NotAvailable bool `json:"not_available"` | ||
| // TODO: stats | ||
| } | ||
|
|
||
| func getSystemData(inf string) vstats { | ||
| var vstats vstats | ||
| loadavg, err := os.ReadFile("/proc/loadavg") | ||
| if err != nil { | ||
| log.Printf("failed to read /proc/loadavg: %s\n", err) | ||
| } | ||
| vstats.ProcLoadavg = strings.TrimSpace(string(loadavg)) | ||
|
|
||
| procNetDev, err := os.ReadFile("/proc/net/dev") | ||
| if err != nil { | ||
| log.Printf("failed to read /proc/net/dev: %s\n", err) | ||
| } | ||
|
|
||
| parts := strings.Split(string(procNetDev), "\n") | ||
| for _, line := range parts { | ||
| if strings.HasPrefix(strings.TrimSpace(line), inf) { | ||
| vstats.ProcNetDev = strings.TrimSpace(line) | ||
| break | ||
| } | ||
| } | ||
|
|
||
| infSpeedFile := fmt.Sprintf("/sys/class/net/%s/speed", inf) | ||
| speedStr, err := os.ReadFile(infSpeedFile) | ||
Check failureCode scanning / CodeQL Uncontrolled data used in path expression
This path depends on a [user-provided value](1).
|
||
| if err != nil { | ||
| log.Printf("failed to read %s: %s\n", infSpeedFile, err) | ||
| } | ||
| speed, err := strconv.ParseInt(strings.TrimSpace(string(speedStr)), 10, 64) | ||
| if err != nil { | ||
| log.Printf("failed to convert speed '%s' to int: %s\n", speedStr, err) | ||
| } | ||
| vstats.InfSpeed = speed | ||
|
|
||
| cmd := exec.Command("systemctl", "status", "varnish.service") | ||
| err = cmd.Run() | ||
| if err != nil { | ||
| log.Printf("failed to run systemctl: %s\n", err) | ||
| } | ||
| if cmd.ProcessState.ExitCode() != 0 { | ||
| vstats.NotAvailable = true | ||
| } | ||
| return vstats | ||
| } | ||
|
|
||
| func getStats(w http.ResponseWriter, r *http.Request) { | ||
| inf := r.URL.Query().Get("inf.name") | ||
| if inf == "" { | ||
| // assume default eth0? | ||
| inf = "eth0" | ||
| } | ||
| inf = strings.ReplaceAll(inf, ".", "") | ||
| inf = strings.ReplaceAll(inf, "/", "") | ||
| vstats := getSystemData(inf) | ||
| encoder := json.NewEncoder(w) | ||
| err := encoder.Encode(vstats) | ||
| if err != nil { | ||
| log.Printf("failed to write Varnish stats: %s", err) | ||
| } | ||
| } | ||
|
|
||
| func main() { | ||
| var port int | ||
| flag.IntVar(&port, "port", 2000, "port to run vstats on") | ||
|
|
||
| flag.Parse() | ||
| http.HandleFunc("/", getStats) | ||
|
|
||
| listenAddress := fmt.Sprintf(":%d", port) | ||
| if err := http.ListenAndServe(listenAddress, nil); err != nil { | ||
| log.Printf("server stopped %s", err) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package cache | ||
|
|
||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "github.com/apache/trafficcontrol/lib/go-log" | ||
| "github.com/apache/trafficcontrol/traffic_monitor/todata" | ||
| jsoniter "github.com/json-iterator/go" | ||
| ) | ||
|
|
||
| func init() { | ||
| registerDecoder("vstats", vstatsParse, vstatsPrecompute) | ||
| } | ||
|
|
||
| // Vstats holds Varnish cache statistics | ||
| type Vstats struct { | ||
| ProcNetDev string `json:"proc.net.dev"` | ||
| ProcLoadAvg string `json:"proc.loadavg"` | ||
| NotAvailable bool `json:"not_available"` | ||
| InfSpeed int64 `json:"inf_speed"` | ||
| Stats map[string]interface{} `json:"stats"` | ||
| } | ||
|
|
||
| func vstatsParse(cacheName string, r io.Reader, _ interface{}) (Statistics, map[string]interface{}, error) { | ||
| var stats Statistics | ||
|
|
||
| if r == nil { | ||
| log.Warnf("%s handler got nil reader", cacheName) | ||
| return stats, nil, errors.New("handler got nil reader") | ||
| } | ||
|
|
||
| var vstats Vstats | ||
| json := jsoniter.ConfigFastest | ||
|
|
||
| if err := json.NewDecoder(r).Decode(&vstats); err != nil { | ||
| return stats, nil, fmt.Errorf("failed to decode reader data: %w", err) | ||
| } | ||
| if err := stats.AddInterfaceFromRawLine(vstats.ProcNetDev); err != nil { | ||
| return stats, nil, fmt.Errorf("failed to add interface data %s: %w", vstats.ProcNetDev, err) | ||
| } | ||
|
|
||
| if loadAvg, err := LoadavgFromRawLine(vstats.ProcLoadAvg); err != nil { | ||
| return stats, nil, fmt.Errorf("failed to read average load data %s: %w", vstats.ProcLoadAvg, err) | ||
| } else { | ||
| stats.Loadavg = loadAvg | ||
| } | ||
|
|
||
| stats.NotAvailable = vstats.NotAvailable | ||
| inf := stats.Interfaces["eth0"] | ||
| inf.Speed = vstats.InfSpeed | ||
| stats.Interfaces["eth0"] = inf | ||
|
|
||
| return stats, vstats.Stats, nil | ||
| } | ||
|
|
||
| func vstatsPrecompute(cacheName string, data todata.TOData, stats Statistics, miscStats map[string]interface{}) PrecomputedData { | ||
| return PrecomputedData{DeliveryServiceStats: map[string]*DSStat{}} | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this struct be shared with anything that Traffic Monitor already deserializes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It definitely should be shared however it's implemented as just "main" package separated from TC so it's not a package that TM can call. However, I think it should be a package under TC and be built with
pkgconsidering statistics will be added too. Should that be in this PR or done separately?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If its ok with you, let's keep it separate, since this one is pretty well baked already
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, will open an issue for it to be done later.