Issues (2963)

app/ApiClients/GraylogApi.php (1 issue)

1
<?php
2
/**
3
 * GraylogApi.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
 *
22
 * @copyright  2018 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\ApiClients;
27
28
use App\Models\Device;
29
use GuzzleHttp\Client;
30
use LibreNMS\Config;
31
32
class GraylogApi
33
{
34
    private $client;
35
    private $api_prefix = '';
36
37
    public function __construct(array $config = [])
38
    {
39
        if (version_compare(Config::get('graylog.version', '2.4'), '2.1', '>=')) {
40
            $this->api_prefix = '/api';
41
        }
42
43
        if (empty($config)) {
44
            $base_uri = Config::get('graylog.server');
45
            if ($port = Config::get('graylog.port')) {
46
                $base_uri .= ':' . $port;
47
            }
48
49
            $config = [
50
                'base_uri' => $base_uri,
51
                'auth' => [Config::get('graylog.username'), Config::get('graylog.password')],
52
                'headers' => ['Accept' => 'application/json'],
53
            ];
54
        }
55
56
        $this->client = new Client($config);
57
    }
58
59
    public function getStreams()
60
    {
61
        if (! $this->isConfigured()) {
62
            return [];
63
        }
64
65
        $uri = $this->api_prefix . '/streams';
66
67
        $response = $this->client->get($uri);
68
        $data = json_decode($response->getBody(), true);
69
70
        return $data ?: [];
71
    }
72
73
    /**
74
     * Query the Graylog server
75
     *
76
     * @param  string  $query
77
     * @param  int  $range
78
     * @param  int  $limit
79
     * @param  int  $offset
80
     * @param  string  $sort  field:asc or field:desc
81
     * @param  string  $filter
82
     * @return array
83
     */
84
    public function query($query = '*', $range = 0, $limit = 0, $offset = 0, $sort = null, $filter = null)
85
    {
86
        if (! $this->isConfigured()) {
87
            return [];
88
        }
89
90
        $uri = Config::get('graylog.base_uri');
91
        if (! $uri) {
92
            $uri = $this->api_prefix . '/search/universal/relative';
93
        }
94
95
        $data = [
96
            'query' => $query,
97
            'range' => $range,
98
            'limit' => $limit,
99
            'offset' => $offset,
100
            'sort' => $sort,
101
            'filter' => $filter,
102
        ];
103
104
        $response = $this->client->get($uri, ['query' => $data]);
105
        $data = json_decode($response->getBody(), true);
106
107
        return $data ?: [];
108
    }
109
110
    /**
111
     * Build a simple query string that searches the messages field and/or filters by device
112
     *
113
     * @param  string  $search  Search the message field for this string
114
     * @param  Device  $device
115
     * @return string
116
     */
117
    public function buildSimpleQuery($search = null, $device = null)
118
    {
119
        $query = [];
120
        if ($search) {
121
            $query[] = 'message:"' . $search . '"';
122
        }
123
124
        if ($device) {
125
            $query[] = 'source: ("' . $this->getAddresses($device)->implode('" OR "') . '")';
126
        }
127
128
        if (empty($query)) {
129
            return '*';
130
        }
131
132
        return implode(' && ', $query);
133
    }
134
135
    public function getAddresses(Device $device)
136
    {
137
        $addresses = collect([
138
            gethostbyname($device->hostname),
139
            $device->hostname,
140
            $device->ip,
141
        ]);
142
143
        if (Config::get('graylog.match-any-address')) {
144
            $addresses = $addresses->merge($device->ipv4->pluck('ipv4_address')
145
                ->filter(
146
                    function ($address) {
147
                        return $address != '127.0.0.1';
148
                    }
149
                ))->merge($device->ipv6->pluck('ipv6_address')
150
                ->filter(
151
                    function ($address) {
152
                        return $address != '0000:0000:0000:0000:0000:0000:0000:0001';
153
                    }
154
                ));
155
        }
156
157
        return $addresses->filter()->unique();
158
    }
159
160
    public function isConfigured()
161
    {
162
        return isset($this->client->getConfig()['base_uri']);
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Client::getConfig() has been deprecated: Client::getConfig will be removed in guzzlehttp/guzzle:8.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

162
        return isset(/** @scrutinizer ignore-deprecated */ $this->client->getConfig()['base_uri']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
163
    }
164
}
165