Skip to content

Commit

Permalink
icmp tweaks to use channels
Browse files Browse the repository at this point in the history
  • Loading branch information
sagostin committed Nov 20, 2022
1 parent 31b26b7 commit 93b4fe9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 136 deletions.
37 changes: 20 additions & 17 deletions icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ import (

// TODO DSCP Tags? https://github.com/rclone/rclone/issues/755

func CheckICMP(t *agent_models.IcmpTarget, duration int) error {
func CheckICMP(t string, duration int, out chan agent_models.IcmpTarget) error {
var icmpTarget = agent_models.IcmpTarget{
Address: t,
}
icmpTarget.Result.StartTimestamp = time.Now()

var cmd *exec.Cmd
switch OsDetect {
case "windows":
log.Println("Windows")
break
case "darwin":
log.Println("OSX")
args := []string{"-c", "./lib/ethr_osx -no -w 1 -x " + t.Address + " -p icmp -t pi -d " +
args := []string{"-c", "./lib/ethr_osx -no -w 1 -x " + t + " -p icmp -t pi -d " +
strconv.FormatInt(int64(duration), 10) + "s -4"}
cmd = exec.CommandContext(context.TODO(), "/bin/bash", args...)
break
Expand All @@ -34,7 +39,7 @@ func CheckICMP(t *agent_models.IcmpTarget, duration int) error {
log.Fatalf("Unknown OS")
}

out, err := cmd.CombinedOutput()
cmdOut, err := cmd.CombinedOutput()
fmt.Printf("%s\n", out)
if err != nil {
log.Error(err)
Expand All @@ -45,7 +50,7 @@ func CheckICMP(t *agent_models.IcmpTarget, duration int) error {
if err != nil {
return err
}
ethrOutput := strings.Split(string(out), "-----------------------------------------------------------------------------------------")
ethrOutput := strings.Split(string(cmdOut), "-----------------------------------------------------------------------------------------")
metrics1 := compile1.FindAllString(ethrOutput[1], -1)
if err != nil {
return err
Expand All @@ -59,7 +64,7 @@ func CheckICMP(t *agent_models.IcmpTarget, duration int) error {
log.Printf("%s", metrics1)
log.Printf("%s", metrics2)

t.Result.Metrics = agent_models.IcmpMetrics{
icmpTarget.Result.Metrics = agent_models.IcmpMetrics{
Avg: metrics2[0],
Min: metrics2[1],
Max: metrics2[8],
Expand All @@ -73,30 +78,28 @@ func CheckICMP(t *agent_models.IcmpTarget, duration int) error {
Percent999: metrics2[6],
Percent9999: metrics2[7],
}
icmpTarget.Result.StopTimestamp = time.Now()

// todo regex 🤪
out <- icmpTarget

// todo regex 🤪
return nil
}

func TestIcmpTargets(t []*agent_models.IcmpTarget, interval int) {
func TestIcmpTargets(t []string, interval int) (out chan agent_models.IcmpTarget) {
var wg sync.WaitGroup

log.Infof("len %v", len(t))
for _, tn := range t {
log.Infof("starting icmp for %s", tn.Address)
defer close(out)
for i := range t {
wg.Add(1)
go func(tn1 *agent_models.IcmpTarget) {
go func(tn1 string) {
defer wg.Done()
err := CheckICMP(tn1, interval)
err := CheckICMP(tn1, interval, out)
if err != nil {
log.Errorf("%s", err)
}
tn1.Result.StopTimestamp = time.Now()
// "sleep" is handled by ethr because it would be running the test
// otherwise it would throw an error.
log.Infof("ending icmp for %s", tn1.Address)
}(tn)
}(t[i])
}
wg.Wait()
return
}
103 changes: 0 additions & 103 deletions mtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,109 +33,6 @@ func TestMtrTargets(t []string, triggered bool) (out chan agent_models.MtrTarget
return
}

/*func CheckMTR(t *agent_models.MtrTarget, duration int) error {
var cmd *exec.Cmd
var regx string
switch OsDetect {
case "windows":
log.Println("Windows")
break
case "darwin":
log.Println("OSX")
args := []string{"-c", "traceroute " + t.Address}
cmd = exec.CommandContext(context.TODO(), "/bin/bash", args...)
// (\d+)\s+(((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}|())\s*([(]((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}[)]|())\s+(((\d+.\d+)\s+([a-z]+))|([*]))\s+(((\d+.\d+)\s+([a-z]+))|([*]))\s+(((\d+.\d+)\s+([a-z]+))|([*]))
regx = "(\\d+)\\s+" +
"(((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}|())\\s*" +
"([(]((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}[)]|())\\s+" +
"(((\\d+.\\d+)\\s+([a-z]+))|([*]))\\s+" +
"(((\\d+.\\d+)\\s+" +
"([a-z]+))|([*]))\\s+" +
"(((\\d+.\\d+)\\s+" +
"([a-z]+))|([*]))"
break
case "linux":
log.Println("Linux")
break
default:
log.Fatalf("Unknown OS")
}
cmd.Wait()
out, err := cmd.CombinedOutput()
fmt.Printf("%s\n", out)
if err != nil {
log.Error(err)
return err
}
regex := *regexp.MustCompile(regx) // worst
if err != nil {
return err
}
ethrLines := strings.Split(string(out), "\n")
t.Result.Metrics = make(map[int]agent_models.MtrMetrics)
for n, ethrLine := range ethrLines {
if n <= 0 || ethrLine == "" {
continue
}
dataMatch := regex.FindStringSubmatch(ethrLine)
log.Infof("%s", len(dataMatch))
avg := "*"
if dataMatch[12] != "*" {
avg = dataMatch[12]
}
best := "*"
if dataMatch[21] != "*" {
avg = dataMatch[20]
}
worst := "*"
if dataMatch[21] != "*" {
avg = dataMatch[20]
}
address := "???"
if dataMatch[9] != "" {
address = dataMatch[9]
}
fqdn := "???"
if dataMatch[2] != "" {
fqdn = dataMatch[2]
}
// hop num = result[1]
t.Result.Metrics[convHandleStrInt(dataMatch[1])] = agent_models.MtrMetrics{
Address: address,
FQDN: fqdn,
Sent: 0,
Received: 0,
Last: "-",
Avg: avg,
Best: best,
Worst: worst,
}
}
j, err := json.Marshal(t.Result)
if err != nil {
return err
}
log.Warnf("%s", j)
return nil
}*/

// CheckMTR change to client controller check
func CheckMTR(host string, duration int, triggered bool, out chan agent_models.MtrTarget) error {
startTime := time.Now()
Expand Down
25 changes: 9 additions & 16 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,34 +201,27 @@ func runSpeedTestCheck(config *agent_models.AgentConfig) {
}

func runIcmpCheck(t *agent_models.AgentConfig, count int) {

var pingTargets []*agent_models.IcmpTarget

for i, n := range t.PingTargets {
pingTargets = append(pingTargets, &agent_models.IcmpTarget{
Address: n,
})
pingTargets[i].Result.StartTimestamp = time.Now()
}

log.Infof("Running ICMP check...")
if t.PingInterval < 2 {
t.PingInterval = 2
}
TestIcmpTargets(pingTargets, t.PingInterval)
c := TestIcmpTargets(t.PingTargets, t.PingInterval)

var targets []*agent_models.IcmpTarget

for _, st := range pingTargets {
_, err := json.Marshal(st)
for target := range c {
j, err := json.Marshal(target)
if err != nil {
log.Errorf("%s", err)
log.Fatal(err)
}
// fmt.Printf("%s\n", string(j))
targets = append(targets, &target)
fmt.Printf("%s\n", string(j))
}

// Upload to server, check if it fails or not,
// then if it does, save to temporary list
// for later upload
resp, err := PostIcmp(pingTargets)
resp, err := PostIcmp(targets)
if err != nil || resp.Response != 200 {
// TODO save to queue
log.Errorf("Failed to push ICMP information.")
Expand Down

0 comments on commit 93b4fe9

Please sign in to comment.