netsnmp 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1dcad05ae1ce9ca55fc653ab0f11d97887229b2e
4
- data.tar.gz: e3bdc4c01e957884fea362c5b6a9c0ba79221ee0
3
+ metadata.gz: 8014fd377e09fcd49d70da331116b3d66bce52e3
4
+ data.tar.gz: 9038971c4dd91219e581ddfdfa11d64b062790bf
5
5
  SHA512:
6
- metadata.gz: 6557ae719bc1492de34849ed911aee5c5d7bdb246eefec39295ec6cc6e61d42dc39f6fc561d375e7edb76c6cf34f0dc5fcbec9d83fd22955a02e290caf7cb13e
7
- data.tar.gz: 1a3599d249d30381276e0aea4c8317ab0e288a97bc5b8cff7fba787d7f8e0dc5f09148f05e9ac40bdf7ae8018d5d8741316d3eacd333dce90de245e984140fc4
6
+ metadata.gz: b1469577a0aa7719c12fa54eb445b4016f3e44b3caa2d51db8931b18f852256af0482261feeff407e195c0af2cf72d3ddb477ca6062b529f9dd0748e1dd2ffcc
7
+ data.tar.gz: 31a013f21505b92217c276b33631c52c0cb42c9d493a24b51a3bd190304f4ac67c3a1e921fa59f6f432f8f5b4b6248f68e65581ef4f2c2f7f044968bda57a4ae
data/lib/netsnmp.rb CHANGED
@@ -58,6 +58,8 @@ end
58
58
 
59
59
  require "netsnmp/errors"
60
60
 
61
+ require "netsnmp/timeticks"
62
+
61
63
  require "netsnmp/oid"
62
64
  require "netsnmp/varbind"
63
65
  require "netsnmp/pdu"
@@ -0,0 +1,70 @@
1
+ module NETSNMP
2
+ class Timetick < Numeric
3
+
4
+ # @param [Integer] ticks number of microseconds since the time it was read
5
+ def initialize(ticks)
6
+ @ticks = ticks
7
+ end
8
+
9
+
10
+ def to_s
11
+ days = days_since
12
+ hours = hours_since(days)
13
+ minutes = minutes_since(hours)
14
+ milliseconds = milliseconds_since(minutes)
15
+ "Timeticks: (#{@ticks}) #{days.to_i} days, #{hours.to_i}:#{minutes.to_i}:#{milliseconds.to_f.round(2)}"
16
+ end
17
+
18
+ def to_i
19
+ @ticks
20
+ end
21
+
22
+ def to_asn
23
+ OpenSSL::ASN1::ASN1Data.new([@ticks].pack("N"), 3, :APPLICATION)
24
+ end
25
+
26
+ def coerce(other)
27
+ [ Timetick.new(other), self]
28
+ end
29
+
30
+ def <=>(other)
31
+ to_i <=> other.to_i
32
+ end
33
+
34
+ def +(other)
35
+ Timetick.new( (to_i + other.to_i))
36
+ end
37
+
38
+ def -(other)
39
+ Timetick.new( (to_i - other.to_i))
40
+ end
41
+
42
+ def *(other)
43
+ Timetick.new( (to_i * other.to_i))
44
+ end
45
+
46
+ def /(other)
47
+ Timetick.new( (to_i / other.to_i))
48
+ end
49
+
50
+ private
51
+
52
+
53
+ def days_since
54
+ Rational(@ticks, 8_640_000)
55
+ end
56
+
57
+ def hours_since(days)
58
+ Rational((days.to_f - days.to_i) * 24)
59
+ end
60
+
61
+ def minutes_since(hours)
62
+ Rational((hours.to_f - hours.to_i) * 60)
63
+ end
64
+
65
+ def milliseconds_since(minutes)
66
+ Rational((minutes.to_f - minutes.to_i) * 60)
67
+ end
68
+
69
+ end
70
+ end
@@ -23,8 +23,7 @@ module NETSNMP
23
23
  def to_asn
