-
Notifications
You must be signed in to change notification settings - Fork 12
/
SnmpWalk.php
296 lines (258 loc) · 6.54 KB
/
SnmpWalk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* This file is part of the FreeDSx SNMP package.
*
* (c) Chad Sikorra <Chad.Sikorra@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FreeDSx\Snmp;
use FreeDSx\Snmp\Exception\EndOfWalkException;
/**
* Provides a simple API to perform an SNMP walk.
*
* @author Chad Sikorra <Chad.Sikorra@gmail.com>
*/
class SnmpWalk
{
/**
* @var SnmpClient
*/
protected $client;
/**
* @var string
*/
protected $startAt;
/**
* @var null|string
*/
protected $endAt;
/**
* @var Oid|null
*/
protected $current;
/**
* @var Oid[]
*/
protected $next = [];
/**
* @var int
*/
protected $count = 0;
/**
* @var bool
*/
protected $subtreeOnly;
/**
* @var null|bool
*/
protected $useGetBulk;
/**
* @var int
*/
protected $maxRepetitions = 100;
/**
* @param SnmpClient $client
* @param null|string $startAt
* @param null|string $endAt
* @param bool $subtreeOnly
*/
public function __construct(SnmpClient $client, ?string $startAt = null, ?string $endAt = null, bool $subtreeOnly = true)
{
$this->client = $client;
$this->startAt = $startAt ?? '1.3.6.1.2.1';
$this->endAt = $endAt;
$this->subtreeOnly = $subtreeOnly;
}
/**
* Get the next OID in the walk.
*
* @return Oid
* @throws Exception\ConnectionException
* @throws Exception\SnmpRequestException
* @throws EndOfWalkException
*/
public function next() : Oid
{
if ($this->isComplete()) {
throw new EndOfWalkException('There are no more OIDs left in the walk.');
}
if (!$this->next) {
$this->next = $this->getNextOid();
}
$this->throwIfNoNextOids();
$this->current = \array_shift($this->next);
$this->count++;
return $this->current;
}
/**
* An alias of the next() method.
*
* @return Oid
* @throws EndOfWalkException
* @throws Exception\ConnectionException
* @throws Exception\SnmpRequestException
*/
public function getOid() : Oid
{
return $this->next();
}
/**
* @return bool
* @throws Exception\ConnectionException
* @throws Exception\SnmpRequestException
* @throws EndOfWalkException
*/
public function isComplete() : bool
{
if ($this->current && $this->current->isEndOfMibView()) {
return true;
}
if ($this->current && $this->current->getOid() === $this->endAt) {
return true;
}
if ($this->subtreeOnly) {
return $this->isEndOfSubtree();
}
return false;
}
/**
* Get the number of OIDs walked.
*
* @return int
*/
public function count() : int
{
return $this->count;
}
/**
* @param bool $subtreeOnly
* @return $this
*/
public function subtreeOnly(bool $subtreeOnly = true)
{
$this->subtreeOnly = $subtreeOnly;
return $this;
}
/**
* Whether or not call the next method will produce more OIDs.
*
* @return bool
* @throws Exception\ConnectionException
* @throws Exception\SnmpRequestException
* @throws EndOfWalkException
*/
public function hasOids() : bool
{
return !$this->isComplete();
}
/**
* Set the walk back to the original start OID.
*
* @return $this
*/
public function restart()
{
$this->current = null;
$this->next = [];
$this->count = 0;
return $this;
}
/**
* Set the walk to begin at a specific OID.
*
* @param string $oid
* @return $this
*/
public function startAt(string $oid)
{
$this->startAt = $oid;
return $this;
}
/**
* Set the walk to end at a specific OID.
*
* @param string $oid
* @return $this
*/
public function endAt(string $oid)
{
$this->endAt = $oid;
return $this;
}
/**
* Set the walk to skip to a specific OID, regardless of where it is currently.
*
* @param string $oid
* @return $this
*/
public function skipTo(string $oid)
{
$this->current = new Oid($oid);
return $this;
}
/**
* Explicitly set whether or not to use the GetBulk method for OID retrieval in a SNMPv2 / SNMPv3 context. If the
* SNMP version is set to v1 then it will only use GetNext regardless.
*
* By default GetBulk is used if the SNMP version supports it.
*
* @param bool $useGetBulk
* @return $this
*/
public function useGetBulk(bool $useGetBulk)
{
$this->useGetBulk = $useGetBulk;
return $this;
}
/**
* Use a specific number of max repetitions (applicable if using GetBulk requests). This is the number of OIDs that
* a GetBulk will request to return at once. Depending on the remote host, this might need to be toggled.
*
* @param int $maxRepetitions
* @return $this
*/
public function maxRepetitions(int $maxRepetitions)
{
$this->maxRepetitions = $maxRepetitions;
return $this;
}
/**
* @return Oid[]
* @throws Exception\ConnectionException
* @throws Exception\SnmpRequestException
*/
protected function getNextOid() : array
{
$currentOid = $this->current ? $this->current->getOid() : $this->startAt;
if (($this->useGetBulk === null || $this->useGetBulk) && $this->client->getOptions()['version'] >= 2) {
return $this->client->getBulk($this->maxRepetitions, 0, $currentOid)->toArray();
} else {
return $this->client->getNext($currentOid)->toArray();
}
}
/**
* @return bool
* @throws Exception\ConnectionException
* @throws Exception\SnmpRequestException
* @throws EndOfWalkException
*/
protected function isEndOfSubtree() : bool
{
if (!$this->next) {
$this->next = $this->getNextOid();
}
$this->throwIfNoNextOids();
return (\substr($this->next[0]->getOid(), 0, \strlen($this->startAt)) !== $this->startAt);
}
/**
* @throws EndOfWalkException
*/
protected function throwIfNoNextOids() : void
{
if (!$this->next) {
throw new EndOfWalkException('There are no more OIDs left in the walk.');
}
}
}