|
| 1 | +// Copyright 2017 Microsoft. All rights reserved. |
| 2 | +// MIT License |
| 3 | + |
| 4 | +package telemetry |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "encoding/json" |
| 9 | + "encoding/xml" |
| 10 | + "fmt" |
| 11 | + "log" |
| 12 | + "net/http" |
| 13 | + "os" |
| 14 | +) |
| 15 | + |
| 16 | +type xmlDocument struct { |
| 17 | + XMLName xml.Name `xml:"Interfaces"` |
| 18 | + Interface []struct { |
| 19 | + XMLName xml.Name `xml:"Interface"` |
| 20 | + MacAddress string `xml:"MacAddress,attr"` |
| 21 | + IsPrimary bool `xml:"IsPrimary,attr"` |
| 22 | + |
| 23 | + IPSubnet []struct { |
| 24 | + XMLName xml.Name `xml:"IPSubnet"` |
| 25 | + Prefix string `xml:"Prefix,attr"` |
| 26 | + |
| 27 | + IPAddress []struct { |
| 28 | + XMLName xml.Name `xml:"IPAddress"` |
| 29 | + Address string `xml:"Address,attr"` |
| 30 | + IsPrimary bool `xml:"IsPrimary,attr"` |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// OS Details structure. |
| 37 | +type OSInfo struct { |
| 38 | + OSType string |
| 39 | + OSVersion string |
| 40 | + KernelVersion string |
| 41 | + OSDistribution string |
| 42 | +} |
| 43 | + |
| 44 | +// System Details structure. |
| 45 | +type SystemInfo struct { |
| 46 | + MemVMTotal uint64 |
| 47 | + MemVMFree uint64 |
| 48 | + MemUsedByProcess uint64 |
| 49 | + DiskVMTotal uint64 |
| 50 | + DiskVMFree uint64 |
| 51 | + CPUCount int |
| 52 | +} |
| 53 | + |
| 54 | +// Interface Details structure. |
| 55 | +type InterfaceInfo struct { |
| 56 | + InterfaceType string |
| 57 | + Subnet string |
| 58 | + PrimaryCA string |
| 59 | + MAC string |
| 60 | + Name string |
| 61 | + SecondaryCATotalCount int |
| 62 | + SecondaryCAUsedCount int |
| 63 | +} |
| 64 | + |
| 65 | +// CNI Bridge Details structure. |
| 66 | +type BridgeInfo struct { |
| 67 | + NetworkMode string |
| 68 | + BridgeName string |
| 69 | +} |
| 70 | + |
| 71 | +type OrchsestratorInfo struct { |
| 72 | + OrchestratorName string |
| 73 | + OrchestratorVersion string |
| 74 | +} |
| 75 | + |
| 76 | +// Azure CNI Telemetry Report structure. |
| 77 | +type Report struct { |
| 78 | + StartFlag bool |
| 79 | + Name string |
| 80 | + Version string |
| 81 | + OrchestratorDetails *OrchsestratorInfo |
| 82 | + OSDetails *OSInfo |
| 83 | + SystemDetails *SystemInfo |
| 84 | + InterfaceDetails *InterfaceInfo |
| 85 | + BridgeDetails *BridgeInfo |
| 86 | + VnetAddressSpace []string |
| 87 | + ErrorMessage string |
| 88 | + Context string |
| 89 | + SubContext string |
| 90 | +} |
| 91 | + |
| 92 | +// ReportManager structure. |
| 93 | +type ReportManager struct { |
| 94 | + HostNetAgentURL string |
| 95 | + IpamQueryURL string |
| 96 | + ReportType string |
| 97 | + Report *Report |
| 98 | +} |
| 99 | + |
| 100 | +// GetReport retrieves orchestrator, system, OS and Interface details and create a report structure. |
| 101 | +func (reportMgr *ReportManager) GetReport(name string, version string) (*Report, error) { |
| 102 | + var err error |
| 103 | + report := &Report{} |
| 104 | + |
| 105 | + report.Name = name |
| 106 | + report.Version = version |
| 107 | + report.OrchestratorDetails = GetOrchestratorDetails() |
| 108 | + |
| 109 | + report.SystemDetails, err = GetSystemDetails() |
| 110 | + if err != nil { |
| 111 | + return report, err |
| 112 | + } |
| 113 | + |
| 114 | + report.OSDetails, err = GetOSDetails() |
| 115 | + if err != nil { |
| 116 | + return report, err |
| 117 | + } |
| 118 | + |
| 119 | + report.InterfaceDetails, err = GetInterfaceDetails(reportMgr.IpamQueryURL) |
| 120 | + if err != nil { |
| 121 | + return report, err |
| 122 | + } |
| 123 | + |
| 124 | + return report, nil |
| 125 | +} |
| 126 | + |
| 127 | +// This function will send telemetry report to HostNetAgent. |
| 128 | +func (reportMgr *ReportManager) SendReport() error { |
| 129 | + var body bytes.Buffer |
| 130 | + |
| 131 | + httpc := &http.Client{} |
| 132 | + json.NewEncoder(&body).Encode(reportMgr.Report) |
| 133 | + |
| 134 | + log.Printf("Going to send Telemetry report to hostnetagent %v", reportMgr.HostNetAgentURL) |
| 135 | + log.Printf("Flag %v Name %v Version %v orchestname %s ErrorMessage %v SystemDetails %v OSDetails %v InterfaceDetails %v", |
| 136 | + reportMgr.Report.StartFlag, reportMgr.Report.Name, reportMgr.Report.Version, reportMgr.Report.OrchestratorDetails, reportMgr.Report.ErrorMessage, |
| 137 | + reportMgr.Report.SystemDetails, reportMgr.Report.OSDetails, reportMgr.Report.InterfaceDetails) |
| 138 | + |
| 139 | + res, err := httpc.Post(reportMgr.HostNetAgentURL, reportMgr.ReportType, &body) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("[Azure CNI] HTTP Post returned error %v", err) |
| 142 | + } |
| 143 | + |
| 144 | + if res.StatusCode != 200 { |
| 145 | + return fmt.Errorf("[Azure CNI] HTTP Post returned statuscode %d", res.StatusCode) |
| 146 | + } |
| 147 | + |
| 148 | + log.Printf("Send telemetry success %d\n", res.StatusCode) |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +// This function will save the state in file if telemetry report sent successfully. |
| 153 | +func (report *Report) SetReportState() error { |
| 154 | + f, err := os.OpenFile(TelemetryFile, os.O_RDWR|os.O_CREATE, 0666) |
| 155 | + if err != nil { |
| 156 | + return fmt.Errorf("Error opening telemetry file %v", err) |
| 157 | + } |
| 158 | + |
| 159 | + defer f.Close() |
| 160 | + |
| 161 | + reportBytes, err := json.Marshal(report) |
| 162 | + if err != nil { |
| 163 | + log.Printf("report write failed due to %v", err) |
| 164 | + _, err = f.WriteString("report write failed") |
| 165 | + } else { |
| 166 | + _, err = f.Write(reportBytes) |
| 167 | + } |
| 168 | + |
| 169 | + if err != nil { |
| 170 | + log.Printf("Error while writing to file %v", err) |
| 171 | + return fmt.Errorf("Error while writing to file %v", err) |
| 172 | + } |
| 173 | + |
| 174 | + report.StartFlag = false |
| 175 | + log.Printf("SetReportState succeeded") |
| 176 | + return nil |
| 177 | +} |
| 178 | + |
| 179 | +// This function will check if report is sent atleast once by checking telemetry file. |
| 180 | +func (report *Report) GetReportState() bool { |
| 181 | + if _, err := os.Stat(TelemetryFile); os.IsNotExist(err) { |
| 182 | + log.Printf("File not exist %v", TelemetryFile) |
| 183 | + report.StartFlag = true |
| 184 | + return false |
| 185 | + } |
| 186 | + |
| 187 | + return true |
| 188 | +} |
0 commit comments