24
24
  asn_oid = OID.to_asn(@oid)
25
25
  asn_val = if @type
26
- asn_type, asn_val = convert_to_asn(@type, @value)
27
- OpenSSL::ASN1::ASN1Data.new(asn_val, asn_type, :APPLICATION)
26
+ convert_to_asn(@type, @value)
28
27
  else
29
28
  case @value
30
29
  when String
@@ -37,6 +36,8 @@ module NETSNMP
37
36
  OpenSSL::ASN1::Null.new(nil)
38
37
  when IPAddr
39
38
  OpenSSL::ASN1::ASN1Data.new(@value.hton, 0, :APPLICATION)
39
+ when Timetick
40
+ @value.to_asn
40
41
  else
41
42
  raise Error, "#{@value}: unsupported varbind type"
42
43
  end
@@ -74,17 +75,24 @@ module NETSNMP
74
75
  end
75
76
 
76
77
  def convert_to_asn(typ, value)
77
- return [typ, value] unless typ.is_a?(Symbol)
78
- case typ
79
- when :ipaddress then 0
80
- when :counter32 then 1
81
- when :gauge then 2
82
- when :timetick then [3, [ value].pack("N") ]
83
- when :opaque then 4
84
- when :nsap then 5
85
- when :counter64 then 6
86
- when :uinteger then 7
78
+ asn_type = typ
79
+ asn_val = value
80
+ if typ.is_a?(Symbol)
81
+ asn_type = case typ
82
+ when :ipaddress then 0
83
+ when :counter32 then 1
84
+ when :gauge then 2
85
+ when :timetick
86
+ return Timetick.new(value).to_asn
87
+ when :opaque then 4
88
+ when :nsap then 5
89
+ when :counter64 then 6
90
+ when :uinteger then 7
91
+ else
92
+ raise Error, "#{typ}: unsupported application type"
93
+ end
87
94
  end
95
+ OpenSSL::ASN1::ASN1Data.new(asn_val, asn_type, :APPLICATION)
88
96
  end
89
97
 
90
98
  def convert_application_asn(asn)
@@ -95,7 +103,7 @@ module NETSNMP
95
103
  asn.value.unpack("n*")[0] || 0
96
104
  when 2 # gauge
97
105
  when 3 # timeticks
98
- asn.value.unpack("N*")[0] || 0
106
+ Timetick.new(asn.value.unpack("N*")[0] || 0)
99
107
  when 4 # opaque
100
108
  when 5 # NSAP
101
109
  when 6 # ASN Counter 64
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module NETSNMP
3
- VERSION = '0.1.2'
3
+ VERSION = '0.1.3'
4
4
  end
data/spec/client_spec.rb CHANGED
@@ -22,12 +22,12 @@ RSpec.describe NETSNMP::Client do
22
22
  let(:walk_result) { <<-WALK
23
23
  1.3.6.1.2.1.1.1.0: Device description
24
24
  1.3.6.1.2.1.1.2.0: 1.3.6.1.4.1.3454
25
- 1.3.6.1.2.1.1.3.0: 78171676
25
+ 1.3.6.1.2.1.1.3.0: Timeticks: (78171676) 9 days, 1:8:36.76
26
26
  1.3.6.1.2.1.1.4.0: The Owner
27
27
  1.3.6.1.2.1.1.5.0: DEVICE-192.168.1.1
28
28
  1.3.6.1.2.1.1.6.0: The Cloud
29
29
  1.3.6.1.2.1.1.7.0: 72
30
- 1.3.6.1.2.1.1.8.0: 0
30
+ 1.3.6.1.2.1.1.8.0: Timeticks: (0) 0 days, 0:0:0.0
31
31
  WALK
32
32
  }
33
33
  let(:set_oid_result) { 43 }
