Completed
Pull Request — develop (#184)
by Neil
25:03
created

RRDTool.php ➔ build_rrdtool()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 3
nop 1
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Generates a filename based on the hostname (or IP) and some extra items
5
 *
6
 * @param array $device Device array
7
 * @param array|string $extra Components of RRD filename - will be separated with "-", or a pre-formed rrdname
8
 * @param string $extension File extension (default is .rrd)
9
 * @return string the name of the rrd file for $host's $extra component
10
 */
11
function rrd_name($device, $extra, $extension = '.rrd')
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
12
{
13
    $filename = safename(is_array($extra) ? implode("-", $extra) : $extra);
14
    return implode("/", array(Settings::get('rrd_dir'), $device->hostname, $filename.$extension));
15
}
16
17
/**
18
 * Escapes strings for RRDtool
19
 *
20
 * @param string $string the string to escape
21
 * @param integer $length if passed, string will be padded and trimmed to exactly this length (after rrdtool unescapes it)
22
 * @return string
23
 */
24
function rrdtool_escape($string, $length = null)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
25
{
26
    $result = shorten_interface_type($string);
27
    $result = str_replace("'", '', $result);            # remove quotes
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
28
    $result = str_replace('%', '%%', $result);          # double percent signs
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
29
    if (is_numeric($length) && strlen($string) > $length) {
30
        $extra = substr_count($string, ':', 0, $length);
31
        $result = substr(str_pad($result, $length), 0, ($length + $extra));
32
        if ($extra > 0) {
33
            $result = substr($result, 0, (-1 * $extra));
34
        }
35
    }
36
37
    $result = str_replace(':', '\:', $result);          # escape colons
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
38
    return $result.' ';
39
} // rrdtool_escape
40
41
function build_rrdtool($arg)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
42
{
43
    $rrdtool       = Settings::get('rrdtool');
44
    $rrdcached     = Settings::get('rrdcached');
45
    $rrdcached_dir = Settings::get('rrdcached_dir');
46
    $rrd_dir       = Settings::get('rrd_dir');
47
    $rrd_daemon    = '';
48
    if (isset($rrdcached)) {
49
        if (isset($rrdcached_dir) && $rrdcached_dir !== false) {
50
            $arg = str_replace($rrd_dir.'/', './'.$rrdcached_dir.'/', $arg);
51
            $arg = str_replace($rrd_dir, './'.$rrdcached_dir.'/', $arg);
52
        }
53
        $rrd_daemon = " --daemon $rrdcached ";
54
    }
55
    return $rrdtool . ' ' . $rrd_daemon . $arg;
56
}