1<?php
2/**
3 * Rrd.php
4 *
5 * -Description-
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 *
20 * @link       https://www.librenms.org
21 * @copyright  2017 Tony Murray
22 * @author     Tony Murray <murraytony@gmail.com>
23 */
24
25namespace LibreNMS\Validations;
26
27use LibreNMS\Config;
28use LibreNMS\Validator;
29
30class Rrd extends BaseValidation
31{
32    /**
33     * Validate this module.
34     * To return ValidationResults, call ok, warn, fail, or result methods on the $validator
35     *
36     * @param Validator $validator
37     */
38    public function validate(Validator $validator)
39    {
40        $versions = $validator->getVersions();
41
42        // Check that rrdtool config version is what we see
43        if (Config::has('rrdtool_version')
44            && version_compare(Config::get('rrdtool_version'), '1.5.5', '<')
45            && version_compare(Config::get('rrdtool_version'), $versions['rrdtool_ver'], '>')
46        ) {
47            $validator->fail(
48                'The rrdtool version you have specified is newer than what is installed.',
49                "Either comment out \$config['rrdtool_version'] = '" .
50                Config::get('rrdtool_version') . "'; or set \$config['rrdtool_version'] = '{$versions['rrdtool_ver']}';"
51            );
52        }
53
54        if (Config::get('rrdcached')) {
55            self::checkRrdcached($validator);
56        } else {
57            $rrd_dir = Config::get('rrd_dir');
58
59            $dir_stat = stat($rrd_dir);
60            if ($dir_stat[4] == 0 || $dir_stat[5] == 0) {
61                $validator->warn('Your RRD directory is owned by root, please consider changing over to user a non-root user');
62            }
63
64            if (substr(sprintf('%o', fileperms($rrd_dir)), -3) != 775) {
65                $validator->warn('Your RRD directory is not set to 0775', "chmod 775 $rrd_dir");
66            }
67        }
68    }
69
70    public static function checkRrdcached(Validator $validator)
71    {
72        [$host,$port] = explode(':', Config::get('rrdcached'));
73        if ($host == 'unix') {
74            // Using socket, check that file exists
75            if (! file_exists($port)) {
76                $validator->fail("$port doesn't appear to exist, rrdcached test failed");
77            }
78        } else {
79            $connection = @fsockopen($host, (int) $port);
80            if (is_resource($connection)) {
81                fclose($connection);
82            } else {
83                $validator->fail('Cannot connect to rrdcached instance');
84            }
85        }
86    }
87}
88