Test Failed
Push — develop ( 55b248...7d9761 )
by Tony
21:25
created

Bits::getRRDGraphDefinition()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 42
nc 2
nop 0
dl 0
loc 46
rs 8.9411
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bits.php
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * @package    LibreNMS
19
 * @link       http://librenms.org
20
 * @copyright  2016 Neil Lathwood
21
 * @author     Neil Lathwood <[email protected]>
22
 */
23
24
namespace App\Graphs\Device;
25
26
use App\Data\RRD;
27
use App\Graphs\Graph;
28
use Illuminate\Database\Query\Builder;
29
30
class Bits extends Graph
31
{
32
    protected function getRelation()
33
    {
34
        if ($this->hasIDs()) {
35
            return [
36
                'ports' => function (Builder $query) {
37
                    $query->whereIn('port_id', $this->getIDs());
38
                },
39
            ];
40
        }
41
42
        return 'ports';
43
    }
44
45
    protected function getData()
46
    {
47
        return $this->device->ports;
48
    }
49
50
    protected function getHeaders()
51
    {
52
        $headers = [];
53
        foreach ($this->getData() as $port) {
54
            $headers[] = 'In: '.$port['ifDescr'];
55
            $headers[] = 'Out: '.$port['ifDescr'];
56
        }
57
        return $headers;
58
    }
59
60
    protected function getRRDGraphDefinition()
61
    {
62
        $defs = " COMMENT:'              Now       Avg      Max' COMMENT:' \\n' ";
63
64
        foreach ($this->getData() as $port) {
65
            $port_id = $port->port_id;
66
            $rrd_file = RRD::getPortFileName($this->device, $port->port_id);
67
            $defs .= " DEF:outoctets$port_id=$rrd_file:OUTOCTETS:AVERAGE \
68
                DEF:inoctets$port_id=$rrd_file:INOCTETS:AVERAGE \
69
                DEF:outoctets_max$port_id=$rrd_file:OUTOCTETS:MAX \
70
                DEF:inoctets_max$port_id=$rrd_file:INOCTETS:MAX \
71
                CDEF:octets$port_id=inoctets$port_id,outoctets$port_id,+ \
72
                CDEF:doutoctets$port_id=outoctets$port_id,-1,* \
73
                CDEF:outbits$port_id=outoctets$port_id,8,* \
74
                CDEF:outbits_max$port_id=outoctets_max$port_id,8,* \
75
                CDEF:doutoctets_max$port_id=outoctets_max$port_id,-1,* \
76
                CDEF:doutbits$port_id=doutoctets$port_id,8,* \
77
                CDEF:doutbits_max$port_id=doutoctets_max$port_id,8,* \
78
                CDEF:inbits$port_id=inoctets$port_id,8,* \
79
                CDEF:inbits_max$port_id=inoctets_max$port_id,8,* \
80
                VDEF:totin$port_id=inoctets$port_id,TOTAL \
81
                VDEF:totout$port_id=outoctets$port_id,TOTAL \
82
                VDEF:tot$port_id=octets$port_id,TOTAL COMMENT:' \\n' \
83
                VDEF:95thin$port_id=inbits$port_id,95,PERCENT \
84
                VDEF:95thout$port_id=outbits$port_id,95,PERCENT \
85
                CDEF:d95thoutn$port_id=doutbits$port_id,-1,* \
86
                VDEF:d95thoutn95$port_id=d95thoutn$port_id,95,PERCENT \
87
                CDEF:d95thoutn95n$port_id=doutbits$port_id,doutbits$port_id,-,d95thoutn95$port_id,-1,*,+ \
88
                VDEF:d95thout$port_id=d95thoutn95n$port_id,FIRST  \
89
                AREA:inbits_max$port_id#D7FFC7: \
90
                AREA:inbits$port_id#90B040: \
91
                LINE:inbits$port_id#608720:'In ".str_pad($port['ifDescr'], 6)."' \
92
                GPRINT:inbits$port_id:LAST:%6.2lf%s \
93
                GPRINT:inbits$port_id:AVERAGE:%6.2lf%s \
94
                GPRINT:inbits_max$port_id:MAX:%6.2lf%s \
95
                AREA:doutbits_max$port_id#E0E0FF: \
96
                AREA:doutbits$port_id#8080C0:  COMMENT:' \\n' \
97
                LINE:doutbits$port_id#606090:'Out ' \
98
                GPRINT:outbits$port_id:LAST:%6.2lf%s \
99
                GPRINT:outbits$port_id:AVERAGE:%6.2lf%s \
100
                GPRINT:outbits_max$port_id:MAX:%6.2lf%s COMMENT:'\n' \
101
                LINE1:95thin$port_id#aa0000 \
102
                LINE1:d95thout$port_id#aa0000";
103
        }
104
        return $defs;
105
    }
106
107
    protected function getRRDXportDefinition()
108
    {
109
        $defs = '';
110
        foreach ($this->getData() as $port) {
111
            $port_id = $port->port_id;
112
            $rrd_file = RRD::getPortFileName($this->device, $port->port_id);
113
            $defs .= "DEF:outoctets$port_id=$rrd_file:OUTOCTETS:AVERAGE \
114
                DEF:inoctets$port_id=$rrd_file:INOCTETS:AVERAGE \
115
                CDEF:doutoctets$port_id=outoctets$port_id,-1,* \
116
                CDEF:doutbits$port_id=doutoctets$port_id,8,* \
117
                CDEF:inbits$port_id=inoctets$port_id,8,* \
118
                XPORT:inbits$port_id:'In' \
119
                XPORT:doutbits$port_id:'Out' ";
120
        }
121
        return $defs;
122
    }
123
}
124