forest-client 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/forest-client/printer.rb +199 -0
- data/lib/forest-client.rb +103 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 90a20be4c22f75bed4993a231b0a15e45fca7591
|
4
|
+
data.tar.gz: f5cfd6a1f9948f0825bd65a9bd35e8df3b12c82f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5f1f587926f51400ae690347c0a0887bfaf9f3260916ae3a8913bf6dcab7fbecde067af13c781149149061961e5089c2ce776d7ce419872fd54fa1893f8b9fd7
|
7
|
+
data.tar.gz: 1bc693424fe77dfe9401e97e0cdbe0e58ddf71ec4bebe86ed2f41c6875acded1fa9afccacd06ff0935f30b9fc7671873281925a7747c22fa70e3fb738eedcdd8
|
@@ -0,0 +1,199 @@
|
|
1
|
+
require 'forest-client'
|
2
|
+
require 'snmp'
|
3
|
+
|
4
|
+
module ForestClient
|
5
|
+
class Printer
|
6
|
+
|
7
|
+
# Create a new instance of Printer by passing its IP address
|
8
|
+
def initialize(ip)
|
9
|
+
@ip = ip
|
10
|
+
end
|
11
|
+
|
12
|
+
# Return the IP address of the printer
|
13
|
+
def get_ip
|
14
|
+
return @ip
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get the value of an SNMP OID
|
18
|
+
def snmp_get(oid)
|
19
|
+
SNMP::Manager.open(:host => @ip) do |manager|
|
20
|
+
query = SNMP::ObjectId.new(oid)
|
21
|
+
response = manager.get(query)
|
22
|
+
response.each_varbind do |res|
|
23
|
+
return res.value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Walk the SNMP tree starting at the given OID
|
29
|
+
def snmp_walk(oid)
|
30
|
+
SNMP::Manager.open(:host => @ip) do |manager|
|
31
|
+
query = SNMP::ObjectId.new(oid)
|
32
|
+
ret = Array.new
|
33
|
+
manager.walk(oid) { |vb| ret.push(vb.value) }
|
34
|
+
return ret
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Query the printer for its model number
|
39
|
+
def get_model
|
40
|
+
return snmp_get('1.3.6.1.2.1.25.3.2.1.3.1')
|
41
|
+
end
|
42
|
+
|
43
|
+
# Query the printer for its serial
|
44
|
+
def get_serial
|
45
|
+
return snmp_walk('1.3.6.1.2.1.43.5.1.1.17').at(0)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Query the printer for its message
|
49
|
+
def get_messages
|
50
|
+
return snmp_walk('1.3.6.1.2.1.43.18.1.1.8').at(0)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Query the printer for its pagecount
|
54
|
+
def get_page_count
|
55
|
+
return Integer(snmp_get('1.3.6.1.2.1.43.10.2.1.4.1.1'))
|
56
|
+
end
|
57
|
+
|
58
|
+
# Query the printer for its display
|
59
|
+
def get_display
|
60
|
+
return snmp_walk('1.3.6.1.2.1.43.16.5.1.2.1').at(0)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Query the printer for its status
|
64
|
+
# The printer will return an integer which we convert
|
65
|
+
# into a meaninful string
|
66
|
+
def get_status
|
67
|
+
case snmp_get('1.3.6.1.2.1.25.3.5.1.1.1')
|
68
|
+
when 1
|
69
|
+
return 'Other'
|
70
|
+
when 3
|
71
|
+
return 'Idle'
|
72
|
+
when 4
|
73
|
+
return 'Printing'
|
74
|
+
when 5
|
75
|
+
return 'Warmup'
|
76
|
+
else
|
77
|
+
return 'Unknown'
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns an array of all device IDs
|
82
|
+
def get_device_ids
|
83
|
+
return snmp_walk('1.3.6.1.2.1.25.3.2.1.1')
|
84
|
+
end
|
85
|
+
|
86
|
+
# Returns the name and status of the specified device as a hash
|
87
|
+
def get_device(id)
|
88
|
+
|
89
|
+
# Get the device name
|
90
|
+
name = snmp_get("1.3.6.1.2.1.25.3.2.1.3.#{id}")
|
91
|
+
|
92
|
+
# Make the status human-readable
|
93
|
+
case snmp_get("1.3.6.1.2.1.25.3.2.1.5.#{id}")
|
94
|
+
when 1
|
95
|
+
status = 'Unknown'
|
96
|
+
when 2
|
97
|
+
status = 'Running'
|
98
|
+
when 3
|
99
|
+
status = 'Warning'
|
100
|
+
when 4
|
101
|
+
status = 'Testing'
|
102
|
+
when 5
|
103
|
+
status = 'Down'
|
104
|
+
else
|
105
|
+
status = 'Unknown'
|
106
|
+
end
|
107
|
+
|
108
|
+
return {:name => name, :status => status}
|
109
|
+
end
|
110
|
+
|
111
|
+
# Returns an array of the results of get_device_status for all of the
|
112
|
+
# printer's devices
|
113
|
+
def get_devices
|
114
|
+
devices = get_device_ids
|
115
|
+
ret = Array.new
|
116
|
+
devices.each do |device|
|
117
|
+
ret.push(get_device(device))
|
118
|
+
end
|
119
|
+
return ret
|
120
|
+
end
|
121
|
+
|
122
|
+
def get_consumable(id)
|
123
|
+
color = snmp_get("1.3.6.1.2.1.43.12.1.1.4.1.#{id}").to_s
|
124
|
+
level = Integer(snmp_get("1.3.6.1.2.1.43.11.1.1.9.1.#{id}"))
|
125
|
+
capacity = Integer(snmp_get("1.3.6.1.2.1.43.11.1.1.8.1.#{id}"))
|
126
|
+
percentage = Float(level) * 100 / Float(capacity)
|
127
|
+
return {:color => color,
|
128
|
+
:level => level,
|
129
|
+
:capacity => capacity,
|
130
|
+
:percentage => percentage}
|
131
|
+
end
|
132
|
+
|
133
|
+
def get_consumable_names
|
134
|
+
return snmp_walk('1.3.6.1.2.1.43.11.1.1.6.1').each { |item| item.chop! }
|
135
|
+
end
|
136
|
+
|
137
|
+
def get_consumables
|
138
|
+
consumables = get_consumable_names
|
139
|
+
|
140
|
+
ret = Array.new
|
141
|
+
|
142
|
+
consumables.length.times do |id|
|
143
|
+
cons = get_consumable(id + 1)
|
144
|
+
cons[:name] = consumables.at(id)
|
145
|
+
ret.push(cons)
|
146
|
+
end
|
147
|
+
|
148
|
+
return ret
|
149
|
+
end
|
150
|
+
|
151
|
+
def get_trays
|
152
|
+
trays = Array.new
|
153
|
+
snmp_walk('1.3.6.1.2.1.43.8.2.1.10.1').length.times do |id|
|
154
|
+
tray = id + 1
|
155
|
+
name = snmp_get("1.3.6.1.2.1.43.8.2.1.13.1.#{tray}")
|
156
|
+
|
157
|
+
rem = snmp_get("1.3.6.1.2.1.43.8.2.1.10.1.#{tray}")
|
158
|
+
case rem
|
159
|
+
when -3
|
160
|
+
status = 'OK'
|
161
|
+
when -2
|
162
|
+
status = 'Unknown'
|
163
|
+
when 0
|
164
|
+
status = 'Empty'
|
165
|
+
else
|
166
|
+
status = "#{rem} sheets remaining"
|
167
|
+
end
|
168
|
+
|
169
|
+
feed_dim = snmp_get("1.3.6.1.2.1.43.8.2.1.4.1.#{tray}")
|
170
|
+
xfeed_dim = snmp_get("1.3.6.1.2.1.43.8.2.1.5.1.#{tray}")
|
171
|
+
dim_units = snmp_get("1.3.6.1.2.1.43.8.2.1.3.1.#{tray}")
|
172
|
+
|
173
|
+
if Integer(dim_units) == 3
|
174
|
+
feed_dim = Float(feed_dim) / 10000
|
175
|
+
xfeed_dim = Float(xfeed_dim) / 10000
|
176
|
+
elsif Integer(dim_units) == 4
|
177
|
+
feed_dim = Float(feed_dim) * 0.0000393700787
|
178
|
+
xfeed_dim = Float(xfeed_dim) * 0.0000393700787
|
179
|
+
end
|
180
|
+
|
181
|
+
capacity = Integer(snmp_get("1.3.6.1.2.1.43.8.2.1.9.1.#{tray}"))
|
182
|
+
|
183
|
+
trays.push({:name => name, :status => status, :y => feed_dim, :x => xfeed_dim, :capacity => capacity})
|
184
|
+
end
|
185
|
+
|
186
|
+
return trays
|
187
|
+
end
|
188
|
+
|
189
|
+
def get_location
|
190
|
+
return snmp_get('1.3.6.1.2.1.1.6.0')
|
191
|
+
end
|
192
|
+
|
193
|
+
def get_name
|
194
|
+
return snmp_get('1.3.6.1.2.1.1.5.0')
|
195
|
+
end
|
196
|
+
|
197
|
+
private :snmp_get, :snmp_walk
|
198
|
+
end
|
199
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
require 'forest-client/printer'
|
5
|
+
|
6
|
+
module ForestClient
|
7
|
+
class Forest
|
8
|
+
|
9
|
+
def api_call(url)
|
10
|
+
url = "https://forest-api.herokuapp.com/api/#{url}"
|
11
|
+
url = URI.escape(url)
|
12
|
+
url = URI.parse(url)
|
13
|
+
content = Net::HTTP.get(url)
|
14
|
+
return JSON.parse(content)
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(org_id)
|
18
|
+
@org_id = org_id
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_printers
|
22
|
+
url = "printerList?organizationId=#{@org_id}"
|
23
|
+
printers = api_call(url)
|
24
|
+
return printers['printers']
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_printer(id)
|
28
|
+
url = "printerGet?organizationId=#{@org_id}&printerId=#{id}"
|
29
|
+
printer = api_call(url)
|
30
|
+
return printer
|
31
|
+
end
|
32
|
+
|
33
|
+
def update_printer(printer)
|
34
|
+
id = printer['id']
|
35
|
+
ip = printer['ipAddress']
|
36
|
+
|
37
|
+
p = Printer.new(ip)
|
38
|
+
url = 'printerUpdate'
|
39
|
+
url += '?printerId=' + id
|
40
|
+
url += '&model=' + p.get_model
|
41
|
+
url += '&location=' + p.get_location
|
42
|
+
url += '&serial=' + p.get_serial
|
43
|
+
api_call(url)
|
44
|
+
return
|
45
|
+
end
|
46
|
+
|
47
|
+
def update_status(printer)
|
48
|
+
id = printer['id']
|
49
|
+
ip = printer['ipAddress']
|
50
|
+
p = Printer.new(ip)
|
51
|
+
|
52
|
+
url = 'statusCreate'
|
53
|
+
url += '?printerId=' + id
|
54
|
+
url += '&status=' + p.get_status
|
55
|
+
url += '&pageCount=' + p.get_page_count.to_s
|
56
|
+
|
57
|
+
trays = Array.new
|
58
|
+
|
59
|
+
p.get_trays.each { |tray|
|
60
|
+
trays.push({
|
61
|
+
'name' => tray[:name],
|
62
|
+
'xdim' => tray[:x],
|
63
|
+
'ydim' => tray[:y],
|
64
|
+
'capacity' => tray[:capacity]})
|
65
|
+
}
|
66
|
+
|
67
|
+
url += '&trays=' + trays.to_s.gsub!('=>', ':')
|
68
|
+
|
69
|
+
consumables = Array.new
|
70
|
+
|
71
|
+
p.get_consumables.each { |cons|
|
72
|
+
consumables.push({
|
73
|
+
'name' => cons[:name],
|
74
|
+
'level' => cons[:level],
|
75
|
+
'capacity' => cons[:capacity],
|
76
|
+
'percentage' => cons[:percentage]
|
77
|
+
})
|
78
|
+
}
|
79
|
+
|
80
|
+
url += '&consumables=' + consumables.to_s.gsub!('=>', ':')
|
81
|
+
|
82
|
+
api_call url
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
class QueryAgent
|
88
|
+
|
89
|
+
def initialize(org_id)
|
90
|
+
@org_id = org_id
|
91
|
+
end
|
92
|
+
|
93
|
+
def run
|
94
|
+
forest = Forest.new(@org_id)
|
95
|
+
|
96
|
+
printers = forest.get_printers
|
97
|
+
printers.each do |printer|
|
98
|
+
forest.update_printer(printer)
|
99
|
+
forest.update_status(printer)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: forest-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Burwell
|
8
|
+
- Andrew Trautman
|
9
|
+
- Macauley Breault
|
10
|
+
- Mike Borowski
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2013-11-15 00:00:00.000000000 Z
|
15
|
+
dependencies: []
|
16
|
+
description: The LAN printer query agent for the Forest printer management system.
|
17
|
+
email: printer.fungineers@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/forest-client.rb
|
23
|
+
- lib/forest-client/printer.rb
|
24
|
+
homepage: https://github.com/printerSystemCSI210
|
25
|
+
licenses:
|
26
|
+
- Unknown
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.0.3
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Forest Printer Management Query Agent
|
48
|
+
test_files: []
|