@@ -48,12 +48,12 @@ WALK
48
48
  let(:walk_result) { <<-WALK
49
49
  1.3.6.1.2.1.1.1.0: Device description
50
50
  1.3.6.1.2.1.1.2.0: 1.3.6.1.4.1.3454
51
- 1.3.6.1.2.1.1.3.0: 78171676
51
+ 1.3.6.1.2.1.1.3.0: Timeticks: (78171676) 9 days, 1:8:36.76
52
52
  1.3.6.1.2.1.1.4.0: The Owner
53
53
  1.3.6.1.2.1.1.5.0: DEVICE-192.168.1.1
54
54
  1.3.6.1.2.1.1.6.0: The Cloud
55
55
  1.3.6.1.2.1.1.7.0: 72
56
- 1.3.6.1.2.1.1.8.0: 0
56
+ 1.3.6.1.2.1.1.8.0: Timeticks: (0) 0 days, 0:0:0.0
57
57
  WALK
58
58
  }
59
59
  let(:set_oid_result) { 43 }
@@ -0,0 +1,12 @@
1
+ # from https://ask.wireshark.org/questions/14002/how-to-decode-timeticks-hundreds-seconds-to-readable-date-time
2
+ RSpec.describe NETSNMP::Timetick do
3
+ subject { described_class.new(1525917187) }
4
+
5
+ describe "as an integer" do
6
+ it { expect((1 + subject).to_i).to be(1525917188) }
7
+ end
8
+
9
+ describe "as an embedded string" do
10
+ it { expect(subject.to_s).to eq("Timeticks: (1525917187) 176 days, 14:39:31.87") }
11
+ end
12
+ end
data/spec/varbind_spec.rb CHANGED
@@ -8,6 +8,19 @@ RSpec.describe NETSNMP::Varbind do
8
8
  varbind = described_class.new(".1.3.6.1.4.1.2011.6.3.1.1.0", value: ipaddr)
9
9
  expect(varbind.to_der).to end_with("@\x04\n\vh\x02".b)
10
10
  end
11
+ it "converts custom timeticks" do
12
+ timetick = NETSNMP::Timetick.new(1) # yes, one timetick
13
+ varbind = described_class.new(".1.3.6.1.2.1.1.3.0", value: timetick)
14
+ expect(varbind.to_der).to end_with("\x04\x00\x00\x00\x01".b) # ends with an octet string rep of 1 timetick
15
+ end
16
+ context "when passed a type" do
17
+ # TODO: tidy this for IP Addresses
18
+ it "converts integer ticks" do
19
+ timetick = 1
20
+ varbind = described_class.new(".1.3.6.1.2.1.1.3.0", type: :timetick, value: timetick)
21
+ expect(varbind.to_der).to end_with("\x04\x00\x00\x00\x01".b)
22
+ end
23
+ end
11
24
  end
12
25
  end
13
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netsnmp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tiago Cardoso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-02 00:00:00.000000000 Z
11
+ date: 2017-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -99,6 +99,7 @@ files:
99
99
  - lib/netsnmp/scoped_pdu.rb
100
100
  - lib/netsnmp/security_parameters.rb
101
101
  - lib/netsnmp/session.rb
102
+ - lib/netsnmp/timeticks.rb
102
103
  - lib/netsnmp/v3_session.rb
103
104
  - lib/netsnmp/varbind.rb
104
105
  - lib/netsnmp/version.rb
@@ -115,6 +116,7 @@ files:
115
116
  - spec/support/request_examples.rb
116
117
  - spec/support/start_docker.sh
117
118
  - spec/support/stop_docker.sh
119
+ - spec/timeticks_spec.rb
118
120
  - spec/v3_session_spec.rb
119
121
  - spec/varbind_spec.rb
120
122
  homepage: ''
@@ -156,5 +158,6 @@ test_files:
156
158
  - spec/support/request_examples.rb
157
159
  - spec/support/start_docker.sh
158
160
  - spec/support/stop_docker.sh
161
+ - spec/timeticks_spec.rb
159
162
  - spec/v3_session_spec.rb
160
163
  - spec/varbind_spec.rb