github.com/ubuntu/ubuntu-report@v1.7.4-0.20240410144652-96f37d845fac/pkg/sysmetrics/api.go (about) 1 // Package sysmetrics Golang bindings: collect and report system and hardware metrics 2 // from your system. 3 package sysmetrics 4 5 import ( 6 "os" 7 8 "github.com/pkg/errors" 9 log "github.com/sirupsen/logrus" 10 "github.com/ubuntu/ubuntu-report/internal/metrics" 11 ) 12 13 // ReportType define the desired kind of interaction in CollectAndSend() 14 type ReportType int 15 16 const ( 17 // ReportInteractive will show report content on stdout and read anwser on stdin 18 ReportInteractive ReportType = iota 19 // ReportAuto will send a report without printing report 20 ReportAuto 21 // ReportOptOut will send opt-out message without printing report 22 ReportOptOut 23 ) 24 25 // Collect system info and return a pretty printed version of collected data 26 func Collect() ([]byte, error) { 27 log.Debug("collect system information") 28 29 m, err := metrics.New() 30 if err != nil { 31 return nil, errors.Wrapf(err, "couldn't create a metric collector") 32 } 33 return metricsCollect(m) 34 } 35 36 // SendReport POST to the baseURL server data coming from a previous collect. 37 // The report will not be sent if a report has already been sent for this version unless "alwaysReport" is true. 38 // If "baseURL" is not an empty string, this overrides the server the report is sent to. 39 func SendReport(data []byte, alwaysReport bool, baseURL string) error { 40 log.Debug("report system information") 41 42 m, err := metrics.New() 43 if err != nil { 44 return errors.Wrapf(err, "couldn't create a metric collector") 45 } 46 return metricsSend(m, data, true, alwaysReport, baseURL, "", os.Stdin, os.Stdout) 47 } 48 49 // SendDecline POST to the baseURL server data denial report message. 50 // The denial message will not be sent if a report has already been sent for this version unless "alwaysReport" is true. 51 // If "baseURL" is not an empty string, this overrides the server the report is sent to. 52 func SendDecline(alwaysReport bool, baseURL string) error { 53 log.Debug("report system information") 54 55 m, err := metrics.New() 56 if err != nil { 57 return errors.Wrapf(err, "couldn't create a metric collector") 58 } 59 return metricsSend(m, nil, false, alwaysReport, baseURL, "", os.Stdin, os.Stdout) 60 } 61 62 // CollectAndSend gather system info and send them 63 // The report will not be sent if a report has already been sent for this version unless "alwaysReport" is true. 64 // If "baseURL" is not an empty string, this overrides the server the report is sent to. 65 func CollectAndSend(r ReportType, alwaysReport bool, baseURL string) error { 66 log.Debug("collect and report system information") 67 68 m, err := metrics.New() 69 if err != nil { 70 return errors.Wrapf(err, "couldn't create a metric collector") 71 } 72 return metricsCollectAndSend(m, r, alwaysReport, baseURL, "", os.Stdin, os.Stdout) 73 } 74 75 // CollectAndSendOnUpgrade gather system info and send them 76 // The report will not be sent if a report has already been sent for this version unless "alwaysReport" is true. 77 // It will only send if a previous report has been found, collect latest report answer (opt-in or opt-out) 78 // and decides what to send on that new version based on those facts. 79 // If "baseURL" is not an empty string, this overrides the server the report is sent to. 80 func CollectAndSendOnUpgrade(alwaysReport bool, baseURL string) error { 81 log.Debug("collect and report system information on upgrade") 82 83 m, err := metrics.New() 84 if err != nil { 85 return errors.Wrapf(err, "couldn't create a metric collector") 86 } 87 return metricsCollectAndSendOnUpgrade(m, alwaysReport, baseURL, "", os.Stdin, os.Stdout) 88 } 89 90 // SendPendingReport will try to send any pending report which didn't succeed previously due to network issues. 91 // It will try sending and exponentially back off until a send is successful. 92 func SendPendingReport(baseURL string) error { 93 log.Debug("try sending previous report") 94 95 m, err := metrics.New() 96 if err != nil { 97 return errors.Wrapf(err, "couldn't create a metric collector") 98 } 99 return metricsSendPendingReport(m, baseURL, "", os.Stdin, os.Stdout) 100 }