|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* App\Models\Device |
|
9
|
|
|
* |
|
10
|
|
|
* @property integer $device_id |
|
11
|
|
|
* @property string $hostname |
|
12
|
|
|
* @property string $sysName |
|
13
|
|
|
* @property mixed $ip |
|
14
|
|
|
* @property string $community |
|
15
|
|
|
* @property string $authlevel |
|
16
|
|
|
* @property string $authname |
|
17
|
|
|
* @property string $authpass |
|
18
|
|
|
* @property string $authalgo |
|
19
|
|
|
* @property string $cryptopass |
|
20
|
|
|
* @property string $cryptoalgo |
|
21
|
|
|
* @property string $snmpver |
|
22
|
|
|
* @property integer $port |
|
23
|
|
|
* @property string $transport |
|
24
|
|
|
* @property integer $timeout |
|
25
|
|
|
* @property integer $retries |
|
26
|
|
|
* @property string $bgpLocalAs |
|
27
|
|
|
* @property string $sysObjectID |
|
28
|
|
|
* @property string $sysDescr |
|
29
|
|
|
* @property string $sysContact |
|
30
|
|
|
* @property string $version |
|
31
|
|
|
* @property string $hardware |
|
32
|
|
|
* @property string $features |
|
33
|
|
|
* @property string $location |
|
34
|
|
|
* @property string $os |
|
35
|
|
|
* @property boolean $status |
|
36
|
|
|
* @property string $status_reason |
|
37
|
|
|
* @property boolean $ignore |
|
38
|
|
|
* @property boolean $disabled |
|
39
|
|
|
* @property integer $uptime |
|
40
|
|
|
* @property integer $agent_uptime |
|
41
|
|
|
* @property string $last_polled |
|
42
|
|
|
* @property string $last_poll_attempted |
|
43
|
|
|
* @property float $last_polled_timetaken |
|
44
|
|
|
* @property float $last_discovered_timetaken |
|
45
|
|
|
* @property string $last_discovered |
|
46
|
|
|
* @property string $last_ping |
|
47
|
|
|
* @property float $last_ping_timetaken |
|
48
|
|
|
* @property string $purpose |
|
49
|
|
|
* @property string $type |
|
50
|
|
|
* @property string $serial |
|
51
|
|
|
* @property string $icon |
|
52
|
|
|
* @property integer $poller_group |
|
53
|
|
|
* @property boolean $override_sysLocation |
|
54
|
|
|
* @property string $notes |
|
55
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $users |
|
56
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Port[] $ports |
|
57
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\General\Syslog[] $syslogs |
|
58
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\General\Eventlog[] $eventlogs |
|
59
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereDeviceId($value) |
|
60
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereHostname($value) |
|
61
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysName($value) |
|
62
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIp($value) |
|
63
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCommunity($value) |
|
64
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthlevel($value) |
|
65
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthname($value) |
|
66
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthpass($value) |
|
67
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthalgo($value) |
|
68
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCryptopass($value) |
|
69
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCryptoalgo($value) |
|
70
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSnmpver($value) |
|
71
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePort($value) |
|
72
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereTransport($value) |
|
73
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereTimeout($value) |
|
74
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereRetries($value) |
|
75
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereBgpLocalAs($value) |
|
76
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysObjectID($value) |
|
77
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysDescr($value) |
|
78
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysContact($value) |
|
79
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereVersion($value) |
|
80
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereHardware($value) |
|
81
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereFeatures($value) |
|
82
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLocation($value) |
|
83
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereOs($value) |
|
84
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereStatus($value) |
|
85
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereStatusReason($value) |
|
86
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIgnore($value) |
|
87
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereDisabled($value) |
|
88
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereUptime($value) |
|
89
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAgentUptime($value) |
|
90
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPolled($value) |
|
91
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPollAttempted($value) |
|
92
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPolledTimetaken($value) |
|
93
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastDiscoveredTimetaken($value) |
|
94
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastDiscovered($value) |
|
95
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPing($value) |
|
96
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPingTimetaken($value) |
|
97
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePurpose($value) |
|
98
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereType($value) |
|
99
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSerial($value) |
|
100
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIcon($value) |
|
101
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePollerGroup($value) |
|
102
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereOverrideSysLocation($value) |
|
103
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereNotes($value) |
|
104
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Notification IsUp() |
|
105
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Notification IsDown() |
|
106
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Notification IsIgnored() |
|
107
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Notification IsDisabled() |
|
108
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Notification NotIgnored() |
|
109
|
|
|
* @mixin \Eloquent |
|
110
|
|
|
*/ |
|
111
|
|
|
class Device extends Model |
|
112
|
|
|
{ |
|
113
|
|
|
/** |
|
114
|
|
|
* Indicates if the model should be timestamped. |
|
115
|
|
|
* |
|
116
|
|
|
* @var bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public $timestamps = false; |
|
119
|
|
|
/** |
|
120
|
|
|
* The table associated with the model. |
|
121
|
|
|
* |
|
122
|
|
|
* @var string |
|
123
|
|
|
*/ |
|
124
|
|
|
protected $table = 'devices'; |
|
125
|
|
|
/** |
|
126
|
|
|
* The primary key column name. |
|
127
|
|
|
* |
|
128
|
|
|
* @var string |
|
129
|
|
|
*/ |
|
130
|
|
|
protected $primaryKey = 'device_id'; |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* The attributes that are mass assignable. |
|
134
|
|
|
* |
|
135
|
|
|
* @var array |
|
136
|
|
|
*/ |
|
137
|
|
|
protected $fillable = ['hostname', 'ip', 'status', 'status_reason']; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Initialize this class |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function boot() |
|
143
|
|
|
{ |
|
144
|
|
|
parent::boot(); |
|
145
|
|
|
|
|
146
|
|
|
static::deleting(function(Device $device) { |
|
147
|
|
|
// delete related data |
|
148
|
|
|
$device->ports()->delete(); |
|
149
|
|
|
$device->syslogs()->delete(); |
|
150
|
|
|
$device->eventlogs()->delete(); |
|
151
|
|
|
}); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// ---- Define Reletionships ---- |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Returns a list of users that can access this device. |
|
158
|
|
|
*/ |
|
159
|
|
|
public function users() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->belongsToMany('App\Models\User', 'devices_perms', 'device_id', 'user_id'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns a list of the ports this device has. |
|
166
|
|
|
*/ |
|
167
|
|
|
public function ports() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->hasMany('App\Models\Port', 'device_id', 'device_id'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Returns a list of the Syslog entries this device has. |
|
174
|
|
|
*/ |
|
175
|
|
|
public function syslogs() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->hasMany('App\Models\General\Syslog', 'device_id', 'device_id'); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Returns a list of the Eventlog entries this device has. |
|
182
|
|
|
*/ |
|
183
|
|
|
public function eventlogs() |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->hasMany('App\Models\General\Eventlog', 'host', 'device_id'); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\hasMany |
|
190
|
|
|
*/ |
|
191
|
|
|
public function alerts() |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->hasMany('App\Models\Alerting\Alert', 'device_id'); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\hasMany |
|
198
|
|
|
*/ |
|
199
|
|
|
public function rules() |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->hasMany('App\Models\Alerting\Rule', 'device_id'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\hasMany |
|
206
|
|
|
*/ |
|
207
|
|
|
public function eventlog() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->hasMany('App\Models\General\Eventlog', 'device_id'); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\hasMany |
|
214
|
|
|
*/ |
|
215
|
|
|
public function sensors() |
|
216
|
|
|
{ |
|
217
|
|
|
return $this->hasMany('App\Models\Sensor', 'device_id'); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
|
|
public function logo() |
|
224
|
|
|
{ |
|
225
|
|
|
$icon = $this->icon; |
|
226
|
|
|
if (isset($icon)) { |
|
227
|
|
|
return asset('images/os/'.$icon.'.png'); |
|
228
|
|
|
} |
|
229
|
|
|
else { |
|
230
|
|
|
return asset('images/os/generic.png'); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param int $seconds |
|
236
|
|
|
* @return string |
|
237
|
|
|
*/ |
|
238
|
|
|
public function formatUptime($seconds) |
|
239
|
|
|
{ |
|
240
|
|
|
$from = new \DateTime("@0"); |
|
241
|
|
|
$to = new \DateTime("@$seconds"); |
|
242
|
|
|
return $from->diff($to)->format('%a d, %h h, %i m and %s s'); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
// ---- Accessors/Mutators ---- |
|
246
|
|
|
|
|
247
|
|
|
public function getIpAttribute($ip) |
|
248
|
|
|
{ |
|
249
|
|
|
if (!empty($ip)) { |
|
250
|
|
|
return inet_ntop($ip); |
|
251
|
|
|
} |
|
252
|
|
|
return null; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function setIpAttribute($ip) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->attributes['ip'] = inet_pton($ip); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
// ---- Query scopes ---- |
|
261
|
|
|
|
|
262
|
|
View Code Duplication |
public function scopeIsUp($query) |
|
|
|
|
|
|
263
|
|
|
{ |
|
264
|
|
|
return $query->where([ |
|
265
|
|
|
['status', '=', 1], |
|
266
|
|
|
['ignore', '=', 0], |
|
267
|
|
|
['disabled', '=', 0] |
|
268
|
|
|
]); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
View Code Duplication |
public function scopeIsDown($query) |
|
|
|
|
|
|
272
|
|
|
{ |
|
273
|
|
|
return $query->where([ |
|
274
|
|
|
['status', '=', 0], |
|
275
|
|
|
['ignore', '=', 0], |
|
276
|
|
|
['disabled', '=', 0] |
|
277
|
|
|
]); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
public function scopeIsIgnored($query) |
|
281
|
|
|
{ |
|
282
|
|
|
return $query->where([ |
|
283
|
|
|
['ignore', '=', 1], |
|
284
|
|
|
['disabled', '=', 0] |
|
285
|
|
|
]); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
public function scopeNotIgnored($query) |
|
289
|
|
|
{ |
|
290
|
|
|
return $query->where([ |
|
291
|
|
|
['ignore', '=', 0] |
|
292
|
|
|
]); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
public function scopeIsDisabled($query) |
|
296
|
|
|
{ |
|
297
|
|
|
return $query->where([ |
|
298
|
|
|
['disabled', '=', 1] |
|
299
|
|
|
]); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
} |
|
303
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.