2018-12-06 20:31:57 +03:00
|
|
|
// Copyright 2018 Microsoft. All rights reserved.
|
2017-12-08 22:56:21 +03:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-12-12 05:36:01 +03:00
|
|
|
|
2020-01-15 01:53:24 +03:00
|
|
|
"github.com/Azure/azure-container-networking/aitelemetry"
|
2017-12-12 05:36:01 +03:00
|
|
|
"github.com/Azure/azure-container-networking/common"
|
2021-11-09 19:19:44 +03:00
|
|
|
"github.com/Azure/azure-container-networking/log"
|
2018-01-04 23:14:28 +03:00
|
|
|
"github.com/Azure/azure-container-networking/platform"
|
2021-09-20 21:55:21 +03:00
|
|
|
"github.com/pkg/errors"
|
2023-10-04 21:43:46 +03:00
|
|
|
"go.uber.org/zap"
|
2017-12-08 22:56:21 +03:00
|
|
|
)
|
|
|
|
|
2018-08-17 00:12:58 +03:00
|
|
|
const (
|
2018-08-17 02:28:46 +03:00
|
|
|
// CNITelemetryFile Path.
|
|
|
|
CNITelemetryFile = platform.CNIRuntimePath + "AzureCNITelemetry.json"
|
2019-04-09 02:20:05 +03:00
|
|
|
// ContentType of JSON
|
2018-08-17 00:12:58 +03:00
|
|
|
ContentType = "application/json"
|
|
|
|
)
|
|
|
|
|
2017-12-08 22:56:21 +03:00
|
|
|
// OS Details structure.
|
|
|
|
type OSInfo struct {
|
|
|
|
OSType string
|
|
|
|
OSVersion string
|
|
|
|
KernelVersion string
|
|
|
|
OSDistribution string
|
2017-12-12 05:36:01 +03:00
|
|
|
ErrorMessage string
|
2017-12-08 22:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// System Details structure.
|
|
|
|
type SystemInfo struct {
|
|
|
|
MemVMTotal uint64
|
|
|
|
MemVMFree uint64
|
|
|
|
MemUsedByProcess uint64
|
|
|
|
DiskVMTotal uint64
|
|
|
|
DiskVMFree uint64
|
|
|
|
CPUCount int
|
2017-12-12 05:36:01 +03:00
|
|
|
ErrorMessage string
|
2017-12-08 22:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Interface Details structure.
|
|
|
|
type InterfaceInfo struct {
|
|
|
|
InterfaceType string
|
|
|
|
Subnet string
|
|
|
|
PrimaryCA string
|
|
|
|
MAC string
|
|
|
|
Name string
|
|
|
|
SecondaryCATotalCount int
|
|
|
|
SecondaryCAUsedCount int
|
2017-12-12 05:36:01 +03:00
|
|
|
ErrorMessage string
|
2017-12-08 22:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CNI Bridge Details structure.
|
|
|
|
type BridgeInfo struct {
|
2017-12-12 05:36:01 +03:00
|
|
|
NetworkMode string
|
|
|
|
BridgeName string
|
|
|
|
ErrorMessage string
|
2017-12-08 22:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Azure CNI Telemetry Report structure.
|
2018-07-20 00:23:11 +03:00
|
|
|
type CNIReport struct {
|
2021-09-20 21:49:08 +03:00
|
|
|
IsNewInstance bool
|
|
|
|
CniSucceeded bool
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
ErrorMessage string
|
|
|
|
EventMessage string
|
|
|
|
OperationType string
|
|
|
|
OperationDuration int
|
|
|
|
Context string
|
|
|
|
SubContext string
|
|
|
|
VMUptime string
|
|
|
|
Timestamp string
|
|
|
|
ContainerName string
|
|
|
|
InfraVnetID string
|
|
|
|
VnetAddressSpace []string
|
|
|
|
OSDetails OSInfo
|
|
|
|
SystemDetails SystemInfo
|
|
|
|
InterfaceDetails InterfaceInfo
|
|
|
|
BridgeDetails BridgeInfo
|
|
|
|
Metadata common.Metadata `json:"compute"`
|
2023-10-13 22:08:58 +03:00
|
|
|
Logger *zap.Logger
|
2017-12-08 22:56:21 +03:00
|
|
|
}
|
|
|
|
|
2020-01-15 01:53:24 +03:00
|
|
|
type AIMetric struct {
|
|
|
|
Metric aitelemetry.Metric
|
|
|
|
}
|
|
|
|
|
2018-07-20 02:06:11 +03:00
|
|
|
// ReportManager structure.
|
2017-12-08 22:56:21 +03:00
|
|
|
type ReportManager struct {
|
|
|
|
HostNetAgentURL string
|
2018-08-17 00:12:58 +03:00
|
|
|
ContentType string
|
|
|
|
Report interface{}
|
2017-12-08 22:56:21 +03:00
|
|
|
}
|
|
|
|
|
2017-12-12 05:36:01 +03:00
|
|
|
// GetReport retrieves orchestrator, system, OS and Interface details and create a report structure.
|
2018-08-17 00:12:58 +03:00
|
|
|
func (report *CNIReport) GetReport(name string, version string, ipamQueryURL string) {
|
|
|
|
report.Name = name
|
2019-01-23 04:07:32 +03:00
|
|
|
report.Version = version
|
2018-08-17 00:12:58 +03:00
|
|
|
|
|
|
|
report.GetSystemDetails()
|
|
|
|
report.GetOSDetails()
|
2017-12-08 22:56:21 +03:00
|
|
|
}
|
|
|
|
|
2018-08-17 00:12:58 +03:00
|
|
|
// SendReport will send telemetry report to HostNetAgent.
|
2019-01-23 04:07:32 +03:00
|
|
|
func (reportMgr *ReportManager) SendReport(tb *TelemetryBuffer) error {
|
2019-02-06 02:14:44 +03:00
|
|
|
var err error
|
2019-03-14 00:01:30 +03:00
|
|
|
var report []byte
|
2017-12-13 00:17:00 +03:00
|
|
|
|
2019-03-14 00:01:30 +03:00
|
|
|
if tb != nil && tb.Connected {
|
|
|
|
report, err = reportMgr.ReportToBytes()
|
2019-01-23 04:07:32 +03:00
|
|
|
if err == nil {
|
|
|
|
if _, err = tb.Write(report); err != nil {
|
2023-10-04 21:43:46 +03:00
|
|
|
if tb.logger != nil {
|
|
|
|
tb.logger.Error("telemetry write failed", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
log.Printf("telemetry write failed:%v", err)
|
|
|
|
}
|
2019-01-23 04:07:32 +03:00
|
|
|
}
|
|
|
|
}
|
2017-12-08 22:56:21 +03:00
|
|
|
}
|
|
|
|
|
2019-02-06 02:14:44 +03:00
|
|
|
return err
|
2018-07-20 00:23:11 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 20:31:57 +03:00
|
|
|
// ReportToBytes - returns the report bytes
|
2019-03-14 00:01:30 +03:00
|
|
|
func (reportMgr *ReportManager) ReportToBytes() ([]byte, error) {
|
2018-12-06 20:31:57 +03:00
|
|
|
switch reportMgr.Report.(type) {
|
|
|
|
case *CNIReport:
|
2020-01-15 01:53:24 +03:00
|
|
|
case *AIMetric:
|
2018-12-06 20:31:57 +03:00
|
|
|
default:
|
2021-09-20 21:55:21 +03:00
|
|
|
return []byte{}, errors.Errorf("Invalid report type: %T", reportMgr.Report)
|
2018-12-06 20:31:57 +03:00
|
|
|
}
|
|
|
|
|
2021-09-20 21:55:21 +03:00
|
|
|
report, err := json.Marshal(reportMgr.Report)
|
2019-03-14 00:01:30 +03:00
|
|
|
return report, err
|
2018-12-06 20:31:57 +03:00
|
|
|
}
|
2020-01-15 01:53:24 +03:00
|
|
|
|
|
|
|
// This function for sending CNI metrics to telemetry service
|
|
|
|
func SendCNIMetric(cniMetric *AIMetric, tb *TelemetryBuffer) error {
|
2021-09-20 21:55:21 +03:00
|
|
|
var err error
|
|
|
|
var report []byte
|
2020-01-15 01:53:24 +03:00
|
|
|
|
|
|
|
if tb != nil && tb.Connected {
|
|
|
|
reportMgr := &ReportManager{Report: cniMetric}
|
|
|
|
report, err = reportMgr.ReportToBytes()
|
|
|
|
if err == nil {
|
|
|
|
if _, err = tb.Write(report); err != nil {
|
2023-10-04 21:43:46 +03:00
|
|
|
tb.logger.Error("Error writing to telemetry socket", zap.Error(err))
|
2020-01-15 01:53:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-01-25 20:44:51 +03:00
|
|
|
|
2022-07-02 01:09:39 +03:00
|
|
|
func SendCNIEvent(tb *TelemetryBuffer, report *CNIReport) {
|
2022-01-25 20:44:51 +03:00
|
|
|
if tb != nil && tb.Connected {
|
2022-07-02 01:09:39 +03:00
|
|
|
reportMgr := &ReportManager{Report: report}
|
|
|
|
reportBytes, err := reportMgr.ReportToBytes()
|
2022-01-25 20:44:51 +03:00
|
|
|
if err == nil {
|
2022-07-02 01:09:39 +03:00
|
|
|
if _, err = tb.Write(reportBytes); err != nil {
|
2023-10-04 21:43:46 +03:00
|
|
|
tb.logger.Error("Error writing to telemetry socket", zap.Error(err))
|
2022-01-25 20:44:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|