Completed
Pull Request — develop (#116)
by Neil
03:20
created

Device::syslogs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
 * @mixin \Eloquent
105
 */
106
class Device extends Model
107
{
108
    /**
109
     * Indicates if the model should be timestamped.
110
     *
111
     * @var bool
112
     */
113
    public $timestamps = false;
114
    /**
115
     * The table associated with the model.
116
     *
117
     * @var string
118
     */
119
    protected $table = 'devices';
120
    /**
121
     * The primary key column name.
122
     *
123
     * @var string
124
     */
125
    protected $primaryKey = 'device_id';
126
127
    /**
128
     * The attributes that are mass assignable.
129
     *
130 20
     * @var array
131
     */
132 20
    protected $fillable = ['hostname', 'ip', 'status', 'status_reason'];
133
134 20
    /**
135
     * Initialize this class
136 1
     */
137 1
    public static function boot()
138 1
    {
139 20
        parent::boot();
140 20
141
        static::deleting(function(Device $device) {
142
            // delete related data
143
            $device->ports()->delete();
144
            $device->syslogs()->delete();
145
            $device->eventlogs()->delete();
146
        });
147
    }
148
149
    // ---- Define Reletionships ----
150
151
    /**
152
     * Returns a list of users that can access this device.
153
     */
154
    public function users()
155 18
    {
156
        return $this->belongsToMany('App\Models\User', 'devices_perms', 'device_id', 'user_id');
157 18
    }
158
159
    /**
160
     * Returns a list of the ports this device has.
161
     */
162
    public function ports()
163 1
    {
164
        return $this->hasMany('App\Models\Port', 'device_id', 'device_id');
165 1
    }
166
167
    /**
168
     * Returns a list of the Syslog entries this device has.
169
     */
170
    public function syslogs()
171 1
    {
172
        return $this->hasMany('App\Models\General\Syslog', 'device_id', 'device_id');
173 1
    }
174
175
    /**
176
     * Returns a list of the Eventlog entries this device has.
177
     */
178
    public function eventlogs()
179
    {
180
        return $this->hasMany('App\Models\General\Eventlog', 'host', 'device_id');
181
    }
182
183
    /**
184
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
185
     */
186
    public function alerts()
187
    {
188
        return $this->hasMany('App\Models\Alerting\Alert', 'device_id');
189
    }
190
191
    /**
192
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
193
     */
194
    public function rules()
195
    {
196
        return $this->hasMany('App\Models\Alerting\Rule', 'device_id');
197
    }
198
199
    /**
200
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
201
     */
202
    public function eventlog()
203
    {
204 1
        return $this->hasMany('App\Models\General\Eventlog', 'device_id');
205
    }
206 1
207 1
    /**
208
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
209
     */
210
    public function sensors()
211
    {
212 18
        return $this->hasMany('App\Models\Sensor', 'device_id');
213
    }
214 18
215 18
    /**
216
     * @return string
217
     */
218
    public function logo()
219
    {
220
        $icon = $this->icon;
221
        if (isset($icon)) {
222
            return asset('images/os/'.$icon.'.png');
223
        }
224
        else {
225
            return asset('images/os/generic.png');
226
        }
227
    }
228
229
    /**
230
     * @param int $seconds
231
     * @return string
232
     */
233
    public function formatUptime($seconds)
234
    {
235
        $from = new \DateTime("@0");
236
        $to = new \DateTime("@$seconds");
237
        return $from->diff($to)->format('%a d, %h h, %i m and %s s');
238
    }
239
240
    // ---- Accessors/Mutators ----
241
242
    public function getIpAttribute($ip)
243
    {
244
        if (!empty($ip)) {
245
            return inet_ntop($ip);
246
        }
247
        return null;
248
    }
249
250
    public function setIpAttribute($ip)
251
    {
252
        $this->attributes['ip'] = inet_pton($ip);
253
    }
254
255
}
256