-
Notifications
You must be signed in to change notification settings - Fork 23
/
netstat.go
65 lines (55 loc) · 1.66 KB
/
netstat.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
package clientreport
import (
"errors"
"fmt"
"sync"
"time"
"github.com/aliyun/aliyun_assist_client/agent/log"
"github.com/aliyun/aliyun_assist_client/agent/taskengine/timermanager"
)
const (
_netstatReportType = "AgentNetworkConnectionStatistics"
_netstatReportIntervalSeconds = 15 * 60
)
var (
_netstatTimer *timermanager.Timer
_netstatTimerInitLock sync.Mutex
errNetstatTimerInitialized = fmt.Errorf("%s timer has been initialized", _netstatReportType)
)
func InitNetstatTimer() error {
if _netstatTimer == nil {
_netstatTimerInitLock.Lock()
defer _netstatTimerInitLock.Unlock()
if _netstatTimer == nil {
timerManager := timermanager.GetTimerManager()
timer, err := timerManager.CreateTimerInSeconds(func() {
_, err := ReportCommandOutput(_netstatReportType, "/bin/sh", []string{"-c", "netstat -tnp | grep aliyun"})
if err != nil {
log.GetLogger().WithError(err).Errorf("Failed to report %s", _netstatReportType)
}
}, _netstatReportIntervalSeconds)
if err != nil {
return err
}
_netstatTimer = timer
// Due to throtting policy of client_report API, netstat job should not be exeucted immediately
mutableSchedule, ok := _netstatTimer.Schedule.(*timermanager.MutableScheduled)
if !ok {
return errors.New("Unexpected schedule type of netstat timer")
}
mutableSchedule.NotImmediately()
_, err = _netstatTimer.Run()
if err != nil {
return err
}
// Inititate another goroutine to sleep a while and issue first invocation
go func() {
time.Sleep(40 * time.Second)
_netstatTimer.SkipWaiting()
}()
return nil
}
return errNetstatTimerInitialized
}
return errNetstatTimerInitialized
}