goSNMPWrapper.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package snmpreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver"
  4. import (
  5. "time"
  6. "github.com/gosnmp/gosnmp"
  7. )
  8. // goSNMPWrapper is mostly to assist with mocking
  9. type goSNMPWrapper interface {
  10. // Connect creates and opens a socket. Because UDP is a connectionless
  11. // protocol, you won't know if the remote host is responding until you send
  12. // packets. And if the host is regularly disappearing and reappearing, you won't
  13. // know if you've only done a Connect().
  14. //
  15. // For historical reasons (ie this is part of the public API), the method won't
  16. // be renamed.
  17. Connect() error
  18. // Close closes the connection
  19. Close() error
  20. // Get sends an SNMP GET request
  21. Get(oids []string) (result *gosnmp.SnmpPacket, err error)
  22. // WalkAll is similar to Walk but returns a filled array of all values rather
  23. // than using a callback function to stream results. Caution: if you have set
  24. // x.AppOpts to 'c', WalkAll may loop indefinitely and cause an Out Of Memory -
  25. // use Walk instead.
  26. WalkAll(rootOid string) (results []gosnmp.SnmpPDU, err error)
  27. // BulkWalkAll is similar to BulkWalk but returns a filled array of all values
  28. // rather than using a callback function to stream results. Caution: if you
  29. // have set x.AppOpts to 'c', BulkWalkAll may loop indefinitely and cause an
  30. // Out Of Memory - use BulkWalk instead.
  31. BulkWalkAll(rootOid string) (results []gosnmp.SnmpPDU, err error)
  32. // GetTransport gets the Transport
  33. GetTransport() string
  34. // SetTransport sets the Transport
  35. SetTransport(transport string)
  36. // GetTarget gets the Target
  37. GetTarget() string
  38. // SetTarget sets the Target
  39. SetTarget(target string)
  40. // GetPort gets the Port
  41. GetPort() uint16
  42. // SetPort sets the Port
  43. SetPort(port uint16)
  44. // GetCommunity gets the Community
  45. GetCommunity() string
  46. // SetCommunity sets the Community
  47. SetCommunity(community string)
  48. // GetVersion gets the Version
  49. GetVersion() gosnmp.SnmpVersion
  50. // SetVersion sets the Version
  51. SetVersion(version gosnmp.SnmpVersion)
  52. // GetTimeout gets the Timeout
  53. GetTimeout() time.Duration
  54. // SetTimeout sets the Timeout
  55. SetTimeout(timeout time.Duration)
  56. // GetMaxOids gets the MaxOids
  57. GetMaxOids() int
  58. // SetMaxOids sets the MaxOids
  59. SetMaxOids(maxOids int)
  60. // GetMsgFlags gets the MsgFlags
  61. GetMsgFlags() gosnmp.SnmpV3MsgFlags
  62. // SetMsgFlags sets the MsgFlags
  63. SetMsgFlags(msgFlags gosnmp.SnmpV3MsgFlags)
  64. // GetSecurityModel gets the SecurityModel
  65. GetSecurityModel() gosnmp.SnmpV3SecurityModel
  66. // SetSecurityModel sets the SecurityModel
  67. SetSecurityModel(securityModel gosnmp.SnmpV3SecurityModel)
  68. // GetSecurityParameters gets the SecurityParameters
  69. GetSecurityParameters() gosnmp.SnmpV3SecurityParameters
  70. // SetSecurityParameters sets the SecurityParameters
  71. SetSecurityParameters(securityParameters gosnmp.SnmpV3SecurityParameters)
  72. }
  73. // otelGoSNMPWrapper is a wrapper around gosnmp
  74. type otelGoSNMPWrapper struct {
  75. gosnmp.GoSNMP
  76. }
  77. // newGoSNMPWrapper creates a new goSNMPWrapper using gosnmp
  78. func newGoSNMPWrapper() goSNMPWrapper {
  79. return &otelGoSNMPWrapper{
  80. gosnmp.GoSNMP{
  81. MaxOids: gosnmp.Default.MaxOids,
  82. },
  83. }
  84. }
  85. // Close closes the GoSNMP connection
  86. func (w *otelGoSNMPWrapper) Close() error {
  87. return w.GoSNMP.Conn.Close()
  88. }
  89. // GetTransport gets the Transport
  90. func (w *otelGoSNMPWrapper) GetTransport() string {
  91. return w.GoSNMP.Transport
  92. }
  93. // SetTransport sets the Transport
  94. func (w *otelGoSNMPWrapper) SetTransport(transport string) {
  95. w.GoSNMP.Transport = transport
  96. }
  97. // GetTarget gets the Target
  98. func (w *otelGoSNMPWrapper) GetTarget() string {
  99. return w.GoSNMP.Target
  100. }
  101. // SetTarget sets the Target
  102. func (w *otelGoSNMPWrapper) SetTarget(target string) {
  103. w.GoSNMP.Target = target
  104. }
  105. // GetPort gets the Port
  106. func (w *otelGoSNMPWrapper) GetPort() uint16 {
  107. return w.GoSNMP.Port
  108. }
  109. // SetPort sets the Port
  110. func (w *otelGoSNMPWrapper) SetPort(port uint16) {
  111. w.GoSNMP.Port = port
  112. }
  113. // GetCommunity gets the Community
  114. func (w *otelGoSNMPWrapper) GetCommunity() string {
  115. return w.GoSNMP.Community
  116. }
  117. // SetCommunity sets the Community
  118. func (w *otelGoSNMPWrapper) SetCommunity(community string) {
  119. w.GoSNMP.Community = community
  120. }
  121. // GetVersion gets the Version
  122. func (w *otelGoSNMPWrapper) GetVersion() gosnmp.SnmpVersion {
  123. return w.GoSNMP.Version
  124. }
  125. // SetVersion sets the Version
  126. func (w *otelGoSNMPWrapper) SetVersion(version gosnmp.SnmpVersion) {
  127. w.GoSNMP.Version = version
  128. }
  129. // GetTimeout gets the Timeout
  130. func (w *otelGoSNMPWrapper) GetTimeout() time.Duration {
  131. return w.GoSNMP.Timeout
  132. }
  133. // SetTimeout sets the Timeout
  134. func (w *otelGoSNMPWrapper) SetTimeout(timeout time.Duration) {
  135. w.GoSNMP.Timeout = timeout
  136. }
  137. // GetMaxOids gets the MaxOids
  138. func (w *otelGoSNMPWrapper) GetMaxOids() int {
  139. return w.GoSNMP.MaxOids
  140. }
  141. // SetMaxOids sets the MaxOids
  142. func (w *otelGoSNMPWrapper) SetMaxOids(maxOids int) {
  143. w.GoSNMP.MaxOids = maxOids
  144. }
  145. // GetMsgFlags gets the MsgFlags
  146. func (w *otelGoSNMPWrapper) GetMsgFlags() gosnmp.SnmpV3MsgFlags {
  147. return w.GoSNMP.MsgFlags
  148. }
  149. // SetMsgFlags sets the MsgFlags
  150. func (w *otelGoSNMPWrapper) SetMsgFlags(msgFlags gosnmp.SnmpV3MsgFlags) {
  151. w.GoSNMP.MsgFlags = msgFlags
  152. }
  153. // GetSecurityModel gets the SecurityModel
  154. func (w *otelGoSNMPWrapper) GetSecurityModel() gosnmp.SnmpV3SecurityModel {
  155. return w.GoSNMP.SecurityModel
  156. }
  157. // SetSecurityModel sets the SecurityModel
  158. func (w *otelGoSNMPWrapper) SetSecurityModel(securityModel gosnmp.SnmpV3SecurityModel) {
  159. w.GoSNMP.SecurityModel = securityModel
  160. }
  161. // GetSecurityParameters gets the SecurityParameters
  162. func (w *otelGoSNMPWrapper) GetSecurityParameters() gosnmp.SnmpV3SecurityParameters {
  163. return w.GoSNMP.SecurityParameters
  164. }
  165. // SetSecurityParameters sets the SecurityParameters
  166. func (w *otelGoSNMPWrapper) SetSecurityParameters(securityParameters gosnmp.SnmpV3SecurityParameters) {
  167. w.GoSNMP.SecurityParameters = securityParameters
  168. }