|
| 1 | +// Copyright 2018 Microsoft. All rights reserved. |
| 2 | +// MIT License |
| 3 | + |
| 4 | +package telemetry |
| 5 | + |
| 6 | +import ( |
| 7 | + "bufio" |
| 8 | + "bytes" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "net" |
| 12 | + "net/http" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +// FdName - file descriptor name |
| 18 | +// Delimiter - delimiter for socket reads/writes |
| 19 | +// HostNetAgentURL - host net agent url of type payload |
| 20 | +// DefaultDncReportsSize - default DNC report slice size |
| 21 | +// DefaultCniReportsSize - default CNI report slice size |
| 22 | +// DefaultNpmReportsSize - default NPM report slice size |
| 23 | +// DefaultInterval - default interval for sending payload to host |
| 24 | +const ( |
| 25 | + FdName = "azure-telemetry" |
| 26 | + Delimiter = '\n' |
| 27 | + HostNetAgentURL = "http://169.254.169.254/machine/plugins?comp=netagent&type=payload" |
| 28 | + DefaultInterval = 1 * time.Minute |
| 29 | +) |
| 30 | + |
| 31 | +// TelemetryBuffer object |
| 32 | +type TelemetryBuffer struct { |
| 33 | + client net.Conn |
| 34 | + listener net.Listener |
| 35 | + connections []net.Conn |
| 36 | + payload Payload |
| 37 | + fdExists bool |
| 38 | + connected bool |
| 39 | + data chan interface{} |
| 40 | + cancel chan bool |
| 41 | +} |
| 42 | + |
| 43 | +// Payload object holds the different types of reports |
| 44 | +type Payload struct { |
| 45 | + DNCReports []DNCReport |
| 46 | + CNIReports []CNIReport |
| 47 | + NPMReports []NPMReport |
| 48 | +} |
| 49 | + |
| 50 | +// NewTelemetryBuffer - create a new TelemetryBuffer |
| 51 | +func NewTelemetryBuffer() (*TelemetryBuffer, error) { |
| 52 | + var tb TelemetryBuffer |
| 53 | + tb.data = make(chan interface{}) |
| 54 | + tb.cancel = make(chan bool, 1) |
| 55 | + tb.connections = make([]net.Conn, 1) |
| 56 | + err := tb.Listen(FdName) |
| 57 | + if err != nil { |
| 58 | + tb.fdExists = strings.Contains(err.Error(), "in use") || strings.Contains(err.Error(), "Access is denied") |
| 59 | + } else { |
| 60 | + // Spawn server goroutine to handle incoming connections |
| 61 | + go func() { |
| 62 | + for { |
| 63 | + // Spawn worker goroutines to communicate with client |
| 64 | + conn, err := tb.listener.Accept() |
| 65 | + if err == nil { |
| 66 | + tb.connections = append(tb.connections, conn) |
| 67 | + go func() { |
| 68 | + for { |
| 69 | + reportStr, err := read(conn) |
| 70 | + if err == nil { |
| 71 | + var tmp map[string]interface{} |
| 72 | + json.Unmarshal(reportStr, &tmp) |
| 73 | + if _, ok := tmp["NpmVersion"]; ok { |
| 74 | + var npmReport NPMReport |
| 75 | + json.Unmarshal([]byte(reportStr), &npmReport) |
| 76 | + tb.data <- npmReport |
| 77 | + } else if _, ok := tmp["CniSucceeded"]; ok { |
| 78 | + var cniReport CNIReport |
| 79 | + json.Unmarshal([]byte(reportStr), &cniReport) |
| 80 | + tb.data <- cniReport |
| 81 | + } else if _, ok := tmp["Allocations"]; ok { |
| 82 | + var dncReport DNCReport |
| 83 | + json.Unmarshal([]byte(reportStr), &dncReport) |
| 84 | + tb.data <- dncReport |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + }() |
| 89 | + } |
| 90 | + } |
| 91 | + }() |
| 92 | + } |
| 93 | + |
| 94 | + err = tb.Dial(FdName) |
| 95 | + if err == nil { |
| 96 | + tb.connected = true |
| 97 | + tb.payload.DNCReports = make([]DNCReport, 0) |
| 98 | + tb.payload.CNIReports = make([]CNIReport, 0) |
| 99 | + tb.payload.NPMReports = make([]NPMReport, 0) |
| 100 | + } else if tb.fdExists { |
| 101 | + tb.cleanup(FdName) |
| 102 | + } |
| 103 | + |
| 104 | + return &tb, err |
| 105 | +} |
| 106 | + |
| 107 | +// Start - start running an instance if it isn't already being run elsewhere |
| 108 | +func (tb *TelemetryBuffer) Start(intervalms time.Duration) { |
| 109 | + defer tb.close() |
| 110 | + if !tb.fdExists && tb.connected { |
| 111 | + if intervalms < DefaultInterval { |
| 112 | + intervalms = DefaultInterval |
| 113 | + } |
| 114 | + |
| 115 | + interval := time.NewTicker(intervalms).C |
| 116 | + for { |
| 117 | + select { |
| 118 | + case <-interval: |
| 119 | + // Send payload to host and clear cache when sent successfully |
| 120 | + // To-do : if we hit max slice size in payload, write to disk and process the logs on disk on future sends |
| 121 | + if err := tb.sendToHost(); err == nil { |
| 122 | + tb.payload.reset() |
| 123 | + } |
| 124 | + case report := <-tb.data: |
| 125 | + tb.payload.push(report) |
| 126 | + case <-tb.cancel: |
| 127 | + goto EXIT |
| 128 | + } |
| 129 | + } |
| 130 | + } else { |
| 131 | + <-tb.cancel |
| 132 | + } |
| 133 | + |
| 134 | +EXIT: |
| 135 | +} |
| 136 | + |
| 137 | +// read - read from the file descriptor |
| 138 | +func read(conn net.Conn) (b []byte, err error) { |
| 139 | + b, err = bufio.NewReader(conn).ReadBytes(Delimiter) |
| 140 | + if err == nil { |
| 141 | + b = b[:len(b)-1] |
| 142 | + } |
| 143 | + |
| 144 | + return |
| 145 | +} |
| 146 | + |
| 147 | +// Write - write to the file descriptor |
| 148 | +func (tb *TelemetryBuffer) Write(b []byte) (c int, err error) { |
| 149 | + b = append(b, Delimiter) |
| 150 | + w := bufio.NewWriter(tb.client) |
| 151 | + c, err = w.Write(b) |
| 152 | + if err == nil { |
| 153 | + w.Flush() |
| 154 | + } |
| 155 | + |
| 156 | + return |
| 157 | +} |
| 158 | + |
| 159 | +// Cancel - signal to tear down telemetry buffer |
| 160 | +func (tb *TelemetryBuffer) Cancel() { |
| 161 | + tb.cancel <- true |
| 162 | +} |
| 163 | + |
| 164 | +// close - close all connections |
| 165 | +func (tb *TelemetryBuffer) close() { |
| 166 | + if tb.client != nil { |
| 167 | + tb.client.Close() |
| 168 | + } |
| 169 | + |
| 170 | + if tb.listener != nil { |
| 171 | + tb.listener.Close() |
| 172 | + } |
| 173 | + |
| 174 | + for _, conn := range tb.connections { |
| 175 | + if conn != nil { |
| 176 | + conn.Close() |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +// sendToHost - send payload to host |
| 182 | +func (tb *TelemetryBuffer) sendToHost() error { |
| 183 | + httpc := &http.Client{} |
| 184 | + var body bytes.Buffer |
| 185 | + json.NewEncoder(&body).Encode(tb.payload) |
| 186 | + resp, err := httpc.Post(HostNetAgentURL, ContentType, &body) |
| 187 | + if err != nil { |
| 188 | + return fmt.Errorf("[Telemetry] HTTP Post returned error %v", err) |
| 189 | + } |
| 190 | + |
| 191 | + defer resp.Body.Close() |
| 192 | + |
| 193 | + if resp.StatusCode != http.StatusOK { |
| 194 | + return fmt.Errorf("[Telemetry] HTTP Post returned statuscode %d", resp.StatusCode) |
| 195 | + } |
| 196 | + |
| 197 | + return nil |
| 198 | +} |
| 199 | + |
| 200 | +// push - push the report (x) to corresponding slice |
| 201 | +func (pl *Payload) push(x interface{}) { |
| 202 | + switch x.(type) { |
| 203 | + case DNCReport: |
| 204 | + pl.DNCReports = append(pl.DNCReports, x.(DNCReport)) |
| 205 | + case CNIReport: |
| 206 | + pl.CNIReports = append(pl.CNIReports, x.(CNIReport)) |
| 207 | + case NPMReport: |
| 208 | + pl.NPMReports = append(pl.NPMReports, x.(NPMReport)) |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +// reset - reset payload slices |
| 213 | +func (pl *Payload) reset() { |
| 214 | + pl.DNCReports = nil |
| 215 | + pl.DNCReports = make([]DNCReport, 0) |
| 216 | + pl.CNIReports = nil |
| 217 | + pl.CNIReports = make([]CNIReport, 0) |
| 218 | + pl.NPMReports = nil |
| 219 | + pl.NPMReports = make([]NPMReport, 0) |
| 220 | +} |
0 commit comments